ModExpression.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "use strict";
  2. var assert = require("assert"),
  3. ModExpression = require("../../../../lib/pipeline/expressions/ModExpression"),
  4. VariablesIdGenerator = require("../../../../lib/pipeline/expressions/VariablesIdGenerator"),
  5. VariablesParseState = require("../../../../lib/pipeline/expressions/VariablesParseState"),
  6. Expression = require("../../../../lib/pipeline/expressions/Expression");
  7. // Mocha one-liner to make these tests self-hosted
  8. if(!module.parent)return(require.cache[__filename]=null,(new(require("mocha"))({ui:"exports",reporter:"spec",grep:process.env.TEST_GREP})).addFile(__filename).run(process.exit));
  9. exports.ModExpression = {
  10. "constructor()": {
  11. "should construct instance": function() {
  12. assert(new ModExpression() instanceof ModExpression);
  13. assert(new ModExpression() instanceof Expression);
  14. },
  15. "should error if given args": function() {
  16. assert.throws(function() {
  17. new ModExpression("bad stuff");
  18. });
  19. },
  20. },
  21. "#getOpName()": {
  22. "should return the correct op name; $mod": function() {
  23. assert.equal(new ModExpression().getOpName(), "$mod");
  24. },
  25. },
  26. "#evaluate()": {
  27. "should return modulus of two numbers": function() {
  28. var idGenerator = new VariablesIdGenerator(),
  29. vps = new VariablesParseState(idGenerator),
  30. expr = Expression.parseOperand({$mod: ["$a", "$b"]}, vps),
  31. input = {a: 6, b: 2};
  32. assert.strictEqual(expr.evaluate(input), 0);
  33. },
  34. "should return null if first is null": function() {
  35. var idGenerator = new VariablesIdGenerator(),
  36. vps = new VariablesParseState(idGenerator),
  37. expr = Expression.parseOperand({$mod: ["$a", "$b"]}, vps),
  38. input = {a: null, b: 2};
  39. assert.strictEqual(expr.evaluate(input), null);
  40. },
  41. "should return null if first is undefined": function() {
  42. var idGenerator = new VariablesIdGenerator(),
  43. vps = new VariablesParseState(idGenerator),
  44. expr = Expression.parseOperand({$mod: ["$a", "$b"]}, vps),
  45. input = {a: undefined, b: 2};
  46. assert.strictEqual(expr.evaluate(input), null);
  47. },
  48. "should return null if second is null": function() {
  49. var idGenerator = new VariablesIdGenerator(),
  50. vps = new VariablesParseState(idGenerator),
  51. expr = Expression.parseOperand({$mod: ["$a", "$b"]}, vps),
  52. input = {a: 11, b: null};
  53. assert.strictEqual(expr.evaluate(input), null);
  54. },
  55. "should return null if second is undefined": function() {
  56. var idGenerator = new VariablesIdGenerator(),
  57. vps = new VariablesParseState(idGenerator),
  58. expr = Expression.parseOperand({$mod: ["$a", "$b"]}, vps),
  59. input = {a: 42, b: undefined};
  60. assert.strictEqual(expr.evaluate(input), null);
  61. },
  62. },
  63. };