项目原始demo,不改动
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
Bu depo arşivlendi. Dosyaları görüntüleyebilir ve klonlayabilirsiniz ama işlem gönderemez ve konu/değişiklik isteği açamazsınız.
 
 
 
 

3097 satır
79 KiB

  1. 'use strict'
  2. const assert = require('chai').assert
  3. const spooks = require('spooks')
  4. const Readable = require('stream').Readable
  5. const events = require('../../src/events')
  6. const modulePath = '../../src/walk'
  7. suite('walk:', () => {
  8. let log
  9. setup(() => {
  10. log = {}
  11. })
  12. test('require does not throw', () => {
  13. assert.doesNotThrow(() => {
  14. require(modulePath)
  15. })
  16. })
  17. test('require returns function', () => {
  18. assert.isFunction(require(modulePath))
  19. })
  20. suite('require:', () => {
  21. let walk
  22. setup(() => {
  23. walk = require(modulePath)
  24. })
  25. test('walk throws without readable stream', () => {
  26. assert.throws(() => {
  27. walk({ on: () => {} })
  28. })
  29. })
  30. test('walk does not throw with readable stream', () => {
  31. assert.doesNotThrow(() => {
  32. walk(new Readable())
  33. })
  34. })
  35. test('walk returns emitter', () => {
  36. assert.instanceOf(walk(new Readable()), require('events').EventEmitter)
  37. })
  38. test('EventEmitter is decorated with pause method', () => {
  39. assert.isFunction(walk(new Readable()).pause)
  40. assert.lengthOf(walk(new Readable()).pause, 0)
  41. })
  42. test('pause method returns continue function', () => {
  43. assert.isFunction(walk(new Readable()).pause())
  44. assert.lengthOf(walk(new Readable()).pause(), 0)
  45. })
  46. suite('empty json:', () => {
  47. let stream, emitter
  48. setup(done => {
  49. stream = new Readable()
  50. stream._read = () => {}
  51. emitter = walk(stream)
  52. stream.push('')
  53. stream.push(null)
  54. Object.keys(events).forEach(key => {
  55. emitter.on(events[key], spooks.fn({
  56. name: key,
  57. log: log
  58. }))
  59. })
  60. emitter.on(events.end, done)
  61. })
  62. test('end event occurred once', () => {
  63. assert.strictEqual(log.counts.end, 1)
  64. })
  65. test('end event was dispatched correctly', () => {
  66. assert.lengthOf(log.args.end[0], 0)
  67. })
  68. test('array event did not occur', () => {
  69. assert.strictEqual(log.counts.array, 0)
  70. })
  71. test('object event did not occur', () => {
  72. assert.strictEqual(log.counts.object, 0)
  73. })
  74. test('property event did not occur', () => {
  75. assert.strictEqual(log.counts.property, 0)
  76. })
  77. test('string event did not occur', () => {
  78. assert.strictEqual(log.counts.string, 0)
  79. })
  80. test('number event did not occur', () => {
  81. assert.strictEqual(log.counts.number, 0)
  82. })
  83. test('literal event did not occur', () => {
  84. assert.strictEqual(log.counts.literal, 0)
  85. })
  86. test('endArray event did not occur', () => {
  87. assert.strictEqual(log.counts.endArray, 0)
  88. })
  89. test('endObject event did not occur', () => {
  90. assert.strictEqual(log.counts.endObject, 0)
  91. })
  92. test('error event did not occur', () => {
  93. assert.strictEqual(log.counts.error, 0)
  94. })
  95. test('endLine event did not occur', () => {
  96. assert.strictEqual(log.counts.endLine, 0)
  97. })
  98. test('endPrefix event did not occur', () => {
  99. assert.strictEqual(log.counts.endPrefix, 0)
  100. })
  101. })
  102. suite('empty array:', () => {
  103. let stream, emitter
  104. setup(done => {
  105. stream = new Readable()
  106. stream._read = () => {}
  107. emitter = walk(stream)
  108. stream.push('[]')
  109. stream.push(null)
  110. Object.keys(events).forEach(key => {
  111. emitter.on(events[key], spooks.fn({
  112. name: key,
  113. log: log
  114. }))
  115. })
  116. emitter.on(events.end, done)
  117. })
  118. test('array event occurred once', () => {
  119. assert.strictEqual(log.counts.array, 1)
  120. })
  121. test('array event was dispatched correctly', () => {
  122. assert.lengthOf(log.args.array[0], 0)
  123. })
  124. test('endArray event occurred once', () => {
  125. assert.strictEqual(log.counts.endArray, 1)
  126. })
  127. test('endArray event was dispatched correctly', () => {
  128. assert.lengthOf(log.args.endArray[0], 0)
  129. })
  130. test('end event occurred once', () => {
  131. assert.strictEqual(log.counts.end, 1)
  132. })
  133. test('object event did not occur', () => {
  134. assert.strictEqual(log.counts.object, 0)
  135. })
  136. test('property event did not occur', () => {
  137. assert.strictEqual(log.counts.property, 0)
  138. })
  139. test('string event did not occur', () => {
  140. assert.strictEqual(log.counts.string, 0)
  141. })
  142. test('number event did not occur', () => {
  143. assert.strictEqual(log.counts.number, 0)
  144. })
  145. test('literal event did not occur', () => {
  146. assert.strictEqual(log.counts.literal, 0)
  147. })
  148. test('endObject event did not occur', () => {
  149. assert.strictEqual(log.counts.endObject, 0)
  150. })
  151. test('error event did not occur', () => {
  152. assert.strictEqual(log.counts.error, 0)
  153. })
  154. test('endLine event did not occur', () => {
  155. assert.strictEqual(log.counts.endLine, 0)
  156. })
  157. test('endPrefix event did not occur', () => {
  158. assert.strictEqual(log.counts.endPrefix, 0)
  159. })
  160. })
  161. suite('empty object:', () => {
  162. let stream, emitter
  163. setup(done => {
  164. stream = new Readable()
  165. stream._read = () => {}
  166. emitter = walk(stream)
  167. stream.push('{}')
  168. stream.push(null)
  169. Object.keys(events).forEach(key => {
  170. emitter.on(events[key], spooks.fn({
  171. name: key,
  172. log: log
  173. }))
  174. })
  175. emitter.on(events.end, done)
  176. })
  177. test('object event occurred once', () => {
  178. assert.strictEqual(log.counts.object, 1)
  179. })
  180. test('object event was dispatched correctly', () => {
  181. assert.lengthOf(log.args.object[0], 0)
  182. })
  183. test('endObject event occurred once', () => {
  184. assert.strictEqual(log.counts.endObject, 1)
  185. })
  186. test('endObject event was dispatched correctly', () => {
  187. assert.lengthOf(log.args.endObject[0], 0)
  188. })
  189. test('end event occurred once', () => {
  190. assert.strictEqual(log.counts.end, 1)
  191. })
  192. test('array event did not occur', () => {
  193. assert.strictEqual(log.counts.array, 0)
  194. })
  195. test('property event did not occur', () => {
  196. assert.strictEqual(log.counts.property, 0)
  197. })
  198. test('string event did not occur', () => {
  199. assert.strictEqual(log.counts.string, 0)
  200. })
  201. test('number event did not occur', () => {
  202. assert.strictEqual(log.counts.number, 0)
  203. })
  204. test('literal event did not occur', () => {
  205. assert.strictEqual(log.counts.literal, 0)
  206. })
  207. test('endArray event did not occur', () => {
  208. assert.strictEqual(log.counts.endArray, 0)
  209. })
  210. test('error event did not occur', () => {
  211. assert.strictEqual(log.counts.error, 0)
  212. })
  213. test('endLine event did not occur', () => {
  214. assert.strictEqual(log.counts.endLine, 0)
  215. })
  216. test('endPrefix event did not occur', () => {
  217. assert.strictEqual(log.counts.endPrefix, 0)
  218. })
  219. })
  220. suite('string:', () => {
  221. let stream, emitter
  222. setup(done => {
  223. stream = new Readable()
  224. stream._read = () => {}
  225. emitter = walk(stream)
  226. stream.push('"\\"the quick brown fox\r\n\\tjumps\\u00a0over the lazy\\u1680dog\\""')
  227. stream.push(null)
  228. Object.keys(events).forEach(key => {
  229. emitter.on(events[key], spooks.fn({
  230. name: key,
  231. log: log
  232. }))
  233. })
  234. emitter.on(events.end, done)
  235. })
  236. test('string event occurred once', () => {
  237. assert.strictEqual(log.counts.string, 1)
  238. })
  239. test('string event was dispatched correctly', () => {
  240. assert.lengthOf(log.args.string[0], 1)
  241. assert.strictEqual(log.args.string[0][0], '"the quick brown fox\r\n\tjumps\u00a0over the lazy\u1680dog"')
  242. })
  243. test('end event occurred once', () => {
  244. assert.strictEqual(log.counts.end, 1)
  245. })
  246. test('array event did not occur', () => {
  247. assert.strictEqual(log.counts.array, 0)
  248. })
  249. test('object event did not occur', () => {
  250. assert.strictEqual(log.counts.object, 0)
  251. })
  252. test('property event did not occur', () => {
  253. assert.strictEqual(log.counts.property, 0)
  254. })
  255. test('number event did not occur', () => {
  256. assert.strictEqual(log.counts.number, 0)
  257. })
  258. test('literal event did not occur', () => {
  259. assert.strictEqual(log.counts.literal, 0)
  260. })
  261. test('endArray event did not occur', () => {
  262. assert.strictEqual(log.counts.endArray, 0)
  263. })
  264. test('endObject event did not occur', () => {
  265. assert.strictEqual(log.counts.endObject, 0)
  266. })
  267. test('error event did not occur', () => {
  268. assert.strictEqual(log.counts.error, 0)
  269. })
  270. test('endLine event did not occur', () => {
  271. assert.strictEqual(log.counts.endLine, 0)
  272. })
  273. test('endPrefix event did not occur', () => {
  274. assert.strictEqual(log.counts.endPrefix, 0)
  275. })
  276. })
  277. suite('number:', () => {
  278. let stream, emitter
  279. setup(done => {
  280. stream = new Readable()
  281. stream._read = () => {}
  282. emitter = walk(stream)
  283. stream.push('-3.14159265359e+42')
  284. stream.push(null)
  285. Object.keys(events).forEach(key => {
  286. emitter.on(events[key], spooks.fn({
  287. name: key,
  288. log: log
  289. }))
  290. })
  291. emitter.on(events.end, done)
  292. })
  293. test('number event occurred once', () => {
  294. assert.strictEqual(log.counts.number, 1)
  295. })
  296. test('number event was dispatched correctly', () => {
  297. assert.lengthOf(log.args.number[0], 1)
  298. assert.strictEqual(log.args.number[0][0], -3.14159265359e+42)
  299. })
  300. test('end event occurred once', () => {
  301. assert.strictEqual(log.counts.end, 1)
  302. })
  303. test('array event did not occur', () => {
  304. assert.strictEqual(log.counts.array, 0)
  305. })
  306. test('object event did not occur', () => {
  307. assert.strictEqual(log.counts.object, 0)
  308. })
  309. test('property event did not occur', () => {
  310. assert.strictEqual(log.counts.property, 0)
  311. })
  312. test('string event did not occur', () => {
  313. assert.strictEqual(log.counts.string, 0)
  314. })
  315. test('literal event did not occur', () => {
  316. assert.strictEqual(log.counts.literal, 0)
  317. })
  318. test('endArray event did not occur', () => {
  319. assert.strictEqual(log.counts.endArray, 0)
  320. })
  321. test('endObject event did not occur', () => {
  322. assert.strictEqual(log.counts.endObject, 0)
  323. })
  324. test('error event did not occur', () => {
  325. assert.strictEqual(log.counts.error, 0)
  326. })
  327. test('endLine event did not occur', () => {
  328. assert.strictEqual(log.counts.endLine, 0)
  329. })
  330. test('endPrefix event did not occur', () => {
  331. assert.strictEqual(log.counts.endPrefix, 0)
  332. })
  333. })
  334. suite('literal false:', () => {
  335. let stream, emitter
  336. setup(done => {
  337. stream = new Readable()
  338. stream._read = () => {}
  339. emitter = walk(stream)
  340. stream.push('false')
  341. stream.push(null)
  342. Object.keys(events).forEach(key => {
  343. emitter.on(events[key], spooks.fn({
  344. name: key,
  345. log: log
  346. }))
  347. })
  348. emitter.on(events.end, done)
  349. })
  350. test('literal event occurred once', () => {
  351. assert.strictEqual(log.counts.literal, 1)
  352. })
  353. test('literal event was dispatched correctly', () => {
  354. assert.lengthOf(log.args.literal[0], 1)
  355. assert.strictEqual(log.args.literal[0][0], false)
  356. })
  357. test('end event occurred once', () => {
  358. assert.strictEqual(log.counts.end, 1)
  359. })
  360. test('array event did not occur', () => {
  361. assert.strictEqual(log.counts.array, 0)
  362. })
  363. test('object event did not occur', () => {
  364. assert.strictEqual(log.counts.object, 0)
  365. })
  366. test('property event did not occur', () => {
  367. assert.strictEqual(log.counts.property, 0)
  368. })
  369. test('string event did not occur', () => {
  370. assert.strictEqual(log.counts.string, 0)
  371. })
  372. test('number event did not occur', () => {
  373. assert.strictEqual(log.counts.number, 0)
  374. })
  375. test('endArray event did not occur', () => {
  376. assert.strictEqual(log.counts.endArray, 0)
  377. })
  378. test('endObject event did not occur', () => {
  379. assert.strictEqual(log.counts.endObject, 0)
  380. })
  381. test('error event did not occur', () => {
  382. assert.strictEqual(log.counts.error, 0)
  383. })
  384. test('endLine event did not occur', () => {
  385. assert.strictEqual(log.counts.endLine, 0)
  386. })
  387. test('endPrefix event did not occur', () => {
  388. assert.strictEqual(log.counts.endPrefix, 0)
  389. })
  390. })
  391. suite('literal null:', () => {
  392. let stream, emitter
  393. setup(done => {
  394. stream = new Readable()
  395. stream._read = () => {}
  396. emitter = walk(stream)
  397. stream.push('null')
  398. stream.push(null)
  399. Object.keys(events).forEach(key => {
  400. emitter.on(events[key], spooks.fn({
  401. name: key,
  402. log: log
  403. }))
  404. })
  405. emitter.on(events.end, done)
  406. })
  407. test('literal event occurred once', () => {
  408. assert.strictEqual(log.counts.literal, 1)
  409. })
  410. test('literal event was dispatched correctly', () => {
  411. assert.strictEqual(log.args.literal[0][0], null)
  412. })
  413. test('end event occurred once', () => {
  414. assert.strictEqual(log.counts.end, 1)
  415. })
  416. test('array event did not occur', () => {
  417. assert.strictEqual(log.counts.array, 0)
  418. })
  419. test('object event did not occur', () => {
  420. assert.strictEqual(log.counts.object, 0)
  421. })
  422. test('property event did not occur', () => {
  423. assert.strictEqual(log.counts.property, 0)
  424. })
  425. test('string event did not occur', () => {
  426. assert.strictEqual(log.counts.string, 0)
  427. })
  428. test('number event did not occur', () => {
  429. assert.strictEqual(log.counts.number, 0)
  430. })
  431. test('endArray event did not occur', () => {
  432. assert.strictEqual(log.counts.endArray, 0)
  433. })
  434. test('endObject event did not occur', () => {
  435. assert.strictEqual(log.counts.endObject, 0)
  436. })
  437. test('error event did not occur', () => {
  438. assert.strictEqual(log.counts.error, 0)
  439. })
  440. test('endLine event did not occur', () => {
  441. assert.strictEqual(log.counts.endLine, 0)
  442. })
  443. test('endPrefix event did not occur', () => {
  444. assert.strictEqual(log.counts.endPrefix, 0)
  445. })
  446. })
  447. suite('literal true:', () => {
  448. let stream, emitter
  449. setup(done => {
  450. stream = new Readable()
  451. stream._read = () => {}
  452. emitter = walk(stream)
  453. stream.push('true')
  454. stream.push(null)
  455. Object.keys(events).forEach(key => {
  456. emitter.on(events[key], spooks.fn({
  457. name: key,
  458. log: log
  459. }))
  460. })
  461. emitter.on(events.end, done)
  462. })
  463. test('literal event occurred once', () => {
  464. assert.strictEqual(log.counts.literal, 1)
  465. })
  466. test('literal event was dispatched correctly', () => {
  467. assert.strictEqual(log.args.literal[0][0], true)
  468. })
  469. test('end event occurred once', () => {
  470. assert.strictEqual(log.counts.end, 1)
  471. })
  472. test('array event did not occur', () => {
  473. assert.strictEqual(log.counts.array, 0)
  474. })
  475. test('object event did not occur', () => {
  476. assert.strictEqual(log.counts.object, 0)
  477. })
  478. test('property event did not occur', () => {
  479. assert.strictEqual(log.counts.property, 0)
  480. })
  481. test('string event did not occur', () => {
  482. assert.strictEqual(log.counts.string, 0)
  483. })
  484. test('number event did not occur', () => {
  485. assert.strictEqual(log.counts.number, 0)
  486. })
  487. test('endArray event did not occur', () => {
  488. assert.strictEqual(log.counts.endArray, 0)
  489. })
  490. test('endObject event did not occur', () => {
  491. assert.strictEqual(log.counts.endObject, 0)
  492. })
  493. test('error event did not occur', () => {
  494. assert.strictEqual(log.counts.error, 0)
  495. })
  496. test('endLine event did not occur', () => {
  497. assert.strictEqual(log.counts.endLine, 0)
  498. })
  499. test('endPrefix event did not occur', () => {
  500. assert.strictEqual(log.counts.endPrefix, 0)
  501. })
  502. })
  503. suite('badly-closed array:', () => {
  504. let stream, emitter
  505. setup(done => {
  506. stream = new Readable()
  507. stream._read = () => {}
  508. emitter = walk(stream)
  509. stream.push('[}')
  510. stream.push(null)
  511. Object.keys(events).forEach(key => {
  512. emitter.on(events[key], spooks.fn({
  513. name: key,
  514. log: log
  515. }))
  516. })
  517. emitter.on(events.end, done)
  518. })
  519. test('array event occurred once', () => {
  520. assert.strictEqual(log.counts.array, 1)
  521. })
  522. test('error event occurred twice', () => {
  523. assert.strictEqual(log.counts.error, 2)
  524. })
  525. test('error event was dispatched correctly first time', () => {
  526. assert.lengthOf(log.args.error[0], 1)
  527. assert.instanceOf(log.args.error[0][0], Error)
  528. assert.strictEqual(log.args.error[0][0].actual, '}')
  529. assert.strictEqual(log.args.error[0][0].expected, 'value')
  530. assert.strictEqual(log.args.error[0][0].lineNumber, 1)
  531. assert.strictEqual(log.args.error[0][0].columnNumber, 2)
  532. })
  533. test('error event was dispatched correctly second time', () => {
  534. assert.strictEqual(log.args.error[1][0].actual, 'EOF')
  535. assert.strictEqual(log.args.error[1][0].expected, ']')
  536. assert.strictEqual(log.args.error[1][0].lineNumber, 1)
  537. assert.strictEqual(log.args.error[1][0].columnNumber, 3)
  538. })
  539. test('end event occurred once', () => {
  540. assert.strictEqual(log.counts.end, 1)
  541. })
  542. test('object event did not occur', () => {
  543. assert.strictEqual(log.counts.object, 0)
  544. })
  545. test('endArray event did not occur', () => {
  546. assert.strictEqual(log.counts.endArray, 0)
  547. })
  548. test('endObject event did not occur', () => {
  549. assert.strictEqual(log.counts.endObject, 0)
  550. })
  551. })
  552. suite('badly-closed object:', () => {
  553. let stream, emitter
  554. setup(done => {
  555. stream = new Readable()
  556. stream._read = () => {}
  557. emitter = walk(stream)
  558. stream.push('{]')
  559. stream.push(null)
  560. Object.keys(events).forEach(key => {
  561. emitter.on(events[key], spooks.fn({
  562. name: key,
  563. log: log
  564. }))
  565. })
  566. emitter.on(events.end, done)
  567. })
  568. test('object event occurred once', () => {
  569. assert.strictEqual(log.counts.object, 1)
  570. })
  571. test('error event occurred three times', () => {
  572. assert.strictEqual(log.counts.error, 3)
  573. })
  574. test('error event was dispatched correctly first time', () => {
  575. assert.strictEqual(log.args.error[0][0].actual, ']')
  576. assert.strictEqual(log.args.error[0][0].expected, '"')
  577. assert.strictEqual(log.args.error[0][0].lineNumber, 1)
  578. assert.strictEqual(log.args.error[0][0].columnNumber, 2)
  579. })
  580. test('error event was dispatched correctly second time', () => {
  581. assert.strictEqual(log.args.error[1][0].actual, 'EOF')
  582. assert.strictEqual(log.args.error[1][0].expected, '"')
  583. assert.strictEqual(log.args.error[1][0].lineNumber, 1)
  584. assert.strictEqual(log.args.error[1][0].columnNumber, 3)
  585. })
  586. test('error event was dispatched correctly third time', () => {
  587. assert.strictEqual(log.args.error[2][0].actual, 'EOF')
  588. assert.strictEqual(log.args.error[2][0].expected, '}')
  589. assert.strictEqual(log.args.error[2][0].lineNumber, 1)
  590. assert.strictEqual(log.args.error[2][0].columnNumber, 3)
  591. })
  592. test('end event occurred once', () => {
  593. assert.strictEqual(log.counts.end, 1)
  594. })
  595. test('array event did not occur', () => {
  596. assert.strictEqual(log.counts.array, 0)
  597. })
  598. test('endArray event did not occur', () => {
  599. assert.strictEqual(log.counts.endArray, 0)
  600. })
  601. test('endObject event did not occur', () => {
  602. assert.strictEqual(log.counts.endObject, 0)
  603. })
  604. })
  605. suite('string containing bad escape sequence:', () => {
  606. let stream, emitter
  607. setup(done => {
  608. stream = new Readable()
  609. stream._read = () => {}
  610. emitter = walk(stream)
  611. stream.push('"\\"the quick brown fox\r\n\\tjumps over the lazy\\xdog\\""')
  612. stream.push(null)
  613. Object.keys(events).forEach(key => {
  614. emitter.on(events[key], spooks.fn({
  615. name: key,
  616. log: log
  617. }))
  618. })
  619. emitter.on(events.end, done)
  620. })
  621. test('error event occurred once', () => {
  622. assert.strictEqual(log.counts.error, 1)
  623. })
  624. test('error event was dispatched correctly', () => {
  625. assert.strictEqual(log.args.error[0][0].actual, 'x')
  626. assert.strictEqual(log.args.error[0][0].expected, 'escape character')
  627. assert.strictEqual(log.args.error[0][0].lineNumber, 2)
  628. assert.strictEqual(log.args.error[0][0].columnNumber, 23)
  629. })
  630. test('string event occurred once', () => {
  631. assert.strictEqual(log.counts.string, 1)
  632. })
  633. test('string event was dispatched correctly', () => {
  634. assert.strictEqual(log.args.string[0][0], '"the quick brown fox\r\n\tjumps over the lazy\\xdog"')
  635. })
  636. test('end event occurred once', () => {
  637. assert.strictEqual(log.counts.end, 1)
  638. })
  639. })
  640. suite('string containing bad unicode escape sequence:', () => {
  641. let stream, emitter
  642. setup(done => {
  643. stream = new Readable()
  644. stream._read = () => {}
  645. emitter = walk(stream)
  646. stream.push('"\\u012g"')
  647. stream.push(null)
  648. Object.keys(events).forEach(key => {
  649. emitter.on(events[key], spooks.fn({
  650. name: key,
  651. log: log
  652. }))
  653. })
  654. emitter.on(events.end, done)
  655. })
  656. test('error event occurred once', () => {
  657. assert.strictEqual(log.counts.error, 1)
  658. })
  659. test('error event was dispatched correctly', () => {
  660. assert.strictEqual(log.args.error[0][0].actual, 'g')
  661. assert.strictEqual(log.args.error[0][0].expected, 'hex digit')
  662. assert.strictEqual(log.args.error[0][0].lineNumber, 1)
  663. assert.strictEqual(log.args.error[0][0].columnNumber, 7)
  664. })
  665. test('string event occurred once', () => {
  666. assert.strictEqual(log.counts.string, 1)
  667. })
  668. test('string event was dispatched correctly', () => {
  669. assert.strictEqual(log.args.string[0][0], '\\u012g')
  670. })
  671. test('end event occurred once', () => {
  672. assert.strictEqual(log.counts.end, 1)
  673. })
  674. })
  675. suite('unterminated string:', () => {
  676. let stream, emitter
  677. setup(done => {
  678. stream = new Readable()
  679. stream._read = () => {}
  680. emitter = walk(stream)
  681. stream.push('"foo')
  682. stream.push(null)
  683. Object.keys(events).forEach(key => {
  684. emitter.on(events[key], spooks.fn({
  685. name: key,
  686. log: log
  687. }))
  688. })
  689. emitter.on(events.end, done)
  690. })
  691. test('error event occurred once', () => {
  692. assert.strictEqual(log.counts.error, 1)
  693. })
  694. test('error event was dispatched correctly', () => {
  695. assert.strictEqual(log.args.error[0][0].actual, 'EOF')
  696. assert.strictEqual(log.args.error[0][0].expected, '"')
  697. assert.strictEqual(log.args.error[0][0].lineNumber, 1)
  698. assert.strictEqual(log.args.error[0][0].columnNumber, 5)
  699. })
  700. test('end event occurred once', () => {
  701. assert.strictEqual(log.counts.end, 1)
  702. })
  703. test('string event did not occur', () => {
  704. assert.strictEqual(log.counts.string, 0)
  705. })
  706. })
  707. suite('bad number:', () => {
  708. let stream, emitter
  709. setup(done => {
  710. stream = new Readable()
  711. stream._read = () => {}
  712. emitter = walk(stream)
  713. stream.push('1e')
  714. stream.push(null)
  715. Object.keys(events).forEach(key => {
  716. emitter.on(events[key], spooks.fn({
  717. name: key,
  718. log: log
  719. }))
  720. })
  721. emitter.on(events.end, done)
  722. })
  723. test('number event did not occur', () => {
  724. assert.strictEqual(log.counts.number, 0)
  725. })
  726. test('error event occurred once', () => {
  727. assert.strictEqual(log.counts.error, 1)
  728. })
  729. test('error event was dispatched correctly first time', () => {
  730. assert.strictEqual(log.args.error[0][0].actual, 'EOF')
  731. assert.strictEqual(log.args.error[0][0].expected, 'exponent')
  732. assert.strictEqual(log.args.error[0][0].lineNumber, 1)
  733. assert.strictEqual(log.args.error[0][0].columnNumber, 3)
  734. })
  735. test('end event occurred once', () => {
  736. assert.strictEqual(log.counts.end, 1)
  737. })
  738. test('literal event did not occur', () => {
  739. assert.strictEqual(log.counts.literal, 0)
  740. })
  741. })
  742. suite('alternative bad number:', () => {
  743. let stream, emitter
  744. setup(done => {
  745. stream = new Readable()
  746. stream._read = () => {}
  747. emitter = walk(stream)
  748. stream.push('42f')
  749. stream.push(null)
  750. Object.keys(events).forEach(key => {
  751. emitter.on(events[key], spooks.fn({
  752. name: key,
  753. log: log
  754. }))
  755. })
  756. emitter.on(events.end, done)
  757. })
  758. test('number event occurred once', () => {
  759. assert.strictEqual(log.counts.number, 1)
  760. })
  761. test('number event was dispatched correctly', () => {
  762. assert.strictEqual(log.args.number[0][0], 42)
  763. })
  764. test('error event occurred twice', () => {
  765. assert.strictEqual(log.counts.error, 2)
  766. })
  767. test('error event was dispatched correctly first time', () => {
  768. assert.strictEqual(log.args.error[0][0].actual, 'f')
  769. assert.strictEqual(log.args.error[0][0].expected, 'EOF')
  770. assert.strictEqual(log.args.error[0][0].lineNumber, 1)
  771. assert.strictEqual(log.args.error[0][0].columnNumber, 3)
  772. })
  773. test('error event was dispatched correctly second time', () => {
  774. assert.strictEqual(log.args.error[1][0].actual, 'EOF')
  775. assert.strictEqual(log.args.error[1][0].expected, 'a')
  776. assert.strictEqual(log.args.error[1][0].lineNumber, 1)
  777. assert.strictEqual(log.args.error[1][0].columnNumber, 4)
  778. })
  779. test('end event occurred once', () => {
  780. assert.strictEqual(log.counts.end, 1)
  781. })
  782. test('literal event did not occur', () => {
  783. assert.strictEqual(log.counts.literal, 0)
  784. })
  785. })
  786. suite('bad literal false:', () => {
  787. let stream, emitter
  788. setup(done => {
  789. stream = new Readable()
  790. stream._read = () => {}
  791. emitter = walk(stream)
  792. stream.push('falsd')
  793. stream.push(null)
  794. Object.keys(events).forEach(key => {
  795. emitter.on(events[key], spooks.fn({
  796. name: key,
  797. log: log
  798. }))
  799. })
  800. emitter.on(events.end, done)
  801. })
  802. test('error event occurred once', () => {
  803. assert.strictEqual(log.counts.error, 1)
  804. })
  805. test('error event was dispatched correctly', () => {
  806. assert.strictEqual(log.args.error[0][0].actual, 'd')
  807. assert.strictEqual(log.args.error[0][0].expected, 'e')
  808. assert.strictEqual(log.args.error[0][0].lineNumber, 1)
  809. assert.strictEqual(log.args.error[0][0].columnNumber, 5)
  810. })
  811. test('end event occurred once', () => {
  812. assert.strictEqual(log.counts.end, 1)
  813. })
  814. test('literal event did not occur', () => {
  815. assert.strictEqual(log.counts.literal, 0)
  816. })
  817. })
  818. suite('bad literal null:', () => {
  819. let stream, emitter
  820. setup(done => {
  821. stream = new Readable()
  822. stream._read = () => {}
  823. emitter = walk(stream)
  824. stream.push('nul')
  825. stream.push(null)
  826. Object.keys(events).forEach(key => {
  827. emitter.on(events[key], spooks.fn({
  828. name: key,
  829. log: log
  830. }))
  831. })
  832. emitter.on(events.end, done)
  833. })
  834. test('error event occurred once', () => {
  835. assert.strictEqual(log.counts.error, 1)
  836. })
  837. test('error event was dispatched correctly', () => {
  838. assert.strictEqual(log.args.error[0][0].actual, 'EOF')
  839. assert.strictEqual(log.args.error[0][0].expected, 'l')
  840. assert.strictEqual(log.args.error[0][0].lineNumber, 1)
  841. assert.strictEqual(log.args.error[0][0].columnNumber, 4)
  842. })
  843. test('end event occurred once', () => {
  844. assert.strictEqual(log.counts.end, 1)
  845. })
  846. test('literal event did not occur', () => {
  847. assert.strictEqual(log.counts.literal, 0)
  848. })
  849. })
  850. suite('bad literal true:', () => {
  851. let stream, emitter
  852. setup(done => {
  853. stream = new Readable()
  854. stream._read = () => {}
  855. emitter = walk(stream)
  856. stream.push('tRue')
  857. stream.push(null)
  858. Object.keys(events).forEach(key => {
  859. emitter.on(events[key], spooks.fn({
  860. name: key,
  861. log: log
  862. }))
  863. })
  864. emitter.on(events.end, done)
  865. })
  866. test('error event occurred four times', () => {
  867. assert.strictEqual(log.counts.error, 4)
  868. })
  869. test('error event was dispatched correctly first time', () => {
  870. assert.strictEqual(log.args.error[0][0].actual, 'R')
  871. assert.strictEqual(log.args.error[0][0].expected, 'r')
  872. assert.strictEqual(log.args.error[0][0].lineNumber, 1)
  873. assert.strictEqual(log.args.error[0][0].columnNumber, 2)
  874. })
  875. test('error event was dispatched correctly second time', () => {
  876. assert.strictEqual(log.args.error[1][0].actual, 'u')
  877. assert.strictEqual(log.args.error[1][0].expected, 'EOF')
  878. assert.strictEqual(log.args.error[1][0].lineNumber, 1)
  879. assert.strictEqual(log.args.error[1][0].columnNumber, 3)
  880. })
  881. test('error event was dispatched correctly third time', () => {
  882. assert.strictEqual(log.args.error[2][0].actual, 'u')
  883. assert.strictEqual(log.args.error[2][0].expected, 'value')
  884. assert.strictEqual(log.args.error[2][0].lineNumber, 1)
  885. assert.strictEqual(log.args.error[2][0].columnNumber, 3)
  886. })
  887. test('error event was dispatched correctly fourth time', () => {
  888. assert.strictEqual(log.args.error[3][0].actual, 'e')
  889. assert.strictEqual(log.args.error[3][0].expected, 'value')
  890. assert.strictEqual(log.args.error[3][0].lineNumber, 1)
  891. assert.strictEqual(log.args.error[3][0].columnNumber, 4)
  892. })
  893. test('end event occurred once', () => {
  894. assert.strictEqual(log.counts.end, 1)
  895. })
  896. test('literal event did not occur', () => {
  897. assert.strictEqual(log.counts.literal, 0)
  898. })
  899. })
  900. suite('array inside array:', () => {
  901. let stream, emitter
  902. setup(done => {
  903. stream = new Readable()
  904. stream._read = () => {}
  905. emitter = walk(stream)
  906. stream.push('[[]]')
  907. stream.push(null)
  908. Object.keys(events).forEach(key => {
  909. emitter.on(events[key], spooks.fn({
  910. name: key,
  911. log: log
  912. }))
  913. })
  914. emitter.on(events.end, done)
  915. })
  916. test('array event occurred twice', () => {
  917. assert.strictEqual(log.counts.array, 2)
  918. })
  919. test('endArray event occurred twice', () => {
  920. assert.strictEqual(log.counts.endArray, 2)
  921. })
  922. test('end event occurred once', () => {
  923. assert.strictEqual(log.counts.end, 1)
  924. })
  925. test('error event did not occur', () => {
  926. assert.strictEqual(log.counts.error, 0)
  927. })
  928. test('endLine event did not occur', () => {
  929. assert.strictEqual(log.counts.endLine, 0)
  930. })
  931. })
  932. suite('two arrays inside array:', () => {
  933. let stream, emitter
  934. setup(done => {
  935. stream = new Readable()
  936. stream._read = () => {}
  937. emitter = walk(stream)
  938. stream.push('[[],[]]')
  939. stream.push(null)
  940. Object.keys(events).forEach(key => {
  941. emitter.on(events[key], spooks.fn({
  942. name: key,
  943. log: log
  944. }))
  945. })
  946. emitter.on(events.end, done)
  947. })
  948. test('array event occurred three times', () => {
  949. assert.strictEqual(log.counts.array, 3)
  950. })
  951. test('endArray event occurred three times', () => {
  952. assert.strictEqual(log.counts.endArray, 3)
  953. })
  954. test('end event occurred once', () => {
  955. assert.strictEqual(log.counts.end, 1)
  956. })
  957. test('error event did not occur', () => {
  958. assert.strictEqual(log.counts.error, 0)
  959. })
  960. })
  961. suite('two arrays inside array with whitespace:', () => {
  962. let stream, emitter
  963. setup(done => {
  964. stream = new Readable()
  965. stream._read = () => {}
  966. emitter = walk(stream)
  967. stream.push(' [ [] , [] ] ')
  968. stream.push(null)
  969. Object.keys(events).forEach(key => {
  970. emitter.on(events[key], spooks.fn({
  971. name: key,
  972. log: log
  973. }))
  974. })
  975. emitter.on(events.end, done)
  976. })
  977. test('array event occurred three times', () => {
  978. assert.strictEqual(log.counts.array, 3)
  979. })
  980. test('endArray event occurred three times', () => {
  981. assert.strictEqual(log.counts.endArray, 3)
  982. })
  983. test('end event occurred once', () => {
  984. assert.strictEqual(log.counts.end, 1)
  985. })
  986. test('error event did not occur', () => {
  987. assert.strictEqual(log.counts.error, 0)
  988. })
  989. })
  990. suite('two arrays inside array without comma:', () => {
  991. let stream, emitter
  992. setup(done => {
  993. stream = new Readable()
  994. stream._read = () => {}
  995. emitter = walk(stream)
  996. stream.push('[[][]]')
  997. stream.push(null)
  998. Object.keys(events).forEach(key => {
  999. emitter.on(events[key], spooks.fn({
  1000. name: key,
  1001. log: log
  1002. }))
  1003. })
  1004. emitter.on(events.end, done)
  1005. })
  1006. test('array event occurred three times', () => {
  1007. assert.strictEqual(log.counts.array, 3)
  1008. })
  1009. test('endArray event occurred three times', () => {
  1010. assert.strictEqual(log.counts.endArray, 3)
  1011. })
  1012. test('error event occurred once', () => {
  1013. assert.strictEqual(log.counts.error, 1)
  1014. })
  1015. test('error event was dispatched correctly', () => {
  1016. assert.strictEqual(log.args.error[0][0].actual, '[')
  1017. assert.strictEqual(log.args.error[0][0].expected, ',')
  1018. assert.strictEqual(log.args.error[0][0].lineNumber, 1)
  1019. assert.strictEqual(log.args.error[0][0].columnNumber, 4)
  1020. })
  1021. test('end event occurred once', () => {
  1022. assert.strictEqual(log.counts.end, 1)
  1023. })
  1024. test('endObject event did not occur', () => {
  1025. assert.strictEqual(log.counts.endObject, 0)
  1026. })
  1027. })
  1028. suite('object inside array:', () => {
  1029. let stream, emitter
  1030. setup(done => {
  1031. stream = new Readable()
  1032. stream._read = () => {}
  1033. emitter = walk(stream)
  1034. stream.push('[{}]')
  1035. stream.push(null)
  1036. Object.keys(events).forEach(key => {
  1037. emitter.on(events[key], spooks.fn({
  1038. name: key,
  1039. log: log
  1040. }))
  1041. })
  1042. emitter.on(events.end, done)
  1043. })
  1044. test('array event occurred once', () => {
  1045. assert.strictEqual(log.counts.array, 1)
  1046. })
  1047. test('object event occurred once', () => {
  1048. assert.strictEqual(log.counts.object, 1)
  1049. })
  1050. test('endObject event occurred once', () => {
  1051. assert.strictEqual(log.counts.endObject, 1)
  1052. })
  1053. test('endArray event occurred once', () => {
  1054. assert.strictEqual(log.counts.endArray, 1)
  1055. })
  1056. test('end event occurred once', () => {
  1057. assert.strictEqual(log.counts.end, 1)
  1058. })
  1059. test('error event did not occur', () => {
  1060. assert.strictEqual(log.counts.error, 0)
  1061. })
  1062. test('endLine event did not occur', () => {
  1063. assert.strictEqual(log.counts.endLine, 0)
  1064. })
  1065. })
  1066. suite('two objects inside array:', () => {
  1067. let stream, emitter
  1068. setup(done => {
  1069. stream = new Readable()
  1070. stream._read = () => {}
  1071. emitter = walk(stream)
  1072. stream.push('[{},{}]')
  1073. stream.push(null)
  1074. Object.keys(events).forEach(key => {
  1075. emitter.on(events[key], spooks.fn({
  1076. name: key,
  1077. log: log
  1078. }))
  1079. })
  1080. emitter.on(events.end, done)
  1081. })
  1082. test('array event occurred once', () => {
  1083. assert.strictEqual(log.counts.array, 1)
  1084. })
  1085. test('object event occurred twice', () => {
  1086. assert.strictEqual(log.counts.object, 2)
  1087. })
  1088. test('endObject event occurred twice', () => {
  1089. assert.strictEqual(log.counts.endObject, 2)
  1090. })
  1091. test('endArray event occurred once', () => {
  1092. assert.strictEqual(log.counts.endArray, 1)
  1093. })
  1094. test('end event occurred once', () => {
  1095. assert.strictEqual(log.counts.end, 1)
  1096. })
  1097. test('error event did not occur', () => {
  1098. assert.strictEqual(log.counts.error, 0)
  1099. })
  1100. })
  1101. suite('two objects inside array with whitespace:', () => {
  1102. let stream, emitter
  1103. setup(done => {
  1104. stream = new Readable()
  1105. stream._read = () => {}
  1106. emitter = walk(stream)
  1107. stream.push('\t[\t{}\t,\r{}\n]\r\n')
  1108. stream.push(null)
  1109. Object.keys(events).forEach(key => {
  1110. emitter.on(events[key], spooks.fn({
  1111. name: key,
  1112. log: log
  1113. }))
  1114. })
  1115. emitter.on(events.end, done)
  1116. })
  1117. test('array event occurred once', () => {
  1118. assert.strictEqual(log.counts.array, 1)
  1119. })
  1120. test('object event occurred twice', () => {
  1121. assert.strictEqual(log.counts.object, 2)
  1122. })
  1123. test('endObject event occurred twice', () => {
  1124. assert.strictEqual(log.counts.endObject, 2)
  1125. })
  1126. test('endArray event occurred once', () => {
  1127. assert.strictEqual(log.counts.endArray, 1)
  1128. })
  1129. test('end event occurred once', () => {
  1130. assert.strictEqual(log.counts.end, 1)
  1131. })
  1132. test('error event did not occur', () => {
  1133. assert.strictEqual(log.counts.error, 0)
  1134. })
  1135. })
  1136. suite('two objects inside array without comma:', () => {
  1137. let stream, emitter
  1138. setup(done => {
  1139. stream = new Readable()
  1140. stream._read = () => {}
  1141. emitter = walk(stream)
  1142. stream.push('[ {} {} ]')
  1143. stream.push(null)
  1144. Object.keys(events).forEach(key => {
  1145. emitter.on(events[key], spooks.fn({
  1146. name: key,
  1147. log: log
  1148. }))
  1149. })
  1150. emitter.on(events.end, done)
  1151. })
  1152. test('array event occurred once', () => {
  1153. assert.strictEqual(log.counts.array, 1)
  1154. })
  1155. test('object event occurred twice', () => {
  1156. assert.strictEqual(log.counts.object, 2)
  1157. })
  1158. test('endObject event occurred twice', () => {
  1159. assert.strictEqual(log.counts.endObject, 2)
  1160. })
  1161. test('error event occurred once', () => {
  1162. assert.strictEqual(log.counts.error, 1)
  1163. })
  1164. test('error event was dispatched correctly', () => {
  1165. assert.strictEqual(log.args.error[0][0].actual, '{')
  1166. assert.strictEqual(log.args.error[0][0].expected, ',')
  1167. assert.strictEqual(log.args.error[0][0].lineNumber, 1)
  1168. assert.strictEqual(log.args.error[0][0].columnNumber, 6)
  1169. })
  1170. test('endArray event occurred once', () => {
  1171. assert.strictEqual(log.counts.endArray, 1)
  1172. })
  1173. test('end event occurred once', () => {
  1174. assert.strictEqual(log.counts.end, 1)
  1175. })
  1176. })
  1177. suite('string inside array:', () => {
  1178. let stream, emitter
  1179. setup(done => {
  1180. stream = new Readable()
  1181. stream._read = () => {}
  1182. emitter = walk(stream)
  1183. stream.push('["foo"]')
  1184. stream.push(null)
  1185. Object.keys(events).forEach(key => {
  1186. emitter.on(events[key], spooks.fn({
  1187. name: key,
  1188. log: log
  1189. }))
  1190. })
  1191. emitter.on(events.end, done)
  1192. })
  1193. test('array event occurred once', () => {
  1194. assert.strictEqual(log.counts.array, 1)
  1195. })
  1196. test('string event occurred once', () => {
  1197. assert.strictEqual(log.counts.string, 1)
  1198. })
  1199. test('string event was dispatched correctly', () => {
  1200. assert.strictEqual(log.args.string[0][0], 'foo')
  1201. })
  1202. test('endArray event occurred once', () => {
  1203. assert.strictEqual(log.counts.endArray, 1)
  1204. })
  1205. test('end event occurred once', () => {
  1206. assert.strictEqual(log.counts.end, 1)
  1207. })
  1208. test('object event did not occur', () => {
  1209. assert.strictEqual(log.counts.object, 0)
  1210. })
  1211. test('error event did not occur', () => {
  1212. assert.strictEqual(log.counts.error, 0)
  1213. })
  1214. test('endLine event did not occur', () => {
  1215. assert.strictEqual(log.counts.endLine, 0)
  1216. })
  1217. })
  1218. suite('two strings inside array:', () => {
  1219. let stream, emitter
  1220. setup(done => {
  1221. stream = new Readable()
  1222. stream._read = () => {}
  1223. emitter = walk(stream)
  1224. stream.push('["foo","bar"]')
  1225. stream.push(null)
  1226. Object.keys(events).forEach(key => {
  1227. emitter.on(events[key], spooks.fn({
  1228. name: key,
  1229. log: log
  1230. }))
  1231. })
  1232. emitter.on(events.end, done)
  1233. })
  1234. test('array event occurred once', () => {
  1235. assert.strictEqual(log.counts.array, 1)
  1236. })
  1237. test('string event occurred twice', () => {
  1238. assert.strictEqual(log.counts.string, 2)
  1239. })
  1240. test('string event was dispatched correctly first time', () => {
  1241. assert.strictEqual(log.args.string[0][0], 'foo')
  1242. })
  1243. test('string event was dispatched correctly second time', () => {
  1244. assert.strictEqual(log.args.string[1][0], 'bar')
  1245. })
  1246. test('endArray event occurred once', () => {
  1247. assert.strictEqual(log.counts.endArray, 1)
  1248. })
  1249. test('end event occurred once', () => {
  1250. assert.strictEqual(log.counts.end, 1)
  1251. })
  1252. test('error event did not occur', () => {
  1253. assert.strictEqual(log.counts.error, 0)
  1254. })
  1255. })
  1256. suite('two strings inside array with whitespace:', () => {
  1257. let stream, emitter
  1258. setup(done => {
  1259. stream = new Readable()
  1260. stream._read = () => {}
  1261. emitter = walk(stream)
  1262. stream.push(' [ "baz" , "qux" ] ')
  1263. stream.push(null)
  1264. Object.keys(events).forEach(key => {
  1265. emitter.on(events[key], spooks.fn({
  1266. name: key,
  1267. log: log
  1268. }))
  1269. })
  1270. emitter.on(events.end, done)
  1271. })
  1272. test('array event occurred once', () => {
  1273. assert.strictEqual(log.counts.array, 1)
  1274. })
  1275. test('string event occurred twice', () => {
  1276. assert.strictEqual(log.counts.string, 2)
  1277. })
  1278. test('string event was dispatched correctly first time', () => {
  1279. assert.strictEqual(log.args.string[0][0], 'baz')
  1280. })
  1281. test('string event was dispatched correctly second time', () => {
  1282. assert.strictEqual(log.args.string[1][0], 'qux')
  1283. })
  1284. test('endArray event occurred once', () => {
  1285. assert.strictEqual(log.counts.endArray, 1)
  1286. })
  1287. test('end event occurred once', () => {
  1288. assert.strictEqual(log.counts.end, 1)
  1289. })
  1290. test('error event did not occur', () => {
  1291. assert.strictEqual(log.counts.error, 0)
  1292. })
  1293. })
  1294. suite('literal inside array:', () => {
  1295. let stream, emitter
  1296. setup(done => {
  1297. stream = new Readable()
  1298. stream._read = () => {}
  1299. emitter = walk(stream)
  1300. stream.push('[false]')
  1301. stream.push(null)
  1302. Object.keys(events).forEach(key => {
  1303. emitter.on(events[key], spooks.fn({
  1304. name: key,
  1305. log: log
  1306. }))
  1307. })
  1308. emitter.on(events.end, done)
  1309. })
  1310. test('array event occurred once', () => {
  1311. assert.strictEqual(log.counts.array, 1)
  1312. })
  1313. test('literal event occurred once', () => {
  1314. assert.strictEqual(log.counts.literal, 1)
  1315. })
  1316. test('literal event was dispatched correctly', () => {
  1317. assert.strictEqual(log.args.literal[0][0], false)
  1318. })
  1319. test('endArray event occurred once', () => {
  1320. assert.strictEqual(log.counts.endArray, 1)
  1321. })
  1322. test('end event occurred once', () => {
  1323. assert.strictEqual(log.counts.end, 1)
  1324. })
  1325. test('error event did not occur', () => {
  1326. assert.strictEqual(log.counts.error, 0)
  1327. })
  1328. })
  1329. suite('two literals inside array:', () => {
  1330. let stream, emitter
  1331. setup(done => {
  1332. stream = new Readable()
  1333. stream._read = () => {}
  1334. emitter = walk(stream)
  1335. stream.push('[true,null]')
  1336. stream.push(null)
  1337. Object.keys(events).forEach(key => {
  1338. emitter.on(events[key], spooks.fn({
  1339. name: key,
  1340. log: log
  1341. }))
  1342. })
  1343. emitter.on(events.end, done)
  1344. })
  1345. test('array event occurred once', () => {
  1346. assert.strictEqual(log.counts.array, 1)
  1347. })
  1348. test('literal event occurred twice', () => {
  1349. assert.strictEqual(log.counts.literal, 2)
  1350. })
  1351. test('literal event was dispatched correctly first time', () => {
  1352. assert.strictEqual(log.args.literal[0][0], true)
  1353. })
  1354. test('literal event was dispatched correctly second time', () => {
  1355. assert.strictEqual(log.args.literal[1][0], null)
  1356. })
  1357. test('endArray event occurred once', () => {
  1358. assert.strictEqual(log.counts.endArray, 1)
  1359. })
  1360. test('end event occurred once', () => {
  1361. assert.strictEqual(log.counts.end, 1)
  1362. })
  1363. test('error event did not occur', () => {
  1364. assert.strictEqual(log.counts.error, 0)
  1365. })
  1366. })
  1367. suite('two literals inside array with whitespace:', () => {
  1368. let stream, emitter
  1369. setup(done => {
  1370. stream = new Readable()
  1371. stream._read = () => {}
  1372. emitter = walk(stream)
  1373. stream.push('[ null , false ]')
  1374. stream.push(null)
  1375. Object.keys(events).forEach(key => {
  1376. emitter.on(events[key], spooks.fn({
  1377. name: key,
  1378. log: log
  1379. }))
  1380. })
  1381. emitter.on(events.end, done)
  1382. })
  1383. test('array event occurred once', () => {
  1384. assert.strictEqual(log.counts.array, 1)
  1385. })
  1386. test('literal event occurred twice', () => {
  1387. assert.strictEqual(log.counts.literal, 2)
  1388. })
  1389. test('literal event was dispatched correctly first time', () => {
  1390. assert.strictEqual(log.args.literal[0][0], null)
  1391. })
  1392. test('literal event was dispatched correctly second time', () => {
  1393. assert.strictEqual(log.args.literal[1][0], false)
  1394. })
  1395. test('endArray event occurred once', () => {
  1396. assert.strictEqual(log.counts.endArray, 1)
  1397. })
  1398. test('end event occurred once', () => {
  1399. assert.strictEqual(log.counts.end, 1)
  1400. })
  1401. test('error event did not occur', () => {
  1402. assert.strictEqual(log.counts.error, 0)
  1403. })
  1404. })
  1405. suite('number inside array:', () => {
  1406. let stream, emitter
  1407. setup(done => {
  1408. stream = new Readable()
  1409. stream._read = () => {}
  1410. emitter = walk(stream)
  1411. stream.push('[0]')
  1412. stream.push(null)
  1413. Object.keys(events).forEach(key => {
  1414. emitter.on(events[key], spooks.fn({
  1415. name: key,
  1416. log: log
  1417. }))
  1418. })
  1419. emitter.on(events.end, done)
  1420. })
  1421. test('array event occurred once', () => {
  1422. assert.strictEqual(log.counts.array, 1)
  1423. })
  1424. test('number event occurred once', () => {
  1425. assert.strictEqual(log.counts.number, 1)
  1426. })
  1427. test('number event was dispatched correctly', () => {
  1428. assert.strictEqual(log.args.number[0][0], 0)
  1429. })
  1430. test('endArray event occurred once', () => {
  1431. assert.strictEqual(log.counts.endArray, 1)
  1432. })
  1433. test('end event occurred once', () => {
  1434. assert.strictEqual(log.counts.end, 1)
  1435. })
  1436. test('error event did not occur', () => {
  1437. assert.strictEqual(log.counts.error, 0)
  1438. })
  1439. })
  1440. suite('two numbers inside array:', () => {
  1441. let stream, emitter
  1442. setup(done => {
  1443. stream = new Readable()
  1444. stream._read = () => {}
  1445. emitter = walk(stream)
  1446. stream.push('[1,2]')
  1447. stream.push(null)
  1448. Object.keys(events).forEach(key => {
  1449. emitter.on(events[key], spooks.fn({
  1450. name: key,
  1451. log: log
  1452. }))
  1453. })
  1454. emitter.on(events.end, done)
  1455. })
  1456. test('array event occurred once', () => {
  1457. assert.strictEqual(log.counts.array, 1)
  1458. })
  1459. test('number event occurred twice', () => {
  1460. assert.strictEqual(log.counts.number, 2)
  1461. })
  1462. test('number event was dispatched correctly first time', () => {
  1463. assert.strictEqual(log.args.number[0][0], 1)
  1464. })
  1465. test('number event was dispatched correctly second time', () => {
  1466. assert.strictEqual(log.args.number[1][0], 2)
  1467. })
  1468. test('endArray event occurred once', () => {
  1469. assert.strictEqual(log.counts.endArray, 1)
  1470. })
  1471. test('end event occurred once', () => {
  1472. assert.strictEqual(log.counts.end, 1)
  1473. })
  1474. test('error event did not occur', () => {
  1475. assert.strictEqual(log.counts.error, 0)
  1476. })
  1477. })
  1478. suite('two numbers inside array with whitespace:', () => {
  1479. let stream, emitter
  1480. setup(done => {
  1481. stream = new Readable()
  1482. stream._read = () => {}
  1483. emitter = walk(stream)
  1484. stream.push('[ 1977 , -1977 ]')
  1485. stream.push(null)
  1486. Object.keys(events).forEach(key => {
  1487. emitter.on(events[key], spooks.fn({
  1488. name: key,
  1489. log: log
  1490. }))
  1491. })
  1492. emitter.on(events.end, done)
  1493. })
  1494. test('array event occurred once', () => {
  1495. assert.strictEqual(log.counts.array, 1)
  1496. })
  1497. test('number event occurred twice', () => {
  1498. assert.strictEqual(log.counts.number, 2)
  1499. })
  1500. test('number event was dispatched correctly first time', () => {
  1501. assert.strictEqual(log.args.number[0][0], 1977)
  1502. })
  1503. test('number event was dispatched correctly second time', () => {
  1504. assert.strictEqual(log.args.number[1][0], -1977)
  1505. })
  1506. test('endArray event occurred once', () => {
  1507. assert.strictEqual(log.counts.endArray, 1)
  1508. })
  1509. test('end event occurred once', () => {
  1510. assert.strictEqual(log.counts.end, 1)
  1511. })
  1512. test('error event did not occur', () => {
  1513. assert.strictEqual(log.counts.error, 0)
  1514. })
  1515. })
  1516. suite('object inside object:', () => {
  1517. let stream, emitter
  1518. setup(done => {
  1519. stream = new Readable()
  1520. stream._read = () => {}
  1521. emitter = walk(stream)
  1522. stream.push('{"foo":{}}')
  1523. stream.push(null)
  1524. Object.keys(events).forEach(key => {
  1525. emitter.on(events[key], spooks.fn({
  1526. name: key,
  1527. log: log
  1528. }))
  1529. })
  1530. emitter.on(events.end, done)
  1531. })
  1532. test('object event occurred twice', () => {
  1533. assert.strictEqual(log.counts.object, 2)
  1534. })
  1535. test('property event occurred once', () => {
  1536. assert.strictEqual(log.counts.property, 1)
  1537. })
  1538. test('property event was dispatched correctly', () => {
  1539. assert.lengthOf(log.args.property[0], 1)
  1540. assert.strictEqual(log.args.property[0][0], 'foo')
  1541. })
  1542. test('endObject event occurred twice', () => {
  1543. assert.strictEqual(log.counts.endObject, 2)
  1544. })
  1545. test('end event occurred once', () => {
  1546. assert.strictEqual(log.counts.end, 1)
  1547. })
  1548. test('error event did not occur', () => {
  1549. assert.strictEqual(log.counts.error, 0)
  1550. })
  1551. })
  1552. suite('array and object inside object:', () => {
  1553. let stream, emitter
  1554. setup(done => {
  1555. stream = new Readable()
  1556. stream._read = () => {}
  1557. emitter = walk(stream)
  1558. stream.push('{"wibble wobble":[],"jelly on the plate":{}}')
  1559. stream.push(null)
  1560. Object.keys(events).forEach(key => {
  1561. emitter.on(events[key], spooks.fn({
  1562. name: key,
  1563. log: log
  1564. }))
  1565. })
  1566. emitter.on(events.end, done)
  1567. })
  1568. test('object event occurred twice', () => {
  1569. assert.strictEqual(log.counts.object, 2)
  1570. })
  1571. test('property event occurred twice', () => {
  1572. assert.strictEqual(log.counts.property, 2)
  1573. })
  1574. test('property event was dispatched correctly first time', () => {
  1575. assert.strictEqual(log.args.property[0][0], 'wibble wobble')
  1576. })
  1577. test('property event was dispatched correctly second time', () => {
  1578. assert.strictEqual(log.args.property[1][0], 'jelly on the plate')
  1579. })
  1580. test('array event occurred once', () => {
  1581. assert.strictEqual(log.counts.array, 1)
  1582. })
  1583. test('endArray event occurred once', () => {
  1584. assert.strictEqual(log.counts.endArray, 1)
  1585. })
  1586. test('endObject event occurred twice', () => {
  1587. assert.strictEqual(log.counts.endObject, 2)
  1588. })
  1589. test('end event occurred once', () => {
  1590. assert.strictEqual(log.counts.end, 1)
  1591. })
  1592. test('error event did not occur', () => {
  1593. assert.strictEqual(log.counts.error, 0)
  1594. })
  1595. })
  1596. suite('string, literal and number inside object with whitespace:', () => {
  1597. let stream, emitter
  1598. setup(done => {
  1599. stream = new Readable()
  1600. stream._read = () => {}
  1601. emitter = walk(stream)
  1602. stream.push(' { "foo" : "bar" ,\t"baz"\t:\tnull\t,\r\n"qux"\r\n:\r\n3.14159265359\r\n} ')
  1603. stream.push(null)
  1604. Object.keys(events).forEach(key => {
  1605. emitter.on(events[key], spooks.fn({
  1606. name: key,
  1607. log: log
  1608. }))
  1609. })
  1610. emitter.on(events.end, done)
  1611. })
  1612. test('object event occurred once', () => {
  1613. assert.strictEqual(log.counts.object, 1)
  1614. })
  1615. test('property event occurred three times', () => {
  1616. assert.strictEqual(log.counts.property, 3)
  1617. })
  1618. test('property event was dispatched correctly first time', () => {
  1619. assert.strictEqual(log.args.property[0][0], 'foo')
  1620. })
  1621. test('property event was dispatched correctly second time', () => {
  1622. assert.strictEqual(log.args.property[1][0], 'baz')
  1623. })
  1624. test('property event was dispatched correctly third time', () => {
  1625. assert.strictEqual(log.args.property[2][0], 'qux')
  1626. })
  1627. test('string event occurred once', () => {
  1628. assert.strictEqual(log.counts.string, 1)
  1629. })
  1630. test('string event was dispatched correctly', () => {
  1631. assert.strictEqual(log.args.string[0][0], 'bar')
  1632. })
  1633. test('literal event occurred once', () => {
  1634. assert.strictEqual(log.counts.literal, 1)
  1635. })
  1636. test('literal event was dispatched correctly', () => {
  1637. assert.isNull(log.args.literal[0][0])
  1638. })
  1639. test('number event occurred once', () => {
  1640. assert.strictEqual(log.counts.number, 1)
  1641. })
  1642. test('number event was dispatched correctly', () => {
  1643. assert.strictEqual(log.args.number[0][0], 3.14159265359)
  1644. })
  1645. test('endObject event occurred once', () => {
  1646. assert.strictEqual(log.counts.endObject, 1)
  1647. })
  1648. test('end event occurred once', () => {
  1649. assert.strictEqual(log.counts.end, 1)
  1650. })
  1651. test('error event did not occur', () => {
  1652. assert.strictEqual(log.counts.error, 0)
  1653. })
  1654. })
  1655. suite('two objects inside object without comma:', () => {
  1656. let stream, emitter
  1657. setup(done => {
  1658. stream = new Readable()
  1659. stream._read = () => {}
  1660. emitter = walk(stream)
  1661. stream.push('{"foo":{}"bar":{}}')
  1662. stream.push(null)
  1663. Object.keys(events).forEach(key => {
  1664. emitter.on(events[key], spooks.fn({
  1665. name: key,
  1666. log: log
  1667. }))
  1668. })
  1669. emitter.on(events.end, done)
  1670. })
  1671. test('object event occurred three times', () => {
  1672. assert.strictEqual(log.counts.object, 3)
  1673. })
  1674. test('property event occurred twice', () => {
  1675. assert.strictEqual(log.counts.property, 2)
  1676. })
  1677. test('property event was dispatched correctly first time', () => {
  1678. assert.strictEqual(log.args.property[0][0], 'foo')
  1679. })
  1680. test('property event was dispatched correctly second time', () => {
  1681. assert.strictEqual(log.args.property[1][0], 'bar')
  1682. })
  1683. test('error event occurred once', () => {
  1684. assert.strictEqual(log.counts.error, 1)
  1685. })
  1686. test('error event was dispatched correctly', () => {
  1687. assert.strictEqual(log.args.error[0][0].actual, '"')
  1688. assert.strictEqual(log.args.error[0][0].expected, ',')
  1689. assert.strictEqual(log.args.error[0][0].lineNumber, 1)
  1690. assert.strictEqual(log.args.error[0][0].columnNumber, 10)
  1691. })
  1692. test('endObject event occurred three times', () => {
  1693. assert.strictEqual(log.counts.endObject, 3)
  1694. })
  1695. test('end event occurred once', () => {
  1696. assert.strictEqual(log.counts.end, 1)
  1697. })
  1698. })
  1699. suite('unquoted property:', () => {
  1700. let stream, emitter
  1701. setup(done => {
  1702. stream = new Readable()
  1703. stream._read = () => {}
  1704. emitter = walk(stream)
  1705. stream.push('{foo:{}}')
  1706. stream.push(null)
  1707. Object.keys(events).forEach(key => {
  1708. emitter.on(events[key], spooks.fn({
  1709. name: key,
  1710. log: log
  1711. }))
  1712. })
  1713. emitter.on(events.end, done)
  1714. })
  1715. test('object event occurred once', () => {
  1716. assert.strictEqual(log.counts.object, 1)
  1717. })
  1718. test('error event occurred once', () => {
  1719. assert.strictEqual(log.counts.error, 3)
  1720. })
  1721. test('error event was dispatched correctly first time', () => {
  1722. assert.strictEqual(log.args.error[0][0].actual, 'f')
  1723. assert.strictEqual(log.args.error[0][0].expected, '"')
  1724. assert.strictEqual(log.args.error[0][0].lineNumber, 1)
  1725. assert.strictEqual(log.args.error[0][0].columnNumber, 2)
  1726. })
  1727. test('error event was dispatched correctly second time', () => {
  1728. assert.strictEqual(log.args.error[1][0].actual, 'EOF')
  1729. assert.strictEqual(log.args.error[1][0].expected, '"')
  1730. assert.strictEqual(log.args.error[1][0].lineNumber, 1)
  1731. assert.strictEqual(log.args.error[1][0].columnNumber, 9)
  1732. })
  1733. test('error event was dispatched correctly third time', () => {
  1734. assert.strictEqual(log.args.error[2][0].actual, 'EOF')
  1735. assert.strictEqual(log.args.error[2][0].expected, '}')
  1736. assert.strictEqual(log.args.error[2][0].lineNumber, 1)
  1737. assert.strictEqual(log.args.error[2][0].columnNumber, 9)
  1738. })
  1739. test('end event occurred once', () => {
  1740. assert.strictEqual(log.counts.end, 1)
  1741. })
  1742. })
  1743. suite('duplicate property:', () => {
  1744. let stream, emitter
  1745. setup(done => {
  1746. stream = new Readable()
  1747. stream._read = () => {}
  1748. emitter = walk(stream)
  1749. // NOTE: RFC 7159 is wishy washy on the subject of duplicates:
  1750. //
  1751. // "The names within an object SHOULD be unique
  1752. //
  1753. // ...
  1754. //
  1755. // An object whose names are all unique is interoperable
  1756. // in the sense that all software implementations receiving
  1757. // that object will agree on the name/value mappings. When
  1758. // the names within an object are not unique, the behavior
  1759. // of software that receives such an object is unpredictable.
  1760. // Many implementations report the last name/value pair only.
  1761. // Other implementations report an error or fail to parse the
  1762. // object, and some implementations report all of the name/value
  1763. // pairs, including duplicates."
  1764. //
  1765. // https://tools.ietf.org/html/rfc7159#section-4
  1766. stream.push('{"foo":{},"foo":{}}')
  1767. stream.push(null)
  1768. Object.keys(events).forEach(key => {
  1769. emitter.on(events[key], spooks.fn({
  1770. name: key,
  1771. log: log
  1772. }))
  1773. })
  1774. emitter.on(events.end, done)
  1775. })
  1776. test('object event occurred three times', () => {
  1777. assert.strictEqual(log.counts.object, 3)
  1778. })
  1779. test('property event occurred twice', () => {
  1780. assert.strictEqual(log.counts.property, 2)
  1781. })
  1782. test('property event was dispatched correctly first time', () => {
  1783. assert.strictEqual(log.args.property[0][0], 'foo')
  1784. })
  1785. test('property event was dispatched correctly second time', () => {
  1786. assert.strictEqual(log.args.property[1][0], 'foo')
  1787. })
  1788. test('endObject event occurred three times', () => {
  1789. assert.strictEqual(log.counts.endObject, 3)
  1790. })
  1791. test('end event occurred once', () => {
  1792. assert.strictEqual(log.counts.end, 1)
  1793. })
  1794. test('error event did not occur', () => {
  1795. assert.strictEqual(log.counts.error, 0)
  1796. })
  1797. })
  1798. suite('empty array containing whitespace:', () => {
  1799. let stream, emitter
  1800. setup(done => {
  1801. stream = new Readable()
  1802. stream._read = () => {}
  1803. emitter = walk(stream)
  1804. stream.push('[ ]')
  1805. stream.push(null)
  1806. Object.keys(events).forEach(key => {
  1807. emitter.on(events[key], spooks.fn({
  1808. name: key,
  1809. log: log
  1810. }))
  1811. })
  1812. emitter.on(events.end, done)
  1813. })
  1814. test('array event occurred once', () => {
  1815. assert.strictEqual(log.counts.array, 1)
  1816. })
  1817. test('endArray event occurred once', () => {
  1818. assert.strictEqual(log.counts.endArray, 1)
  1819. })
  1820. test('end event occurred once', () => {
  1821. assert.strictEqual(log.counts.end, 1)
  1822. })
  1823. test('error event did not occur', () => {
  1824. assert.strictEqual(log.counts.error, 0)
  1825. })
  1826. })
  1827. suite('chunked empty array:', () => {
  1828. let stream, emitter
  1829. setup(done => {
  1830. stream = new Readable()
  1831. stream._read = () => {}
  1832. emitter = walk(stream)
  1833. stream.push('[')
  1834. Object.keys(events).forEach(key => {
  1835. emitter.on(events[key], spooks.fn({
  1836. name: key,
  1837. log: log
  1838. }))
  1839. })
  1840. emitter.on(events.end, done)
  1841. emitter.on(events.array, stream.push.bind(stream, ']'))
  1842. emitter.on(events.endArray, stream.push.bind(stream, null))
  1843. })
  1844. test('array event occurred once', () => {
  1845. assert.strictEqual(log.counts.array, 1)
  1846. })
  1847. test('endArray event occurred once', () => {
  1848. assert.strictEqual(log.counts.endArray, 1)
  1849. })
  1850. test('end event occurred once', () => {
  1851. assert.strictEqual(log.counts.end, 1)
  1852. })
  1853. test('error event did not occur', () => {
  1854. assert.strictEqual(log.counts.error, 0)
  1855. })
  1856. })
  1857. suite('chunked empty object with whitespace:', () => {
  1858. let stream, emitter
  1859. setup(done => {
  1860. stream = new Readable()
  1861. stream._read = () => {}
  1862. emitter = walk(stream)
  1863. stream.push(' {')
  1864. Object.keys(events).forEach(key => {
  1865. emitter.on(events[key], spooks.fn({
  1866. name: key,
  1867. log: log
  1868. }))
  1869. })
  1870. emitter.on(events.end, done)
  1871. emitter.on(events.object, () => {
  1872. setTimeout(stream.push.bind(stream, ' }'), 20)
  1873. })
  1874. emitter.on(events.endObject, () => {
  1875. setTimeout(stream.push.bind(stream, null), 20)
  1876. })
  1877. })
  1878. test('object event occurred once', () => {
  1879. assert.strictEqual(log.counts.object, 1)
  1880. })
  1881. test('endObject event occurred once', () => {
  1882. assert.strictEqual(log.counts.endObject, 1)
  1883. })
  1884. test('end event occurred once', () => {
  1885. assert.strictEqual(log.counts.end, 1)
  1886. })
  1887. test('error event did not occur', () => {
  1888. assert.strictEqual(log.counts.error, 0)
  1889. })
  1890. })
  1891. suite('chunked string:', () => {
  1892. let stream, emitter
  1893. setup(done => {
  1894. stream = new Readable()
  1895. stream._read = () => {}
  1896. emitter = walk(stream)
  1897. stream.push('"')
  1898. Object.keys(events).forEach(key => {
  1899. emitter.on(events[key], spooks.fn({
  1900. name: key,
  1901. log: log
  1902. }))
  1903. })
  1904. emitter.on(events.end, done)
  1905. emitter.on(events.string, () => {
  1906. setTimeout(stream.push.bind(stream, null), 20)
  1907. })
  1908. setTimeout(stream.push.bind(stream, '\\'), 20)
  1909. setTimeout(stream.push.bind(stream, 't\\u'), 40)
  1910. setTimeout(stream.push.bind(stream, '00'), 60)
  1911. setTimeout(stream.push.bind(stream, 'a0'), 80)
  1912. setTimeout(stream.push.bind(stream, '"'), 100)
  1913. })
  1914. test('string event occurred once', () => {
  1915. assert.strictEqual(log.counts.string, 1)
  1916. })
  1917. test('string event was dispatched correctly', () => {
  1918. assert.strictEqual(log.args.string[0][0], '\t\u00a0')
  1919. })
  1920. test('end event occurred once', () => {
  1921. assert.strictEqual(log.counts.end, 1)
  1922. })
  1923. test('error event did not occur', () => {
  1924. assert.strictEqual(log.counts.error, 0)
  1925. })
  1926. })
  1927. suite('chunked number:', () => {
  1928. let stream, emitter
  1929. setup(done => {
  1930. stream = new Readable()
  1931. stream._read = () => {}
  1932. emitter = walk(stream)
  1933. stream.push('-')
  1934. Object.keys(events).forEach(key => {
  1935. emitter.on(events[key], spooks.fn({
  1936. name: key,
  1937. log: log
  1938. }))
  1939. })
  1940. emitter.on(events.end, done)
  1941. setTimeout(stream.push.bind(stream, '3'), 20)
  1942. setTimeout(stream.push.bind(stream, '.'), 40)
  1943. setTimeout(stream.push.bind(stream, '14159'), 60)
  1944. setTimeout(stream.push.bind(stream, '265359'), 80)
  1945. setTimeout(stream.push.bind(stream, 'e'), 100)
  1946. setTimeout(stream.push.bind(stream, '-'), 120)
  1947. setTimeout(stream.push.bind(stream, '7'), 140)
  1948. setTimeout(stream.push.bind(stream, null), 160)
  1949. })
  1950. test('number event occurred once', () => {
  1951. assert.strictEqual(log.counts.number, 1)
  1952. })
  1953. test('number event was dispatched correctly', () => {
  1954. assert.strictEqual(log.args.number[0][0], -3.14159265359e-7)
  1955. })
  1956. test('end event occurred once', () => {
  1957. assert.strictEqual(log.counts.end, 1)
  1958. })
  1959. test('error event did not occur', () => {
  1960. assert.strictEqual(log.counts.error, 0)
  1961. })
  1962. })
  1963. suite('chunked literal:', () => {
  1964. let stream, emitter
  1965. setup(done => {
  1966. stream = new Readable()
  1967. stream._read = () => {}
  1968. emitter = walk(stream)
  1969. stream.push('n')
  1970. Object.keys(events).forEach(key => {
  1971. emitter.on(events[key], spooks.fn({
  1972. name: key,
  1973. log: log
  1974. }))
  1975. })
  1976. emitter.on(events.end, done)
  1977. setTimeout(stream.push.bind(stream, 'u'), 20)
  1978. setTimeout(stream.push.bind(stream, 'l'), 40)
  1979. setTimeout(stream.push.bind(stream, 'l'), 60)
  1980. setTimeout(stream.push.bind(stream, null), 80)
  1981. })
  1982. test('literal event occurred once', () => {
  1983. assert.strictEqual(log.counts.literal, 1)
  1984. })
  1985. test('literal event was dispatched correctly', () => {
  1986. assert.strictEqual(log.args.literal[0][0], null)
  1987. })
  1988. test('end event occurred once', () => {
  1989. assert.strictEqual(log.counts.end, 1)
  1990. })
  1991. test('error event did not occur', () => {
  1992. assert.strictEqual(log.counts.error, 0)
  1993. })
  1994. })
  1995. suite('populated array with discard=1:', () => {
  1996. let stream, emitter
  1997. setup(done => {
  1998. stream = new Readable()
  1999. stream._read = () => {}
  2000. emitter = walk(stream, { discard: 1 })
  2001. stream.push(' ')
  2002. Object.keys(events).forEach(key => {
  2003. emitter.on(events[key], spooks.fn({
  2004. name: key,
  2005. log: log
  2006. }))
  2007. })
  2008. emitter.on(events.end, done)
  2009. emitter.on(events.array, () => {
  2010. stream.push(' ""')
  2011. })
  2012. emitter.on(events.string, () => {
  2013. stream.push(' ]')
  2014. })
  2015. emitter.on(events.endArray, () => {
  2016. stream.push(null)
  2017. })
  2018. setImmediate(stream.push.bind(stream, '['))
  2019. })
  2020. test('array event occurred once', () => {
  2021. assert.strictEqual(log.counts.array, 1)
  2022. })
  2023. test('string event was dispatched correctly', () => {
  2024. assert.strictEqual(log.args.string[0][0], "")
  2025. })
  2026. test('endArray event occurred once', () => {
  2027. assert.strictEqual(log.counts.endArray, 1)
  2028. })
  2029. test('end event occurred once', () => {
  2030. assert.strictEqual(log.counts.end, 1)
  2031. })
  2032. test('string event occurred once', () => {
  2033. assert.strictEqual(log.counts.string, 1)
  2034. })
  2035. test('error event did not occur', () => {
  2036. assert.strictEqual(log.counts.error, 0)
  2037. })
  2038. })
  2039. suite('throw errors from event handlers:', () => {
  2040. let stream, emitter
  2041. setup(done => {
  2042. stream = new Readable()
  2043. stream._read = () => {}
  2044. emitter = walk(stream)
  2045. stream.push('[null,false,true,0,"",{"foo":"bar"}]')
  2046. stream.push(null)
  2047. Object.keys(events).forEach(key => {
  2048. const event = events[key]
  2049. emitter.on(event, spooks.fn({
  2050. name: key,
  2051. log: log
  2052. }))
  2053. if (event !== events.end) {
  2054. emitter.on(event, () => { throw 0 })
  2055. }
  2056. })
  2057. emitter.on(events.end, done)
  2058. })
  2059. test('array event occurred once', () => {
  2060. assert.strictEqual(log.counts.array, 1)
  2061. })
  2062. test('endArray event occurred once', () => {
  2063. assert.strictEqual(log.counts.endArray, 1)
  2064. })
  2065. test('literal event occurred three times', () => {
  2066. assert.strictEqual(log.counts.literal, 3)
  2067. })
  2068. test('number event occurred once', () => {
  2069. assert.strictEqual(log.counts.number, 1)
  2070. })
  2071. test('string event occurred twice', () => {
  2072. assert.strictEqual(log.counts.string, 2)
  2073. })
  2074. test('property event occurred once', () => {
  2075. assert.strictEqual(log.counts.property, 1)
  2076. })
  2077. test('object event occurred once', () => {
  2078. assert.strictEqual(log.counts.object, 1)
  2079. })
  2080. test('endObject event occurred once', () => {
  2081. assert.strictEqual(log.counts.endObject, 1)
  2082. })
  2083. test('error event occurred eleven times', () => {
  2084. assert.strictEqual(log.counts.error, 11)
  2085. })
  2086. test('end event occurred once', () => {
  2087. assert.strictEqual(log.counts.end, 1)
  2088. })
  2089. })
  2090. suite('error occurs on stream:', () => {
  2091. let stream, emitter
  2092. setup(done => {
  2093. stream = new Readable()
  2094. stream._read = () => {}
  2095. emitter = walk(stream)
  2096. Object.keys(events).forEach(key => {
  2097. emitter.on(events[key], spooks.fn({
  2098. name: key,
  2099. log: log
  2100. }))
  2101. })
  2102. stream.emit('error', new Error('wibble'))
  2103. stream.push(null)
  2104. emitter.on(events.end, done)
  2105. })
  2106. test('error event occurred once', () => {
  2107. assert.strictEqual(log.counts.error, 1)
  2108. })
  2109. test('error event was dispatched correctly', () => {
  2110. assert.strictEqual(log.args.error[0][0].message, 'wibble')
  2111. assert.isUndefined(log.args.error[0][0].actual)
  2112. assert.isUndefined(log.args.error[0][0].expected)
  2113. assert.isUndefined(log.args.error[0][0].lineNumber)
  2114. assert.isUndefined(log.args.error[0][0].columnNumber)
  2115. })
  2116. test('end event occurred once', () => {
  2117. assert.strictEqual(log.counts.end, 1)
  2118. })
  2119. })
  2120. suite('two values separated by newline:', () => {
  2121. let stream, emitter
  2122. setup(done => {
  2123. stream = new Readable()
  2124. stream._read = () => {}
  2125. emitter = walk(stream)
  2126. stream.push('[]\n"foo"')
  2127. stream.push(null)
  2128. Object.keys(events).forEach(key => {
  2129. emitter.on(events[key], spooks.fn({
  2130. name: key,
  2131. log: log
  2132. }))
  2133. })
  2134. emitter.on(events.end, done)
  2135. })
  2136. test('array event occurred once', () => {
  2137. assert.strictEqual(log.counts.array, 1)
  2138. })
  2139. test('endArray event occurred once', () => {
  2140. assert.strictEqual(log.counts.endArray, 1)
  2141. })
  2142. test('string event occurred once', () => {
  2143. assert.strictEqual(log.counts.string, 1)
  2144. })
  2145. test('error event occurred once', () => {
  2146. assert.strictEqual(log.counts.error, 1)
  2147. })
  2148. test('end event occurred once', () => {
  2149. assert.strictEqual(log.counts.end, 1)
  2150. })
  2151. test('endLine event did not occur', () => {
  2152. assert.strictEqual(log.counts.endLine, 0)
  2153. })
  2154. })
  2155. suite('two values separated by newline, ndjson=true:', () => {
  2156. let stream, emitter
  2157. setup(done => {
  2158. stream = new Readable()
  2159. stream._read = () => {}
  2160. emitter = walk(stream, { ndjson: true })
  2161. stream.push('[]\n"foo"')
  2162. stream.push(null)
  2163. Object.keys(events).forEach(key => {
  2164. emitter.on(events[key], spooks.fn({
  2165. name: key,
  2166. log: log
  2167. }))
  2168. })
  2169. emitter.on(events.end, done)
  2170. })
  2171. test('array event occurred once', () => {
  2172. assert.strictEqual(log.counts.array, 1)
  2173. })
  2174. test('endArray event occurred once', () => {
  2175. assert.strictEqual(log.counts.endArray, 1)
  2176. })
  2177. test('string event occurred once', () => {
  2178. assert.strictEqual(log.counts.string, 1)
  2179. })
  2180. test('endLine event occurred once', () => {
  2181. assert.strictEqual(log.counts.endLine, 1)
  2182. })
  2183. test('end event occurred once', () => {
  2184. assert.strictEqual(log.counts.end, 1)
  2185. })
  2186. test('error event did not occur', () => {
  2187. assert.strictEqual(log.counts.error, 0)
  2188. })
  2189. })
  2190. suite('two values separated by newline, ndjson=true, with embedded newlines in a value:', () => {
  2191. let stream, emitter
  2192. setup(done => {
  2193. stream = new Readable()
  2194. stream._read = () => {}
  2195. emitter = walk(stream, { ndjson: true })
  2196. stream.push('[\n\n\n"foo"\n\n,\n"bar"]\n"baz"')
  2197. stream.push(null)
  2198. Object.keys(events).forEach(key => {
  2199. emitter.on(events[key], spooks.fn({
  2200. name: key,
  2201. log: log
  2202. }))
  2203. })
  2204. emitter.on(events.end, done)
  2205. })
  2206. test('array event occurred once', () => {
  2207. assert.strictEqual(log.counts.array, 1)
  2208. })
  2209. test('endArray event occurred once', () => {
  2210. assert.strictEqual(log.counts.endArray, 1)
  2211. })
  2212. test('string event occurred three times', () => {
  2213. assert.strictEqual(log.counts.string, 3)
  2214. })
  2215. test('endLine event occurred once', () => {
  2216. assert.strictEqual(log.counts.endLine, 1)
  2217. })
  2218. test('end event occurred once', () => {
  2219. assert.strictEqual(log.counts.end, 1)
  2220. })
  2221. test('error event did not occur', () => {
  2222. assert.strictEqual(log.counts.error, 0)
  2223. })
  2224. })
  2225. suite('two values not separated by newline, ndjson=true:', () => {
  2226. let stream, emitter
  2227. setup(done => {
  2228. stream = new Readable()
  2229. stream._read = () => {}
  2230. emitter = walk(stream, { ndjson: true })
  2231. stream.push('[]"foo"')
  2232. stream.push(null)
  2233. Object.keys(events).forEach(key => {
  2234. emitter.on(events[key], spooks.fn({
  2235. name: key,
  2236. log: log
  2237. }))
  2238. })
  2239. emitter.on(events.end, done)
  2240. })
  2241. test('array event occurred once', () => {
  2242. assert.strictEqual(log.counts.array, 1)
  2243. })
  2244. test('endArray event occurred once', () => {
  2245. assert.strictEqual(log.counts.endArray, 1)
  2246. })
  2247. test('end event occurred once', () => {
  2248. assert.strictEqual(log.counts.end, 1)
  2249. })
  2250. test('error event occurred five times', () => {
  2251. assert.strictEqual(log.counts.error, 5)
  2252. })
  2253. test('string event did not occurr', () => {
  2254. assert.strictEqual(log.counts.string, 0)
  2255. })
  2256. test('endLine event did not occur', () => {
  2257. assert.strictEqual(log.counts.endLine, 0)
  2258. })
  2259. })
  2260. suite('two values separated by two newlines, ndjson=true:', () => {
  2261. let stream, emitter
  2262. setup(done => {
  2263. stream = new Readable()
  2264. stream._read = () => {}
  2265. emitter = walk(stream, { ndjson: true })
  2266. stream.push('[]\r\n\r\n"foo"')
  2267. stream.push(null)
  2268. Object.keys(events).forEach(key => {
  2269. emitter.on(events[key], spooks.fn({
  2270. name: key,
  2271. log: log
  2272. }))
  2273. })
  2274. emitter.on(events.end, done)
  2275. })
  2276. test('array event occurred once', () => {
  2277. assert.strictEqual(log.counts.array, 1)
  2278. })
  2279. test('endArray event occurred once', () => {
  2280. assert.strictEqual(log.counts.endArray, 1)
  2281. })
  2282. test('string event occurred once', () => {
  2283. assert.strictEqual(log.counts.string, 1)
  2284. })
  2285. test('endLine event occurred twice', () => {
  2286. assert.strictEqual(log.counts.endLine, 2)
  2287. })
  2288. test('end event occurred once', () => {
  2289. assert.strictEqual(log.counts.end, 1)
  2290. })
  2291. test('error event did not occur', () => {
  2292. assert.strictEqual(log.counts.error, 0)
  2293. })
  2294. })
  2295. suite('chunked ndjson:', () => {
  2296. let stream, emitter
  2297. setup(done => {
  2298. stream = new Readable()
  2299. stream._read = () => {}
  2300. emitter = walk(stream, { ndjson: true })
  2301. stream.push('[]')
  2302. Object.keys(events).forEach(key => {
  2303. emitter.on(events[key], spooks.fn({
  2304. name: key,
  2305. log: log
  2306. }))
  2307. })
  2308. emitter.on(events.end, done)
  2309. setTimeout(stream.push.bind(stream, ' '), 20)
  2310. setTimeout(stream.push.bind(stream, '\n'), 40)
  2311. setTimeout(stream.push.bind(stream, ' '), 60)
  2312. setTimeout(stream.push.bind(stream, '"'), 80)
  2313. setTimeout(stream.push.bind(stream, 'foo"'), 100)
  2314. setTimeout(stream.push.bind(stream, null), 120)
  2315. })
  2316. test('array event occurred once', () => {
  2317. assert.strictEqual(log.counts.array, 1)
  2318. })
  2319. test('endArray event occurred once', () => {
  2320. assert.strictEqual(log.counts.endArray, 1)
  2321. })
  2322. test('endLine event occurred once', () => {
  2323. assert.strictEqual(log.counts.endLine, 1)
  2324. })
  2325. test('string event occurred once', () => {
  2326. assert.strictEqual(log.counts.string, 1)
  2327. })
  2328. test('end event occurred once', () => {
  2329. assert.strictEqual(log.counts.end, 1)
  2330. })
  2331. test('error event did not occur', () => {
  2332. assert.strictEqual(log.counts.error, 0)
  2333. })
  2334. })
  2335. })
  2336. })