eventify.js 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357
  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.entries(events).forEach(([ key, value ]) => {
  45. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  97. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  149. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  201. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  253. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  301. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  349. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  405. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  461. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  513. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  538. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  590. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  642. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  669. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  705. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  766. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  814. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  856. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  898. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  962. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  1016. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  1066. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  1119. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  1168. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  1221. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  1274. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  1335. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  1384. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  1439. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  1488. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  1551. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  1591. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  1632. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  1664. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  1695. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  1726. emitter.on(value, 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.entries(events).forEach(([ key, value ]) => {
  1756. emitter.on(value, spooks.fn({
  1757. name: key,
  1758. log: log
  1759. }))
  1760. if (value !== events.end) {
  1761. emitter.on(value, () => { throw 0 })
  1762. }
  1763. })
  1764. emitter.on(events.end, done)
  1765. })
  1766. test('end event occurred once', () => {
  1767. assert.strictEqual(log.counts.end, 1)
  1768. })
  1769. test('array event occured once', () => {
  1770. assert.strictEqual(log.counts.array, 1)
  1771. })
  1772. test('literal event occured three times', () => {
  1773. assert.strictEqual(log.counts.literal, 3)
  1774. })
  1775. test('number event occured once', () => {
  1776. assert.strictEqual(log.counts.number, 1)
  1777. })
  1778. test('string event occured twice', () => {
  1779. assert.strictEqual(log.counts.string, 2)
  1780. })
  1781. test('object event occured once', () => {
  1782. assert.strictEqual(log.counts.object, 1)
  1783. })
  1784. test('property event occured once', () => {
  1785. assert.strictEqual(log.counts.property, 1)
  1786. })
  1787. test('endObject event occured once', () => {
  1788. assert.strictEqual(log.counts.endObject, 1)
  1789. })
  1790. test('endArray event occured once', () => {
  1791. assert.strictEqual(log.counts.endArray, 1)
  1792. })
  1793. test('error event occured eleven times', () => {
  1794. assert.strictEqual(log.counts.error, 11)
  1795. })
  1796. test('dataError event did not occur', () => {
  1797. assert.strictEqual(log.counts.dataError, 0)
  1798. })
  1799. })
  1800. })
  1801. })