parse.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. 'use strict'
  2. const assert = require('chai').assert
  3. const proxyquire = require('proxyquire')
  4. const spooks = require('spooks')
  5. const Promise = require('bluebird')
  6. const modulePath = '../../src/parse'
  7. suite('parse:', () => {
  8. test('require does not throw', () => {
  9. assert.doesNotThrow(() => {
  10. require(modulePath)
  11. })
  12. })
  13. test('require returns function', () => {
  14. assert.isFunction(require(modulePath))
  15. })
  16. suite('require:', () => {
  17. let log, results, parse
  18. setup(() => {
  19. log = {}
  20. results = {
  21. walk: [
  22. {
  23. on: spooks.fn({ name: 'on', log: log }),
  24. pause: spooks.fn({ name: 'pause', log: log, results: [ () => {} ] }),
  25. removeAllListeners: spooks.fn({ name: 'removeAllListeners', log: log })
  26. }
  27. ]
  28. }
  29. parse = proxyquire(modulePath, {
  30. './walk': spooks.fn({
  31. name: 'walk',
  32. log: log,
  33. results: results.walk
  34. })
  35. })
  36. })
  37. test('parse expects one argument', () => {
  38. assert.lengthOf(parse, 1)
  39. })
  40. test('parse does not throw', () => {
  41. assert.doesNotThrow(() => {
  42. parse()
  43. })
  44. })
  45. test('parse does not throw if reviver is an object', () => {
  46. assert.doesNotThrow(() => {
  47. parse({}, { reviver: {} }).catch(() => {})
  48. })
  49. })
  50. test('parse does not throw if revive is a function', () => {
  51. assert.doesNotThrow(() => {
  52. parse({}, { reviver: () => {} })
  53. })
  54. })
  55. test('parse returns a promise', () => {
  56. assert.instanceOf(parse(), Promise)
  57. })
  58. test('parse returns a different type of promise if the option is set', () => {
  59. assert.isFunction(global.Promise)
  60. assert.notStrictEqual(Promise, global.Promise)
  61. assert.instanceOf(parse('', { Promise: global.Promise }), global.Promise)
  62. })
  63. test('parse rejects immediately if reviver is an object', () => {
  64. return parse({}, { reviver: {} })
  65. .then(() => assert(false))
  66. .catch(error => assert.instanceOf(error, Error))
  67. })
  68. test('parse does not reject immediately if reviver is a function', () => {
  69. parse({}, { reviver: () => {} })
  70. .catch(error => assert(false))
  71. })
  72. test('walk was not called', () => {
  73. assert.strictEqual(log.counts.walk, 0)
  74. })
  75. test('EventEmitter.on was not called', () => {
  76. assert.strictEqual(log.counts.on, 0)
  77. })
  78. suite('parse:', () => {
  79. let stream, options
  80. setup(() => {
  81. stream = {}
  82. options = {}
  83. parse(stream, options)
  84. .then(spooks.fn({ name: 'resolve', log: log }))
  85. .catch(spooks.fn({ name: 'reject', log: log }))
  86. })
  87. test('walk was called once', () => {
  88. assert.strictEqual(log.counts.walk, 1)
  89. assert.isUndefined(log.these.walk[0])
  90. })
  91. test('walk was called correctly', () => {
  92. assert.lengthOf(log.args.walk[0], 2)
  93. assert.strictEqual(log.args.walk[0][0], stream)
  94. assert.lengthOf(Object.keys(log.args.walk[0][0]), 0)
  95. assert.strictEqual(log.args.walk[0][1], options)
  96. assert.lengthOf(Object.keys(log.args.walk[0][1]), 0)
  97. })
  98. test('EventEmitter.on was called eleven times', () => {
  99. assert.strictEqual(log.counts.on, 11)
  100. assert.strictEqual(log.these.on[0], results.walk[0])
  101. assert.strictEqual(log.these.on[1], results.walk[0])
  102. assert.strictEqual(log.these.on[2], results.walk[0])
  103. assert.strictEqual(log.these.on[3], results.walk[0])
  104. assert.strictEqual(log.these.on[4], results.walk[0])
  105. assert.strictEqual(log.these.on[5], results.walk[0])
  106. assert.strictEqual(log.these.on[6], results.walk[0])
  107. assert.strictEqual(log.these.on[7], results.walk[0])
  108. assert.strictEqual(log.these.on[8], results.walk[0])
  109. assert.strictEqual(log.these.on[9], results.walk[0])
  110. assert.strictEqual(log.these.on[10], results.walk[0])
  111. })
  112. test('EventEmitter.on was called correctly first time', () => {
  113. assert.lengthOf(log.args.on[0], 2)
  114. assert.strictEqual(log.args.on[0][0], 'arr')
  115. assert.isFunction(log.args.on[0][1])
  116. })
  117. test('EventEmitter.on was called correctly second time', () => {
  118. assert.lengthOf(log.args.on[1], 2)
  119. assert.strictEqual(log.args.on[1][0], 'obj')
  120. assert.isFunction(log.args.on[1][1])
  121. assert.notStrictEqual(log.args.on[1][1], log.args.on[0][1])
  122. })
  123. test('EventEmitter.on was called correctly third time', () => {
  124. assert.lengthOf(log.args.on[2], 2)
  125. assert.strictEqual(log.args.on[2][0], 'pro')
  126. assert.isFunction(log.args.on[2][1])
  127. assert.notStrictEqual(log.args.on[2][1], log.args.on[0][1])
  128. assert.notStrictEqual(log.args.on[2][1], log.args.on[1][1])
  129. })
  130. test('EventEmitter.on was called correctly fourth time', () => {
  131. assert.lengthOf(log.args.on[3], 2)
  132. assert.strictEqual(log.args.on[3][0], 'str')
  133. assert.isFunction(log.args.on[3][1])
  134. assert.notStrictEqual(log.args.on[3][1], log.args.on[0][1])
  135. assert.notStrictEqual(log.args.on[3][1], log.args.on[1][1])
  136. assert.notStrictEqual(log.args.on[3][1], log.args.on[2][1])
  137. })
  138. test('EventEmitter.on was called correctly fifth time', () => {
  139. assert.lengthOf(log.args.on[4], 2)
  140. assert.strictEqual(log.args.on[4][0], 'num')
  141. assert.isFunction(log.args.on[4][1])
  142. assert.strictEqual(log.args.on[4][1], log.args.on[3][1])
  143. })
  144. test('EventEmitter.on was called correctly sixth time', () => {
  145. assert.lengthOf(log.args.on[5], 2)
  146. assert.strictEqual(log.args.on[5][0], 'lit')
  147. assert.isFunction(log.args.on[5][1])
  148. assert.strictEqual(log.args.on[5][1], log.args.on[3][1])
  149. })
  150. test('EventEmitter.on was called correctly seventh time', () => {
  151. assert.lengthOf(log.args.on[6], 2)
  152. assert.strictEqual(log.args.on[6][0], 'end-arr')
  153. assert.isFunction(log.args.on[6][1])
  154. assert.notStrictEqual(log.args.on[6][1], log.args.on[0][1])
  155. assert.notStrictEqual(log.args.on[6][1], log.args.on[1][1])
  156. assert.notStrictEqual(log.args.on[6][1], log.args.on[2][1])
  157. assert.notStrictEqual(log.args.on[6][1], log.args.on[3][1])
  158. })
  159. test('EventEmitter.on was called correctly eighth time', () => {
  160. assert.lengthOf(log.args.on[7], 2)
  161. assert.strictEqual(log.args.on[7][0], 'end-obj')
  162. assert.isFunction(log.args.on[7][1])
  163. assert.strictEqual(log.args.on[7][1], log.args.on[6][1])
  164. })
  165. test('EventEmitter.on was called correctly ninth time', () => {
  166. assert.lengthOf(log.args.on[8], 2)
  167. assert.strictEqual(log.args.on[8][0], 'end')
  168. assert.isFunction(log.args.on[8][1])
  169. assert.notStrictEqual(log.args.on[8][1], log.args.on[0][1])
  170. assert.notStrictEqual(log.args.on[8][1], log.args.on[1][1])
  171. assert.notStrictEqual(log.args.on[8][1], log.args.on[2][1])
  172. assert.notStrictEqual(log.args.on[8][1], log.args.on[3][1])
  173. assert.notStrictEqual(log.args.on[8][1], log.args.on[6][1])
  174. })
  175. test('EventEmitter.on was called correctly tenth time', () => {
  176. assert.lengthOf(log.args.on[9], 2)
  177. assert.strictEqual(log.args.on[9][0], 'err')
  178. assert.isFunction(log.args.on[9][1])
  179. assert.notStrictEqual(log.args.on[9][1], log.args.on[0][1])
  180. assert.notStrictEqual(log.args.on[9][1], log.args.on[1][1])
  181. assert.notStrictEqual(log.args.on[9][1], log.args.on[2][1])
  182. assert.notStrictEqual(log.args.on[9][1], log.args.on[3][1])
  183. assert.notStrictEqual(log.args.on[9][1], log.args.on[6][1])
  184. assert.notStrictEqual(log.args.on[9][1], log.args.on[8][1])
  185. })
  186. test('EventEmitter.on was called correctly eleventh time', () => {
  187. assert.lengthOf(log.args.on[10], 2)
  188. assert.strictEqual(log.args.on[10][0], 'err-data')
  189. assert.isFunction(log.args.on[10][1])
  190. assert.strictEqual(log.args.on[10][1], log.args.on[9][1])
  191. })
  192. suite('array event:', () => {
  193. setup(() => {
  194. log.args.on[0][1]()
  195. })
  196. test('resolve was not called', () => {
  197. assert.strictEqual(log.counts.resolve, 0)
  198. })
  199. suite('end event:', () => {
  200. setup(done => {
  201. log.args.on[8][1]()
  202. setImmediate(done)
  203. })
  204. test('resolve was called once', () => {
  205. assert.strictEqual(log.counts.resolve, 1)
  206. })
  207. test('resolve was called correctly', () => {
  208. assert.isUndefined(log.these.resolve[0])
  209. assert.lengthOf(log.args.resolve[0], 1)
  210. assert.isArray(log.args.resolve[0][0])
  211. assert.lengthOf(log.args.resolve[0][0], 0)
  212. })
  213. test('reject was not called', () => {
  214. assert.strictEqual(log.counts.reject, 0)
  215. })
  216. })
  217. suite('string event:', () => {
  218. setup(() => {
  219. log.args.on[3][1]('foo')
  220. })
  221. test('resolve was not called', () => {
  222. assert.strictEqual(log.counts.resolve, 0)
  223. })
  224. suite('end event:', () => {
  225. setup(done => {
  226. log.args.on[8][1]()
  227. setImmediate(done)
  228. })
  229. test('resolve was called once', () => {
  230. assert.strictEqual(log.counts.resolve, 1)
  231. })
  232. test('resolve was called correctly', () => {
  233. assert.lengthOf(log.args.resolve[0], 1)
  234. assert.isArray(log.args.resolve[0][0])
  235. assert.lengthOf(log.args.resolve[0][0], 1)
  236. assert.strictEqual(log.args.resolve[0][0][0], 'foo')
  237. })
  238. })
  239. suite('string event:', () => {
  240. setup(() => {
  241. log.args.on[3][1]('bar')
  242. })
  243. test('resolve was not called', () => {
  244. assert.strictEqual(log.counts.resolve, 0)
  245. })
  246. suite('end event:', () => {
  247. setup(done => {
  248. log.args.on[8][1]()
  249. setImmediate(done)
  250. })
  251. test('resolve was called once', () => {
  252. assert.strictEqual(log.counts.resolve, 1)
  253. })
  254. test('resolve was called correctly', () => {
  255. assert.lengthOf(log.args.resolve[0][0], 2)
  256. assert.strictEqual(log.args.resolve[0][0][0], 'foo')
  257. assert.strictEqual(log.args.resolve[0][0][1], 'bar')
  258. })
  259. })
  260. })
  261. suite('array event:', () => {
  262. setup(() => {
  263. log.args.on[0][1]()
  264. })
  265. test('resolve was not called', () => {
  266. assert.strictEqual(log.counts.resolve, 0)
  267. })
  268. suite('end event:', () => {
  269. setup(done => {
  270. log.args.on[8][1]()
  271. setImmediate(done)
  272. })
  273. test('resolve was called once', () => {
  274. assert.strictEqual(log.counts.resolve, 1)
  275. })
  276. test('resolve was called correctly', () => {
  277. assert.lengthOf(log.args.resolve[0][0], 2)
  278. assert.strictEqual(log.args.resolve[0][0][0], 'foo')
  279. assert.isArray(log.args.resolve[0][0][1])
  280. assert.lengthOf(log.args.resolve[0][0][1], 0)
  281. })
  282. })
  283. suite('string event:', () => {
  284. setup(() => {
  285. log.args.on[3][1]('bar')
  286. })
  287. test('resolve was not called', () => {
  288. assert.strictEqual(log.counts.resolve, 0)
  289. })
  290. suite('end event:', () => {
  291. setup(done => {
  292. log.args.on[8][1]()
  293. setImmediate(done)
  294. })
  295. test('resolve was called once', () => {
  296. assert.strictEqual(log.counts.resolve, 1)
  297. })
  298. test('resolve was called correctly', () => {
  299. assert.lengthOf(log.args.resolve[0][0], 2)
  300. assert.strictEqual(log.args.resolve[0][0][0], 'foo')
  301. assert.isArray(log.args.resolve[0][0][1])
  302. assert.lengthOf(log.args.resolve[0][0][1], 1)
  303. assert.strictEqual(log.args.resolve[0][0][1][0], 'bar')
  304. })
  305. })
  306. suite('string event:', () => {
  307. setup(() => {
  308. log.args.on[3][1]('baz')
  309. })
  310. test('resolve was not called', () => {
  311. assert.strictEqual(log.counts.resolve, 0)
  312. })
  313. suite('end event:', () => {
  314. setup(done => {
  315. log.args.on[8][1]()
  316. setImmediate(done)
  317. })
  318. test('resolve was called once', () => {
  319. assert.strictEqual(log.counts.resolve, 1)
  320. })
  321. test('resolve was called correctly', () => {
  322. assert.lengthOf(log.args.resolve[0][0], 2)
  323. assert.strictEqual(log.args.resolve[0][0][0], 'foo')
  324. assert.isArray(log.args.resolve[0][0][1])
  325. assert.lengthOf(log.args.resolve[0][0][1], 2)
  326. assert.strictEqual(log.args.resolve[0][0][1][0], 'bar')
  327. assert.strictEqual(log.args.resolve[0][0][1][1], 'baz')
  328. })
  329. })
  330. })
  331. suite('endArray event:', () => {
  332. setup(() => {
  333. log.args.on[6][1]()
  334. })
  335. suite('string event:', () => {
  336. setup(() => {
  337. log.args.on[3][1]('baz')
  338. })
  339. test('resolve was not called', () => {
  340. assert.strictEqual(log.counts.resolve, 0)
  341. })
  342. suite('end event:', () => {
  343. setup(done => {
  344. log.args.on[8][1]()
  345. setImmediate(done)
  346. })
  347. test('resolve was called once', () => {
  348. assert.strictEqual(log.counts.resolve, 1)
  349. })
  350. test('resolve was called correctly', () => {
  351. assert.lengthOf(log.args.resolve[0][0], 3)
  352. assert.strictEqual(log.args.resolve[0][0][0], 'foo')
  353. assert.isArray(log.args.resolve[0][0][1])
  354. assert.lengthOf(log.args.resolve[0][0][1], 1)
  355. assert.strictEqual(log.args.resolve[0][0][1][0], 'bar')
  356. assert.strictEqual(log.args.resolve[0][0][2], 'baz')
  357. })
  358. })
  359. })
  360. })
  361. })
  362. })
  363. suite('object event:', () => {
  364. setup(() => {
  365. log.args.on[1][1]()
  366. })
  367. test('resolve was not called', () => {
  368. assert.strictEqual(log.counts.resolve, 0)
  369. })
  370. suite('end event:', () => {
  371. setup(done => {
  372. log.args.on[8][1]()
  373. setImmediate(done)
  374. })
  375. test('resolve was called once', () => {
  376. assert.strictEqual(log.counts.resolve, 1)
  377. })
  378. test('resolve was called correctly', () => {
  379. assert.lengthOf(log.args.resolve[0][0], 2)
  380. assert.strictEqual(log.args.resolve[0][0][0], 'foo')
  381. assert.isObject(log.args.resolve[0][0][1])
  382. assert.lengthOf(Object.keys(log.args.resolve[0][0][1]), 0)
  383. })
  384. })
  385. suite('property event:', () => {
  386. setup(() => {
  387. log.args.on[2][1]('bar')
  388. })
  389. suite('string event:', () => {
  390. setup(() => {
  391. log.args.on[3][1]('baz')
  392. })
  393. test('resolve was not called', () => {
  394. assert.strictEqual(log.counts.resolve, 0)
  395. })
  396. suite('end event:', () => {
  397. setup(done => {
  398. log.args.on[8][1]()
  399. setImmediate(done)
  400. })
  401. test('resolve was called once', () => {
  402. assert.strictEqual(log.counts.resolve, 1)
  403. })
  404. test('resolve was called correctly', () => {
  405. assert.lengthOf(log.args.resolve[0][0], 2)
  406. assert.strictEqual(log.args.resolve[0][0][0], 'foo')
  407. assert.isObject(log.args.resolve[0][0][1])
  408. assert.lengthOf(Object.keys(log.args.resolve[0][0][1]), 1)
  409. assert.strictEqual(log.args.resolve[0][0][1].bar, 'baz')
  410. })
  411. })
  412. suite('property event:', () => {
  413. setup(() => {
  414. log.args.on[2][1]('qux')
  415. })
  416. suite('string event:', () => {
  417. setup(() => {
  418. log.args.on[3][1]('wibble')
  419. })
  420. test('resolve was not called', () => {
  421. assert.strictEqual(log.counts.resolve, 0)
  422. })
  423. suite('end event:', () => {
  424. setup(done => {
  425. log.args.on[8][1]()
  426. setImmediate(done)
  427. })
  428. test('resolve was called once', () => {
  429. assert.strictEqual(log.counts.resolve, 1)
  430. })
  431. test('resolve was called correctly', () => {
  432. assert.lengthOf(log.args.resolve[0][0], 2)
  433. assert.strictEqual(log.args.resolve[0][0][0], 'foo')
  434. assert.isObject(log.args.resolve[0][0][1])
  435. assert.lengthOf(Object.keys(log.args.resolve[0][0][1]), 2)
  436. assert.strictEqual(log.args.resolve[0][0][1].bar, 'baz')
  437. assert.strictEqual(log.args.resolve[0][0][1].qux, 'wibble')
  438. })
  439. })
  440. })
  441. })
  442. suite('endObject event:', () => {
  443. setup(() => {
  444. log.args.on[7][1]()
  445. })
  446. suite('string event:', () => {
  447. setup(() => {
  448. log.args.on[3][1]('wibble')
  449. })
  450. test('resolve was not called', () => {
  451. assert.strictEqual(log.counts.resolve, 0)
  452. })
  453. suite('end event:', () => {
  454. setup(done => {
  455. log.args.on[8][1]()
  456. setImmediate(done)
  457. })
  458. test('resolve was called once', () => {
  459. assert.strictEqual(log.counts.resolve, 1)
  460. })
  461. test('resolve was called correctly', () => {
  462. assert.lengthOf(log.args.resolve[0][0], 3)
  463. assert.strictEqual(log.args.resolve[0][0][0], 'foo')
  464. assert.isObject(log.args.resolve[0][0][1])
  465. assert.lengthOf(Object.keys(log.args.resolve[0][0][1]), 1)
  466. assert.strictEqual(log.args.resolve[0][0][1].bar, 'baz')
  467. assert.strictEqual(log.args.resolve[0][0][2], 'wibble')
  468. })
  469. })
  470. })
  471. })
  472. })
  473. })
  474. })
  475. })
  476. suite('error event:', () => {
  477. setup(() => {
  478. log.args.on[9][1]('foo')
  479. })
  480. test('reject was not called', () => {
  481. assert.strictEqual(log.counts.reject, 0)
  482. })
  483. suite('end event:', () => {
  484. setup(done => {
  485. log.args.on[8][1]()
  486. setImmediate(done)
  487. })
  488. test('reject was called once', () => {
  489. assert.strictEqual(log.counts.reject, 1)
  490. })
  491. test('reject was called correctly', () => {
  492. assert.isUndefined(log.these.reject[0])
  493. assert.lengthOf(log.args.reject[0], 1)
  494. assert.strictEqual(log.args.reject[0][0], 'foo')
  495. })
  496. })
  497. suite('error event:', () => {
  498. setup(() => {
  499. log.args.on[9][1]('bar')
  500. })
  501. test('reject was not called', () => {
  502. assert.strictEqual(log.counts.reject, 0)
  503. })
  504. suite('end event:', () => {
  505. setup(done => {
  506. log.args.on[8][1]()
  507. setImmediate(done)
  508. })
  509. test('reject was called once', () => {
  510. assert.strictEqual(log.counts.reject, 1)
  511. })
  512. test('reject was called correctly', () => {
  513. assert.strictEqual(log.args.reject[0][0], 'foo')
  514. })
  515. })
  516. })
  517. })
  518. })
  519. suite('object event:', () => {
  520. setup(() => {
  521. log.args.on[1][1]()
  522. })
  523. test('resolve was not called', () => {
  524. assert.strictEqual(log.counts.resolve, 0)
  525. })
  526. suite('end event:', () => {
  527. setup(done => {
  528. log.args.on[8][1]()
  529. setImmediate(done)
  530. })
  531. test('resolve was called once', () => {
  532. assert.strictEqual(log.counts.resolve, 1)
  533. })
  534. test('resolve was called correctly', () => {
  535. assert.isObject(log.args.resolve[0][0])
  536. assert.lengthOf(Object.keys(log.args.resolve[0][0]), 0)
  537. })
  538. })
  539. })
  540. })
  541. suite('parse with reviver:', () => {
  542. let stream, options
  543. setup(() => {
  544. stream = {}
  545. options = { reviver: spooks.fn({ name: 'reviver', log: log, results: [ 'reviver result' ] }) }
  546. parse(stream, options)
  547. .then(spooks.fn({ name: 'resolve', log: log }))
  548. .catch(spooks.fn({ name: 'reject', log: log }))
  549. })
  550. test('walk was called once', () => {
  551. assert.strictEqual(log.counts.walk, 1)
  552. })
  553. test('EventEmitter.on was called eleven times', () => {
  554. assert.strictEqual(log.counts.on, 11)
  555. })
  556. suite('populated array events:', () => {
  557. setup(() => {
  558. log.args.on[0][1]()
  559. log.args.on[3][1]('foo')
  560. log.args.on[3][1]('bar')
  561. log.args.on[0][1]()
  562. log.args.on[0][1]()
  563. log.args.on[3][1]('baz')
  564. log.args.on[6][1]()
  565. log.args.on[3][1]('qux')
  566. log.args.on[6][1]()
  567. log.args.on[6][1]()
  568. })
  569. test('resolve was not called', () => {
  570. assert.strictEqual(log.counts.resolve, 0)
  571. })
  572. test('reviver was not called', () => {
  573. assert.strictEqual(log.counts.resolve, 0)
  574. })
  575. suite('end event:', () => {
  576. setup(done => {
  577. log.args.on[8][1]()
  578. setImmediate(done)
  579. })
  580. test('resolve was called once', () => {
  581. assert.strictEqual(log.counts.resolve, 1)
  582. })
  583. test('reviver was called six times', () => {
  584. assert.strictEqual(log.counts.reviver, 7)
  585. })
  586. test('reviver was called correctly first time', () => {
  587. assert.lengthOf(log.args.reviver[0], 2)
  588. assert.strictEqual(log.args.reviver[0][0], '0')
  589. assert.strictEqual(log.args.reviver[0][1], 'foo')
  590. })
  591. test('reviver was called correctly second time', () => {
  592. assert.strictEqual(log.args.reviver[1][0], '1')
  593. assert.strictEqual(log.args.reviver[1][1], 'bar')
  594. })
  595. test('reviver was called correctly third time', () => {
  596. assert.strictEqual(log.args.reviver[2][0], '0')
  597. assert.strictEqual(log.args.reviver[2][1], 'baz')
  598. })
  599. test('reviver was called correctly fourth time', () => {
  600. assert.strictEqual(log.args.reviver[3][0], '0')
  601. assert.isArray(log.args.reviver[3][1])
  602. assert.lengthOf(log.args.reviver[3][1], 1)
  603. assert.strictEqual(log.args.reviver[3][1][0], 'reviver result')
  604. })
  605. test('reviver was called correctly fifth time', () => {
  606. assert.strictEqual(log.args.reviver[4][0], '1')
  607. assert.strictEqual(log.args.reviver[4][1], 'qux')
  608. })
  609. test('reviver was called correctly sixth time', () => {
  610. assert.strictEqual(log.args.reviver[5][0], '2')
  611. assert.isArray(log.args.reviver[5][1])
  612. assert.lengthOf(log.args.reviver[5][1], 2)
  613. assert.strictEqual(log.args.reviver[5][1][0], 'reviver result')
  614. assert.strictEqual(log.args.reviver[5][1][1], 'reviver result')
  615. })
  616. test('reviver was called correctly seventh time', () => {
  617. assert.strictEqual(log.args.reviver[6][0], '')
  618. assert.isArray(log.args.reviver[6][1])
  619. assert.lengthOf(log.args.reviver[6][1], 3)
  620. assert.strictEqual(log.args.reviver[6][1][0], 'reviver result')
  621. assert.strictEqual(log.args.reviver[6][1][1], 'reviver result')
  622. assert.strictEqual(log.args.reviver[6][1][2], 'reviver result')
  623. })
  624. })
  625. })
  626. suite('populated object events:', () => {
  627. setup(() => {
  628. log.args.on[1][1]()
  629. log.args.on[2][1]('foo')
  630. log.args.on[1][1]()
  631. log.args.on[2][1]('bar')
  632. log.args.on[4][1](3.14159265359)
  633. log.args.on[7][1]()
  634. log.args.on[2][1]('baz')
  635. log.args.on[5][1](null)
  636. log.args.on[7][1]()
  637. })
  638. test('resolve was not called', () => {
  639. assert.strictEqual(log.counts.resolve, 0)
  640. })
  641. test('reviver was not called', () => {
  642. assert.strictEqual(log.counts.resolve, 0)
  643. })
  644. suite('end event:', () => {
  645. setup(done => {
  646. log.args.on[8][1]()
  647. setImmediate(done)
  648. })
  649. test('resolve was called once', () => {
  650. assert.strictEqual(log.counts.resolve, 1)
  651. })
  652. test('reviver was called four times', () => {
  653. assert.strictEqual(log.counts.reviver, 4)
  654. })
  655. test('reviver was called correctly first time', () => {
  656. assert.lengthOf(log.args.reviver[0], 2)
  657. assert.strictEqual(log.args.reviver[0][0], 'bar')
  658. assert.strictEqual(log.args.reviver[0][1], 3.14159265359)
  659. })
  660. test('reviver was called correctly second time', () => {
  661. assert.strictEqual(log.args.reviver[1][0], 'foo')
  662. assert.isObject(log.args.reviver[1][1])
  663. assert.lengthOf(Object.keys(log.args.reviver[1][1]), 1)
  664. assert.strictEqual(log.args.reviver[1][1].bar, 'reviver result')
  665. })
  666. test('reviver was called correctly third time', () => {
  667. assert.strictEqual(log.args.reviver[2][0], 'baz')
  668. assert.isNull(log.args.reviver[2][1])
  669. })
  670. test('reviver was called correctly fourth time', () => {
  671. assert.strictEqual(log.args.reviver[3][0], '')
  672. assert.isObject(log.args.reviver[3][1])
  673. assert.lengthOf(Object.keys(log.args.reviver[3][1]), 2)
  674. assert.strictEqual(log.args.reviver[3][1].foo, 'reviver result')
  675. assert.strictEqual(log.args.reviver[3][1].baz, 'reviver result')
  676. })
  677. })
  678. })
  679. })
  680. suite('parse with ndjson:', () => {
  681. let stream
  682. setup(() => {
  683. stream = {}
  684. parse(stream, { ndjson: true })
  685. .then(spooks.fn({ name: 'resolve', log: log }))
  686. .catch(spooks.fn({ name: 'reject', log: log }))
  687. })
  688. test('walk was called once', () => {
  689. assert.strictEqual(log.counts.walk, 1)
  690. })
  691. test('EventEmitter.on was called twelve times', () => {
  692. assert.strictEqual(log.counts.on, 12)
  693. assert.strictEqual(log.these.on[11], results.walk[0])
  694. })
  695. test('EventEmitter.on was called correctly first eleven times', () => {
  696. assert.strictEqual(log.args.on[0][0], 'arr')
  697. assert.strictEqual(log.args.on[1][0], 'obj')
  698. assert.strictEqual(log.args.on[2][0], 'pro')
  699. assert.strictEqual(log.args.on[3][0], 'str')
  700. assert.strictEqual(log.args.on[4][0], 'num')
  701. assert.strictEqual(log.args.on[5][0], 'lit')
  702. assert.strictEqual(log.args.on[6][0], 'end-arr')
  703. assert.strictEqual(log.args.on[7][0], 'end-obj')
  704. assert.strictEqual(log.args.on[8][0], 'end')
  705. assert.strictEqual(log.args.on[9][0], 'err')
  706. assert.strictEqual(log.args.on[10][0], 'err-data')
  707. })
  708. test('EventEmitter.on was called correctly twelfth time', () => {
  709. assert.lengthOf(log.args.on[11], 2)
  710. assert.strictEqual(log.args.on[11][0], 'end-line')
  711. assert.isFunction(log.args.on[11][1])
  712. assert.notStrictEqual(log.args.on[11][1], log.args.on[0][1])
  713. assert.notStrictEqual(log.args.on[11][1], log.args.on[1][1])
  714. assert.notStrictEqual(log.args.on[11][1], log.args.on[2][1])
  715. assert.notStrictEqual(log.args.on[11][1], log.args.on[3][1])
  716. assert.notStrictEqual(log.args.on[11][1], log.args.on[6][1])
  717. assert.notStrictEqual(log.args.on[11][1], log.args.on[8][1])
  718. assert.notStrictEqual(log.args.on[11][1], log.args.on[9][1])
  719. })
  720. test('emitter.pause was not called', () => {
  721. assert.strictEqual(log.counts.pause, 0)
  722. })
  723. test('emitter.removeAllListeners was not called', () => {
  724. assert.strictEqual(log.counts.removeAllListeners, 0)
  725. })
  726. suite('array, endArray, endLine:', () => {
  727. setup(done => {
  728. log.args.on[0][1]()
  729. log.args.on[6][1]()
  730. log.args.on[11][1]()
  731. setImmediate(done)
  732. })
  733. test('resolve was called once', () => {
  734. assert.strictEqual(log.counts.resolve, 1)
  735. })
  736. test('resolve was called correctly', () => {
  737. assert.lengthOf(log.args.resolve[0], 1)
  738. assert.isArray(log.args.resolve[0][0])
  739. assert.lengthOf(log.args.resolve[0][0], 0)
  740. })
  741. test('emitter.pause was called once', () => {
  742. assert.strictEqual(log.counts.pause, 1)
  743. })
  744. test('emitter.pause was called correctly', () => {
  745. assert.lengthOf(log.args.pause[0], 0)
  746. })
  747. test('emitter.removeAllListeners was called once', () => {
  748. assert.strictEqual(log.counts.removeAllListeners, 1)
  749. })
  750. test('emitter.removeAllListeners was called correctly', () => {
  751. assert.lengthOf(log.args.removeAllListeners[0], 0)
  752. })
  753. test('reject was not called', () => {
  754. assert.strictEqual(log.counts.reject, 0)
  755. })
  756. suite('parse with ndjson:', () => {
  757. setup(() => {
  758. parse(stream, { ndjson: true })
  759. .then(spooks.fn({ name: 'resolve2', log: log }))
  760. .catch(spooks.fn({ name: 'reject2', log: log }))
  761. })
  762. test('EventEmitter.on was called twelve times', () => {
  763. assert.strictEqual(log.counts.on, 24)
  764. })
  765. test('walk was not called', () => {
  766. assert.strictEqual(log.counts.walk, 1)
  767. })
  768. suite('string, end:', () => {
  769. setup(done => {
  770. log.args.on[15][1]('foo')
  771. log.args.on[20][1]()
  772. setImmediate(done)
  773. })
  774. test('resolve was called once', () => {
  775. assert.strictEqual(log.counts.resolve, 1)
  776. assert.strictEqual(log.counts.resolve2, 1)
  777. })
  778. test('resolve was called correctly', () => {
  779. assert.lengthOf(log.args.resolve2[0], 1)
  780. assert.strictEqual(log.args.resolve2[0][0], 'foo')
  781. })
  782. test('emitter.pause was called once', () => {
  783. assert.strictEqual(log.counts.pause, 2)
  784. })
  785. test('emitter.removeAllListeners was called once', () => {
  786. assert.strictEqual(log.counts.removeAllListeners, 2)
  787. })
  788. test('reject was not called', () => {
  789. assert.strictEqual(log.counts.reject, 0)
  790. assert.strictEqual(log.counts.reject2, 0)
  791. })
  792. suite('parse with ndjson:', () => {
  793. setup(() => {
  794. parse(stream, { ndjson: true })
  795. .then(spooks.fn({ name: 'resolve3', log: log }))
  796. .catch(spooks.fn({ name: 'reject3', log: log }))
  797. })
  798. test('EventEmitter.on was called twelve times', () => {
  799. assert.strictEqual(log.counts.on, 36)
  800. })
  801. test('walk was not called', () => {
  802. assert.strictEqual(log.counts.walk, 1)
  803. })
  804. suite('end:', () => {
  805. setup(done => {
  806. log.args.on[32][1]()
  807. setImmediate(done)
  808. })
  809. test('resolve was called once', () => {
  810. assert.strictEqual(log.counts.resolve, 1)
  811. assert.strictEqual(log.counts.resolve2, 1)
  812. assert.strictEqual(log.counts.resolve3, 1)
  813. })
  814. test('resolve was called correctly', () => {
  815. assert.lengthOf(log.args.resolve3[0], 1)
  816. assert.strictEqual(log.args.resolve3[0][0], undefined)
  817. })
  818. test('emitter.pause was called once', () => {
  819. assert.strictEqual(log.counts.pause, 3)
  820. })
  821. test('emitter.removeAllListeners was called once', () => {
  822. assert.strictEqual(log.counts.removeAllListeners, 3)
  823. })
  824. test('reject was not called', () => {
  825. assert.strictEqual(log.counts.reject, 0)
  826. assert.strictEqual(log.counts.reject2, 0)
  827. assert.strictEqual(log.counts.reject3, 0)
  828. })
  829. })
  830. })
  831. })
  832. })
  833. suite('parse with ndjson and fresh stream:', () => {
  834. setup(() => {
  835. parse({}, { ndjson: true })
  836. .then(spooks.fn({ name: 'resolve2', log: log }))
  837. .catch(spooks.fn({ name: 'reject2', log: log }))
  838. })
  839. test('EventEmitter.on was called twelve times', () => {
  840. assert.strictEqual(log.counts.on, 24)
  841. })
  842. test('walk was called once', () => {
  843. assert.strictEqual(log.counts.walk, 2)
  844. })
  845. })
  846. })
  847. })
  848. })
  849. })