MultiplyExpression_test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. MultiplyExpression = require("../../../../lib/pipeline/expressions/MultiplyExpression"),
  5. VariablesParseState = require("../../../../lib/pipeline/expressions/VariablesParseState"),
  6. VariablesIdGenerator = require("../../../../lib/pipeline/expressions/VariablesIdGenerator"),
  7. FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression"),
  8. ConstantExpression = require("../../../../lib/pipeline/expressions/ConstantExpression"),
  9. Expression = require("../../../../lib/pipeline/expressions/Expression");
  10. exports.MultiplyExpression = {
  11. beforeEach: function(){
  12. this.vps = new VariablesParseState(new VariablesIdGenerator());
  13. },
  14. "constructor()": {
  15. "should not throw Error when constructing without args": function() {
  16. assert.doesNotThrow(function(){
  17. new MultiplyExpression();
  18. });
  19. },
  20. "should throw Error when constructing with args": function() {
  21. assert.throws(function(){
  22. new MultiplyExpression(1);
  23. });
  24. },
  25. },
  26. "#getOpName()": {
  27. "should return the correct op name; $multiply": function() {
  28. assert.equal(new MultiplyExpression().getOpName(), "$multiply");
  29. },
  30. },
  31. "#evaluate()": {
  32. "should multiply constants": function () {
  33. assert.strictEqual(Expression.parseOperand({$multiply: [2, 3, 4]}, this.vps).evaluate(), 2 * 3 * 4);
  34. },
  35. "should 'splode if an operand is a string": function () {
  36. assert.throws(function () {
  37. Expression.parseOperand({$multiply: [2, "x", 4]}, this.vps).evaluate();
  38. });
  39. },
  40. "should 'splode if an operand is a boolean": function () {
  41. assert.throws(function () {
  42. Expression.parseOperand({$multiply: [2, "x", 4]}, this.vps).evaluate();
  43. });
  44. },
  45. "should 'splode if an operand is a date": function () {
  46. assert.throws(function () {
  47. Expression.parseOperand({$multiply: [2, "x", 4]}, this.vps).evaluate();
  48. });
  49. },
  50. "should handle a null operand": function(){
  51. assert.strictEqual(Expression.parseOperand({$multiply: [2, null]}, this.vps).evaluate(), null);
  52. },
  53. "should handle an undefined operand": function(){
  54. assert.strictEqual(Expression.parseOperand({$multiply: [2, undefined]}, this.vps).evaluate(), null);
  55. },
  56. "should multiply mixed numbers": function () {
  57. assert.strictEqual(Expression.parseOperand({$multiply: [2.1, 3, 4.4]}, this.vps).evaluate(), 2.1 * 3 * 4.4);
  58. },
  59. "should return result of multiplying simple variables": function () {
  60. assert.equal(Expression.parseOperand({$multiply: ["$a", "$b"]}, this.vps).evaluate({a: 1, b: 2}), 1 * 2);
  61. },
  62. "should return result of multiplying large variables": function () {
  63. var spec = {$multiply: ["$a", "$b", "$c"]},
  64. doc = {a: 1.345, b: 2e45, c: 0},
  65. expr = Expression.parseOperand(spec, this.vps);
  66. assert.strictEqual(expr.evaluate(doc), 1.345 * 2e45 * 0);
  67. },
  68. "should return result of multiplying one number": function () {
  69. assert.strictEqual(Expression.parseOperand({$multiply: ["$a"]}, this.vps).evaluate({a: 1}), 1);
  70. },
  71. "should throw an exception if the result is not a number": function () {
  72. assert.throws(function() {
  73. Expression.parseOperand({$multiply: ["$a", "$b"]}, this.vps).evaluate({a: 1e199, b: 1e199});
  74. });
  75. },
  76. },
  77. "optimize": {
  78. "should optimize out constants separated by a variable": function () {
  79. var a = Expression.parseOperand({$multiply: [2, 3, 4, 5, "$a", 6, 7, 8]}, this.vps).optimize();
  80. assert(a instanceof MultiplyExpression);
  81. assert.equal(a.operands.length, 2);
  82. assert(a.operands[0] instanceof FieldPathExpression);
  83. assert(a.operands[1] instanceof ConstantExpression);
  84. assert.equal(a.operands[1].evaluateInternal(), 2*3*4*5*6*7*8);
  85. },
  86. },
  87. };