pipeline_test.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. module('lunr.Pipeline', {
  2. setup: function () {
  3. this.existingRegisteredFunctions = lunr.Pipeline.registeredFunctions
  4. lunr.Pipeline.registeredFunctions = {}
  5. this.existingWarnIfFunctionNotRegistered = lunr.Pipeline.warnIfFunctionNotRegistered
  6. lunr.Pipeline.warnIfFunctionNotRegistered = $.noop
  7. },
  8. teardown: function () {
  9. lunr.Pipeline.registeredFunctions = this.existingRegisteredFunctions
  10. lunr.Pipeline.warnIfFunctionNotRegistered = this.existingWarnIfFunctionNotRegistered
  11. }
  12. })
  13. test("adding a new item to the pipeline", function () {
  14. var pipeline = new lunr.Pipeline
  15. equal(pipeline._stack.length, 0)
  16. pipeline.add($.noop)
  17. equal(pipeline._stack.length, 1)
  18. })
  19. test("adding multiple items to the pipeline in one go", function () {
  20. var pipeline = new lunr.Pipeline
  21. pipeline.add($.noop, $.noop)
  22. equal(pipeline._stack.length, 2)
  23. })
  24. test("removing an item from the pipeline", function () {
  25. var pipeline = new lunr.Pipeline,
  26. fn = $.noop
  27. pipeline.add(fn)
  28. equal(pipeline._stack.length, 1)
  29. pipeline.remove(fn)
  30. equal(pipeline._stack.length, 0)
  31. })
  32. test("removing a nonexistent item from the pipeline", function () {
  33. var pipeline = new lunr.Pipeline,
  34. fn1 = $.noop,
  35. fn2 = function () {}
  36. pipeline.add(fn1)
  37. equal(pipeline._stack.length, 1)
  38. pipeline.remove(fn2)
  39. equal(pipeline._stack.length, 1)
  40. })
  41. test("adding an item to the pipeline before another item", function () {
  42. var pipeline = new lunr.Pipeline,
  43. fn1 = $.noop,
  44. fn2 = function () {}
  45. pipeline.add(fn1)
  46. pipeline.before(fn1, fn2)
  47. deepEqual(pipeline._stack, [fn2, fn1])
  48. })
  49. test("adding an item to the pipeline before nonexistent item", function () {
  50. var pipeline = new lunr.Pipeline,
  51. fn1 = $.noop,
  52. fn2 = function () {},
  53. fn3 = function () {}
  54. pipeline.add(fn1, fn2)
  55. throws(function () {
  56. pipeline.before(fn3, fn1)
  57. })
  58. deepEqual(pipeline._stack, [fn1, fn2])
  59. })
  60. test("adding an item to the pipeline after another item", function () {
  61. var pipeline = new lunr.Pipeline,
  62. fn1 = $.noop,
  63. fn2 = function () {},
  64. fn3 = function () {}
  65. pipeline.add(fn1, fn2)
  66. pipeline.after(fn1, fn3)
  67. deepEqual(pipeline._stack, [fn1, fn3, fn2])
  68. })
  69. test("adding an item to the pipeline after nonexistent item", function () {
  70. var pipeline = new lunr.Pipeline,
  71. fn1 = $.noop,
  72. fn2 = function () {},
  73. fn3 = function () {}
  74. pipeline.add(fn1, fn2)
  75. throws(function () {
  76. pipeline.after(fn3, fn1)
  77. })
  78. deepEqual(pipeline._stack, [fn1, fn2])
  79. })
  80. test("run calls each member of the pipeline for each input", function () {
  81. var pipeline = new lunr.Pipeline,
  82. count1 = 0, count2 = 0,
  83. fn1 = function (token) { count1++ ; return token },
  84. fn2 = function (token) { count2++ ; return token }
  85. pipeline.add(fn1, fn2)
  86. pipeline.run([1,2,3])
  87. equal(count1, 3)
  88. equal(count2, 3)
  89. })
  90. test("run should pass three inputs to the pipeline fn", function () {
  91. var pipeline = new lunr.Pipeline,
  92. input, index, arr,
  93. fn1 = function () { input = arguments[0], index = arguments[1], arr = arguments[2] }
  94. pipeline.add(fn1)
  95. pipeline.run(['a'])
  96. equal(input, 'a')
  97. equal(index, 0)
  98. deepEqual(arr, ['a'])
  99. })
  100. test("run should pass the output of one into the input of the next", function () {
  101. var pipeline = new lunr.Pipeline,
  102. output,
  103. fn1 = function (t1) { return t1.toUpperCase() },
  104. fn2 = function (t2) { output = t2 }
  105. pipeline.add(fn1)
  106. pipeline.add(fn2)
  107. pipeline.run(['a'])
  108. equal(output, 'A')
  109. })
  110. test("run should return the result of running the entire pipeline on each element", function () {
  111. var pipeline = new lunr.Pipeline,
  112. fn1 = function (t1) { return t1.toUpperCase() }
  113. pipeline.add(fn1)
  114. deepEqual(pipeline.run(['a']), ['A'])
  115. })
  116. test("run should filter out any undefined values at each stage in the pipeline", function () {
  117. var pipeline = new lunr.Pipeline,
  118. fn2Count = 0,
  119. fn1 = function (t) { if (t < 5) return t },
  120. fn2 = function (t) { fn2Count++ ; return t }
  121. pipeline.add(fn1, fn2)
  122. var output = pipeline.run([0,1,2,3,4,5,6,7,8,9])
  123. equal(fn2Count, 5)
  124. equal(output.length, 5)
  125. })
  126. test("run should filter out any empty string values at each stage in the pipeline", function () {
  127. var pipeline = new lunr.Pipeline,
  128. fn2Count = 0,
  129. fn1 = function (t) {
  130. if (t === "foo") {
  131. return ""
  132. } else {
  133. return t
  134. }
  135. },
  136. fn2 = function (t) { fn2Count++ ; return t }
  137. pipeline.add(fn1, fn2)
  138. var output = pipeline.run(["foo", "bar", "baz", ""])
  139. equal(fn2Count, 2)
  140. equal(output.length, 2)
  141. deepEqual(output, ["bar", "baz"])
  142. })
  143. test('toJSON', function () {
  144. var pipeline = new lunr.Pipeline,
  145. fn1 = function () {},
  146. fn2 = function () {}
  147. lunr.Pipeline.registerFunction(fn1, 'fn1')
  148. lunr.Pipeline.registerFunction(fn2, 'fn2')
  149. pipeline.add(fn1, fn2)
  150. deepEqual(pipeline.toJSON(), ['fn1', 'fn2'])
  151. })
  152. test('registering a pipeline function', function () {
  153. var fn1 = function () {}
  154. equal(Object.keys(lunr.Pipeline.registeredFunctions).length, 0)
  155. lunr.Pipeline.registerFunction(fn1, 'fn1')
  156. equal(fn1.label, 'fn1')
  157. equal(Object.keys(lunr.Pipeline.registeredFunctions).length, 1)
  158. deepEqual(lunr.Pipeline.registeredFunctions['fn1'], fn1)
  159. })
  160. test('load', function () {
  161. var fn1 = function () {},
  162. fn2 = function () {}
  163. lunr.Pipeline.registerFunction(fn1, 'fn1')
  164. lunr.Pipeline.registerFunction(fn2, 'fn2')
  165. var serialised = ['fn1', 'fn2']
  166. var pipeline = lunr.Pipeline.load(serialised)
  167. equal(pipeline._stack.length, 2)
  168. deepEqual(pipeline._stack[0], fn1)
  169. deepEqual(pipeline._stack[1], fn2)
  170. })
  171. test('loading an un-registered pipeline function', function () {
  172. var serialised = ['fn1']
  173. throws(function () {
  174. lunr.Pipeline.load(serialised)
  175. })
  176. })
  177. test('resetting the pipeline', function () {
  178. var fn1 = function () {},
  179. fn2 = function () {},
  180. pipeline = new lunr.Pipeline
  181. pipeline.add(fn1, fn2)
  182. deepEqual(pipeline._stack, [fn1, fn2])
  183. pipeline.reset()
  184. deepEqual(pipeline._stack, [])
  185. })