Browse Source

Fixed munge subtract expression and added test cases. fixes #976

http://source.rd.rcg.local/trac/eagle6/changeset/1288/Eagle6_SVN
Philip Murray 12 years ago
parent
commit
a9a55a058c

+ 7 - 3
lib/pipeline/expressions/SubtractExpression.js

@@ -1,6 +1,8 @@
 var SubtractExpression = module.exports = (function(){
 	// CONSTRUCTOR
-	/** A $subtract pipeline expression. @see evaluate **/
+	/** 
+	* A $subtract pipeline expression. @see evaluate 
+	**/
 	var klass = function SubtractExpression(){
 		if(arguments.length !== 0) throw new Error("zero args expected");
 		base.call(this);
@@ -16,10 +18,12 @@ var Value = require("../Value");
 
 	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 subtracts the second from the first, returning their difference. **/
+	/** 
+	* Takes an array that contains a pair of numbers and subtracts the second from the first, returning their difference. 
+	**/
 	proto.evaluate = function evaluate(doc) {
 		this.checkArgCount(2);
 		var left = this.operands[0].evaluate(doc),

+ 46 - 0
test/lib/pipeline/expressions/SubtractExpression.js

@@ -0,0 +1,46 @@
+var assert = require("assert"),
+	SubtractExpression = require("../../../../lib/pipeline/expressions/SubtractExpression"),
+	Expression = require("../../../../lib/pipeline/expressions/Expression");
+
+module.exports = {
+
+	"SubtractExpression": {
+
+		"constructor()": {
+
+			"should not throw Error when constructing without args": function testConstructor(){
+				assert.doesNotThrow(function(){
+					new SubtractExpression();
+				});
+			}
+
+		},
+
+		"#getOpName()": {
+
+			"should return the correct op name; $subtract": function testOpName(){
+				assert.equal(new SubtractExpression().getOpName(), "$subtract");
+			}
+
+		},
+
+		"#getFactory()": {
+
+			"should return the constructor for this class": function factoryIsConstructor(){
+				assert.strictEqual(new SubtractExpression().getFactory(), undefined);
+			}
+
+		},
+
+		"#evaluate()": {
+
+			"should return the result of subtraction between two numbers": function testStuff(){
+				assert.strictEqual(Expression.parseOperand({$subtract:["$a", "$b"]}).evaluate({a:35636364, b:-0.5656}), 35636364-(-0.5656));
+			}
+		}
+
+	}
+
+};
+
+if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);