ModExpression_test.js 2.5 KB

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