AddExpression_test.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. "use strict";
  2. var assert = require("assert"),
  3. Expression = require("../../../../lib/pipeline/expressions/Expression"),
  4. AddExpression = require("../../../../lib/pipeline/expressions/AddExpression"),
  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. // 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. var TestBase = function TestBase(overrides) {
  12. //NOTE: DEVIATION FROM MONGO: using this base class to make things easier to initialize
  13. for (var key in overrides)
  14. this[key] = overrides[key];
  15. },
  16. ExpectedResultBase = (function() {
  17. var klass = function ExpectedResultBase() {
  18. base.apply(this, arguments);
  19. }, base = TestBase, proto = klass.prototype = Object.create(base.prototype);
  20. proto.run = function() {
  21. var expr = new AddExpression();
  22. this.populateOperands(expr);
  23. var expectedResult = this.expectedResult instanceof Function ? this.expectedResult() : this.expectedResult;
  24. if (expectedResult instanceof Date) //NOTE: DEVIATION FROM MONGO: special case for Date
  25. return assert.strictEqual(Date(expectedResult), Date(expr.evaluate({})));
  26. assert.strictEqual(expectedResult, expr.evaluate({}));
  27. };
  28. return klass;
  29. })(),
  30. SingleOperandBase = (function() {
  31. var klass = function SingleOperandBase() {
  32. base.apply(this, arguments);
  33. }, base = ExpectedResultBase, proto = klass.prototype = Object.create(base.prototype);
  34. proto.populateOperands = function(expr) {
  35. var operand = this.operand instanceof Function ? this.operand() : this.operand;
  36. expr.addOperand(ConstantExpression.create(operand));
  37. };
  38. proto.expectedResult = function() {
  39. var operand = this.operand instanceof Function ? this.operand() : this.operand;
  40. return operand;
  41. };
  42. return klass;
  43. })(),
  44. TwoOperandBase = (function() {
  45. var klass = function TwoOperandBase() {
  46. base.apply(this, arguments);
  47. }, base = ExpectedResultBase, proto = klass.prototype = Object.create(base.prototype);
  48. proto.run = function() {
  49. base.prototype.run.call(this);
  50. // Now add the operands in the reverse direction.
  51. this._reverse = true;
  52. base.prototype.run.call(this);
  53. };
  54. proto.populateOperands = function(expr) {
  55. var operand1 = this.operand1 instanceof Function ? this.operand1() : this.operand1,
  56. operand2 = this.operand1 instanceof Function ? this.operand2() : this.operand2;
  57. expr.addOperand(ConstantExpression.create(this._reverse ? operand2 : operand1));
  58. expr.addOperand(ConstantExpression.create(this._reverse ? operand1 : operand2));
  59. };
  60. proto._reverse = false;
  61. return klass;
  62. })();
  63. exports.AddExpression = {
  64. "constructor()": {
  65. "should construct instance": function() {
  66. assert(new AddExpression() instanceof AddExpression);
  67. assert(new AddExpression() instanceof Expression);
  68. },
  69. "should error if given args": function() {
  70. assert.throws(function() {
  71. new AddExpression("bad stuff");
  72. });
  73. },
  74. },
  75. "#getOpName()": {
  76. "should return the correct op name; $add": function() {
  77. assert.equal(new AddExpression().getOpName(), "$add");
  78. }
  79. },
  80. "#evaluate()": {
  81. "should return the operand if null document is given": function testNullDocument() {
  82. /** $add with a NULL Document pointer, as called by ExpressionNary::optimize(). */
  83. var expr = new AddExpression();
  84. expr.addOperand(ConstantExpression.create(2));
  85. assert.strictEqual(expr.evaluate({}), 2);
  86. },
  87. "should return 0 if no operands were given": function testNoOperands() {
  88. /** $add without operands. */
  89. var expr = new AddExpression();
  90. assert.strictEqual(expr.evaluate({}), 0);
  91. },
  92. "should throw Error if a String operand was given": function testString() {
  93. /** String type unsupported. */
  94. var expr = new AddExpression();
  95. expr.addOperand(ConstantExpression.create("a"));
  96. assert.throws(function () {
  97. expr.evaluate({});
  98. });
  99. },
  100. "should throw Error if a Boolean operand was given": function testBool() {
  101. var expr = new AddExpression();
  102. expr.addOperand(ConstantExpression.create(true));
  103. assert.throws(function () {
  104. expr.evaluate({});
  105. });
  106. },
  107. "w/ 1 operand": {
  108. "should pass through a single int": function testInt() {
  109. /** Single int argument. */
  110. new SingleOperandBase({
  111. operand: 1,
  112. }).run();
  113. },
  114. //SKIPPED: Long -- would be same as Int above
  115. "should pass through a single float": function testDouble() {
  116. /** Single double argument. */
  117. new SingleOperandBase({
  118. operand: 99.99,
  119. }).run();
  120. },
  121. "should pass through a single date": function testDate() {
  122. /** Single Date argument. */
  123. new SingleOperandBase({
  124. operand: new Date(12345),
  125. }).run();
  126. },
  127. "should pass through a single null": function testNull() {
  128. /** Single null argument. */
  129. new SingleOperandBase({
  130. operand: null,
  131. }).run();
  132. },
  133. "should pass through a single undefined": function testUndefined() {
  134. /** Single undefined argument. */
  135. new SingleOperandBase({
  136. operand: undefined,
  137. expectedResult: null,
  138. }).run();
  139. },
  140. },
  141. "w/ 2 operands": {
  142. "should add two ints": function testIntInt() {
  143. /** Add two ints. */
  144. new TwoOperandBase({
  145. operand1: 1,
  146. operand2: 5,
  147. expectedResult: 6,
  148. }).run();
  149. },
  150. //SKIPPED: IntIntNoOverflow
  151. //SKIPPED: IntLong
  152. //SKIPPED: IntLongOverflow
  153. "should add int and double": function testIntDouble() {
  154. /** Adding an int and a double produces a double. */
  155. new TwoOperandBase({
  156. operand1: 9,
  157. operand2: 1.1,
  158. expectedResult: 10.1,
  159. }).run();
  160. },
  161. "should add int and date": function testIntDate() {
  162. /** Adding an int and a Date produces a Date. */
  163. new TwoOperandBase({
  164. operand1: 6,
  165. operand2: new Date(123450),
  166. expectedResult: new Date(123456),
  167. }).run();
  168. },
  169. //SKIPPED: LongDouble
  170. //SKIPPED: LongDoubleNoOverflow
  171. "should add int and null": function testIntNull() {
  172. /** Adding an int and null. */
  173. new TwoOperandBase({
  174. operand1: 1,
  175. operand2: null,
  176. expectedResult: null,
  177. }).run();
  178. },
  179. "should add long and undefined": function testLongUndefined() {
  180. /** Adding a long and undefined. */
  181. new TwoOperandBase({
  182. operand1: 5e11,
  183. operand2: undefined,
  184. expectedResult: null,
  185. }).run();
  186. },
  187. }
  188. },
  189. "optimize": {
  190. "should understand a single number": function() {
  191. var vps = new VariablesParseState(new VariablesIdGenerator()),
  192. expr = Expression.parseOperand({$add:[123]}, vps).optimize();
  193. assert.strictEqual(expr.operands.length, 0, "should optimize operands away");
  194. assert(expr instanceof ConstantExpression);
  195. assert.strictEqual(expr.evaluate(), 123);
  196. },
  197. "should optimize strings of numbers without regard to their order": function() {
  198. var vps = new VariablesParseState(new VariablesIdGenerator()),
  199. expr = Expression.parseOperand({$add:[1,2,3,'$a',4,5,6]}, vps).optimize();
  200. assert.strictEqual(expr.operands.length, 2, "should optimize operands away");
  201. assert(expr.operands[0] instanceof FieldPathExpression);
  202. assert(expr.operands[1] instanceof ConstantExpression);
  203. debugger
  204. assert.strictEqual(expr.operands[1].evaluate(), 1 + 2 + 3 + 4 + 5 + 6);
  205. },
  206. },
  207. };
  208. if (!module.parent)(new (require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);