eventify.js 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358
  1. 'use strict'
  2. const assert = require('chai').assert
  3. const events = require('../../src/events')
  4. const Promise = require('bluebird')
  5. const spooks = require('spooks')
  6. const modulePath = '../../src/eventify'
  7. suite('eventify:', () => {
  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 eventify
  22. setup(() => {
  23. eventify = require(modulePath)
  24. })
  25. test('eventify does not throw', () => {
  26. assert.doesNotThrow(() => {
  27. eventify()
  28. })
  29. })
  30. test('eventify returns EventEmitter', () => {
  31. assert.instanceOf(eventify(), require('events').EventEmitter)
  32. })
  33. test('EventEmitter is decorated with pause method', () => {
  34. assert.isFunction(eventify().pause)
  35. assert.lengthOf(eventify().pause, 0)
  36. })
  37. test('pause method returns continue function', () => {
  38. assert.isFunction(eventify().pause())
  39. assert.lengthOf(eventify().pause(), 0)
  40. })
  41. suite('undefined:', () => {
  42. setup(done => {
  43. const emitter = eventify(undefined)
  44. Object.keys(events).forEach(key => {
  45. emitter.on(events[key], spooks.fn({
  46. name: key,
  47. log: log
  48. }))
  49. })
  50. emitter.on(events.end, done)
  51. })
  52. test('end event occurred once', () => {
  53. assert.strictEqual(log.counts.end, 1)
  54. })
  55. test('end event was dispatched correctly', () => {
  56. assert.lengthOf(log.args.end[0], 1)
  57. assert.isUndefined(log.args.end[0][0])
  58. })
  59. test('array event did not occur', () => {
  60. assert.strictEqual(log.counts.array, 0)
  61. })
  62. test('object event did not occur', () => {
  63. assert.strictEqual(log.counts.object, 0)
  64. })
  65. test('property event did not occur', () => {
  66. assert.strictEqual(log.counts.property, 0)
  67. })
  68. test('string event did not occur', () => {
  69. assert.strictEqual(log.counts.string, 0)
  70. })
  71. test('number event did not occur', () => {
  72. assert.strictEqual(log.counts.number, 0)
  73. })
  74. test('literal event did not occur', () => {
  75. assert.strictEqual(log.counts.literal, 0)
  76. })
  77. test('endArray event did not occur', () => {
  78. assert.strictEqual(log.counts.endArray, 0)
  79. })
  80. test('endObject event did not occur', () => {
  81. assert.strictEqual(log.counts.endObject, 0)
  82. })
  83. test('error event did not occur', () => {
  84. assert.strictEqual(log.counts.error, 0)
  85. })
  86. test('dataError event did not occur', () => {
  87. assert.strictEqual(log.counts.dataError, 0)
  88. })
  89. test('endPrefix event did not occur', () => {
  90. assert.strictEqual(log.counts.endPrefix, 0)
  91. })
  92. })
  93. suite('NaN:', () => {
  94. setup(done => {
  95. const emitter = eventify(NaN)
  96. Object.keys(events).forEach(key => {
  97. emitter.on(events[key], spooks.fn({
  98. name: key,
  99. log: log
  100. }))
  101. })
  102. emitter.on(events.end, done)
  103. })
  104. test('end event occurred once', () => {
  105. assert.strictEqual(log.counts.end, 1)
  106. })
  107. test('end event was dispatched correctly', () => {
  108. assert.lengthOf(log.args.end[0], 1)
  109. assert.isUndefined(log.args.end[0][0])
  110. })
  111. test('array event did not occur', () => {
  112. assert.strictEqual(log.counts.array, 0)
  113. })
  114. test('object event did not occur', () => {
  115. assert.strictEqual(log.counts.object, 0)
  116. })
  117. test('property event did not occur', () => {
  118. assert.strictEqual(log.counts.property, 0)
  119. })
  120. test('string event did not occur', () => {
  121. assert.strictEqual(log.counts.string, 0)
  122. })
  123. test('number event did not occur', () => {
  124. assert.strictEqual(log.counts.number, 0)
  125. })
  126. test('literal event did not occur', () => {
  127. assert.strictEqual(log.counts.literal, 0)
  128. })
  129. test('endArray event did not occur', () => {
  130. assert.strictEqual(log.counts.endArray, 0)
  131. })
  132. test('endObject event did not occur', () => {
  133. assert.strictEqual(log.counts.endObject, 0)
  134. })
  135. test('error event did not occur', () => {
  136. assert.strictEqual(log.counts.error, 0)
  137. })
  138. test('dataError event did not occur', () => {
  139. assert.strictEqual(log.counts.dataError, 0)
  140. })
  141. test('endPrefix event did not occur', () => {
  142. assert.strictEqual(log.counts.endPrefix, 0)
  143. })
  144. })
  145. suite('Infinity:', () => {
  146. setup(done => {
  147. const emitter = eventify(Infinity)
  148. Object.keys(events).forEach(key => {
  149. emitter.on(events[key], spooks.fn({
  150. name: key,
  151. log: log
  152. }))
  153. })
  154. emitter.on(events.end, done)
  155. })
  156. test('end event occurred once', () => {
  157. assert.strictEqual(log.counts.end, 1)
  158. })
  159. test('end event was dispatched correctly', () => {
  160. assert.lengthOf(log.args.end[0], 1)
  161. assert.isUndefined(log.args.end[0][0])
  162. })
  163. test('array event did not occur', () => {
  164. assert.strictEqual(log.counts.array, 0)
  165. })
  166. test('object event did not occur', () => {
  167. assert.strictEqual(log.counts.object, 0)
  168. })
  169. test('property event did not occur', () => {
  170. assert.strictEqual(log.counts.property, 0)
  171. })
  172. test('string event did not occur', () => {
  173. assert.strictEqual(log.counts.string, 0)
  174. })
  175. test('number event did not occur', () => {
  176. assert.strictEqual(log.counts.number, 0)
  177. })
  178. test('literal event did not occur', () => {
  179. assert.strictEqual(log.counts.literal, 0)
  180. })
  181. test('endArray event did not occur', () => {
  182. assert.strictEqual(log.counts.endArray, 0)
  183. })
  184. test('endObject event did not occur', () => {
  185. assert.strictEqual(log.counts.endObject, 0)
  186. })
  187. test('error event did not occur', () => {
  188. assert.strictEqual(log.counts.error, 0)
  189. })
  190. test('dataError event did not occur', () => {
  191. assert.strictEqual(log.counts.dataError, 0)
  192. })
  193. test('endPrefix event did not occur', () => {
  194. assert.strictEqual(log.counts.endPrefix, 0)
  195. })
  196. })
  197. suite('Number.NEGATIVE_INFINITY:', () => {
  198. setup(done => {
  199. const emitter = eventify(Number.NEGATIVE_INFINITY)
  200. Object.keys(events).forEach(key => {
  201. emitter.on(events[key], spooks.fn({
  202. name: key,
  203. log: log
  204. }))
  205. })
  206. emitter.on(events.end, done)
  207. })
  208. test('end event occurred once', () => {
  209. assert.strictEqual(log.counts.end, 1)
  210. })
  211. test('end event was dispatched correctly', () => {
  212. assert.lengthOf(log.args.end[0], 1)
  213. assert.isUndefined(log.args.end[0][0])
  214. })
  215. test('array event did not occur', () => {
  216. assert.strictEqual(log.counts.array, 0)
  217. })
  218. test('object event did not occur', () => {
  219. assert.strictEqual(log.counts.object, 0)
  220. })
  221. test('property event did not occur', () => {
  222. assert.strictEqual(log.counts.property, 0)
  223. })
  224. test('string event did not occur', () => {
  225. assert.strictEqual(log.counts.string, 0)
  226. })
  227. test('number event did not occur', () => {
  228. assert.strictEqual(log.counts.number, 0)
  229. })
  230. test('literal event did not occur', () => {
  231. assert.strictEqual(log.counts.literal, 0)
  232. })
  233. test('endArray event did not occur', () => {
  234. assert.strictEqual(log.counts.endArray, 0)
  235. })
  236. test('endObject event did not occur', () => {
  237. assert.strictEqual(log.counts.endObject, 0)
  238. })
  239. test('error event did not occur', () => {
  240. assert.strictEqual(log.counts.error, 0)
  241. })
  242. test('dataError event did not occur', () => {
  243. assert.strictEqual(log.counts.dataError, 0)
  244. })
  245. test('endPrefix event did not occur', () => {
  246. assert.strictEqual(log.counts.endPrefix, 0)
  247. })
  248. })
  249. suite('function:', () => {
  250. setup(done => {
  251. const emitter = eventify(() => {})
  252. Object.keys(events).forEach(key => {
  253. emitter.on(events[key], spooks.fn({
  254. name: key,
  255. log: log
  256. }))
  257. })
  258. emitter.on(events.end, done)
  259. })
  260. test('end event occurred once', () => {
  261. assert.strictEqual(log.counts.end, 1)
  262. })
  263. test('array event did not occur', () => {
  264. assert.strictEqual(log.counts.array, 0)
  265. })
  266. test('object event did not occur', () => {
  267. assert.strictEqual(log.counts.object, 0)
  268. })
  269. test('property event did not occur', () => {
  270. assert.strictEqual(log.counts.property, 0)
  271. })
  272. test('string event did not occur', () => {
  273. assert.strictEqual(log.counts.string, 0)
  274. })
  275. test('number event did not occur', () => {
  276. assert.strictEqual(log.counts.number, 0)
  277. })
  278. test('literal event did not occur', () => {
  279. assert.strictEqual(log.counts.literal, 0)
  280. })
  281. test('endArray event did not occur', () => {
  282. assert.strictEqual(log.counts.endArray, 0)
  283. })
  284. test('endObject event did not occur', () => {
  285. assert.strictEqual(log.counts.endObject, 0)
  286. })
  287. test('error event did not occur', () => {
  288. assert.strictEqual(log.counts.error, 0)
  289. })
  290. test('dataError event did not occur', () => {
  291. assert.strictEqual(log.counts.dataError, 0)
  292. })
  293. test('endPrefix event did not occur', () => {
  294. assert.strictEqual(log.counts.endPrefix, 0)
  295. })
  296. })
  297. suite('symbol:', () => {
  298. setup(done => {
  299. const emitter = eventify(Symbol('foo'))
  300. Object.keys(events).forEach(key => {
  301. emitter.on(events[key], spooks.fn({
  302. name: key,
  303. log: log
  304. }))
  305. })
  306. emitter.on(events.end, done)
  307. })
  308. test('end event occurred once', () => {
  309. assert.strictEqual(log.counts.end, 1)
  310. })
  311. test('array event did not occur', () => {
  312. assert.strictEqual(log.counts.array, 0)
  313. })
  314. test('object event did not occur', () => {
  315. assert.strictEqual(log.counts.object, 0)
  316. })
  317. test('property event did not occur', () => {
  318. assert.strictEqual(log.counts.property, 0)
  319. })
  320. test('string event did not occur', () => {
  321. assert.strictEqual(log.counts.string, 0)
  322. })
  323. test('number event did not occur', () => {
  324. assert.strictEqual(log.counts.number, 0)
  325. })
  326. test('literal event did not occur', () => {
  327. assert.strictEqual(log.counts.literal, 0)
  328. })
  329. test('endArray event did not occur', () => {
  330. assert.strictEqual(log.counts.endArray, 0)
  331. })
  332. test('endObject event did not occur', () => {
  333. assert.strictEqual(log.counts.endObject, 0)
  334. })
  335. test('error event did not occur', () => {
  336. assert.strictEqual(log.counts.error, 0)
  337. })
  338. test('dataError event did not occur', () => {
  339. assert.strictEqual(log.counts.dataError, 0)
  340. })
  341. test('endPrefix event did not occur', () => {
  342. assert.strictEqual(log.counts.endPrefix, 0)
  343. })
  344. })
  345. suite('empty array:', () => {
  346. setup(done => {
  347. const emitter = eventify([])
  348. Object.keys(events).forEach(key => {
  349. emitter.on(events[key], spooks.fn({
  350. name: key,
  351. log: log
  352. }))
  353. })
  354. emitter.on(events.end, done)
  355. })
  356. test('array event occurred once', () => {
  357. assert.strictEqual(log.counts.array, 1)
  358. })
  359. test('array event was dispatched correctly', () => {
  360. assert.lengthOf(log.args.array[0], 1)
  361. assert.isUndefined(log.args.array[0][0])
  362. })
  363. test('endArray event occurred once', () => {
  364. assert.strictEqual(log.counts.endArray, 1)
  365. })
  366. test('endArray event was dispatched correctly', () => {
  367. assert.lengthOf(log.args.endArray[0], 1)
  368. assert.isUndefined(log.args.endArray[0][0])
  369. })
  370. test('end event occurred once', () => {
  371. assert.strictEqual(log.counts.end, 1)
  372. })
  373. test('object event did not occur', () => {
  374. assert.strictEqual(log.counts.object, 0)
  375. })
  376. test('property event did not occur', () => {
  377. assert.strictEqual(log.counts.property, 0)
  378. })
  379. test('string event did not occur', () => {
  380. assert.strictEqual(log.counts.string, 0)
  381. })
  382. test('number event did not occur', () => {
  383. assert.strictEqual(log.counts.number, 0)
  384. })
  385. test('literal event did not occur', () => {
  386. assert.strictEqual(log.counts.literal, 0)
  387. })
  388. test('endObject event did not occur', () => {
  389. assert.strictEqual(log.counts.endObject, 0)
  390. })
  391. test('error event did not occur', () => {
  392. assert.strictEqual(log.counts.error, 0)
  393. })
  394. test('dataError event did not occur', () => {
  395. assert.strictEqual(log.counts.dataError, 0)
  396. })
  397. test('endPrefix event did not occur', () => {
  398. assert.strictEqual(log.counts.endPrefix, 0)
  399. })
  400. })
  401. suite('empty object:', () => {
  402. setup(done => {
  403. const emitter = eventify({})
  404. Object.keys(events).forEach(key => {
  405. emitter.on(events[key], spooks.fn({
  406. name: key,
  407. log: log
  408. }))
  409. })
  410. emitter.on(events.end, done)
  411. })
  412. test('object event occurred once', () => {
  413. assert.strictEqual(log.counts.object, 1)
  414. })
  415. test('object event was dispatched correctly', () => {
  416. assert.lengthOf(log.args.object[0], 1)
  417. assert.isUndefined(log.args.object[0][0])
  418. })
  419. test('endObject event occurred once', () => {
  420. assert.strictEqual(log.counts.endObject, 1)
  421. })
  422. test('endObject event was dispatched correctly', () => {
  423. assert.lengthOf(log.args.endObject[0], 1)
  424. assert.isUndefined(log.args.endObject[0][0])
  425. })
  426. test('end event occurred once', () => {
  427. assert.strictEqual(log.counts.end, 1)
  428. })
  429. test('array event did not occur', () => {
  430. assert.strictEqual(log.counts.array, 0)
  431. })
  432. test('property event did not occur', () => {
  433. assert.strictEqual(log.counts.property, 0)
  434. })
  435. test('string event did not occur', () => {
  436. assert.strictEqual(log.counts.string, 0)
  437. })
  438. test('number event did not occur', () => {
  439. assert.strictEqual(log.counts.number, 0)
  440. })
  441. test('literal event did not occur', () => {
  442. assert.strictEqual(log.counts.literal, 0)
  443. })
  444. test('endArray event did not occur', () => {
  445. assert.strictEqual(log.counts.endArray, 0)
  446. })
  447. test('error event did not occur', () => {
  448. assert.strictEqual(log.counts.error, 0)
  449. })
  450. test('dataError event did not occur', () => {
  451. assert.strictEqual(log.counts.dataError, 0)
  452. })
  453. test('endPrefix event did not occur', () => {
  454. assert.strictEqual(log.counts.endPrefix, 0)
  455. })
  456. })
  457. suite('string:', () => {
  458. setup(done => {
  459. const emitter = eventify('foo')
  460. Object.keys(events).forEach(key => {
  461. emitter.on(events[key], spooks.fn({
  462. name: key,
  463. log: log
  464. }))
  465. })
  466. emitter.on(events.end, done)
  467. })
  468. test('string event occurred once', () => {
  469. assert.strictEqual(log.counts.string, 1)
  470. })
  471. test('string event was dispatched correctly', () => {
  472. assert.lengthOf(log.args.string[0], 1)
  473. assert.strictEqual(log.args.string[0][0], 'foo')
  474. })
  475. test('end event occurred once', () => {
  476. assert.strictEqual(log.counts.end, 1)
  477. })
  478. test('array event did not occur', () => {
  479. assert.strictEqual(log.counts.array, 0)
  480. })
  481. test('object event did not occur', () => {
  482. assert.strictEqual(log.counts.object, 0)
  483. })
  484. test('property event did not occur', () => {
  485. assert.strictEqual(log.counts.property, 0)
  486. })
  487. test('number event did not occur', () => {
  488. assert.strictEqual(log.counts.number, 0)
  489. })
  490. test('literal event did not occur', () => {
  491. assert.strictEqual(log.counts.literal, 0)
  492. })
  493. test('endArray event did not occur', () => {
  494. assert.strictEqual(log.counts.endArray, 0)
  495. })
  496. test('endObject event did not occur', () => {
  497. assert.strictEqual(log.counts.endObject, 0)
  498. })
  499. test('error event did not occur', () => {
  500. assert.strictEqual(log.counts.error, 0)
  501. })
  502. test('dataError event did not occur', () => {
  503. assert.strictEqual(log.counts.dataError, 0)
  504. })
  505. test('endPrefix event did not occur', () => {
  506. assert.strictEqual(log.counts.endPrefix, 0)
  507. })
  508. })
  509. suite('string with special characters:', () => {
  510. setup(done => {
  511. const emitter = eventify('foo\nbar\t"baz"')
  512. Object.keys(events).forEach(key => {
  513. emitter.on(events[key], spooks.fn({
  514. name: key,
  515. log: log
  516. }))
  517. })
  518. emitter.on(events.end, done)
  519. })
  520. test('string event occurred once', () => {
  521. assert.strictEqual(log.counts.string, 1)
  522. })
  523. test('string event was dispatched correctly', () => {
  524. assert.lengthOf(log.args.string[0], 1)
  525. assert.strictEqual(log.args.string[0][0], 'foo\\nbar\\t\\"baz\\"')
  526. })
  527. test('error event did not occur', () => {
  528. assert.strictEqual(log.counts.error, 0)
  529. })
  530. test('dataError event did not occur', () => {
  531. assert.strictEqual(log.counts.dataError, 0)
  532. })
  533. })
  534. suite('number:', () => {
  535. setup(done => {
  536. const emitter = eventify(42)
  537. Object.keys(events).forEach(key => {
  538. emitter.on(events[key], spooks.fn({
  539. name: key,
  540. log: log
  541. }))
  542. })
  543. emitter.on(events.end, done)
  544. })
  545. test('number event occurred once', () => {
  546. assert.strictEqual(log.counts.number, 1)
  547. })
  548. test('number event was dispatched correctly', () => {
  549. assert.lengthOf(log.args.number[0], 1)
  550. assert.strictEqual(log.args.number[0][0], 42)
  551. })
  552. test('end event occurred once', () => {
  553. assert.strictEqual(log.counts.end, 1)
  554. })
  555. test('array event did not occur', () => {
  556. assert.strictEqual(log.counts.array, 0)
  557. })
  558. test('object event did not occur', () => {
  559. assert.strictEqual(log.counts.object, 0)
  560. })
  561. test('property event did not occur', () => {
  562. assert.strictEqual(log.counts.property, 0)
  563. })
  564. test('string event did not occur', () => {
  565. assert.strictEqual(log.counts.string, 0)
  566. })
  567. test('literal event did not occur', () => {
  568. assert.strictEqual(log.counts.literal, 0)
  569. })
  570. test('endArray event did not occur', () => {
  571. assert.strictEqual(log.counts.endArray, 0)
  572. })
  573. test('endObject event did not occur', () => {
  574. assert.strictEqual(log.counts.endObject, 0)
  575. })
  576. test('error event did not occur', () => {
  577. assert.strictEqual(log.counts.error, 0)
  578. })
  579. test('dataError event did not occur', () => {
  580. assert.strictEqual(log.counts.dataError, 0)
  581. })
  582. test('endPrefix event did not occur', () => {
  583. assert.strictEqual(log.counts.endPrefix, 0)
  584. })
  585. })
  586. suite('false:', () => {
  587. setup(done => {
  588. const emitter = eventify(false)
  589. Object.keys(events).forEach(key => {
  590. emitter.on(events[key], spooks.fn({
  591. name: key,
  592. log: log
  593. }))
  594. })
  595. emitter.on(events.end, done)
  596. })
  597. test('literal event occurred once', () => {
  598. assert.strictEqual(log.counts.literal, 1)
  599. })
  600. test('literal event was dispatched correctly', () => {
  601. assert.lengthOf(log.args.literal[0], 1)
  602. assert.isFalse(log.args.literal[0][0])
  603. })
  604. test('end event occurred once', () => {
  605. assert.strictEqual(log.counts.end, 1)
  606. })
  607. test('array event did not occur', () => {
  608. assert.strictEqual(log.counts.array, 0)
  609. })
  610. test('object event did not occur', () => {
  611. assert.strictEqual(log.counts.object, 0)
  612. })
  613. test('property event did not occur', () => {
  614. assert.strictEqual(log.counts.property, 0)
  615. })
  616. test('string event did not occur', () => {
  617. assert.strictEqual(log.counts.string, 0)
  618. })
  619. test('number event did not occur', () => {
  620. assert.strictEqual(log.counts.number, 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. test('error event did not occur', () => {
  629. assert.strictEqual(log.counts.error, 0)
  630. })
  631. test('dataError event did not occur', () => {
  632. assert.strictEqual(log.counts.dataError, 0)
  633. })
  634. test('endPrefix event did not occur', () => {
  635. assert.strictEqual(log.counts.endPrefix, 0)
  636. })
  637. })
  638. suite('true:', () => {
  639. setup(done => {
  640. const emitter = eventify(true)
  641. Object.keys(events).forEach(key => {
  642. emitter.on(events[key], spooks.fn({
  643. name: key,
  644. log: log
  645. }))
  646. })
  647. emitter.on(events.end, done)
  648. })
  649. test('literal event occurred once', () => {
  650. assert.strictEqual(log.counts.literal, 1)
  651. })
  652. test('literal event was dispatched correctly', () => {
  653. assert.isTrue(log.args.literal[0][0])
  654. })
  655. test('end event occurred once', () => {
  656. assert.strictEqual(log.counts.end, 1)
  657. })
  658. test('error event did not occur', () => {
  659. assert.strictEqual(log.counts.error, 0)
  660. })
  661. test('dataError event did not occur', () => {
  662. assert.strictEqual(log.counts.dataError, 0)
  663. })
  664. })
  665. suite('null:', () => {
  666. setup(done => {
  667. const emitter = eventify(null)
  668. Object.keys(events).forEach(key => {
  669. emitter.on(events[key], spooks.fn({
  670. name: key,
  671. log: log
  672. }))
  673. })
  674. emitter.on(events.end, done)
  675. })
  676. test('literal event occurred once', () => {
  677. assert.strictEqual(log.counts.literal, 1)
  678. })
  679. test('literal event was dispatched correctly', () => {
  680. assert.isNull(log.args.literal[0][0])
  681. })
  682. test('end event occurred once', () => {
  683. assert.strictEqual(log.counts.end, 1)
  684. })
  685. test('error event did not occur', () => {
  686. assert.strictEqual(log.counts.error, 0)
  687. })
  688. test('dataError event did not occur', () => {
  689. assert.strictEqual(log.counts.dataError, 0)
  690. })
  691. })
  692. suite('array with items:', () => {
  693. setup(done => {
  694. const emitter = eventify([
  695. undefined,
  696. NaN,
  697. Number.POSITIVE_INFINITY,
  698. Number.NEGATIVE_INFINITY,
  699. 'foo',
  700. () => {},
  701. 'bar',
  702. Symbol('baz')
  703. ])
  704. Object.keys(events).forEach(key => {
  705. emitter.on(events[key], spooks.fn({
  706. name: key,
  707. log: log
  708. }))
  709. })
  710. emitter.on(events.end, done)
  711. })
  712. test('array event occurred once', () => {
  713. assert.strictEqual(log.counts.array, 1)
  714. })
  715. test('literal event occurred six times', () => {
  716. assert.strictEqual(log.counts.literal, 6)
  717. })
  718. test('literal event was dispatched correctly first time', () => {
  719. assert.isNull(log.args.literal[0][0])
  720. })
  721. test('literal event was dispatched correctly second time', () => {
  722. assert.isNull(log.args.literal[1][0])
  723. })
  724. test('literal event was dispatched correctly third time', () => {
  725. assert.isNull(log.args.literal[2][0])
  726. })
  727. test('literal event was dispatched correctly fourth time', () => {
  728. assert.isNull(log.args.literal[3][0])
  729. })
  730. test('literal event was dispatched correctly fifth time', () => {
  731. assert.isNull(log.args.literal[4][0])
  732. })
  733. test('literal event was dispatched correctly sixth time', () => {
  734. assert.isNull(log.args.literal[5][0])
  735. })
  736. test('string event occurred twice', () => {
  737. assert.strictEqual(log.counts.string, 2)
  738. })
  739. test('string event was dispatched correctly first time', () => {
  740. assert.strictEqual(log.args.string[0][0], 'foo')
  741. })
  742. test('string event was dispatched correctly second time', () => {
  743. assert.strictEqual(log.args.string[1][0], 'bar')
  744. })
  745. test('endArray event occurred once', () => {
  746. assert.strictEqual(log.counts.endArray, 1)
  747. })
  748. test('end event occurred once', () => {
  749. assert.strictEqual(log.counts.end, 1)
  750. })
  751. test('error event did not occur', () => {
  752. assert.strictEqual(log.counts.error, 0)
  753. })
  754. test('dataError event did not occur', () => {
  755. assert.strictEqual(log.counts.dataError, 0)
  756. })
  757. })
  758. suite('object with properties:', () => {
  759. setup(done => {
  760. const emitter = eventify({ foo: 42,
  761. bar: undefined,
  762. baz: 3.14159265359,
  763. qux: Symbol('qux')
  764. })
  765. Object.keys(events).forEach(key => {
  766. emitter.on(events[key], spooks.fn({
  767. name: key,
  768. log: log
  769. }))
  770. })
  771. emitter.on(events.end, done)
  772. })
  773. test('object event occurred once', () => {
  774. assert.strictEqual(log.counts.object, 1)
  775. })
  776. test('property event occurred twice', () => {
  777. assert.strictEqual(log.counts.property, 2)
  778. })
  779. test('property event was dispatched correctly first time', () => {
  780. assert.strictEqual(log.args.property[0][0], 'foo')
  781. })
  782. test('property event was dispatched correctly second time', () => {
  783. assert.strictEqual(log.args.property[1][0], 'baz')
  784. })
  785. test('number event occurred twice', () => {
  786. assert.strictEqual(log.counts.number, 2)
  787. })
  788. test('number event was dispatched correctly first time', () => {
  789. assert.strictEqual(log.args.number[0][0], 42)
  790. })
  791. test('number event was dispatched correctly second time', () => {
  792. assert.strictEqual(log.args.number[1][0], 3.14159265359)
  793. })
  794. test('endObject event occurred once', () => {
  795. assert.strictEqual(log.counts.endObject, 1)
  796. })
  797. test('end event occurred once', () => {
  798. assert.strictEqual(log.counts.end, 1)
  799. })
  800. test('literal event did not occur', () => {
  801. assert.strictEqual(log.counts.literal, 0)
  802. })
  803. test('error event did not occur', () => {
  804. assert.strictEqual(log.counts.error, 0)
  805. })
  806. test('dataError event did not occur', () => {
  807. assert.strictEqual(log.counts.dataError, 0)
  808. })
  809. })
  810. suite('object with keys containing special characters:', () => {
  811. setup(done => {
  812. const emitter = eventify({ 'foo\n"bar"': 42 })
  813. Object.keys(events).forEach(key => {
  814. emitter.on(events[key], spooks.fn({
  815. name: key,
  816. log: log
  817. }))
  818. })
  819. emitter.on(events.end, done)
  820. })
  821. test('object event occurred once', () => {
  822. assert.strictEqual(log.counts.object, 1)
  823. })
  824. test('property event occurred once', () => {
  825. assert.strictEqual(log.counts.property, 1)
  826. })
  827. test('property event was dispatched correctly', () => {
  828. assert.strictEqual(log.args.property[0][0], 'foo\\n\\"bar\\"')
  829. })
  830. test('number event occurred once', () => {
  831. assert.strictEqual(log.counts.number, 1)
  832. })
  833. test('number event was dispatched correctly', () => {
  834. assert.strictEqual(log.args.number[0][0], 42)
  835. })
  836. test('endObject event occurred once', () => {
  837. assert.strictEqual(log.counts.endObject, 1)
  838. })
  839. test('end event occurred once', () => {
  840. assert.strictEqual(log.counts.end, 1)
  841. })
  842. test('literal event did not occur', () => {
  843. assert.strictEqual(log.counts.literal, 0)
  844. })
  845. test('error event did not occur', () => {
  846. assert.strictEqual(log.counts.error, 0)
  847. })
  848. test('dataError event did not occur', () => {
  849. assert.strictEqual(log.counts.dataError, 0)
  850. })
  851. })
  852. suite('nested array:', () => {
  853. setup(done => {
  854. const emitter = eventify([ 'foo', [ 'bar', [ 'baz', 'qux' ] ] ])
  855. Object.keys(events).forEach(key => {
  856. emitter.on(events[key], spooks.fn({
  857. name: key,
  858. log: log
  859. }))
  860. })
  861. emitter.on(events.end, done)
  862. })
  863. test('array event occurred three times', () => {
  864. assert.strictEqual(log.counts.array, 3)
  865. })
  866. test('string event occurred four times', () => {
  867. assert.strictEqual(log.counts.string, 4)
  868. })
  869. test('string event was dispatched correctly first time', () => {
  870. assert.strictEqual(log.args.string[0][0], 'foo')
  871. })
  872. test('string event was dispatched correctly second time', () => {
  873. assert.strictEqual(log.args.string[1][0], 'bar')
  874. })
  875. test('string event was dispatched correctly third time', () => {
  876. assert.strictEqual(log.args.string[2][0], 'baz')
  877. })
  878. test('string event was dispatched correctly fourth time', () => {
  879. assert.strictEqual(log.args.string[3][0], 'qux')
  880. })
  881. test('endArray event occurred three times', () => {
  882. assert.strictEqual(log.counts.endArray, 3)
  883. })
  884. test('end event occurred once', () => {
  885. assert.strictEqual(log.counts.end, 1)
  886. })
  887. test('error event did not occur', () => {
  888. assert.strictEqual(log.counts.error, 0)
  889. })
  890. test('dataError event did not occur', () => {
  891. assert.strictEqual(log.counts.dataError, 0)
  892. })
  893. })
  894. suite('nested object:', () => {
  895. setup(done => {
  896. const emitter = eventify({ foo: { bar: { baz: 1, qux: 2 }, wibble: 3 }, wobble: 4 })
  897. Object.keys(events).forEach(key => {
  898. emitter.on(events[key], spooks.fn({
  899. name: key,
  900. log: log
  901. }))
  902. })
  903. emitter.on(events.end, done)
  904. })
  905. test('object event occurred three times', () => {
  906. assert.strictEqual(log.counts.object, 3)
  907. })
  908. test('property event occurred six times', () => {
  909. assert.strictEqual(log.counts.property, 6)
  910. })
  911. test('property event was dispatched correctly first time', () => {
  912. assert.strictEqual(log.args.property[0][0], 'foo')
  913. })
  914. test('property event was dispatched correctly second time', () => {
  915. assert.strictEqual(log.args.property[1][0], 'bar')
  916. })
  917. test('property event was dispatched correctly third time', () => {
  918. assert.strictEqual(log.args.property[2][0], 'baz')
  919. })
  920. test('property event was dispatched correctly fourth time', () => {
  921. assert.strictEqual(log.args.property[3][0], 'qux')
  922. })
  923. test('property event was dispatched correctly fifth time', () => {
  924. assert.strictEqual(log.args.property[4][0], 'wibble')
  925. })
  926. test('property event was dispatched correctly sixth time', () => {
  927. assert.strictEqual(log.args.property[5][0], 'wobble')
  928. })
  929. test('number event occurred four times', () => {
  930. assert.strictEqual(log.counts.number, 4)
  931. })
  932. test('number event was dispatched correctly first time', () => {
  933. assert.strictEqual(log.args.number[0][0], 1)
  934. })
  935. test('number event was dispatched correctly second time', () => {
  936. assert.strictEqual(log.args.number[1][0], 2)
  937. })
  938. test('number event was dispatched correctly third time', () => {
  939. assert.strictEqual(log.args.number[2][0], 3)
  940. })
  941. test('number event was dispatched correctly fourth time', () => {
  942. assert.strictEqual(log.args.number[3][0], 4)
  943. })
  944. test('endObject event occurred three times', () => {
  945. assert.strictEqual(log.counts.endObject, 3)
  946. })
  947. test('end event occurred once', () => {
  948. assert.strictEqual(log.counts.end, 1)
  949. })
  950. test('error event did not occur', () => {
  951. assert.strictEqual(log.counts.error, 0)
  952. })
  953. test('dataError event did not occur', () => {
  954. assert.strictEqual(log.counts.dataError, 0)
  955. })
  956. })
  957. suite('promise:', () => {
  958. setup(done => {
  959. let resolve
  960. const emitter = eventify(new Promise(res => resolve = res), { poll: 4 })
  961. Object.keys(events).forEach(key => {
  962. emitter.on(events[key], spooks.fn({
  963. name: key,
  964. log: log
  965. }))
  966. })
  967. emitter.on(events.end, done)
  968. setTimeout(resolve.bind(null, 'foo'), 20)
  969. })
  970. test('string event occurred once', () => {
  971. assert.strictEqual(log.counts.string, 1)
  972. })
  973. test('string event was dispatched correctly', () => {
  974. assert.lengthOf(log.args.string[0], 1)
  975. assert.strictEqual(log.args.string[0][0], 'foo')
  976. })
  977. test('end event occurred once', () => {
  978. assert.strictEqual(log.counts.end, 1)
  979. })
  980. test('array event did not occur', () => {
  981. assert.strictEqual(log.counts.array, 0)
  982. })
  983. test('object event did not occur', () => {
  984. assert.strictEqual(log.counts.object, 0)
  985. })
  986. test('property event did not occur', () => {
  987. assert.strictEqual(log.counts.property, 0)
  988. })
  989. test('number event did not occur', () => {
  990. assert.strictEqual(log.counts.number, 0)
  991. })
  992. test('literal event did not occur', () => {
  993. assert.strictEqual(log.counts.literal, 0)
  994. })
  995. test('endArray event did not occur', () => {
  996. assert.strictEqual(log.counts.endArray, 0)
  997. })
  998. test('endObject event did not occur', () => {
  999. assert.strictEqual(log.counts.endObject, 0)
  1000. })
  1001. test('error event did not occur', () => {
  1002. assert.strictEqual(log.counts.error, 0)
  1003. })
  1004. test('dataError event did not occur', () => {
  1005. assert.strictEqual(log.counts.dataError, 0)
  1006. })
  1007. test('endPrefix event did not occur', () => {
  1008. assert.strictEqual(log.counts.endPrefix, 0)
  1009. })
  1010. })
  1011. suite('ignore promise:', () => {
  1012. setup(done => {
  1013. let resolve
  1014. const emitter = eventify(new Promise(res => resolve = res), { poll: 4, promises: 'ignore' })
  1015. Object.keys(events).forEach(key => {
  1016. emitter.on(events[key], spooks.fn({
  1017. name: key,
  1018. log: log
  1019. }))
  1020. })
  1021. emitter.on(events.end, done)
  1022. setTimeout(resolve.bind(null, 'foo'), 20)
  1023. })
  1024. test('end event occurred once', () => {
  1025. assert.strictEqual(log.counts.end, 1)
  1026. })
  1027. test('array event did not occur', () => {
  1028. assert.strictEqual(log.counts.array, 0)
  1029. })
  1030. test('object event did not occur', () => {
  1031. assert.strictEqual(log.counts.object, 0)
  1032. })
  1033. test('property event did not occur', () => {
  1034. assert.strictEqual(log.counts.property, 0)
  1035. })
  1036. test('string event did not occur', () => {
  1037. assert.strictEqual(log.counts.string, 0)
  1038. })
  1039. test('number event did not occur', () => {
  1040. assert.strictEqual(log.counts.number, 0)
  1041. })
  1042. test('literal event did not occur', () => {
  1043. assert.strictEqual(log.counts.literal, 0)
  1044. })
  1045. test('endArray event did not occur', () => {
  1046. assert.strictEqual(log.counts.endArray, 0)
  1047. })
  1048. test('endObject event did not occur', () => {
  1049. assert.strictEqual(log.counts.endObject, 0)
  1050. })
  1051. test('error event did not occur', () => {
  1052. assert.strictEqual(log.counts.error, 0)
  1053. })
  1054. test('dataError event did not occur', () => {
  1055. assert.strictEqual(log.counts.dataError, 0)
  1056. })
  1057. test('endPrefix event did not occur', () => {
  1058. assert.strictEqual(log.counts.endPrefix, 0)
  1059. })
  1060. })
  1061. suite('buffer:', () => {
  1062. setup(done => {
  1063. let resolve
  1064. const emitter = eventify(new Buffer('foo bar baz qux'))
  1065. Object.keys(events).forEach(key => {
  1066. emitter.on(events[key], spooks.fn({
  1067. name: key,
  1068. log: log
  1069. }))
  1070. })
  1071. emitter.on(events.end, done)
  1072. })
  1073. test('string event occurred once', () => {
  1074. assert.strictEqual(log.counts.string, 1)
  1075. })
  1076. test('string event was dispatched correctly', () => {
  1077. assert.lengthOf(log.args.string[0], 1)
  1078. assert.strictEqual(log.args.string[0][0], 'foo bar baz qux')
  1079. })
  1080. test('end event occurred once', () => {
  1081. assert.strictEqual(log.counts.end, 1)
  1082. })
  1083. test('array event did not occur', () => {
  1084. assert.strictEqual(log.counts.array, 0)
  1085. })
  1086. test('object event did not occur', () => {
  1087. assert.strictEqual(log.counts.object, 0)
  1088. })
  1089. test('property event did not occur', () => {
  1090. assert.strictEqual(log.counts.property, 0)
  1091. })
  1092. test('number event did not occur', () => {
  1093. assert.strictEqual(log.counts.number, 0)
  1094. })
  1095. test('literal event did not occur', () => {
  1096. assert.strictEqual(log.counts.literal, 0)
  1097. })
  1098. test('endArray event did not occur', () => {
  1099. assert.strictEqual(log.counts.endArray, 0)
  1100. })
  1101. test('endObject event did not occur', () => {
  1102. assert.strictEqual(log.counts.endObject, 0)
  1103. })
  1104. test('error event did not occur', () => {
  1105. assert.strictEqual(log.counts.error, 0)
  1106. })
  1107. test('dataError event did not occur', () => {
  1108. assert.strictEqual(log.counts.dataError, 0)
  1109. })
  1110. test('endPrefix event did not occur', () => {
  1111. assert.strictEqual(log.counts.endPrefix, 0)
  1112. })
  1113. })
  1114. suite('ignore buffer:', () => {
  1115. setup(done => {
  1116. let resolve
  1117. const emitter = eventify(new Buffer('foo bar baz qux'), { buffers: 'ignore' })
  1118. Object.keys(events).forEach(key => {
  1119. emitter.on(events[key], spooks.fn({
  1120. name: key,
  1121. log: log
  1122. }))
  1123. })
  1124. emitter.on(events.end, done)
  1125. })
  1126. test('end event occurred once', () => {
  1127. assert.strictEqual(log.counts.end, 1)
  1128. })
  1129. test('array event did not occur', () => {
  1130. assert.strictEqual(log.counts.array, 0)
  1131. })
  1132. test('object event did not occur', () => {
  1133. assert.strictEqual(log.counts.object, 0)
  1134. })
  1135. test('property event did not occur', () => {
  1136. assert.strictEqual(log.counts.property, 0)
  1137. })
  1138. test('string event did not occur', () => {
  1139. assert.strictEqual(log.counts.string, 0)
  1140. })
  1141. test('number event did not occur', () => {
  1142. assert.strictEqual(log.counts.number, 0)
  1143. })
  1144. test('literal event did not occur', () => {
  1145. assert.strictEqual(log.counts.literal, 0)
  1146. })
  1147. test('endArray event did not occur', () => {
  1148. assert.strictEqual(log.counts.endArray, 0)
  1149. })
  1150. test('endObject event did not occur', () => {
  1151. assert.strictEqual(log.counts.endObject, 0)
  1152. })
  1153. test('error event did not occur', () => {
  1154. assert.strictEqual(log.counts.error, 0)
  1155. })
  1156. test('dataError event did not occur', () => {
  1157. assert.strictEqual(log.counts.dataError, 0)
  1158. })
  1159. test('endPrefix event did not occur', () => {
  1160. assert.strictEqual(log.counts.endPrefix, 0)
  1161. })
  1162. })
  1163. suite('date:', () => {
  1164. setup(done => {
  1165. let resolve
  1166. const emitter = eventify(new Date('1977-06-10T10:30:00.000Z'))
  1167. Object.keys(events).forEach(key => {
  1168. emitter.on(events[key], spooks.fn({
  1169. name: key,
  1170. log: log
  1171. }))
  1172. })
  1173. emitter.on(events.end, done)
  1174. })
  1175. test('string event occurred once', () => {
  1176. assert.strictEqual(log.counts.string, 1)
  1177. })
  1178. test('string event was dispatched correctly', () => {
  1179. assert.lengthOf(log.args.string[0], 1)
  1180. assert.strictEqual(log.args.string[0][0], '1977-06-10T10:30:00.000Z')
  1181. })
  1182. test('end event occurred once', () => {
  1183. assert.strictEqual(log.counts.end, 1)
  1184. })
  1185. test('array event did not occur', () => {
  1186. assert.strictEqual(log.counts.array, 0)
  1187. })
  1188. test('object event did not occur', () => {
  1189. assert.strictEqual(log.counts.object, 0)
  1190. })
  1191. test('property event did not occur', () => {
  1192. assert.strictEqual(log.counts.property, 0)
  1193. })
  1194. test('number event did not occur', () => {
  1195. assert.strictEqual(log.counts.number, 0)
  1196. })
  1197. test('literal event did not occur', () => {
  1198. assert.strictEqual(log.counts.literal, 0)
  1199. })
  1200. test('endArray event did not occur', () => {
  1201. assert.strictEqual(log.counts.endArray, 0)
  1202. })
  1203. test('endObject event did not occur', () => {
  1204. assert.strictEqual(log.counts.endObject, 0)
  1205. })
  1206. test('error event did not occur', () => {
  1207. assert.strictEqual(log.counts.error, 0)
  1208. })
  1209. test('dataError event did not occur', () => {
  1210. assert.strictEqual(log.counts.dataError, 0)
  1211. })
  1212. test('endPrefix event did not occur', () => {
  1213. assert.strictEqual(log.counts.endPrefix, 0)
  1214. })
  1215. })
  1216. suite('object with toJSON method:', () => {
  1217. setup(done => {
  1218. let resolve
  1219. const emitter = eventify({ toJSON () { return 'foo' } })
  1220. Object.keys(events).forEach(key => {
  1221. emitter.on(events[key], spooks.fn({
  1222. name: key,
  1223. log: log
  1224. }))
  1225. })
  1226. emitter.on(events.end, done)
  1227. })
  1228. test('string event occurred once', () => {
  1229. assert.strictEqual(log.counts.string, 1)
  1230. })
  1231. test('string event was dispatched correctly', () => {
  1232. assert.lengthOf(log.args.string[0], 1)
  1233. assert.strictEqual(log.args.string[0][0], 'foo')
  1234. })
  1235. test('end event occurred once', () => {
  1236. assert.strictEqual(log.counts.end, 1)
  1237. })
  1238. test('array event did not occur', () => {
  1239. assert.strictEqual(log.counts.array, 0)
  1240. })
  1241. test('object event did not occur', () => {
  1242. assert.strictEqual(log.counts.object, 0)
  1243. })
  1244. test('property event did not occur', () => {
  1245. assert.strictEqual(log.counts.property, 0)
  1246. })
  1247. test('number event did not occur', () => {
  1248. assert.strictEqual(log.counts.number, 0)
  1249. })
  1250. test('literal event did not occur', () => {
  1251. assert.strictEqual(log.counts.literal, 0)
  1252. })
  1253. test('endArray event did not occur', () => {
  1254. assert.strictEqual(log.counts.endArray, 0)
  1255. })
  1256. test('endObject event did not occur', () => {
  1257. assert.strictEqual(log.counts.endObject, 0)
  1258. })
  1259. test('error event did not occur', () => {
  1260. assert.strictEqual(log.counts.error, 0)
  1261. })
  1262. test('dataError event did not occur', () => {
  1263. assert.strictEqual(log.counts.dataError, 0)
  1264. })
  1265. test('endPrefix event did not occur', () => {
  1266. assert.strictEqual(log.counts.endPrefix, 0)
  1267. })
  1268. })
  1269. suite('map:', () => {
  1270. setup(done => {
  1271. let resolve
  1272. const emitter = eventify(new Map([['foo','bar'],['baz','qux']]))
  1273. Object.keys(events).forEach(key => {
  1274. emitter.on(events[key], spooks.fn({
  1275. name: key,
  1276. log: log
  1277. }))
  1278. })
  1279. emitter.on(events.end, done)
  1280. })
  1281. test('object event occurred once', () => {
  1282. assert.strictEqual(log.counts.object, 1)
  1283. })
  1284. test('property event occurred twice', () => {
  1285. assert.strictEqual(log.counts.property, 2)
  1286. })
  1287. test('property event was dispatched correctly first time', () => {
  1288. assert.strictEqual(log.args.property[0][0], 'foo')
  1289. })
  1290. test('property event was dispatched correctly second time', () => {
  1291. assert.strictEqual(log.args.property[1][0], 'baz')
  1292. })
  1293. test('string event occurred twice', () => {
  1294. assert.strictEqual(log.counts.string, 2)
  1295. })
  1296. test('string event was dispatched correctly first time', () => {
  1297. assert.strictEqual(log.args.string[0][0], 'bar')
  1298. })
  1299. test('string event was dispatched correctly second time', () => {
  1300. assert.strictEqual(log.args.string[1][0], 'qux')
  1301. })
  1302. test('endObject event occurred once', () => {
  1303. assert.strictEqual(log.counts.endObject, 1)
  1304. })
  1305. test('end event occurred once', () => {
  1306. assert.strictEqual(log.counts.end, 1)
  1307. })
  1308. test('array event did not occur', () => {
  1309. assert.strictEqual(log.counts.array, 0)
  1310. })
  1311. test('number event did not occur', () => {
  1312. assert.strictEqual(log.counts.number, 0)
  1313. })
  1314. test('literal event did not occur', () => {
  1315. assert.strictEqual(log.counts.literal, 0)
  1316. })
  1317. test('endArray event did not occur', () => {
  1318. assert.strictEqual(log.counts.endArray, 0)
  1319. })
  1320. test('error event did not occur', () => {
  1321. assert.strictEqual(log.counts.error, 0)
  1322. })
  1323. test('dataError event did not occur', () => {
  1324. assert.strictEqual(log.counts.dataError, 0)
  1325. })
  1326. test('endPrefix event did not occur', () => {
  1327. assert.strictEqual(log.counts.endPrefix, 0)
  1328. })
  1329. })
  1330. suite('ignore map:', () => {
  1331. setup(done => {
  1332. let resolve
  1333. const emitter = eventify(new Map([['foo','bar'],['baz','qux']]), { maps: 'ignore' })
  1334. Object.keys(events).forEach(key => {
  1335. emitter.on(events[key], spooks.fn({
  1336. name: key,
  1337. log: log
  1338. }))
  1339. })
  1340. emitter.on(events.end, done)
  1341. })
  1342. test('end event occurred once', () => {
  1343. assert.strictEqual(log.counts.end, 1)
  1344. })
  1345. test('array event did not occur', () => {
  1346. assert.strictEqual(log.counts.array, 0)
  1347. })
  1348. test('object event did not occur', () => {
  1349. assert.strictEqual(log.counts.object, 0)
  1350. })
  1351. test('property event did not occur', () => {
  1352. assert.strictEqual(log.counts.property, 0)
  1353. })
  1354. test('string event did not occur', () => {
  1355. assert.strictEqual(log.counts.string, 0)
  1356. })
  1357. test('number event did not occur', () => {
  1358. assert.strictEqual(log.counts.number, 0)
  1359. })
  1360. test('literal event did not occur', () => {
  1361. assert.strictEqual(log.counts.literal, 0)
  1362. })
  1363. test('endArray event did not occur', () => {
  1364. assert.strictEqual(log.counts.endArray, 0)
  1365. })
  1366. test('endObject event did not occur', () => {
  1367. assert.strictEqual(log.counts.endObject, 0)
  1368. })
  1369. test('error event did not occur', () => {
  1370. assert.strictEqual(log.counts.error, 0)
  1371. })
  1372. test('dataError event did not occur', () => {
  1373. assert.strictEqual(log.counts.dataError, 0)
  1374. })
  1375. test('endPrefix event did not occur', () => {
  1376. assert.strictEqual(log.counts.endPrefix, 0)
  1377. })
  1378. })
  1379. suite('set:', () => {
  1380. setup(done => {
  1381. let resolve
  1382. const emitter = eventify(new Set(['foo','bar']))
  1383. Object.keys(events).forEach(key => {
  1384. emitter.on(events[key], spooks.fn({
  1385. name: key,
  1386. log: log
  1387. }))
  1388. })
  1389. emitter.on(events.end, done)
  1390. })
  1391. test('array event occurred once', () => {
  1392. assert.strictEqual(log.counts.array, 1)
  1393. })
  1394. test('string event occurred twice', () => {
  1395. assert.strictEqual(log.counts.string, 2)
  1396. })
  1397. test('string event was dispatched correctly first time', () => {
  1398. assert.strictEqual(log.args.string[0][0], 'foo')
  1399. })
  1400. test('string event was dispatched correctly second time', () => {
  1401. assert.strictEqual(log.args.string[1][0], 'bar')
  1402. })
  1403. test('endArray event occurred once', () => {
  1404. assert.strictEqual(log.counts.endArray, 1)
  1405. })
  1406. test('end event occurred once', () => {
  1407. assert.strictEqual(log.counts.end, 1)
  1408. })
  1409. test('object event did not occur', () => {
  1410. assert.strictEqual(log.counts.object, 0)
  1411. })
  1412. test('property event did not occur', () => {
  1413. assert.strictEqual(log.counts.number, 0)
  1414. })
  1415. test('number event did not occur', () => {
  1416. assert.strictEqual(log.counts.number, 0)
  1417. })
  1418. test('literal event did not occur', () => {
  1419. assert.strictEqual(log.counts.literal, 0)
  1420. })
  1421. test('endObject event did not occur', () => {
  1422. assert.strictEqual(log.counts.endObject, 0)
  1423. })
  1424. test('error event did not occur', () => {
  1425. assert.strictEqual(log.counts.error, 0)
  1426. })
  1427. test('dataError event did not occur', () => {
  1428. assert.strictEqual(log.counts.dataError, 0)
  1429. })
  1430. test('endPrefix event did not occur', () => {
  1431. assert.strictEqual(log.counts.endPrefix, 0)
  1432. })
  1433. })
  1434. suite('ignore set:', () => {
  1435. setup(done => {
  1436. let resolve
  1437. const emitter = eventify(new Set(['foo','bar']), { iterables: 'ignore' })
  1438. Object.keys(events).forEach(key => {
  1439. emitter.on(events[key], spooks.fn({
  1440. name: key,
  1441. log: log
  1442. }))
  1443. })
  1444. emitter.on(events.end, done)
  1445. })
  1446. test('end event occurred once', () => {
  1447. assert.strictEqual(log.counts.end, 1)
  1448. })
  1449. test('array event did not occur', () => {
  1450. assert.strictEqual(log.counts.array, 0)
  1451. })
  1452. test('object event did not occur', () => {
  1453. assert.strictEqual(log.counts.object, 0)
  1454. })
  1455. test('property event did not occur', () => {
  1456. assert.strictEqual(log.counts.property, 0)
  1457. })
  1458. test('string event did not occur', () => {
  1459. assert.strictEqual(log.counts.string, 0)
  1460. })
  1461. test('number event did not occur', () => {
  1462. assert.strictEqual(log.counts.number, 0)
  1463. })
  1464. test('literal event did not occur', () => {
  1465. assert.strictEqual(log.counts.literal, 0)
  1466. })
  1467. test('endArray event did not occur', () => {
  1468. assert.strictEqual(log.counts.endArray, 0)
  1469. })
  1470. test('endObject event did not occur', () => {
  1471. assert.strictEqual(log.counts.endObject, 0)
  1472. })
  1473. test('error event did not occur', () => {
  1474. assert.strictEqual(log.counts.error, 0)
  1475. })
  1476. test('dataError event did not occur', () => {
  1477. assert.strictEqual(log.counts.dataError, 0)
  1478. })
  1479. test('endPrefix event did not occur', () => {
  1480. assert.strictEqual(log.counts.endPrefix, 0)
  1481. })
  1482. })
  1483. suite('promise resolved to a map:', () => {
  1484. setup(done => {
  1485. let resolve
  1486. const emitter = eventify(new Promise(res => resolve = res), { poll: 4 })
  1487. Object.keys(events).forEach(key => {
  1488. emitter.on(events[key], spooks.fn({
  1489. name: key,
  1490. log: log
  1491. }))
  1492. })
  1493. emitter.on(events.end, done)
  1494. setImmediate(resolve.bind(null, new Map([['foo','bar'],['baz','qux']])))
  1495. })
  1496. test('object event occurred once', () => {
  1497. assert.strictEqual(log.counts.object, 1)
  1498. })
  1499. test('property event occurred twice', () => {
  1500. assert.strictEqual(log.counts.property, 2)
  1501. })
  1502. test('property event was dispatched correctly first time', () => {
  1503. assert.strictEqual(log.args.property[0][0], 'foo')
  1504. })
  1505. test('property event was dispatched correctly second time', () => {
  1506. assert.strictEqual(log.args.property[1][0], 'baz')
  1507. })
  1508. test('string event occurred twice', () => {
  1509. assert.strictEqual(log.counts.string, 2)
  1510. })
  1511. test('string event was dispatched correctly first time', () => {
  1512. assert.strictEqual(log.args.string[0][0], 'bar')
  1513. })
  1514. test('string event was dispatched correctly second time', () => {
  1515. assert.strictEqual(log.args.string[1][0], 'qux')
  1516. })
  1517. test('endObject event occurred once', () => {
  1518. assert.strictEqual(log.counts.endObject, 1)
  1519. })
  1520. test('end event occurred once', () => {
  1521. assert.strictEqual(log.counts.end, 1)
  1522. })
  1523. test('array event did not occur', () => {
  1524. assert.strictEqual(log.counts.array, 0)
  1525. })
  1526. test('number event did not occur', () => {
  1527. assert.strictEqual(log.counts.number, 0)
  1528. })
  1529. test('literal event did not occur', () => {
  1530. assert.strictEqual(log.counts.literal, 0)
  1531. })
  1532. test('endArray event did not occur', () => {
  1533. assert.strictEqual(log.counts.endArray, 0)
  1534. })
  1535. test('error event did not occur', () => {
  1536. assert.strictEqual(log.counts.error, 0)
  1537. })
  1538. test('dataError event did not occur', () => {
  1539. assert.strictEqual(log.counts.dataError, 0)
  1540. })
  1541. test('endPrefix event did not occur', () => {
  1542. assert.strictEqual(log.counts.endPrefix, 0)
  1543. })
  1544. })
  1545. suite('array circular reference:', () => {
  1546. setup(done => {
  1547. const array = [ 'foo' ]
  1548. array[1] = array
  1549. const emitter = eventify(array)
  1550. Object.keys(events).forEach(key => {
  1551. emitter.on(events[key], spooks.fn({
  1552. name: key,
  1553. log: log
  1554. }))
  1555. })
  1556. emitter.on(events.end, done)
  1557. })
  1558. test('array event occurred twice', () => {
  1559. assert.strictEqual(log.counts.array, 2)
  1560. })
  1561. test('string event occurred once', () => {
  1562. assert.strictEqual(log.counts.string, 1)
  1563. })
  1564. test('string event was dispatched correctly', () => {
  1565. assert.strictEqual(log.args.string[0][0], 'foo')
  1566. })
  1567. test('endArray event occurred twice', () => {
  1568. assert.strictEqual(log.counts.endArray, 2)
  1569. })
  1570. test('end event occurred once', () => {
  1571. assert.strictEqual(log.counts.end, 1)
  1572. })
  1573. test('dataError event occurred once', () => {
  1574. assert.strictEqual(log.counts.dataError, 1)
  1575. })
  1576. test('dataError event was dispatched correctly', () => {
  1577. assert.lengthOf(log.args.dataError[0], 1)
  1578. assert.instanceOf(log.args.dataError[0][0], Error)
  1579. assert.strictEqual(log.args.dataError[0][0].message, 'Circular reference.')
  1580. })
  1581. test('error event did not occur', () => {
  1582. assert.strictEqual(log.counts.error, 0)
  1583. })
  1584. })
  1585. suite('object circular reference:', () => {
  1586. setup(done => {
  1587. const object = { foo: 'bar' }
  1588. object.self = object
  1589. const emitter = eventify(object)
  1590. Object.keys(events).forEach(key => {
  1591. emitter.on(events[key], spooks.fn({
  1592. name: key,
  1593. log: log
  1594. }))
  1595. })
  1596. emitter.on(events.end, done)
  1597. })
  1598. test('object event occurred twice', () => {
  1599. assert.strictEqual(log.counts.object, 2)
  1600. })
  1601. test('property event occurred twice', () => {
  1602. assert.strictEqual(log.counts.property, 2)
  1603. })
  1604. test('property event was dispatched correctly first time', () => {
  1605. assert.strictEqual(log.args.property[0][0], 'foo')
  1606. })
  1607. test('property event was dispatched correctly second time', () => {
  1608. assert.strictEqual(log.args.property[1][0], 'self')
  1609. })
  1610. test('endObject event occurred twice', () => {
  1611. assert.strictEqual(log.counts.endObject, 2)
  1612. })
  1613. test('end event occurred once', () => {
  1614. assert.strictEqual(log.counts.end, 1)
  1615. })
  1616. test('dataError event occurred once', () => {
  1617. assert.strictEqual(log.counts.dataError, 1)
  1618. })
  1619. test('dataError event was dispatched correctly', () => {
  1620. assert.strictEqual(log.args.dataError[0][0].message, 'Circular reference.')
  1621. })
  1622. test('error event did not occur', () => {
  1623. assert.strictEqual(log.counts.error, 0)
  1624. })
  1625. })
  1626. suite('array circular reference with ignore set:', () => {
  1627. setup(done => {
  1628. const array = [ 'foo' ]
  1629. array[1] = array
  1630. const emitter = eventify(array, { circular: 'ignore' })
  1631. Object.keys(events).forEach(key => {
  1632. emitter.on(events[key], spooks.fn({
  1633. name: key,
  1634. log: log
  1635. }))
  1636. })
  1637. emitter.on(events.end, done)
  1638. })
  1639. test('array event occurred twice', () => {
  1640. assert.strictEqual(log.counts.array, 2)
  1641. })
  1642. test('string event occurred once', () => {
  1643. assert.strictEqual(log.counts.string, 1)
  1644. })
  1645. test('endArray event occurred twice', () => {
  1646. assert.strictEqual(log.counts.endArray, 2)
  1647. })
  1648. test('end event occurred once', () => {
  1649. assert.strictEqual(log.counts.end, 1)
  1650. })
  1651. test('error event did not occur', () => {
  1652. assert.strictEqual(log.counts.error, 0)
  1653. })
  1654. test('dataError event did not occur', () => {
  1655. assert.strictEqual(log.counts.dataError, 0)
  1656. })
  1657. })
  1658. suite('object circular reference with ignore set:', () => {
  1659. setup(done => {
  1660. const object = { foo: 'bar' }
  1661. object.self = object
  1662. const emitter = eventify(object, { circular: 'ignore' })
  1663. Object.keys(events).forEach(key => {
  1664. emitter.on(events[key], spooks.fn({
  1665. name: key,
  1666. log: log
  1667. }))
  1668. })
  1669. emitter.on(events.end, done)
  1670. })
  1671. test('object event occurred twice', () => {
  1672. assert.strictEqual(log.counts.object, 2)
  1673. })
  1674. test('property event occurred twice', () => {
  1675. assert.strictEqual(log.counts.property, 2)
  1676. })
  1677. test('endObject event occurred twice', () => {
  1678. assert.strictEqual(log.counts.endObject, 2)
  1679. })
  1680. test('end event occurred once', () => {
  1681. assert.strictEqual(log.counts.end, 1)
  1682. })
  1683. test('error event did not occur', () => {
  1684. assert.strictEqual(log.counts.error, 0)
  1685. })
  1686. test('dataError event did not occur', () => {
  1687. assert.strictEqual(log.counts.dataError, 0)
  1688. })
  1689. })
  1690. suite('parallel array reference:', () => {
  1691. setup(done => {
  1692. const array = [ 'foo' ]
  1693. const emitter = eventify([ array, array ])
  1694. Object.keys(events).forEach(key => {
  1695. emitter.on(events[key], spooks.fn({
  1696. name: key,
  1697. log: log
  1698. }))
  1699. })
  1700. emitter.on(events.end, done)
  1701. })
  1702. test('array event occurred three times', () => {
  1703. assert.strictEqual(log.counts.array, 3)
  1704. })
  1705. test('string event occurred twice', () => {
  1706. assert.strictEqual(log.counts.string, 2)
  1707. })
  1708. test('endArray event occurred three times', () => {
  1709. assert.strictEqual(log.counts.endArray, 3)
  1710. })
  1711. test('end event occurred once', () => {
  1712. assert.strictEqual(log.counts.end, 1)
  1713. })
  1714. test('error event did not occur', () => {
  1715. assert.strictEqual(log.counts.error, 0)
  1716. })
  1717. test('dataError event did not occur', () => {
  1718. assert.strictEqual(log.counts.dataError, 0)
  1719. })
  1720. })
  1721. suite('parallel object reference:', () => {
  1722. setup(done => {
  1723. const object = { foo: 'bar' }
  1724. const emitter = eventify({ baz: object, qux: object })
  1725. Object.keys(events).forEach(key => {
  1726. emitter.on(events[key], spooks.fn({
  1727. name: key,
  1728. log: log
  1729. }))
  1730. })
  1731. emitter.on(events.end, done)
  1732. })
  1733. test('object event occurred three times', () => {
  1734. assert.strictEqual(log.counts.object, 3)
  1735. })
  1736. test('property event occurred four times', () => {
  1737. assert.strictEqual(log.counts.property, 4)
  1738. })
  1739. test('endObject event occurred three times', () => {
  1740. assert.strictEqual(log.counts.endObject, 3)
  1741. })
  1742. test('end event occurred once', () => {
  1743. assert.strictEqual(log.counts.end, 1)
  1744. })
  1745. test('error event did not occur', () => {
  1746. assert.strictEqual(log.counts.error, 0)
  1747. })
  1748. test('dataError event did not occur', () => {
  1749. assert.strictEqual(log.counts.dataError, 0)
  1750. })
  1751. })
  1752. suite('throw errors from event handlers:', () => {
  1753. setup(done => {
  1754. const emitter = eventify([null,false,true,0,"",{"foo":"bar"}])
  1755. Object.keys(events).forEach(key => {
  1756. const event = events[key]
  1757. emitter.on(event, spooks.fn({
  1758. name: key,
  1759. log: log
  1760. }))
  1761. if (event !== events.end) {
  1762. emitter.on(event, () => { throw 0 })
  1763. }
  1764. })
  1765. emitter.on(events.end, done)
  1766. })
  1767. test('end event occurred once', () => {
  1768. assert.strictEqual(log.counts.end, 1)
  1769. })
  1770. test('array event occured once', () => {
  1771. assert.strictEqual(log.counts.array, 1)
  1772. })
  1773. test('literal event occured three times', () => {
  1774. assert.strictEqual(log.counts.literal, 3)
  1775. })
  1776. test('number event occured once', () => {
  1777. assert.strictEqual(log.counts.number, 1)
  1778. })
  1779. test('string event occured twice', () => {
  1780. assert.strictEqual(log.counts.string, 2)
  1781. })
  1782. test('object event occured once', () => {
  1783. assert.strictEqual(log.counts.object, 1)
  1784. })
  1785. test('property event occured once', () => {
  1786. assert.strictEqual(log.counts.property, 1)
  1787. })
  1788. test('endObject event occured once', () => {
  1789. assert.strictEqual(log.counts.endObject, 1)
  1790. })
  1791. test('endArray event occured once', () => {
  1792. assert.strictEqual(log.counts.endArray, 1)
  1793. })
  1794. test('error event occured eleven times', () => {
  1795. assert.strictEqual(log.counts.error, 11)
  1796. })
  1797. test('dataError event did not occur', () => {
  1798. assert.strictEqual(log.counts.dataError, 0)
  1799. })
  1800. })
  1801. })
  1802. })