streamify.js 33 KB

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