Browse Source

Fixed munge mod expression and added test cases. fixes #969

http://source.rd.rcg.local/trac/eagle6/changeset/1281/Eagle6_SVN
Philip Murray 13 years ago
parent
commit
04e4214b9e

+ 5 - 3
lib/pipeline/expressions/ModExpression.js

@@ -16,14 +16,16 @@ var ModExpression = module.exports = (function(){
 
 	proto.addOperand = function addOperand(expr) {
 		this.checkArgLimit(2);
-		base.addOperand(expr);
+		base.prototype.addOperand.call(this, expr);
 	};
 
-	/** Takes an array that contains a pair of numbers and returns the remainder of the first number divided by the second number. **/
+	/** 
+	* Takes an array that contains a pair of numbers and returns the remainder of the first number divided by the second number. 
+	**/
 	proto.evaluate = function evaluate(doc){
 		this.checkArgCount(2);
 		var left = this.operands[0].evaluate(doc),
-			right = this.operands[0].evaluate(doc);
+			right = this.operands[1].evaluate(doc);
 		if(left instanceof Date || right instanceof Date) throw new Error("$mod does not support dates; code 16374");
 
 		// pass along jstNULLs and Undefineds

+ 60 - 0
test/lib/pipeline/expressions/ModExpression.js

@@ -0,0 +1,60 @@
+var assert = require("assert"),
+	ModExpression = require("../../../../lib/pipeline/expressions/ModExpression"),
+	Expression = require("../../../../lib/pipeline/expressions/Expression");
+
+module.exports = {
+
+	"ModExpression": {
+
+		"constructor()": {
+
+			"should not throw Error when constructing without args": function testConstructor(){
+				assert.doesNotThrow(function(){
+					new ModExpression();
+				});
+			}
+
+		},
+
+		"#getOpName()": {
+
+			"should return the correct op name; $mod": function testOpName(){
+				assert.equal(new ModExpression().getOpName(), "$mod");
+			}
+
+		},
+
+		"#getFactory()": {
+
+			"should return the constructor for this class": function factoryIsConstructor(){
+				assert.strictEqual(new ModExpression().getFactory(), undefined);
+			}
+
+		},
+
+		"#evaluate()": {
+
+			"should return rhs if rhs is undefined or null": function testStuff(){
+				assert.strictEqual(Expression.parseOperand({$mod:["$lhs", "$rhs"]}).evaluate({lhs:20.453, rhs:null}), null);
+				assert.strictEqual(Expression.parseOperand({$mod:["$lhs", "$rhs"]}).evaluate({lhs:20.453}), undefined);
+			},
+			"should return lhs if lhs is undefined or null": function testStuff(){
+				assert.strictEqual(Expression.parseOperand({$mod:["$lhs", "$rhs"]}).evaluate({lhs:null, rhs:20.453}), null);
+				assert.strictEqual(Expression.parseOperand({$mod:["$lhs", "$rhs"]}).evaluate({rhs:20.453}), undefined);
+			},
+			"should return undefined if rhs is 0": function testStuff(){
+				assert.strictEqual(Expression.parseOperand({$mod:["$lhs", "$rhs"]}).evaluate({lhs:20.453, rhs:0}), undefined);
+			},
+			"should return proper mod of rhs and lhs if both are numbers": function testStuff(){
+				assert.strictEqual(Expression.parseOperand({$mod:["$lhs", "$rhs"]}).evaluate({lhs:234.4234, rhs:45}), 234.4234 % 45);
+				assert.strictEqual(Expression.parseOperand({$mod:["$lhs", "$rhs"]}).evaluate({lhs:0, rhs:45}), 0 % 45);
+				assert.strictEqual(Expression.parseOperand({$mod:["$lhs", "$rhs"]}).evaluate({lhs:-6, rhs:-0.5}), -6 % -0.5);
+			}
+
+		}
+
+	}
+
+};
+
+if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);