Browse Source

EAGLESIX-2709 Made the multiply expression better match the c++ code. Added test cases for null and undefined operands.

Tony Ennis 11 years ago
parent
commit
ba8a8e86db

+ 9 - 2
lib/pipeline/expressions/MultiplyExpression.js

@@ -33,9 +33,16 @@ proto.evaluateInternal = function evaluateInternal(vars){
 	var product = 1;
 	for(var i = 0, n = this.operands.length; i < n; ++i){
 		var value = this.operands[i].evaluateInternal(vars);
-		if(value instanceof Date) throw new Error("$multiply does not support dates; code 16375");
-		product *= Value.coerceToDouble(value);
+		if (typeof value == "number") {
+			product *= Value.coerceToDouble(value);
+		} else if (value == null || value == undefined) {
+			return null;
+		} else {
+			throw new Error("$multiply only supports numeric types, not "+typeof value+"; code 16555");
+		}
 	}
+	//NOTE: DEVIATION FROM MONGO: The c++ code (expressions.cpp line 1659) deals with types that
+	// do not seem to apply to javascript.
 	if(typeof(product) != "number") throw new Error("$multiply resulted in a non-numeric type; code 16418");
 	return product;
 };

+ 8 - 0
test/lib/pipeline/expressions/MultiplyExpression_test.js

@@ -63,6 +63,14 @@ module.exports = {
 				});
 			},
 
+			"should handle a null operand": function(){
+				assert.strictEqual(Expression.parseOperand({$multiply: [2, null]}, this.vps).evaluate(), null);
+			},
+
+			"should handle an undefined operand": function(){
+				assert.strictEqual(Expression.parseOperand({$multiply: [2, undefined]}, this.vps).evaluate(), null);
+			},
+
 			"should multiply mixed numbers": function () {
 				assert.strictEqual(Expression.parseOperand({$multiply: [2.1, 3, 4.4]}, this.vps).evaluate(), 2.1 * 3 * 4.4);
 			},