IndexOfExpression.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";
  2. var assert = require("assert"),
  3. IndexOfExpression = require("../../../../lib/pipeline/expressions/IndexOfExpression"),
  4. Expression = require("../../../../lib/pipeline/expressions/Expression");
  5. module.exports = {
  6. "IndexOfExpression": {
  7. "constructor()": {
  8. "should not throw Error when constructing without args": function testConstructor(){
  9. assert.doesNotThrow(function(){
  10. new IndexOfExpression();
  11. });
  12. }
  13. },
  14. "#getOpName()": {
  15. "should return the correct op name; $indexOf": function testOpName(){
  16. assert.equal(new IndexOfExpression().getOpName(), "$indexOf");
  17. }
  18. },
  19. "#getFactory()": {
  20. "should return the constructor for this class": function factoryIsConstructor(){
  21. assert.strictEqual(new IndexOfExpression().getFactory(), undefined);
  22. }
  23. },
  24. "#evaluate()": {
  25. "should return the index of the first match if found": function testFound(){
  26. assert.strictEqual(Expression.parseOperand({$indexOf:["$needle", "$haystack"]}).evaluate({needle:1, haystack:[1,2,3]}), 0);
  27. assert.strictEqual(Expression.parseOperand({$indexOf:["$needle", "$haystack"]}).evaluate({needle:3, haystack:[1,2,3]}), 2);
  28. },
  29. "should return `-1` if not found": function testNotFound(){
  30. assert.strictEqual(Expression.parseOperand({$indexOf:["$needle", "$haystack"]}).evaluate({needle:0, haystack:[1,2,3]}), -1);
  31. },
  32. "should return `undefined` if the 1st arg (the `needle`) is undefined or missing": function testNeedleUndefinedOrMissing(){
  33. assert.strictEqual(Expression.parseOperand({$indexOf:["$needle", "$haystack"]}).evaluate({needle:undefined, haystack:[1,2,3]}), undefined);
  34. assert.strictEqual(Expression.parseOperand({$indexOf:["$needle", "$haystack"]}).evaluate({haystack:[1,2,3]}), undefined);
  35. },
  36. "should return `undefined` if the 1st arg (the `haystack`) is undefined or missing": function testHaystackUndefinedOrMissing(){
  37. assert.strictEqual(Expression.parseOperand({$indexOf:["$needle", "$haystack"]}).evaluate({needle:0, haystack:undefined}), undefined);
  38. assert.strictEqual(Expression.parseOperand({$indexOf:["$needle", "$haystack"]}).evaluate({needle:0}), undefined);
  39. },
  40. "should throw if 2nd arg (the `haystack`) is not an `Array`": function testHaystackNonArray(){
  41. assert.throws(function(){
  42. Expression.parseOperand({$indexOf:["$needle", "$haystack"]}).evaluate({needle:0, haystack:0});
  43. });
  44. }
  45. }
  46. }
  47. };
  48. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);