MultiplyExpression_test.js 3.8 KB

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