Procházet zdrojové kódy

EAGLESIX-2707 Added the 'commutitive...' function to AddExpression. Added 'optimize' test cases. comment out test cases relating to dates which we do not support.

Tony Ennis před 11 roky
rodič
revize
ffc44fe128

+ 3 - 0
lib/pipeline/expressions/AddExpression.js

@@ -22,6 +22,9 @@ proto.getOpName = function getOpName(){
 	return klass.opName;
 };
 
+proto.isAssociativeAndCommutative = function isAssociativeAndCommutative() { return true; };
+
+
 /**
  * Takes an array of one or more numbers and adds them together, returning the sum.
  * @method @evaluate

+ 1 - 1
lib/pipeline/expressions/AndExpression.js

@@ -40,7 +40,7 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 	return true;
 };
 
-proto.isAssociativeAndCommutative = function isAssociativeAndCommutative() { return true; }
+proto.isAssociativeAndCommutative = function isAssociativeAndCommutative() { return true; };
 
 proto.optimize = function optimize() {
 	var expr = base.prototype.optimize.call(this); //optimize the conjunction as much as possible

+ 38 - 15
test/lib/pipeline/expressions/AddExpression_test.js

@@ -1,6 +1,10 @@
 "use strict";
 var assert = require("assert"),
 	AddExpression = require("../../../../lib/pipeline/expressions/AddExpression"),
+	Expression = require("../../../../lib/pipeline/expressions/Expression"),
+	VariablesParseState = require("../../../../lib/pipeline/expressions/VariablesParseState"),
+	VariablesIdGenerator = require("../../../../lib/pipeline/expressions/VariablesIdGenerator"),
+	FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression"),
 	ConstantExpression = require("../../../../lib/pipeline/expressions/ConstantExpression");
 
 
@@ -92,10 +96,11 @@ module.exports = {
 					var v = 123.234;
 					this.expectedResult(v, v);
 				},
-				"should pass through a single date": function () {
-					var v = new Date();
-					this.expectedResult(v, v);
-				}
+//NOTE: DEVIATION FROM MONGO: We don't support dates
+//				"should pass through a single date": function () {
+//					var v = new Date();
+//					this.expectedResult(v, v);
+//				}
 			},
 
 			"TwoOperand": {
@@ -139,20 +144,38 @@ module.exports = {
 					this.compareBothWays([1.1, 2.2, 3.3], 6.6);
 				},
 
-				"should add an int and a date and get a date": function () {
-					var d = new Date();
-					this.compareBothWays([d, 10], d.getTime() + 10);
-				},
-
-				"We can't add 2 dates": function () {
-					assert(function () {
-						this.compareBothWays([new Date(), new Date()], 0);
-					});
-				}
+//NOTE: DEVIATION FROM MONGO: We don't support dates
+//				"should add an int and a date and get a date": function () {
+//					var d = new Date();
+//					this.compareBothWays([d, 10], d.getTime() + 10);
+//				},
+
+//NOTE: DEVIATION FROM MONGO: We don't support dates
+//				"We can't add 2 dates": function () {
+//					var d = new Date();
+//					this.compareBothWays([d, d], 0);
+//				}
+			}
+		},
+		"optimize": {
+			beforeEach: function(){
+			this.vps = new VariablesParseState(new VariablesIdGenerator());
+			},
+			"should understand a single number": function() {
+				var a = Expression.parseOperand({$add:[123]}, this.vps).optimize();
+				assert.equal(a.operands.length, 0, "The operands should have been optimized away");
+				assert(a instanceof ConstantExpression);
+				assert.equal(a.evaluateInternal(), 123);
+			},
+			"should optimize strings of numbers without regard to their order": function() {
+				var a = Expression.parseOperand({$add:[1,2,3,'$a',4,5,6]}, this.vps).optimize();
+				assert.equal(a.operands.length, 2, "The operands should have been optimized away");
+				assert(a.operands[0] instanceof FieldPathExpression);
+				assert(a.operands[1] instanceof ConstantExpression);
+				assert(a.operands[1].evaluateInternal(), 1+2+3+4+5+6);
 			}
 		}
 	}
-
 };
 
 if (!module.parent)(new (require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);