Browse Source

Fixed munge ifNull expression and added test cases. fixes #967

http://source.rd.rcg.local/trac/eagle6/changeset/1286/Eagle6_SVN
Philip Murray 12 years ago
parent
commit
86fca1e1b5

+ 8 - 4
lib/pipeline/expressions/IfNullExpression.js

@@ -1,6 +1,8 @@
 var IfNullExpression = module.exports = (function(){
 	// CONSTRUCTOR
-	/** An $ifNull pipeline expression. @see evaluate **/
+	/** 
+	* An $ifNull pipeline expression. @see evaluate 
+	**/
 	var klass = function IfNullExpression(){
 		if(arguments.length !== 0) throw new Error("zero args expected");
 		base.call(this);
@@ -13,10 +15,12 @@ var IfNullExpression = module.exports = (function(){
 
 	proto.addOperand = function addOperand(expr) {
 		this.checkArgLimit(2);
-		base.addOperand(expr);
+		base.prototype.addOperand.call(this, expr);
 	};
 
-	/** Use the $ifNull operator with the following syntax: { $ifNull: [ <expression>, <replacement-if-null> ] } **/
+	/**
+	* Use the $ifNull operator with the following syntax: { $ifNull: [ <expression>, <replacement-if-null> ] } 
+	**/
 	proto.evaluate = function evaluate(doc){
 		this.checkArgCount(2);
 		var left = this.operands[0].evaluate(doc);
@@ -26,4 +30,4 @@ var IfNullExpression = module.exports = (function(){
 	};
 
 	return klass;
-})();
+})();

+ 50 - 0
test/lib/pipeline/expressions/IfNullExpression.js

@@ -0,0 +1,50 @@
+var assert = require("assert"),
+	IfNullExpression = require("../../../../lib/pipeline/expressions/IfNullExpression"),
+	Expression = require("../../../../lib/pipeline/expressions/Expression");
+
+module.exports = {
+
+	"IfNullExpression": {
+
+		"constructor()": {
+
+			"should not throw Error when constructing without args": function testConstructor(){
+				assert.doesNotThrow(function(){
+					new IfNullExpression();
+				});
+			}
+
+		},
+
+		"#getOpName()": {
+
+			"should return the correct op name; $ifNull": function testOpName(){
+				assert.equal(new IfNullExpression().getOpName(), "$ifNull");
+			}
+
+		},
+
+		"#getFactory()": {
+
+			"should return the constructor for this class": function factoryIsConstructor(){
+				assert.strictEqual(new IfNullExpression().getFactory(), undefined);
+			}
+
+		},
+
+		"#evaluate()": {
+
+			"should return the left hand side if the left hand side is not null or undefined": function testStuff(){
+				assert.strictEqual(Expression.parseOperand({$ifNull:["$a", "$b"]}).evaluate({a:1, b:2}), 1);
+			},
+			"should return the right hand side if the left hand side is null or undefined": function testStuff(){
+				assert.strictEqual(Expression.parseOperand({$ifNull:["$a", "$b"]}).evaluate({a:null, b:2}), 2);
+				assert.strictEqual(Expression.parseOperand({$ifNull:["$a", "$b"]}).evaluate({b:2}), 2);
+			}
+		}
+
+	}
+
+};
+
+if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);