streamify.js 34 KB

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