match.js 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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. log.args.on[0][1]()
  633. log.args.on[1][1]()
  634. log.args.on[2][1]('foo')
  635. log.args.on[5][1]('bar')
  636. log.args.on[4][1]()
  637. log.args.on[5][1]('baz')
  638. log.args.on[6][1](1)
  639. log.args.on[7][1](true)
  640. log.args.on[3][1]()
  641. log.args.on[8][1]()
  642. })
  643. test('results.push was called once', () => {
  644. assert.strictEqual(log.counts.push, 1)
  645. })
  646. test('results.push was called correctly', () => {
  647. assert.isNull(log.args.push[0][0])
  648. })
  649. test('results.emit was not called', () => {
  650. assert.strictEqual(log.counts.emit, 0)
  651. })
  652. })
  653. })
  654. suite('match with string:', () => {
  655. let stream, options, result
  656. setup(() => {
  657. result = match({}, 'foo', {})
  658. })
  659. test('DataStream was called once', () => {
  660. assert.strictEqual(log.counts.DataStream, 1)
  661. })
  662. test('walk was called once', () => {
  663. assert.strictEqual(log.counts.walk, 1)
  664. })
  665. test('EventEmitter.on was called eleven times', () => {
  666. assert.strictEqual(log.counts.on, 11)
  667. })
  668. suite('read events:', () => {
  669. setup(() => {
  670. log.args.DataStream[0][0]()
  671. log.args.on[1][1]()
  672. log.args.on[2][1]('foo')
  673. log.args.on[5][1]('bar')
  674. log.args.on[2][1]('baz')
  675. log.args.on[5][1]('qux')
  676. log.args.on[2][1]('foo')
  677. log.args.on[5][1]('wibble')
  678. log.args.on[4][1]()
  679. log.args.on[8][1]()
  680. })
  681. test('results.push was called three times', () => {
  682. assert.strictEqual(log.counts.push, 3)
  683. })
  684. test('results.push was called correctly first time', () => {
  685. assert.strictEqual(log.args.push[0][0], 'bar')
  686. })
  687. test('results.push was called correctly second time', () => {
  688. assert.strictEqual(log.args.push[1][0], 'wibble')
  689. })
  690. test('results.push was called correctly third time', () => {
  691. assert.isNull(log.args.push[2][0])
  692. })
  693. test('results.emit was not called', () => {
  694. assert.strictEqual(log.counts.emit, 0)
  695. })
  696. })
  697. })
  698. suite('match with regular expression:', () => {
  699. let stream, options, result
  700. setup(() => {
  701. result = match({}, /oo/, {})
  702. })
  703. test('DataStream was called once', () => {
  704. assert.strictEqual(log.counts.DataStream, 1)
  705. })
  706. test('walk was called once', () => {
  707. assert.strictEqual(log.counts.walk, 1)
  708. })
  709. test('EventEmitter.on was called eleven times', () => {
  710. assert.strictEqual(log.counts.on, 11)
  711. })
  712. suite('read events:', () => {
  713. setup(() => {
  714. log.args.DataStream[0][0]()
  715. log.args.on[1][1]()
  716. log.args.on[2][1]('foo')
  717. log.args.on[5][1]('bar')
  718. log.args.on[2][1]('fo')
  719. log.args.on[5][1]('baz')
  720. log.args.on[2][1]('oo')
  721. log.args.on[5][1]('qux')
  722. log.args.on[4][1]()
  723. log.args.on[8][1]()
  724. })
  725. test('results.push was called three times', () => {
  726. assert.strictEqual(log.counts.push, 3)
  727. })
  728. test('results.push was called correctly first time', () => {
  729. assert.strictEqual(log.args.push[0][0], 'bar')
  730. })
  731. test('results.push was called correctly second time', () => {
  732. assert.strictEqual(log.args.push[1][0], 'qux')
  733. })
  734. test('results.push was called correctly third time', () => {
  735. assert.isNull(log.args.push[2][0])
  736. })
  737. test('results.emit was not called', () => {
  738. assert.strictEqual(log.counts.emit, 0)
  739. })
  740. })
  741. })
  742. suite('match with numbers=true:', () => {
  743. let stream, options, result
  744. setup(() => {
  745. result = match({}, '1', { numbers: true })
  746. })
  747. test('DataStream was called once', () => {
  748. assert.strictEqual(log.counts.DataStream, 1)
  749. })
  750. test('walk was called once', () => {
  751. assert.strictEqual(log.counts.walk, 1)
  752. })
  753. test('EventEmitter.on was called eleven times', () => {
  754. assert.strictEqual(log.counts.on, 11)
  755. })
  756. suite('read events:', () => {
  757. setup(() => {
  758. log.args.DataStream[0][0]()
  759. log.args.on[1][1]()
  760. log.args.on[2][1]('0')
  761. log.args.on[5][1]('foo')
  762. log.args.on[2][1]('1')
  763. log.args.on[5][1]('bar')
  764. log.args.on[2][1]('2')
  765. log.args.on[0][1]()
  766. log.args.on[5][1]('baz')
  767. log.args.on[5][1]('qux')
  768. log.args.on[3][1]()
  769. log.args.on[4][1]()
  770. log.args.on[8][1]()
  771. })
  772. test('results.push was called three times', () => {
  773. assert.strictEqual(log.counts.push, 3)
  774. })
  775. test('results.push was called correctly first time', () => {
  776. assert.strictEqual(log.args.push[0][0], 'bar')
  777. })
  778. test('results.push was called correctly second time', () => {
  779. assert.strictEqual(log.args.push[1][0], 'qux')
  780. })
  781. test('results.push was called correctly third time', () => {
  782. assert.isNull(log.args.push[2][0])
  783. })
  784. test('results.emit was not called', () => {
  785. assert.strictEqual(log.counts.emit, 0)
  786. })
  787. })
  788. })
  789. suite('match with bufferLength=3:', () => {
  790. let stream, options, result
  791. setup(() => {
  792. result = match({}, 'foo', { bufferLength: 3 })
  793. })
  794. test('DataStream was called once', () => {
  795. assert.strictEqual(log.counts.DataStream, 1)
  796. })
  797. test('walk was called once', () => {
  798. assert.strictEqual(log.counts.walk, 1)
  799. })
  800. test('EventEmitter.on was called eleven times', () => {
  801. assert.strictEqual(log.counts.on, 11)
  802. })
  803. suite('two matching events:', () => {
  804. setup(() => {
  805. log.args.on[1][1]()
  806. log.args.on[2][1]('foo')
  807. log.args.on[5][1]('bar')
  808. log.args.on[2][1]('baz')
  809. log.args.on[5][1]('qux')
  810. log.args.on[2][1]('foo')
  811. log.args.on[5][1]('wibble')
  812. log.args.on[2][1]('foo')
  813. })
  814. test('EventEmitter.pause was not called', () => {
  815. assert.strictEqual(log.counts.pause, 0)
  816. })
  817. suite('matching event:', () => {
  818. setup(() => {
  819. log.args.on[5][1]('blee')
  820. })
  821. test('results.push was not called', () => {
  822. assert.strictEqual(log.counts.push, 0)
  823. })
  824. test('EventEmitter.pause was called once', () => {
  825. assert.strictEqual(log.counts.pause, 1)
  826. })
  827. test('resume was not called', () => {
  828. assert.strictEqual(log.counts.resume, 0)
  829. })
  830. suite('read:', () => {
  831. setup(() => {
  832. log.args.DataStream[0][0]()
  833. })
  834. test('resume was called once', () => {
  835. assert.strictEqual(log.counts.resume, 1)
  836. })
  837. test('results.push was called three times', () => {
  838. assert.strictEqual(log.counts.push, 3)
  839. })
  840. test('results.push was called correctly first time', () => {
  841. assert.strictEqual(log.args.push[0][0], 'bar')
  842. })
  843. test('results.push was called correctly second time', () => {
  844. assert.strictEqual(log.args.push[1][0], 'wibble')
  845. })
  846. test('results.push was called correctly third time', () => {
  847. assert.strictEqual(log.args.push[2][0], 'blee')
  848. })
  849. test('results.emit was not called', () => {
  850. assert.strictEqual(log.counts.emit, 0)
  851. })
  852. })
  853. })
  854. })
  855. })
  856. })
  857. })