walk.js 85 KB

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