Browse Source

EAGLESIX-2651: IfNull: misc formatting from 2.6.5 port

Kyle P Davis 11 years ago
parent
commit
b3634cdf42

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

@@ -11,35 +11,20 @@
 var IfNullExpression = module.exports = function IfNullExpression() {
 	if (arguments.length !== 0) throw new Error("zero args expected");
 	base.call(this);
-}, klass = IfNullExpression,
-	base = require("./FixedArityExpressionT")(klass, 2),
-	proto = klass.prototype = Object.create(base.prototype, {
-		constructor: {
-			value: klass
-		}
-	});
+}, klass = IfNullExpression, base = require("./FixedArityExpressionT")(IfNullExpression, 2), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
-// DEPENDENCIES
 var Expression = require("./Expression");
 
-// PROTOTYPE MEMBERS
-klass.opName =  "$ifNull";
-proto.getOpName = function getOpName() {
-	return klass.opName;
-};
-
-// virtuals from ExpressionNary
-
-/**
- * Use the $ifNull operator with the following syntax: { $ifNull: [ <expression>, <replacement-if-null> ] }
- * @method evaluate
- **/
 proto.evaluateInternal = function evaluateInternal(vars) {
 	var left = this.operands[0].evaluateInternal(vars);
-	if (left !== undefined && left !== null) return left;
+	if (left !== undefined && left !== null)
+		return left;
 	var right = this.operands[1].evaluateInternal(vars);
 	return right;
 };
 
-/** Register Expression */
-Expression.registerExpression(klass.opName, base.parse);
+Expression.registerExpression("$ifNull", base.parse);
+
+proto.getOpName = function getOpName() {
+	return "$ifNull";
+};

+ 40 - 36
test/lib/pipeline/expressions/IfNullExpression_test.js

@@ -6,50 +6,54 @@ var assert = require("assert"),
 	Variables = require("../../../../lib/pipeline/expressions/Variables"),
 	Expression = require("../../../../lib/pipeline/expressions/Expression");
 
+// Mocha one-liner to make these tests self-hosted
+if(!module.parent)return(require.cache[__filename]=null,(new(require("mocha"))({ui:"exports",reporter:"spec",grep:process.env.TEST_GREP})).addFile(__filename).run(process.exit));
 
-module.exports = {
+exports.IfNullExpression = {
 
-	"IfNullExpression": {
+	"constructor()": {
 
-		"constructor()": {
+		"should not throw Error when constructing without args": function() {
+			assert.doesNotThrow(function () {
+				new IfNullExpression();
+			});
+		},
 
-			"should not throw Error when constructing without args": function() {
-				assert.doesNotThrow(function () {
-					new IfNullExpression();
-				});
-			},
-			"should throw Error when constructing with args": function () {
-				assert.throws(function () {
-					new IfNullExpression(1);
-				});
-			}
+		"should throw Error when constructing with args": function () {
+			assert.throws(function () {
+				new IfNullExpression(1);
+			});
 		},
 
-		"#getOpName()": {
+	},
 
-			"should return the correct op name; $ifNull": function() {
-				assert.equal(new IfNullExpression().getOpName(), "$ifNull");
-			}
+	"#getOpName()": {
 
+		"should return the correct op name; $ifNull": function() {
+			assert.equal(new IfNullExpression().getOpName(), "$ifNull");
 		},
 
-		"#evaluateInternal()": {
-			beforeEach: function () {
-				this.vps = new VariablesParseState(new VariablesIdGenerator());
-				this.parsed = Expression.parseExpression("$ifNull", ["$a", "$b"], this.vps);
-			},
-
-			"should return the left hand side if the left hand side is not null or undefined": function() {
-				assert.strictEqual(this.parsed.evaluate({a: 1, b: 2}), 1);
-			},
-			"should return the right hand side if the left hand side is null": function() {
-				assert.strictEqual(this.parsed.evaluate({a: null, b: 2}), 2);
-			},
-			"should return the right hand side if the left hand side is undefined": function() {
-				assert.strictEqual(this.parsed.evaluate({b: 2}), 2);
-			}
-		}
-	}
-};
+	},
+
+	"#evaluate()": {
+
+		beforeEach: function () {
+			this.vps = new VariablesParseState(new VariablesIdGenerator());
+			this.parsed = Expression.parseExpression("$ifNull", ["$a", "$b"], this.vps);
+		},
+
+		"should return the left hand side if the left hand side is not null or undefined": function() {
+			assert.strictEqual(this.parsed.evaluate({a: 1, b: 2}), 1);
+		},
 
-if (!module.parent)(new (require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);
+		"should return the right hand side if the left hand side is null": function() {
+			assert.strictEqual(this.parsed.evaluate({a: null, b: 2}), 2);
+		},
+
+		"should return the right hand side if the left hand side is undefined": function() {
+			assert.strictEqual(this.parsed.evaluate({b: 2}), 2);
+		},
+
+	},
+
+};