match.js 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  1. 'use strict'
  2. const assert = require('chai').assert
  3. const proxyquire = require('proxyquire')
  4. const spooks = require('spooks')
  5. const modulePath = '../../src/match'
  6. suite('match:', () => {
  7. test('require does not throw', () => {
  8. assert.doesNotThrow(() => {
  9. require(modulePath)
  10. })
  11. })
  12. test('require returns function', () => {
  13. assert.isFunction(require(modulePath))
  14. })
  15. suite('require, results.push returns true:', () => {
  16. let log, resume, results, match
  17. setup(() => {
  18. log = {}
  19. resume = spooks.fn({ name: 'resume', log })
  20. results = {
  21. walk: [
  22. {
  23. on: spooks.fn({ name: 'on', log: log }),
  24. pause: spooks.fn({ name: 'pause', log: log, results: [ resume ] })
  25. }
  26. ],
  27. push: [ true ]
  28. }
  29. match = proxyquire(modulePath, {
  30. './walk': spooks.fn({
  31. name: 'walk',
  32. log: log,
  33. results: results.walk
  34. }),
  35. './datastream': spooks.ctor({
  36. name: 'DataStream',
  37. log: log,
  38. archetype: { instance: { push: () => {}, emit: () => {} } },
  39. results: results
  40. })
  41. })
  42. })
  43. test('match expects two arguments', () => {
  44. assert.lengthOf(match, 2)
  45. })
  46. test('match does not throw with match function', () => {
  47. assert.doesNotThrow(() => match(null, () => {}))
  48. })
  49. test('match does not throw with match string', () => {
  50. assert.doesNotThrow(() => match(null, ' '))
  51. })
  52. test('match throws with empty match string', () => {
  53. assert.throws(() => match(null, ''))
  54. })
  55. test('match does not throw with match regex', () => {
  56. assert.doesNotThrow(() => match(null, /.*/))
  57. })
  58. test('match throws with invalid match arg', () => {
  59. assert.throws(() => match(null, {}))
  60. })
  61. test('match returns stream', () => {
  62. assert.isFunction(match(null, /.*/).push)
  63. assert.isFunction(match(null, /.*/).emit)
  64. })
  65. test('DataStream was not called', () => {
  66. assert.strictEqual(log.counts.DataStream, 0)
  67. })
  68. test('walk was not called', () => {
  69. assert.strictEqual(log.counts.walk, 0)
  70. })
  71. test('EventEmitter.on was not called', () => {
  72. assert.strictEqual(log.counts.on, 0)
  73. })
  74. test('EventEmitter.pause was not called', () => {
  75. assert.strictEqual(log.counts.pause, 0)
  76. })
  77. suite('match with predicate returning true:', () => {
  78. let stream, predicate, options, result
  79. setup(() => {
  80. stream = {}
  81. predicate = spooks.fn({ name: 'predicate', log, results: [ true ] })
  82. options = { foo: 'bar', highWaterMark: 42 }
  83. result = match(stream, predicate, options)
  84. })
  85. test('DataStream was called once', () => {
  86. assert.strictEqual(log.counts.DataStream, 1)
  87. assert.isObject(log.these.DataStream[0])
  88. })
  89. test('DataStream was called correctly', () => {
  90. assert.lengthOf(log.args.DataStream[0], 2)
  91. assert.isFunction(log.args.DataStream[0][0])
  92. assert.deepEqual(log.args.DataStream[0][1], { highWaterMark: 42 })
  93. })
  94. test('walk was called once', () => {
  95. assert.strictEqual(log.counts.walk, 1)
  96. assert.isUndefined(log.these.walk[0])
  97. })
  98. test('walk was called correctly', () => {
  99. assert.lengthOf(log.args.walk[0], 2)
  100. assert.strictEqual(log.args.walk[0][0], stream)
  101. assert.lengthOf(Object.keys(log.args.walk[0][0]), 0)
  102. assert.strictEqual(log.args.walk[0][1], options)
  103. assert.lengthOf(Object.keys(log.args.walk[0][1]), 2)
  104. })
  105. test('EventEmitter.on was called eleven times', () => {
  106. assert.strictEqual(log.counts.on, 11)
  107. assert.strictEqual(log.these.on[0], results.walk[0])
  108. assert.strictEqual(log.these.on[1], results.walk[0])
  109. assert.strictEqual(log.these.on[2], results.walk[0])
  110. assert.strictEqual(log.these.on[3], results.walk[0])
  111. assert.strictEqual(log.these.on[4], results.walk[0])
  112. assert.strictEqual(log.these.on[5], results.walk[0])
  113. assert.strictEqual(log.these.on[6], results.walk[0])
  114. assert.strictEqual(log.these.on[7], results.walk[0])
  115. assert.strictEqual(log.these.on[8], results.walk[0])
  116. assert.strictEqual(log.these.on[9], results.walk[0])
  117. assert.strictEqual(log.these.on[10], results.walk[0])
  118. })
  119. test('EventEmitter.on was called correctly first time', () => {
  120. assert.lengthOf(log.args.on[0], 2)
  121. assert.strictEqual(log.args.on[0][0], 'arr')
  122. assert.isFunction(log.args.on[0][1])
  123. })
  124. test('EventEmitter.on was called correctly second time', () => {
  125. assert.lengthOf(log.args.on[1], 2)
  126. assert.strictEqual(log.args.on[1][0], 'obj')
  127. assert.isFunction(log.args.on[1][1])
  128. })
  129. test('EventEmitter.on was called correctly third time', () => {
  130. assert.lengthOf(log.args.on[2], 2)
  131. assert.strictEqual(log.args.on[2][0], 'pro')
  132. assert.isFunction(log.args.on[2][1])
  133. })
  134. test('EventEmitter.on was called correctly fourth time', () => {
  135. assert.lengthOf(log.args.on[3], 2)
  136. assert.strictEqual(log.args.on[3][0], 'end-arr')
  137. assert.isFunction(log.args.on[3][1])
  138. })
  139. test('EventEmitter.on was called correctly fifth time', () => {
  140. assert.lengthOf(log.args.on[4], 2)
  141. assert.strictEqual(log.args.on[4][0], 'end-obj')
  142. assert.isFunction(log.args.on[4][1])
  143. })
  144. test('EventEmitter.on was called correctly sixth time', () => {
  145. assert.lengthOf(log.args.on[5], 2)
  146. assert.strictEqual(log.args.on[5][0], 'str')
  147. assert.isFunction(log.args.on[5][1])
  148. })
  149. test('EventEmitter.on was called correctly seventh time', () => {
  150. assert.lengthOf(log.args.on[6], 2)
  151. assert.strictEqual(log.args.on[6][0], 'num')
  152. assert.isFunction(log.args.on[6][1])
  153. })
  154. test('EventEmitter.on was called correctly eighth time', () => {
  155. assert.lengthOf(log.args.on[7], 2)
  156. assert.strictEqual(log.args.on[7][0], 'lit')
  157. assert.isFunction(log.args.on[7][1])
  158. })
  159. test('EventEmitter.on was called correctly ninth time', () => {
  160. assert.lengthOf(log.args.on[8], 2)
  161. assert.strictEqual(log.args.on[8][0], 'end')
  162. assert.isFunction(log.args.on[8][1])
  163. })
  164. test('EventEmitter.on was called correctly tenth time', () => {
  165. assert.lengthOf(log.args.on[9], 2)
  166. assert.strictEqual(log.args.on[9][0], 'err')
  167. assert.isFunction(log.args.on[9][1])
  168. })
  169. test('EventEmitter.on was called correctly eleventh time', () => {
  170. assert.lengthOf(log.args.on[10], 2)
  171. assert.strictEqual(log.args.on[10][0], 'err-data')
  172. assert.isFunction(log.args.on[10][1])
  173. })
  174. suite('array event:', () => {
  175. setup(() => {
  176. log.args.on[0][1]()
  177. })
  178. test('results.push was not called', () => {
  179. assert.strictEqual(log.counts.push, 0)
  180. })
  181. suite('end event:', () => {
  182. setup(() => {
  183. log.args.on[8][1]()
  184. })
  185. test('results.push was not called', () => {
  186. assert.strictEqual(log.counts.push, 0)
  187. })
  188. suite('read stream:', () => {
  189. setup(() => {
  190. log.args.DataStream[0][0]()
  191. })
  192. test('results.push was called once', () => {
  193. assert.strictEqual(log.counts.push, 1)
  194. })
  195. test('results.push was called correctly', () => {
  196. assert.lengthOf(log.args.push[0], 1)
  197. assert.isNull(log.args.push[0][0])
  198. })
  199. test('predicate was not called', () => {
  200. assert.strictEqual(log.counts.predicate, 0)
  201. })
  202. })
  203. })
  204. suite('endArray and end events:', () => {
  205. setup(() => {
  206. log.args.on[3][1]()
  207. log.args.on[8][1]()
  208. })
  209. test('predicate was called once', () => {
  210. assert.strictEqual(log.counts.predicate, 1)
  211. })
  212. test('predicate was called correctly', () => {
  213. assert.lengthOf(log.args.predicate[0], 3)
  214. assert.isUndefined(log.args.predicate[0][0])
  215. assert.deepEqual(log.args.predicate[0][1], [])
  216. assert.strictEqual(log.args.predicate[0][2], 0)
  217. })
  218. test('results.push was not called', () => {
  219. assert.strictEqual(log.counts.push, 0)
  220. })
  221. suite('read stream:', () => {
  222. setup(() => {
  223. log.args.DataStream[0][0]()
  224. })
  225. test('results.push was called twice', () => {
  226. assert.strictEqual(log.counts.push, 2)
  227. })
  228. test('results.push was called correctly first time', () => {
  229. assert.lengthOf(log.args.push[0], 1)
  230. assert.deepEqual(log.args.push[0][0], [])
  231. })
  232. test('results.push was called correctly second time', () => {
  233. assert.lengthOf(log.args.push[1], 1)
  234. assert.isNull(log.args.push[1][0])
  235. })
  236. test('results.emit was not called', () => {
  237. assert.strictEqual(log.counts.emit, 0)
  238. })
  239. })
  240. })
  241. suite('read stream:', () => {
  242. setup(() => {
  243. log.args.DataStream[0][0]()
  244. })
  245. test('results.push was not called', () => {
  246. assert.strictEqual(log.counts.push, 0)
  247. })
  248. suite('end event:', () => {
  249. setup(() => {
  250. log.args.on[8][1]()
  251. })
  252. test('results.push was called once', () => {
  253. assert.strictEqual(log.counts.push, 1)
  254. })
  255. test('results.push was called correctly', () => {
  256. assert.isNull(log.args.push[0][0])
  257. })
  258. test('results.emit was not called', () => {
  259. assert.strictEqual(log.counts.emit, 0)
  260. })
  261. })
  262. suite('dataError event:', () => {
  263. setup(() => {
  264. log.args.on[10][1]('foo')
  265. })
  266. test('results.push was not called', () => {
  267. assert.strictEqual(log.counts.push, 0)
  268. })
  269. test('results.emit was called once', () => {
  270. assert.strictEqual(log.counts.emit, 1)
  271. })
  272. test('results.emit was called correctly', () => {
  273. assert.lengthOf(log.args.emit[0], 2)
  274. assert.strictEqual(log.args.emit[0][0], 'dataError')
  275. assert.strictEqual(log.args.emit[0][1], 'foo')
  276. })
  277. test('predicate was not called', () => {
  278. assert.strictEqual(log.counts.predicate, 0)
  279. })
  280. })
  281. suite('string event:', () => {
  282. setup(() => {
  283. log.args.on[5][1]('foo')
  284. })
  285. test('predicate was called once', () => {
  286. assert.strictEqual(log.counts.predicate, 1)
  287. })
  288. test('predicate was called correctly', () => {
  289. assert.lengthOf(log.args.predicate[0], 3)
  290. assert.strictEqual(log.args.predicate[0][0], 0)
  291. assert.strictEqual(log.args.predicate[0][1], 'foo')
  292. assert.strictEqual(log.args.predicate[0][2], 1)
  293. })
  294. test('results.push was called once', () => {
  295. assert.strictEqual(log.counts.push, 1)
  296. })
  297. test('results.push was called correctly', () => {
  298. assert.strictEqual(log.args.push[0][0], 'foo')
  299. })
  300. suite('string event:', () => {
  301. setup(() => {
  302. log.args.on[5][1]('bar')
  303. })
  304. test('predicate was called once', () => {
  305. assert.strictEqual(log.counts.predicate, 2)
  306. })
  307. test('predicate was called correctly', () => {
  308. assert.strictEqual(log.args.predicate[1][0], 1)
  309. assert.strictEqual(log.args.predicate[1][1], 'bar')
  310. assert.strictEqual(log.args.predicate[1][2], 1)
  311. })
  312. test('results.push was called once', () => {
  313. assert.strictEqual(log.counts.push, 2)
  314. })
  315. test('results.push was called correctly', () => {
  316. assert.strictEqual(log.args.push[1][0], 'bar')
  317. })
  318. })
  319. suite('array event:', () => {
  320. setup(() => {
  321. log.args.on[0][1]()
  322. })
  323. test('predicate was not called', () => {
  324. assert.strictEqual(log.counts.predicate, 1)
  325. })
  326. test('results.push was not called', () => {
  327. assert.strictEqual(log.counts.push, 1)
  328. })
  329. suite('endArray event:', () => {
  330. setup(() => {
  331. log.args.on[3][1]()
  332. })
  333. test('predicate was called once', () => {
  334. assert.strictEqual(log.counts.predicate, 2)
  335. })
  336. test('predicate was called correctly', () => {
  337. assert.strictEqual(log.args.predicate[1][0], 1)
  338. assert.deepEqual(log.args.predicate[1][1], [])
  339. assert.strictEqual(log.args.predicate[1][2], 1)
  340. })
  341. test('results.push was called once', () => {
  342. assert.strictEqual(log.counts.push, 2)
  343. })
  344. test('results.push was called correctly', () => {
  345. assert.deepEqual(log.args.push[1][0], [])
  346. })
  347. suite('endArray event:', () => {
  348. setup(() => {
  349. log.args.on[3][1]()
  350. })
  351. test('predicate was called once', () => {
  352. assert.strictEqual(log.counts.predicate, 3)
  353. })
  354. test('predicate was called correctly', () => {
  355. assert.isUndefined(log.args.predicate[2][0])
  356. assert.deepEqual(log.args.predicate[2][1], [ 'foo', [] ])
  357. assert.strictEqual(log.args.predicate[2][2], 0)
  358. })
  359. test('results.push was called once', () => {
  360. assert.strictEqual(log.counts.push, 3)
  361. })
  362. test('results.push was called correctly', () => {
  363. assert.deepEqual(log.args.push[2][0], [ 'foo', [] ])
  364. })
  365. test('EventEmitter.pause was not called', () => {
  366. assert.strictEqual(log.counts.pause, 0)
  367. })
  368. })
  369. })
  370. })
  371. suite('object event:', () => {
  372. setup(() => {
  373. log.args.on[1][1]()
  374. })
  375. test('results.push was not called', () => {
  376. assert.strictEqual(log.counts.push, 1)
  377. })
  378. suite('property event:', () => {
  379. setup(() => {
  380. log.args.on[2][1]('bar')
  381. })
  382. test('predicate was not called', () => {
  383. assert.strictEqual(log.counts.predicate, 1)
  384. })
  385. test('results.push was not called', () => {
  386. assert.strictEqual(log.counts.push, 1)
  387. })
  388. suite('string event:', () => {
  389. setup(() => {
  390. log.args.on[5][1]('baz')
  391. })
  392. test('predicate was called once', () => {
  393. assert.strictEqual(log.counts.predicate, 2)
  394. })
  395. test('predicate was called correctly', () => {
  396. assert.strictEqual(log.args.predicate[1][0], 'bar')
  397. assert.strictEqual(log.args.predicate[1][1], 'baz')
  398. assert.strictEqual(log.args.predicate[1][2], 2)
  399. })
  400. test('results.push was called once', () => {
  401. assert.strictEqual(log.counts.push, 2)
  402. })
  403. test('results.push was called correctly', () => {
  404. assert.strictEqual(log.args.push[1][0], 'baz')
  405. })
  406. suite('property event:', () => {
  407. setup(() => {
  408. log.args.on[2][1]('nested')
  409. })
  410. test('results.push was not called', () => {
  411. assert.strictEqual(log.counts.push, 2)
  412. })
  413. suite('object event:', () => {
  414. setup(() => {
  415. log.args.on[1][1]()
  416. })
  417. test('predicate was not called', () => {
  418. assert.strictEqual(log.counts.predicate, 2)
  419. })
  420. test('results.push was not called', () => {
  421. assert.strictEqual(log.counts.push, 2)
  422. })
  423. suite('endObject event:', () => {
  424. setup(() => {
  425. log.args.on[4][1]()
  426. })
  427. test('predicate was called once', () => {
  428. assert.strictEqual(log.counts.predicate, 3)
  429. })
  430. test('predicate was called correctly', () => {
  431. assert.strictEqual(log.args.predicate[2][0], 'nested')
  432. assert.deepEqual(log.args.predicate[2][1], {})
  433. assert.strictEqual(log.args.predicate[2][2], 2)
  434. })
  435. test('results.push was called once', () => {
  436. assert.strictEqual(log.counts.push, 3)
  437. })
  438. test('results.push was called correctly', () => {
  439. assert.deepEqual(log.args.push[2][0], {})
  440. })
  441. suite('endObject event:', () => {
  442. setup(() => {
  443. log.args.on[4][1]()
  444. })
  445. test('predicate was called once', () => {
  446. assert.strictEqual(log.counts.predicate, 4)
  447. })
  448. test('predicate was called correctly', () => {
  449. assert.strictEqual(log.args.predicate[3][0], 1)
  450. assert.deepEqual(log.args.predicate[3][1], { bar: 'baz', nested: {} })
  451. assert.strictEqual(log.args.predicate[3][2], 1)
  452. })
  453. test('results.push was called once', () => {
  454. assert.strictEqual(log.counts.push, 4)
  455. })
  456. test('results.push was called correctly', () => {
  457. assert.deepEqual(log.args.push[3][0], { bar: 'baz', nested: {} })
  458. })
  459. test('EventEmitter.pause was not called', () => {
  460. assert.strictEqual(log.counts.pause, 0)
  461. })
  462. })
  463. })
  464. })
  465. })
  466. })
  467. })
  468. })
  469. })
  470. suite('string events, push returns false:', () => {
  471. setup(() => {
  472. results.push[0] = false
  473. log.args.on[5][1]('foo')
  474. log.args.on[5][1]('bar')
  475. })
  476. teardown(() => {
  477. results.push[0] = true
  478. })
  479. test('predicate was called twice', () => {
  480. assert.strictEqual(log.counts.predicate, 2)
  481. })
  482. test('results.push was called once', () => {
  483. assert.strictEqual(log.counts.push, 1)
  484. })
  485. test('results.push was called correctly', () => {
  486. assert.strictEqual(log.args.push[0][0], 'foo')
  487. })
  488. test('emitter.pause was called once', () => {
  489. assert.strictEqual(log.counts.pause, 1)
  490. assert.strictEqual(log.these.pause[0], results.walk[0])
  491. })
  492. test('emitter.pause was called correctly', () => {
  493. assert.lengthOf(log.args.pause[0], 0)
  494. })
  495. test('resume was not called', () => {
  496. assert.strictEqual(log.counts.resume, 0)
  497. })
  498. suite('read stream:', () => {
  499. setup(() => {
  500. log.args.DataStream[0][0]()
  501. })
  502. test('resume was called once', () => {
  503. assert.strictEqual(log.counts.resume, 1)
  504. assert.isUndefined(log.these.resume[0])
  505. })
  506. test('resume was called correctly', () => {
  507. assert.lengthOf(log.args.resume[0], 0)
  508. })
  509. test('results.push was called once', () => {
  510. assert.strictEqual(log.counts.push, 2)
  511. })
  512. test('results.push was called correctly', () => {
  513. assert.strictEqual(log.args.push[1][0], 'bar')
  514. })
  515. })
  516. })
  517. })
  518. suite('all events then read:', () => {
  519. setup(() => {
  520. log.args.on[1][1]()
  521. log.args.on[2][1]('foo')
  522. log.args.on[5][1]('bar')
  523. log.args.on[4][1]()
  524. log.args.on[5][1]('')
  525. log.args.on[6][1](0)
  526. log.args.on[7][1](null)
  527. log.args.on[7][1](false)
  528. log.args.on[3][1]()
  529. log.args.on[8][1]()
  530. log.args.DataStream[0][0]()
  531. })
  532. test('predicate was called six times', () => {
  533. assert.strictEqual(log.counts.predicate, 6)
  534. })
  535. test('predicate was called correctly first time', () => {
  536. assert.strictEqual(log.args.predicate[0][0], 'foo')
  537. assert.strictEqual(log.args.predicate[0][1], 'bar')
  538. assert.strictEqual(log.args.predicate[0][2], 2)
  539. })
  540. test('predicate was called correctly second time', () => {
  541. assert.strictEqual(log.args.predicate[1][0], 0)
  542. assert.deepEqual(log.args.predicate[1][1], { foo: 'bar' })
  543. assert.strictEqual(log.args.predicate[1][2], 1)
  544. })
  545. test('predicate was called correctly third time', () => {
  546. assert.strictEqual(log.args.predicate[2][0], 1)
  547. assert.strictEqual(log.args.predicate[2][1], '')
  548. assert.strictEqual(log.args.predicate[2][2], 1)
  549. })
  550. test('predicate was called correctly fourth time', () => {
  551. assert.strictEqual(log.args.predicate[3][0], 2)
  552. assert.strictEqual(log.args.predicate[3][1], 0)
  553. assert.strictEqual(log.args.predicate[3][2], 1)
  554. })
  555. test('predicate was called correctly fifth time', () => {
  556. assert.strictEqual(log.args.predicate[4][0], 4)
  557. assert.strictEqual(log.args.predicate[4][1], false)
  558. assert.strictEqual(log.args.predicate[4][2], 1)
  559. })
  560. test('predicate was called correctly sixth time', () => {
  561. assert.isUndefined(log.args.predicate[5][0])
  562. assert.deepEqual(log.args.predicate[5][1], [ { foo: 'bar' }, '', 0, null, false ])
  563. assert.strictEqual(log.args.predicate[5][2], 0)
  564. })
  565. test('results.push was called seven times', () => {
  566. assert.strictEqual(log.counts.push, 7)
  567. })
  568. test('results.push was called correctly', () => {
  569. assert.strictEqual(log.args.push[0][0], 'bar')
  570. assert.deepEqual(log.args.push[1][0], { foo: 'bar' })
  571. assert.strictEqual(log.args.push[2][0], '')
  572. assert.strictEqual(log.args.push[3][0], 0)
  573. assert.strictEqual(log.args.push[4][0], false)
  574. assert.deepEqual(log.args.push[5][0], [ { foo: 'bar' }, '', 0, null, false ])
  575. assert.isNull(log.args.push[6][0])
  576. })
  577. test('results.emit was not called', () => {
  578. assert.strictEqual(log.counts.emit, 0)
  579. })
  580. })
  581. })
  582. suite('read then all events:', () => {
  583. setup(() => {
  584. log.args.DataStream[0][0]()
  585. log.args.on[0][1]()
  586. log.args.on[1][1]()
  587. log.args.on[2][1]('foo')
  588. log.args.on[5][1]('bar')
  589. log.args.on[4][1]()
  590. log.args.on[5][1]('')
  591. log.args.on[6][1](0)
  592. log.args.on[7][1](null)
  593. log.args.on[7][1](false)
  594. log.args.on[3][1]()
  595. log.args.on[8][1]()
  596. })
  597. test('results.push was called seven times', () => {
  598. assert.strictEqual(log.counts.push, 7)
  599. })
  600. test('results.push was called correctly', () => {
  601. assert.strictEqual(log.args.push[0][0], 'bar')
  602. assert.deepEqual(log.args.push[1][0], { foo: 'bar' })
  603. assert.strictEqual(log.args.push[2][0], '')
  604. assert.strictEqual(log.args.push[3][0], 0)
  605. assert.strictEqual(log.args.push[4][0], false)
  606. assert.deepEqual(log.args.push[5][0], [ { foo: 'bar' }, '', 0, null, false ])
  607. assert.isNull(log.args.push[6][0])
  608. })
  609. test('results.emit was not called', () => {
  610. assert.strictEqual(log.counts.emit, 0)
  611. })
  612. })
  613. })
  614. suite('match with predicate returning false:', () => {
  615. let stream, predicate, options, result
  616. setup(() => {
  617. predicate = spooks.fn({ name: 'predicate', log, results: [ false ] })
  618. result = match({}, predicate, {})
  619. })
  620. test('DataStream was called once', () => {
  621. assert.strictEqual(log.counts.DataStream, 1)
  622. })
  623. test('walk was called once', () => {
  624. assert.strictEqual(log.counts.walk, 1)
  625. })
  626. test('EventEmitter.on was called eleven times', () => {
  627. assert.strictEqual(log.counts.on, 11)
  628. })
  629. suite('read events:', () => {
  630. setup(() => {
  631. log.args.DataStream[0][0]()
  632. // [ { "foo": "bar" }, "baz", 1, true ]
  633. log.args.on[0][1]()
  634. log.args.on[1][1]()
  635. log.args.on[2][1]('foo')
  636. log.args.on[5][1]('bar')
  637. log.args.on[4][1]()
  638. log.args.on[5][1]('baz')
  639. log.args.on[6][1](1)
  640. log.args.on[7][1](true)
  641. log.args.on[3][1]()
  642. log.args.on[8][1]()
  643. })
  644. test('results.push was called once', () => {
  645. assert.strictEqual(log.counts.push, 1)
  646. })
  647. test('results.push was called correctly', () => {
  648. assert.isNull(log.args.push[0][0])
  649. })
  650. test('results.emit was not called', () => {
  651. assert.strictEqual(log.counts.emit, 0)
  652. })
  653. })
  654. })
  655. suite('match with string:', () => {
  656. let stream, options, result
  657. setup(() => {
  658. result = match({}, 'foo', {})
  659. })
  660. test('DataStream was called once', () => {
  661. assert.strictEqual(log.counts.DataStream, 1)
  662. })
  663. test('walk was called once', () => {
  664. assert.strictEqual(log.counts.walk, 1)
  665. })
  666. test('EventEmitter.on was called eleven times', () => {
  667. assert.strictEqual(log.counts.on, 11)
  668. })
  669. suite('read events:', () => {
  670. setup(() => {
  671. log.args.DataStream[0][0]()
  672. // { "foo": "bar", "baz": "qux", "foo": "wibble" }
  673. log.args.on[1][1]()
  674. log.args.on[2][1]('foo')
  675. log.args.on[5][1]('bar')
  676. log.args.on[2][1]('baz')
  677. log.args.on[5][1]('qux')
  678. log.args.on[2][1]('foo')
  679. log.args.on[5][1]('wibble')
  680. log.args.on[4][1]()
  681. log.args.on[8][1]()
  682. })
  683. test('results.push was called three times', () => {
  684. assert.strictEqual(log.counts.push, 3)
  685. })
  686. test('results.push was called correctly first time', () => {
  687. assert.strictEqual(log.args.push[0][0], 'bar')
  688. })
  689. test('results.push was called correctly second time', () => {
  690. assert.strictEqual(log.args.push[1][0], 'wibble')
  691. })
  692. test('results.push was called correctly third time', () => {
  693. assert.isNull(log.args.push[2][0])
  694. })
  695. test('results.emit was not called', () => {
  696. assert.strictEqual(log.counts.emit, 0)
  697. })
  698. })
  699. })
  700. suite('match with regular expression:', () => {
  701. let stream, options, result
  702. setup(() => {
  703. result = match({}, /oo/, {})
  704. })
  705. test('DataStream was called once', () => {
  706. assert.strictEqual(log.counts.DataStream, 1)
  707. })
  708. test('walk was called once', () => {
  709. assert.strictEqual(log.counts.walk, 1)
  710. })
  711. test('EventEmitter.on was called eleven times', () => {
  712. assert.strictEqual(log.counts.on, 11)
  713. })
  714. suite('read events:', () => {
  715. setup(() => {
  716. log.args.DataStream[0][0]()
  717. // { "foo": "bar", "fo": "baz", "oo": "qux" }
  718. log.args.on[1][1]()
  719. log.args.on[2][1]('foo')
  720. log.args.on[5][1]('bar')
  721. log.args.on[2][1]('fo')
  722. log.args.on[5][1]('baz')
  723. log.args.on[2][1]('oo')
  724. log.args.on[5][1]('qux')
  725. log.args.on[4][1]()
  726. log.args.on[8][1]()
  727. })
  728. test('results.push was called three times', () => {
  729. assert.strictEqual(log.counts.push, 3)
  730. })
  731. test('results.push was called correctly first time', () => {
  732. assert.strictEqual(log.args.push[0][0], 'bar')
  733. })
  734. test('results.push was called correctly second time', () => {
  735. assert.strictEqual(log.args.push[1][0], 'qux')
  736. })
  737. test('results.push was called correctly third time', () => {
  738. assert.isNull(log.args.push[2][0])
  739. })
  740. test('results.emit was not called', () => {
  741. assert.strictEqual(log.counts.emit, 0)
  742. })
  743. })
  744. })
  745. suite('match with numbers=true:', () => {
  746. let stream, options, result
  747. setup(() => {
  748. result = match({}, '1', { numbers: true })
  749. })
  750. test('DataStream was called once', () => {
  751. assert.strictEqual(log.counts.DataStream, 1)
  752. })
  753. test('walk was called once', () => {
  754. assert.strictEqual(log.counts.walk, 1)
  755. })
  756. test('EventEmitter.on was called eleven times', () => {
  757. assert.strictEqual(log.counts.on, 11)
  758. })
  759. suite('read events:', () => {
  760. setup(() => {
  761. log.args.DataStream[0][0]()
  762. // { "0": "foo", "1": "bar", "2": [ "baz", "qux" ] }
  763. log.args.on[1][1]()
  764. log.args.on[2][1]('0')
  765. log.args.on[5][1]('foo')
  766. log.args.on[2][1]('1')
  767. log.args.on[5][1]('bar')
  768. log.args.on[2][1]('2')
  769. log.args.on[0][1]()
  770. log.args.on[5][1]('baz')
  771. log.args.on[5][1]('qux')
  772. log.args.on[3][1]()
  773. log.args.on[4][1]()
  774. log.args.on[8][1]()
  775. })
  776. test('results.push was called three times', () => {
  777. assert.strictEqual(log.counts.push, 3)
  778. })
  779. test('results.push was called correctly first time', () => {
  780. assert.strictEqual(log.args.push[0][0], 'bar')
  781. })
  782. test('results.push was called correctly second time', () => {
  783. assert.strictEqual(log.args.push[1][0], 'qux')
  784. })
  785. test('results.push was called correctly third time', () => {
  786. assert.isNull(log.args.push[2][0])
  787. })
  788. test('results.emit was not called', () => {
  789. assert.strictEqual(log.counts.emit, 0)
  790. })
  791. })
  792. })
  793. suite('match with bufferLength=3:', () => {
  794. let stream, options, result
  795. setup(() => {
  796. result = match({}, 'foo', { bufferLength: 3 })
  797. })
  798. test('DataStream was called once', () => {
  799. assert.strictEqual(log.counts.DataStream, 1)
  800. })
  801. test('walk was called once', () => {
  802. assert.strictEqual(log.counts.walk, 1)
  803. })
  804. test('EventEmitter.on was called eleven times', () => {
  805. assert.strictEqual(log.counts.on, 11)
  806. })
  807. suite('two matching events:', () => {
  808. setup(() => {
  809. log.args.on[1][1]()
  810. log.args.on[2][1]('foo')
  811. log.args.on[5][1]('bar')
  812. log.args.on[2][1]('baz')
  813. log.args.on[5][1]('qux')
  814. log.args.on[2][1]('foo')
  815. log.args.on[5][1]('wibble')
  816. log.args.on[2][1]('foo')
  817. })
  818. test('EventEmitter.pause was not called', () => {
  819. assert.strictEqual(log.counts.pause, 0)
  820. })
  821. suite('matching event:', () => {
  822. setup(() => {
  823. log.args.on[5][1]('blee')
  824. })
  825. test('results.push was not called', () => {
  826. assert.strictEqual(log.counts.push, 0)
  827. })
  828. test('EventEmitter.pause was called once', () => {
  829. assert.strictEqual(log.counts.pause, 1)
  830. })
  831. test('resume was not called', () => {
  832. assert.strictEqual(log.counts.resume, 0)
  833. })
  834. suite('read:', () => {
  835. setup(() => {
  836. log.args.DataStream[0][0]()
  837. })
  838. test('resume was called once', () => {
  839. assert.strictEqual(log.counts.resume, 1)
  840. })
  841. test('results.push was called three times', () => {
  842. assert.strictEqual(log.counts.push, 3)
  843. })
  844. test('results.push was called correctly first time', () => {
  845. assert.strictEqual(log.args.push[0][0], 'bar')
  846. })
  847. test('results.push was called correctly second time', () => {
  848. assert.strictEqual(log.args.push[1][0], 'wibble')
  849. })
  850. test('results.push was called correctly third time', () => {
  851. assert.strictEqual(log.args.push[2][0], 'blee')
  852. })
  853. test('results.emit was not called', () => {
  854. assert.strictEqual(log.counts.emit, 0)
  855. })
  856. })
  857. })
  858. })
  859. })
  860. suite('match with minDepth=1:', () => {
  861. let stream, predicate, options, result
  862. setup(() => {
  863. predicate = spooks.fn({ name: 'predicate', log, results: [ true ] })
  864. result = match({}, predicate, { minDepth: 1 })
  865. })
  866. test('DataStream was called once', () => {
  867. assert.strictEqual(log.counts.DataStream, 1)
  868. })
  869. test('walk was called once', () => {
  870. assert.strictEqual(log.counts.walk, 1)
  871. })
  872. test('EventEmitter.on was called eleven times', () => {
  873. assert.strictEqual(log.counts.on, 11)
  874. })
  875. suite('read events:', () => {
  876. setup(() => {
  877. log.args.DataStream[0][0]()
  878. // { "foo": { "bar": { "baz": "qux" } } }
  879. log.args.on[1][1]()
  880. log.args.on[2][1]('foo')
  881. log.args.on[1][1]()
  882. log.args.on[2][1]('bar')
  883. log.args.on[1][1]()
  884. log.args.on[2][1]('baz')
  885. log.args.on[5][1]('qux')
  886. log.args.on[4][1]()
  887. log.args.on[4][1]()
  888. log.args.on[4][1]()
  889. log.args.on[8][1]()
  890. })
  891. test('results.push was called four times', () => {
  892. assert.strictEqual(log.counts.push, 4)
  893. })
  894. test('results.push was called correctly first time', () => {
  895. const args = log.args.push[0]
  896. assert.lengthOf(args, 1)
  897. assert.equal(args[0], 'qux')
  898. })
  899. test('results.push was called correctly second time', () => {
  900. const args = log.args.push[1]
  901. assert.lengthOf(args, 1)
  902. assert.deepEqual(args[0], { baz: 'qux' })
  903. })
  904. test('results.push was called correctly third time', () => {
  905. const args = log.args.push[2]
  906. assert.lengthOf(args, 1)
  907. assert.deepEqual(args[0], { bar: { baz: 'qux' } })
  908. })
  909. test('results.push was called correctly fourth time', () => {
  910. const args = log.args.push[3]
  911. assert.lengthOf(args, 1)
  912. assert.isNull(args[0])
  913. })
  914. test('results.emit was not called', () => {
  915. assert.strictEqual(log.counts.emit, 0)
  916. })
  917. })
  918. })
  919. suite('match with minDepth=2:', () => {
  920. let stream, predicate, options, result
  921. setup(() => {
  922. predicate = spooks.fn({ name: 'predicate', log, results: [ true ] })
  923. result = match({}, predicate, { minDepth: 2 })
  924. })
  925. test('DataStream was called once', () => {
  926. assert.strictEqual(log.counts.DataStream, 1)
  927. })
  928. test('walk was called once', () => {
  929. assert.strictEqual(log.counts.walk, 1)
  930. })
  931. test('EventEmitter.on was called eleven times', () => {
  932. assert.strictEqual(log.counts.on, 11)
  933. })
  934. suite('read events:', () => {
  935. setup(() => {
  936. log.args.DataStream[0][0]()
  937. // { "foo": { "bar": { "baz": "qux" } } }
  938. log.args.on[1][1]()
  939. log.args.on[2][1]('foo')
  940. log.args.on[1][1]()
  941. log.args.on[2][1]('bar')
  942. log.args.on[1][1]()
  943. log.args.on[2][1]('baz')
  944. log.args.on[5][1]('qux')
  945. log.args.on[4][1]()
  946. log.args.on[4][1]()
  947. log.args.on[4][1]()
  948. log.args.on[8][1]()
  949. })
  950. test('results.push was called three times', () => {
  951. assert.strictEqual(log.counts.push, 3)
  952. })
  953. test('results.push was called correctly first time', () => {
  954. const args = log.args.push[0]
  955. assert.lengthOf(args, 1)
  956. assert.equal(args[0], 'qux')
  957. })
  958. test('results.push was called correctly second time', () => {
  959. const args = log.args.push[1]
  960. assert.lengthOf(args, 1)
  961. assert.deepEqual(args[0], { baz: 'qux' })
  962. })
  963. test('results.push was called correctly third time', () => {
  964. const args = log.args.push[2]
  965. assert.lengthOf(args, 1)
  966. assert.isNull(args[0])
  967. })
  968. test('results.emit was not called', () => {
  969. assert.strictEqual(log.counts.emit, 0)
  970. })
  971. })
  972. })
  973. })
  974. })