eventify.js 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304
  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('nested array:', () => {
  811. setup(done => {
  812. const emitter = eventify([ 'foo', [ 'bar', [ 'baz', 'qux' ] ] ])
  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('array event occurred three times', () => {
  822. assert.strictEqual(log.counts.array, 3)
  823. })
  824. test('string event occurred four times', () => {
  825. assert.strictEqual(log.counts.string, 4)
  826. })
  827. test('string event was dispatched correctly first time', () => {
  828. assert.strictEqual(log.args.string[0][0], 'foo')
  829. })
  830. test('string event was dispatched correctly second time', () => {
  831. assert.strictEqual(log.args.string[1][0], 'bar')
  832. })
  833. test('string event was dispatched correctly third time', () => {
  834. assert.strictEqual(log.args.string[2][0], 'baz')
  835. })
  836. test('string event was dispatched correctly fourth time', () => {
  837. assert.strictEqual(log.args.string[3][0], 'qux')
  838. })
  839. test('endArray event occurred three times', () => {
  840. assert.strictEqual(log.counts.endArray, 3)
  841. })
  842. test('end event occurred once', () => {
  843. assert.strictEqual(log.counts.end, 1)
  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 object:', () => {
  853. setup(done => {
  854. const emitter = eventify({ foo: { bar: { baz: 1, qux: 2 }, wibble: 3 }, wobble: 4 })
  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('object event occurred three times', () => {
  864. assert.strictEqual(log.counts.object, 3)
  865. })
  866. test('property event occurred six times', () => {
  867. assert.strictEqual(log.counts.property, 6)
  868. })
  869. test('property event was dispatched correctly first time', () => {
  870. assert.strictEqual(log.args.property[0][0], 'foo')
  871. })
  872. test('property event was dispatched correctly second time', () => {
  873. assert.strictEqual(log.args.property[1][0], 'bar')
  874. })
  875. test('property event was dispatched correctly third time', () => {
  876. assert.strictEqual(log.args.property[2][0], 'baz')
  877. })
  878. test('property event was dispatched correctly fourth time', () => {
  879. assert.strictEqual(log.args.property[3][0], 'qux')
  880. })
  881. test('property event was dispatched correctly fifth time', () => {
  882. assert.strictEqual(log.args.property[4][0], 'wibble')
  883. })
  884. test('property event was dispatched correctly sixth time', () => {
  885. assert.strictEqual(log.args.property[5][0], 'wobble')
  886. })
  887. test('number event occurred four times', () => {
  888. assert.strictEqual(log.counts.number, 4)
  889. })
  890. test('number event was dispatched correctly first time', () => {
  891. assert.strictEqual(log.args.number[0][0], 1)
  892. })
  893. test('number event was dispatched correctly second time', () => {
  894. assert.strictEqual(log.args.number[1][0], 2)
  895. })
  896. test('number event was dispatched correctly third time', () => {
  897. assert.strictEqual(log.args.number[2][0], 3)
  898. })
  899. test('number event was dispatched correctly fourth time', () => {
  900. assert.strictEqual(log.args.number[3][0], 4)
  901. })
  902. test('endObject event occurred three times', () => {
  903. assert.strictEqual(log.counts.endObject, 3)
  904. })
  905. test('end event occurred once', () => {
  906. assert.strictEqual(log.counts.end, 1)
  907. })
  908. test('error event did not occur', () => {
  909. assert.strictEqual(log.counts.error, 0)
  910. })
  911. test('dataError event did not occur', () => {
  912. assert.strictEqual(log.counts.dataError, 0)
  913. })
  914. })
  915. suite('promise:', () => {
  916. setup(done => {
  917. let resolve
  918. const emitter = eventify(new Promise(res => resolve = res), { poll: 4 })
  919. Object.keys(events).forEach(key => {
  920. emitter.on(events[key], spooks.fn({
  921. name: key,
  922. log: log
  923. }))
  924. })
  925. emitter.on(events.end, done)
  926. setTimeout(resolve.bind(null, 'foo'), 20)
  927. })
  928. test('string event occurred once', () => {
  929. assert.strictEqual(log.counts.string, 1)
  930. })
  931. test('string event was dispatched correctly', () => {
  932. assert.lengthOf(log.args.string[0], 1)
  933. assert.strictEqual(log.args.string[0][0], 'foo')
  934. })
  935. test('end event occurred once', () => {
  936. assert.strictEqual(log.counts.end, 1)
  937. })
  938. test('array event did not occur', () => {
  939. assert.strictEqual(log.counts.array, 0)
  940. })
  941. test('object event did not occur', () => {
  942. assert.strictEqual(log.counts.object, 0)
  943. })
  944. test('property event did not occur', () => {
  945. assert.strictEqual(log.counts.property, 0)
  946. })
  947. test('number event did not occur', () => {
  948. assert.strictEqual(log.counts.number, 0)
  949. })
  950. test('literal event did not occur', () => {
  951. assert.strictEqual(log.counts.literal, 0)
  952. })
  953. test('endArray event did not occur', () => {
  954. assert.strictEqual(log.counts.endArray, 0)
  955. })
  956. test('endObject event did not occur', () => {
  957. assert.strictEqual(log.counts.endObject, 0)
  958. })
  959. test('error event did not occur', () => {
  960. assert.strictEqual(log.counts.error, 0)
  961. })
  962. test('dataError event did not occur', () => {
  963. assert.strictEqual(log.counts.dataError, 0)
  964. })
  965. test('endPrefix event did not occur', () => {
  966. assert.strictEqual(log.counts.endPrefix, 0)
  967. })
  968. })
  969. suite('ignore promise:', () => {
  970. setup(done => {
  971. let resolve
  972. const emitter = eventify(new Promise(res => resolve = res), { poll: 4, promises: 'ignore' })
  973. Object.keys(events).forEach(key => {
  974. emitter.on(events[key], spooks.fn({
  975. name: key,
  976. log: log
  977. }))
  978. })
  979. emitter.on(events.end, done)
  980. setTimeout(resolve.bind(null, 'foo'), 20)
  981. })
  982. test('end event occurred once', () => {
  983. assert.strictEqual(log.counts.end, 1)
  984. })
  985. test('array event did not occur', () => {
  986. assert.strictEqual(log.counts.array, 0)
  987. })
  988. test('object event did not occur', () => {
  989. assert.strictEqual(log.counts.object, 0)
  990. })
  991. test('property event did not occur', () => {
  992. assert.strictEqual(log.counts.property, 0)
  993. })
  994. test('string event did not occur', () => {
  995. assert.strictEqual(log.counts.string, 0)
  996. })
  997. test('number event did not occur', () => {
  998. assert.strictEqual(log.counts.number, 0)
  999. })
  1000. test('literal event did not occur', () => {
  1001. assert.strictEqual(log.counts.literal, 0)
  1002. })
  1003. test('endArray event did not occur', () => {
  1004. assert.strictEqual(log.counts.endArray, 0)
  1005. })
  1006. test('endObject event did not occur', () => {
  1007. assert.strictEqual(log.counts.endObject, 0)
  1008. })
  1009. test('error event did not occur', () => {
  1010. assert.strictEqual(log.counts.error, 0)
  1011. })
  1012. test('dataError event did not occur', () => {
  1013. assert.strictEqual(log.counts.dataError, 0)
  1014. })
  1015. test('endPrefix event did not occur', () => {
  1016. assert.strictEqual(log.counts.endPrefix, 0)
  1017. })
  1018. })
  1019. suite('buffer:', () => {
  1020. setup(done => {
  1021. let resolve
  1022. const emitter = eventify(new Buffer('foo bar baz qux'))
  1023. Object.keys(events).forEach(key => {
  1024. emitter.on(events[key], spooks.fn({
  1025. name: key,
  1026. log: log
  1027. }))
  1028. })
  1029. emitter.on(events.end, done)
  1030. })
  1031. test('string event occurred once', () => {
  1032. assert.strictEqual(log.counts.string, 1)
  1033. })
  1034. test('string event was dispatched correctly', () => {
  1035. assert.lengthOf(log.args.string[0], 1)
  1036. assert.strictEqual(log.args.string[0][0], 'foo bar baz qux')
  1037. })
  1038. test('end event occurred once', () => {
  1039. assert.strictEqual(log.counts.end, 1)
  1040. })
  1041. test('array event did not occur', () => {
  1042. assert.strictEqual(log.counts.array, 0)
  1043. })
  1044. test('object event did not occur', () => {
  1045. assert.strictEqual(log.counts.object, 0)
  1046. })
  1047. test('property event did not occur', () => {
  1048. assert.strictEqual(log.counts.property, 0)
  1049. })
  1050. test('number event did not occur', () => {
  1051. assert.strictEqual(log.counts.number, 0)
  1052. })
  1053. test('literal event did not occur', () => {
  1054. assert.strictEqual(log.counts.literal, 0)
  1055. })
  1056. test('endArray event did not occur', () => {
  1057. assert.strictEqual(log.counts.endArray, 0)
  1058. })
  1059. test('endObject event did not occur', () => {
  1060. assert.strictEqual(log.counts.endObject, 0)
  1061. })
  1062. test('error event did not occur', () => {
  1063. assert.strictEqual(log.counts.error, 0)
  1064. })
  1065. test('dataError event did not occur', () => {
  1066. assert.strictEqual(log.counts.dataError, 0)
  1067. })
  1068. test('endPrefix event did not occur', () => {
  1069. assert.strictEqual(log.counts.endPrefix, 0)
  1070. })
  1071. })
  1072. suite('ignore buffer:', () => {
  1073. setup(done => {
  1074. let resolve
  1075. const emitter = eventify(new Buffer('foo bar baz qux'), { buffers: 'ignore' })
  1076. Object.keys(events).forEach(key => {
  1077. emitter.on(events[key], spooks.fn({
  1078. name: key,
  1079. log: log
  1080. }))
  1081. })
  1082. emitter.on(events.end, done)
  1083. })
  1084. test('end event occurred once', () => {
  1085. assert.strictEqual(log.counts.end, 1)
  1086. })
  1087. test('array event did not occur', () => {
  1088. assert.strictEqual(log.counts.array, 0)
  1089. })
  1090. test('object event did not occur', () => {
  1091. assert.strictEqual(log.counts.object, 0)
  1092. })
  1093. test('property event did not occur', () => {
  1094. assert.strictEqual(log.counts.property, 0)
  1095. })
  1096. test('string event did not occur', () => {
  1097. assert.strictEqual(log.counts.string, 0)
  1098. })
  1099. test('number event did not occur', () => {
  1100. assert.strictEqual(log.counts.number, 0)
  1101. })
  1102. test('literal event did not occur', () => {
  1103. assert.strictEqual(log.counts.literal, 0)
  1104. })
  1105. test('endArray event did not occur', () => {
  1106. assert.strictEqual(log.counts.endArray, 0)
  1107. })
  1108. test('endObject event did not occur', () => {
  1109. assert.strictEqual(log.counts.endObject, 0)
  1110. })
  1111. test('error event did not occur', () => {
  1112. assert.strictEqual(log.counts.error, 0)
  1113. })
  1114. test('dataError event did not occur', () => {
  1115. assert.strictEqual(log.counts.dataError, 0)
  1116. })
  1117. test('endPrefix event did not occur', () => {
  1118. assert.strictEqual(log.counts.endPrefix, 0)
  1119. })
  1120. })
  1121. suite('date:', () => {
  1122. setup(done => {
  1123. let resolve
  1124. const emitter = eventify(new Date('1977-06-10T10:30:00.000Z'))
  1125. Object.keys(events).forEach(key => {
  1126. emitter.on(events[key], spooks.fn({
  1127. name: key,
  1128. log: log
  1129. }))
  1130. })
  1131. emitter.on(events.end, done)
  1132. })
  1133. test('string event occurred once', () => {
  1134. assert.strictEqual(log.counts.string, 1)
  1135. })
  1136. test('string event was dispatched correctly', () => {
  1137. assert.lengthOf(log.args.string[0], 1)
  1138. assert.strictEqual(log.args.string[0][0], '1977-06-10T10:30:00.000Z')
  1139. })
  1140. test('end event occurred once', () => {
  1141. assert.strictEqual(log.counts.end, 1)
  1142. })
  1143. test('array event did not occur', () => {
  1144. assert.strictEqual(log.counts.array, 0)
  1145. })
  1146. test('object event did not occur', () => {
  1147. assert.strictEqual(log.counts.object, 0)
  1148. })
  1149. test('property event did not occur', () => {
  1150. assert.strictEqual(log.counts.property, 0)
  1151. })
  1152. test('number event did not occur', () => {
  1153. assert.strictEqual(log.counts.number, 0)
  1154. })
  1155. test('literal event did not occur', () => {
  1156. assert.strictEqual(log.counts.literal, 0)
  1157. })
  1158. test('endArray event did not occur', () => {
  1159. assert.strictEqual(log.counts.endArray, 0)
  1160. })
  1161. test('endObject event did not occur', () => {
  1162. assert.strictEqual(log.counts.endObject, 0)
  1163. })
  1164. test('error event did not occur', () => {
  1165. assert.strictEqual(log.counts.error, 0)
  1166. })
  1167. test('dataError event did not occur', () => {
  1168. assert.strictEqual(log.counts.dataError, 0)
  1169. })
  1170. test('endPrefix event did not occur', () => {
  1171. assert.strictEqual(log.counts.endPrefix, 0)
  1172. })
  1173. })
  1174. suite('object with toJSON method:', () => {
  1175. setup(done => {
  1176. let resolve
  1177. const emitter = eventify({ toJSON () { return 'foo' } })
  1178. Object.keys(events).forEach(key => {
  1179. emitter.on(events[key], spooks.fn({
  1180. name: key,
  1181. log: log
  1182. }))
  1183. })
  1184. emitter.on(events.end, done)
  1185. })
  1186. test('string event occurred once', () => {
  1187. assert.strictEqual(log.counts.string, 1)
  1188. })
  1189. test('string event was dispatched correctly', () => {
  1190. assert.lengthOf(log.args.string[0], 1)
  1191. assert.strictEqual(log.args.string[0][0], 'foo')
  1192. })
  1193. test('end event occurred once', () => {
  1194. assert.strictEqual(log.counts.end, 1)
  1195. })
  1196. test('array event did not occur', () => {
  1197. assert.strictEqual(log.counts.array, 0)
  1198. })
  1199. test('object event did not occur', () => {
  1200. assert.strictEqual(log.counts.object, 0)
  1201. })
  1202. test('property event did not occur', () => {
  1203. assert.strictEqual(log.counts.property, 0)
  1204. })
  1205. test('number event did not occur', () => {
  1206. assert.strictEqual(log.counts.number, 0)
  1207. })
  1208. test('literal event did not occur', () => {
  1209. assert.strictEqual(log.counts.literal, 0)
  1210. })
  1211. test('endArray event did not occur', () => {
  1212. assert.strictEqual(log.counts.endArray, 0)
  1213. })
  1214. test('endObject event did not occur', () => {
  1215. assert.strictEqual(log.counts.endObject, 0)
  1216. })
  1217. test('error event did not occur', () => {
  1218. assert.strictEqual(log.counts.error, 0)
  1219. })
  1220. test('dataError event did not occur', () => {
  1221. assert.strictEqual(log.counts.dataError, 0)
  1222. })
  1223. test('endPrefix event did not occur', () => {
  1224. assert.strictEqual(log.counts.endPrefix, 0)
  1225. })
  1226. })
  1227. suite('map:', () => {
  1228. setup(done => {
  1229. let resolve
  1230. const emitter = eventify(new Map([['foo','bar'],['baz','qux']]))
  1231. Object.keys(events).forEach(key => {
  1232. emitter.on(events[key], spooks.fn({
  1233. name: key,
  1234. log: log
  1235. }))
  1236. })
  1237. emitter.on(events.end, done)
  1238. })
  1239. test('object event occurred once', () => {
  1240. assert.strictEqual(log.counts.object, 1)
  1241. })
  1242. test('property event occurred twice', () => {
  1243. assert.strictEqual(log.counts.property, 2)
  1244. })
  1245. test('property event was dispatched correctly first time', () => {
  1246. assert.strictEqual(log.args.property[0][0], 'foo')
  1247. })
  1248. test('property event was dispatched correctly second time', () => {
  1249. assert.strictEqual(log.args.property[1][0], 'baz')
  1250. })
  1251. test('string event occurred twice', () => {
  1252. assert.strictEqual(log.counts.string, 2)
  1253. })
  1254. test('string event was dispatched correctly first time', () => {
  1255. assert.strictEqual(log.args.string[0][0], 'bar')
  1256. })
  1257. test('string event was dispatched correctly second time', () => {
  1258. assert.strictEqual(log.args.string[1][0], 'qux')
  1259. })
  1260. test('endObject event occurred once', () => {
  1261. assert.strictEqual(log.counts.endObject, 1)
  1262. })
  1263. test('end event occurred once', () => {
  1264. assert.strictEqual(log.counts.end, 1)
  1265. })
  1266. test('array event did not occur', () => {
  1267. assert.strictEqual(log.counts.array, 0)
  1268. })
  1269. test('number event did not occur', () => {
  1270. assert.strictEqual(log.counts.number, 0)
  1271. })
  1272. test('literal event did not occur', () => {
  1273. assert.strictEqual(log.counts.literal, 0)
  1274. })
  1275. test('endArray event did not occur', () => {
  1276. assert.strictEqual(log.counts.endArray, 0)
  1277. })
  1278. test('error event did not occur', () => {
  1279. assert.strictEqual(log.counts.error, 0)
  1280. })
  1281. test('dataError event did not occur', () => {
  1282. assert.strictEqual(log.counts.dataError, 0)
  1283. })
  1284. test('endPrefix event did not occur', () => {
  1285. assert.strictEqual(log.counts.endPrefix, 0)
  1286. })
  1287. })
  1288. suite('ignore map:', () => {
  1289. setup(done => {
  1290. let resolve
  1291. const emitter = eventify(new Map([['foo','bar'],['baz','qux']]), { maps: 'ignore' })
  1292. Object.keys(events).forEach(key => {
  1293. emitter.on(events[key], spooks.fn({
  1294. name: key,
  1295. log: log
  1296. }))
  1297. })
  1298. emitter.on(events.end, done)
  1299. })
  1300. test('end event occurred once', () => {
  1301. assert.strictEqual(log.counts.end, 1)
  1302. })
  1303. test('array event did not occur', () => {
  1304. assert.strictEqual(log.counts.array, 0)
  1305. })
  1306. test('object event did not occur', () => {
  1307. assert.strictEqual(log.counts.object, 0)
  1308. })
  1309. test('property event did not occur', () => {
  1310. assert.strictEqual(log.counts.property, 0)
  1311. })
  1312. test('string event did not occur', () => {
  1313. assert.strictEqual(log.counts.string, 0)
  1314. })
  1315. test('number event did not occur', () => {
  1316. assert.strictEqual(log.counts.number, 0)
  1317. })
  1318. test('literal event did not occur', () => {
  1319. assert.strictEqual(log.counts.literal, 0)
  1320. })
  1321. test('endArray event did not occur', () => {
  1322. assert.strictEqual(log.counts.endArray, 0)
  1323. })
  1324. test('endObject event did not occur', () => {
  1325. assert.strictEqual(log.counts.endObject, 0)
  1326. })
  1327. test('error event did not occur', () => {
  1328. assert.strictEqual(log.counts.error, 0)
  1329. })
  1330. test('dataError event did not occur', () => {
  1331. assert.strictEqual(log.counts.dataError, 0)
  1332. })
  1333. test('endPrefix event did not occur', () => {
  1334. assert.strictEqual(log.counts.endPrefix, 0)
  1335. })
  1336. })
  1337. suite('set:', () => {
  1338. setup(done => {
  1339. let resolve
  1340. const emitter = eventify(new Set(['foo','bar']))
  1341. Object.keys(events).forEach(key => {
  1342. emitter.on(events[key], spooks.fn({
  1343. name: key,
  1344. log: log
  1345. }))
  1346. })
  1347. emitter.on(events.end, done)
  1348. })
  1349. test('array event occurred once', () => {
  1350. assert.strictEqual(log.counts.array, 1)
  1351. })
  1352. test('string event occurred twice', () => {
  1353. assert.strictEqual(log.counts.string, 2)
  1354. })
  1355. test('string event was dispatched correctly first time', () => {
  1356. assert.strictEqual(log.args.string[0][0], 'foo')
  1357. })
  1358. test('string event was dispatched correctly second time', () => {
  1359. assert.strictEqual(log.args.string[1][0], 'bar')
  1360. })
  1361. test('endArray event occurred once', () => {
  1362. assert.strictEqual(log.counts.endArray, 1)
  1363. })
  1364. test('end event occurred once', () => {
  1365. assert.strictEqual(log.counts.end, 1)
  1366. })
  1367. test('object event did not occur', () => {
  1368. assert.strictEqual(log.counts.object, 0)
  1369. })
  1370. test('property event did not occur', () => {
  1371. assert.strictEqual(log.counts.number, 0)
  1372. })
  1373. test('number event did not occur', () => {
  1374. assert.strictEqual(log.counts.number, 0)
  1375. })
  1376. test('literal event did not occur', () => {
  1377. assert.strictEqual(log.counts.literal, 0)
  1378. })
  1379. test('endObject event did not occur', () => {
  1380. assert.strictEqual(log.counts.endObject, 0)
  1381. })
  1382. test('error event did not occur', () => {
  1383. assert.strictEqual(log.counts.error, 0)
  1384. })
  1385. test('dataError event did not occur', () => {
  1386. assert.strictEqual(log.counts.dataError, 0)
  1387. })
  1388. test('endPrefix event did not occur', () => {
  1389. assert.strictEqual(log.counts.endPrefix, 0)
  1390. })
  1391. })
  1392. suite('ignore set:', () => {
  1393. setup(done => {
  1394. let resolve
  1395. const emitter = eventify(new Set(['foo','bar']), { iterables: 'ignore' })
  1396. Object.keys(events).forEach(key => {
  1397. emitter.on(events[key], spooks.fn({
  1398. name: key,
  1399. log: log
  1400. }))
  1401. })
  1402. emitter.on(events.end, done)
  1403. })
  1404. test('end event occurred once', () => {
  1405. assert.strictEqual(log.counts.end, 1)
  1406. })
  1407. test('array event did not occur', () => {
  1408. assert.strictEqual(log.counts.array, 0)
  1409. })
  1410. test('object event did not occur', () => {
  1411. assert.strictEqual(log.counts.object, 0)
  1412. })
  1413. test('property event did not occur', () => {
  1414. assert.strictEqual(log.counts.property, 0)
  1415. })
  1416. test('string event did not occur', () => {
  1417. assert.strictEqual(log.counts.string, 0)
  1418. })
  1419. test('number event did not occur', () => {
  1420. assert.strictEqual(log.counts.number, 0)
  1421. })
  1422. test('literal event did not occur', () => {
  1423. assert.strictEqual(log.counts.literal, 0)
  1424. })
  1425. test('endArray event did not occur', () => {
  1426. assert.strictEqual(log.counts.endArray, 0)
  1427. })
  1428. test('endObject event did not occur', () => {
  1429. assert.strictEqual(log.counts.endObject, 0)
  1430. })
  1431. test('error event did not occur', () => {
  1432. assert.strictEqual(log.counts.error, 0)
  1433. })
  1434. test('dataError event did not occur', () => {
  1435. assert.strictEqual(log.counts.dataError, 0)
  1436. })
  1437. test('endPrefix event did not occur', () => {
  1438. assert.strictEqual(log.counts.endPrefix, 0)
  1439. })
  1440. })
  1441. suite('promise resolved to a map:', () => {
  1442. setup(done => {
  1443. let resolve
  1444. const emitter = eventify(new Promise(res => resolve = res), { poll: 4 })
  1445. Object.keys(events).forEach(key => {
  1446. emitter.on(events[key], spooks.fn({
  1447. name: key,
  1448. log: log
  1449. }))
  1450. })
  1451. emitter.on(events.end, done)
  1452. setImmediate(resolve.bind(null, new Map([['foo','bar'],['baz','qux']])))
  1453. })
  1454. test('object event occurred once', () => {
  1455. assert.strictEqual(log.counts.object, 1)
  1456. })
  1457. test('property event occurred twice', () => {
  1458. assert.strictEqual(log.counts.property, 2)
  1459. })
  1460. test('property event was dispatched correctly first time', () => {
  1461. assert.strictEqual(log.args.property[0][0], 'foo')
  1462. })
  1463. test('property event was dispatched correctly second time', () => {
  1464. assert.strictEqual(log.args.property[1][0], 'baz')
  1465. })
  1466. test('string event occurred twice', () => {
  1467. assert.strictEqual(log.counts.string, 2)
  1468. })
  1469. test('string event was dispatched correctly first time', () => {
  1470. assert.strictEqual(log.args.string[0][0], 'bar')
  1471. })
  1472. test('string event was dispatched correctly second time', () => {
  1473. assert.strictEqual(log.args.string[1][0], 'qux')
  1474. })
  1475. test('endObject event occurred once', () => {
  1476. assert.strictEqual(log.counts.endObject, 1)
  1477. })
  1478. test('end event occurred once', () => {
  1479. assert.strictEqual(log.counts.end, 1)
  1480. })
  1481. test('array event did not occur', () => {
  1482. assert.strictEqual(log.counts.array, 0)
  1483. })
  1484. test('number event did not occur', () => {
  1485. assert.strictEqual(log.counts.number, 0)
  1486. })
  1487. test('literal event did not occur', () => {
  1488. assert.strictEqual(log.counts.literal, 0)
  1489. })
  1490. test('endArray event did not occur', () => {
  1491. assert.strictEqual(log.counts.endArray, 0)
  1492. })
  1493. test('error event did not occur', () => {
  1494. assert.strictEqual(log.counts.error, 0)
  1495. })
  1496. test('dataError event did not occur', () => {
  1497. assert.strictEqual(log.counts.dataError, 0)
  1498. })
  1499. test('endPrefix event did not occur', () => {
  1500. assert.strictEqual(log.counts.endPrefix, 0)
  1501. })
  1502. })
  1503. suite('array circular reference:', () => {
  1504. setup(done => {
  1505. const array = [ 'foo' ]
  1506. array[1] = array
  1507. const emitter = eventify(array)
  1508. Object.keys(events).forEach(key => {
  1509. emitter.on(events[key], spooks.fn({
  1510. name: key,
  1511. log: log
  1512. }))
  1513. })
  1514. emitter.on(events.end, done)
  1515. })
  1516. test('array event occurred twice', () => {
  1517. assert.strictEqual(log.counts.array, 2)
  1518. })
  1519. test('string event occurred once', () => {
  1520. assert.strictEqual(log.counts.string, 1)
  1521. })
  1522. test('string event was dispatched correctly', () => {
  1523. assert.strictEqual(log.args.string[0][0], 'foo')
  1524. })
  1525. test('endArray event occurred twice', () => {
  1526. assert.strictEqual(log.counts.endArray, 2)
  1527. })
  1528. test('end event occurred once', () => {
  1529. assert.strictEqual(log.counts.end, 1)
  1530. })
  1531. test('dataError event occurred once', () => {
  1532. assert.strictEqual(log.counts.dataError, 1)
  1533. })
  1534. test('dataError event was dispatched correctly', () => {
  1535. assert.lengthOf(log.args.dataError[0], 1)
  1536. assert.instanceOf(log.args.dataError[0][0], Error)
  1537. assert.strictEqual(log.args.dataError[0][0].message, 'Circular reference.')
  1538. })
  1539. test('error event did not occur', () => {
  1540. assert.strictEqual(log.counts.error, 0)
  1541. })
  1542. })
  1543. suite('object circular reference:', () => {
  1544. setup(done => {
  1545. const object = { foo: 'bar' }
  1546. object.self = object
  1547. const emitter = eventify(object)
  1548. Object.keys(events).forEach(key => {
  1549. emitter.on(events[key], spooks.fn({
  1550. name: key,
  1551. log: log
  1552. }))
  1553. })
  1554. emitter.on(events.end, done)
  1555. })
  1556. test('object event occurred twice', () => {
  1557. assert.strictEqual(log.counts.object, 2)
  1558. })
  1559. test('property event occurred twice', () => {
  1560. assert.strictEqual(log.counts.property, 2)
  1561. })
  1562. test('property event was dispatched correctly first time', () => {
  1563. assert.strictEqual(log.args.property[0][0], 'foo')
  1564. })
  1565. test('property event was dispatched correctly second time', () => {
  1566. assert.strictEqual(log.args.property[1][0], 'self')
  1567. })
  1568. test('endObject event occurred twice', () => {
  1569. assert.strictEqual(log.counts.endObject, 2)
  1570. })
  1571. test('end event occurred once', () => {
  1572. assert.strictEqual(log.counts.end, 1)
  1573. })
  1574. test('dataError event occurred once', () => {
  1575. assert.strictEqual(log.counts.dataError, 1)
  1576. })
  1577. test('dataError event was dispatched correctly', () => {
  1578. assert.strictEqual(log.args.dataError[0][0].message, 'Circular reference.')
  1579. })
  1580. test('error event did not occur', () => {
  1581. assert.strictEqual(log.counts.error, 0)
  1582. })
  1583. })
  1584. suite('array circular reference with ignore set:', () => {
  1585. setup(done => {
  1586. const array = [ 'foo' ]
  1587. array[1] = array
  1588. const emitter = eventify(array, { circular: 'ignore' })
  1589. Object.keys(events).forEach(key => {
  1590. emitter.on(events[key], spooks.fn({
  1591. name: key,
  1592. log: log
  1593. }))
  1594. })
  1595. emitter.on(events.end, done)
  1596. })
  1597. test('array event occurred twice', () => {
  1598. assert.strictEqual(log.counts.array, 2)
  1599. })
  1600. test('string event occurred once', () => {
  1601. assert.strictEqual(log.counts.string, 1)
  1602. })
  1603. test('endArray event occurred twice', () => {
  1604. assert.strictEqual(log.counts.endArray, 2)
  1605. })
  1606. test('end event occurred once', () => {
  1607. assert.strictEqual(log.counts.end, 1)
  1608. })
  1609. test('error event did not occur', () => {
  1610. assert.strictEqual(log.counts.error, 0)
  1611. })
  1612. test('dataError event did not occur', () => {
  1613. assert.strictEqual(log.counts.dataError, 0)
  1614. })
  1615. })
  1616. suite('object circular reference with ignore set:', () => {
  1617. setup(done => {
  1618. const object = { foo: 'bar' }
  1619. object.self = object
  1620. const emitter = eventify(object, { circular: 'ignore' })
  1621. Object.keys(events).forEach(key => {
  1622. emitter.on(events[key], spooks.fn({
  1623. name: key,
  1624. log: log
  1625. }))
  1626. })
  1627. emitter.on(events.end, done)
  1628. })
  1629. test('object event occurred twice', () => {
  1630. assert.strictEqual(log.counts.object, 2)
  1631. })
  1632. test('property event occurred twice', () => {
  1633. assert.strictEqual(log.counts.property, 2)
  1634. })
  1635. test('endObject event occurred twice', () => {
  1636. assert.strictEqual(log.counts.endObject, 2)
  1637. })
  1638. test('end event occurred once', () => {
  1639. assert.strictEqual(log.counts.end, 1)
  1640. })
  1641. test('error event did not occur', () => {
  1642. assert.strictEqual(log.counts.error, 0)
  1643. })
  1644. test('dataError event did not occur', () => {
  1645. assert.strictEqual(log.counts.dataError, 0)
  1646. })
  1647. })
  1648. suite('parallel array reference:', () => {
  1649. setup(done => {
  1650. const array = [ 'foo' ]
  1651. const emitter = eventify([ array, array ])
  1652. Object.keys(events).forEach(key => {
  1653. emitter.on(events[key], spooks.fn({
  1654. name: key,
  1655. log: log
  1656. }))
  1657. })
  1658. emitter.on(events.end, done)
  1659. })
  1660. test('array event occurred three times', () => {
  1661. assert.strictEqual(log.counts.array, 3)
  1662. })
  1663. test('string event occurred twice', () => {
  1664. assert.strictEqual(log.counts.string, 2)
  1665. })
  1666. test('endArray event occurred three times', () => {
  1667. assert.strictEqual(log.counts.endArray, 3)
  1668. })
  1669. test('end event occurred once', () => {
  1670. assert.strictEqual(log.counts.end, 1)
  1671. })
  1672. test('error event did not occur', () => {
  1673. assert.strictEqual(log.counts.error, 0)
  1674. })
  1675. test('dataError event did not occur', () => {
  1676. assert.strictEqual(log.counts.dataError, 0)
  1677. })
  1678. })
  1679. suite('parallel object reference:', () => {
  1680. setup(done => {
  1681. const object = { foo: 'bar' }
  1682. const emitter = eventify({ baz: object, qux: object })
  1683. Object.keys(events).forEach(key => {
  1684. emitter.on(events[key], spooks.fn({
  1685. name: key,
  1686. log: log
  1687. }))
  1688. })
  1689. emitter.on(events.end, done)
  1690. })
  1691. test('object event occurred three times', () => {
  1692. assert.strictEqual(log.counts.object, 3)
  1693. })
  1694. test('property event occurred four times', () => {
  1695. assert.strictEqual(log.counts.property, 4)
  1696. })
  1697. test('endObject event occurred three times', () => {
  1698. assert.strictEqual(log.counts.endObject, 3)
  1699. })
  1700. test('end event occurred once', () => {
  1701. assert.strictEqual(log.counts.end, 1)
  1702. })
  1703. test('error event did not occur', () => {
  1704. assert.strictEqual(log.counts.error, 0)
  1705. })
  1706. test('dataError event did not occur', () => {
  1707. assert.strictEqual(log.counts.dataError, 0)
  1708. })
  1709. })
  1710. suite('throw errors from event handlers:', () => {
  1711. setup(done => {
  1712. const emitter = eventify([null,false,true,0,"",{"foo":"bar"}])
  1713. Object.keys(events).forEach(key => {
  1714. const event = events[key]
  1715. emitter.on(event, spooks.fn({
  1716. name: key,
  1717. log: log
  1718. }))
  1719. if (event !== events.end) {
  1720. emitter.on(event, () => { throw 0 })
  1721. }
  1722. })
  1723. emitter.on(events.end, done)
  1724. })
  1725. test('end event occurred once', () => {
  1726. assert.strictEqual(log.counts.end, 1)
  1727. })
  1728. test('array event occured once', () => {
  1729. assert.strictEqual(log.counts.array, 1)
  1730. })
  1731. test('literal event occured three times', () => {
  1732. assert.strictEqual(log.counts.literal, 3)
  1733. })
  1734. test('number event occured once', () => {
  1735. assert.strictEqual(log.counts.number, 1)
  1736. })
  1737. test('string event occured twice', () => {
  1738. assert.strictEqual(log.counts.string, 2)
  1739. })
  1740. test('object event occured once', () => {
  1741. assert.strictEqual(log.counts.object, 1)
  1742. })
  1743. test('property event occured once', () => {
  1744. assert.strictEqual(log.counts.property, 1)
  1745. })
  1746. test('endObject event occured once', () => {
  1747. assert.strictEqual(log.counts.endObject, 1)
  1748. })
  1749. test('endArray event occured once', () => {
  1750. assert.strictEqual(log.counts.endArray, 1)
  1751. })
  1752. test('error event occured eleven times', () => {
  1753. assert.strictEqual(log.counts.error, 11)
  1754. })
  1755. test('dataError event did not occur', () => {
  1756. assert.strictEqual(log.counts.dataError, 0)
  1757. })
  1758. })
  1759. })
  1760. })