pipeline_test.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. suite('lunr.Pipeline', function () {
  2. var noop = function () {}
  3. setup(function () {
  4. this.existingRegisteredFunctions = lunr.Pipeline.registeredFunctions
  5. this.existingWarnIfFunctionNotRegistered = lunr.Pipeline.warnIfFunctionNotRegistered
  6. lunr.Pipeline.registeredFunctions = {}
  7. lunr.Pipeline.warnIfFunctionNotRegistered = noop
  8. this.pipeline = new lunr.Pipeline
  9. })
  10. teardown(function () {
  11. lunr.Pipeline.registeredFunctions = this.existingRegisteredFunctions
  12. lunr.Pipeline.warnIfFunctionNotRegistered = this.existingWarnIfFunctionNotRegistered
  13. })
  14. suite('#add', function () {
  15. test('add function to pipeline', function () {
  16. this.pipeline.add(noop)
  17. assert.equal(1, this.pipeline._stack.length)
  18. })
  19. test('add multiple functions to the pipeline', function () {
  20. this.pipeline.add(noop, noop)
  21. assert.equal(2, this.pipeline._stack.length)
  22. })
  23. })
  24. suite('#remove', function () {
  25. test('function exists in pipeline', function () {
  26. this.pipeline.add(noop)
  27. assert.equal(1, this.pipeline._stack.length)
  28. this.pipeline.remove(noop)
  29. assert.equal(0, this.pipeline._stack.length)
  30. })
  31. test('function does not exist in pipeline', function () {
  32. var fn = function () {}
  33. this.pipeline.add(noop)
  34. assert.equal(1, this.pipeline._stack.length)
  35. this.pipeline.remove(fn)
  36. assert.equal(1, this.pipeline._stack.length)
  37. })
  38. })
  39. suite('#before', function () {
  40. var fn = function () {}
  41. test('other function exists', function () {
  42. this.pipeline.add(noop)
  43. this.pipeline.before(noop, fn)
  44. assert.deepEqual([fn, noop], this.pipeline._stack)
  45. })
  46. test('other function does not exist', function () {
  47. var action = function () {
  48. this.pipeline.before(noop, fn)
  49. }
  50. assert.throws(action.bind(this))
  51. assert.equal(0, this.pipeline._stack.length)
  52. })
  53. })
  54. suite('#after', function () {
  55. var fn = function () {}
  56. test('other function exists', function () {
  57. this.pipeline.add(noop)
  58. this.pipeline.after(noop, fn)
  59. assert.deepEqual([noop, fn], this.pipeline._stack)
  60. })
  61. test('other function does not exist', function () {
  62. var action = function () {
  63. this.pipeline.after(noop, fn)
  64. }
  65. assert.throws(action.bind(this))
  66. assert.equal(0, this.pipeline._stack.length)
  67. })
  68. })
  69. suite('#run', function () {
  70. test('calling each function for each token', function () {
  71. var count1 = 0, count2 = 0,
  72. fn1 = function (t) { count1++; return t },
  73. fn2 = function (t) { count2++; return t }
  74. this.pipeline.add(fn1, fn2)
  75. this.pipeline.run([1,2,3])
  76. assert.equal(3, count1)
  77. assert.equal(3, count2)
  78. })
  79. test('passes token to pipeline function', function () {
  80. this.pipeline.add(function (token) {
  81. assert.equal('foo', token)
  82. })
  83. this.pipeline.run(['foo'])
  84. })
  85. test('passes index to pipeline function', function () {
  86. this.pipeline.add(function (_, index) {
  87. assert.equal(0, index)
  88. })
  89. this.pipeline.run(['foo'])
  90. })
  91. test('passes entire token array to pipeline function', function () {
  92. this.pipeline.add(function (_, _, tokens) {
  93. assert.deepEqual(['foo'], tokens)
  94. })
  95. this.pipeline.run(['foo'])
  96. })
  97. test('passes output of one function as input to the next', function () {
  98. this.pipeline.add(function (t) {
  99. return t.toUpperCase()
  100. })
  101. this.pipeline.add(function (t) {
  102. assert.equal('FOO', t)
  103. })
  104. this.pipeline.run(['foo'])
  105. })
  106. test('returns the results of the last function', function () {
  107. this.pipeline.add(function (t) {
  108. return t.toUpperCase()
  109. })
  110. assert.deepEqual(['FOO'], this.pipeline.run(['foo']))
  111. })
  112. test('filters out null, undefined and empty string values', function () {
  113. var tokens = [],
  114. output
  115. // only pass on tokens for even token indexes
  116. // return null for 'foo'
  117. // return undefined for 'bar'
  118. // return '' for 'baz'
  119. this.pipeline.add(function (t, i) {
  120. if (i == 4) {
  121. return null
  122. } else if (i == 5) {
  123. return ''
  124. } if (i % 2) {
  125. return t
  126. } else {
  127. return undefined
  128. }
  129. })
  130. this.pipeline.add(function (t) {
  131. tokens.push(t)
  132. return t
  133. })
  134. output = this.pipeline.run(['a', 'b', 'c', 'd', 'foo', 'bar', 'baz'])
  135. assert.sameMembers(['b', 'd'], tokens)
  136. assert.sameMembers(['b', 'd'], output)
  137. })
  138. suite('expanding tokens', function () {
  139. test('passed to output', function () {
  140. this.pipeline.add(function (t) {
  141. return [t, t.toUpperCase()]
  142. })
  143. assert.sameMembers(["foo", "FOO"], this.pipeline.run(['foo']))
  144. })
  145. test('not passed to same function', function () {
  146. var received = []
  147. this.pipeline.add(function (t) {
  148. received.push(t)
  149. return [t, t.toUpperCase()]
  150. })
  151. this.pipeline.run(['foo'])
  152. assert.sameMembers(['foo'], received)
  153. })
  154. test('passed to the next pipeline function', function () {
  155. var received = []
  156. this.pipeline.add(function (t) {
  157. return [t, t.toUpperCase()]
  158. })
  159. this.pipeline.add(function (t) {
  160. received.push(t)
  161. })
  162. this.pipeline.run(['foo'])
  163. assert.sameMembers(['foo', 'FOO'], received)
  164. })
  165. })
  166. })
  167. suite('#toJSON', function () {
  168. test('returns an array of registered function labels', function () {
  169. var fn = function () {}
  170. lunr.Pipeline.registerFunction(fn, 'fn')
  171. this.pipeline.add(fn)
  172. assert.sameMembers(['fn'], this.pipeline.toJSON())
  173. })
  174. })
  175. suite('.registerFunction', function () {
  176. setup(function () {
  177. this.fn = function () {}
  178. })
  179. test('adds a label property to the function', function () {
  180. lunr.Pipeline.registerFunction(this.fn, 'fn')
  181. assert.equal('fn', this.fn.label)
  182. })
  183. test('adds function to the list of registered functions', function () {
  184. lunr.Pipeline.registerFunction(this.fn, 'fn')
  185. assert.equal(this.fn, lunr.Pipeline.registeredFunctions['fn'])
  186. })
  187. })
  188. suite('.load', function () {
  189. test('with registered functions', function () {
  190. var fn = function () {},
  191. serializedPipeline = ['fn'],
  192. pipeline
  193. lunr.Pipeline.registerFunction(fn, 'fn')
  194. pipeline = lunr.Pipeline.load(serializedPipeline)
  195. assert.equal(1, pipeline._stack.length)
  196. assert.equal(fn, pipeline._stack[0])
  197. })
  198. test('with unregisterd functions', function () {
  199. var serializedPipeline = ['fn']
  200. assert.throws(function () {
  201. lunr.Pipeline.load(serializedPipeline)
  202. })
  203. })
  204. })
  205. suite('#reset', function () {
  206. test('empties the stack', function () {
  207. this.pipeline.add(function () {})
  208. assert.equal(1, this.pipeline._stack.length)
  209. this.pipeline.reset()
  210. assert.equal(0, this.pipeline._stack.length)
  211. })
  212. })
  213. })