浏览代码

EAGLESIX- filling out the test cases, minor changes to expression classes.

Tony Ennis 11 年之前
父节点
当前提交
3f2676354c

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

@@ -9,11 +9,10 @@
  * @constructor
  **/
 var IfNullExpression = module.exports = function IfNullExpression() {
-	this.nargs = 2;
 	if (arguments.length !== 0) throw new Error("zero args expected");
 	base.call(this);
 }, klass = IfNullExpression,
-	base = require("./NaryExpression"),
+	base = require("./FixedArityExpressionT")(klass, 2),
 	proto = klass.prototype = Object.create(base.prototype, {
 		constructor: {
 			value: klass
@@ -24,8 +23,9 @@ var IfNullExpression = module.exports = function IfNullExpression() {
 var Expression = require("./Expression");
 
 // PROTOTYPE MEMBERS
+klass.opName =  "$ifNull";
 proto.getOpName = function getOpName() {
-	return "$ifNull";
+	return klass.opName;
 };
 
 // virtuals from ExpressionNary
@@ -42,4 +42,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 };
 
 /** Register Expression */
-Expression.registerExpression("$ifNull", base.parse(IfNullExpression));
+Expression.registerExpression(klass.opName, base.parse);

+ 3 - 2
lib/pipeline/expressions/NotExpression.js

@@ -24,8 +24,9 @@ var Value = require("../Value"),
 	Expression = require("./Expression");
 
 // PROTOTYPE MEMBERS
+klass.opName = "$not";
 proto.getOpName = function getOpName() {
-	return "$not";
+	return klass.opName;
 };
 
 /**
@@ -38,4 +39,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 };
 
 /** Register Expression */
-Expression.registerExpression("$not", base.parse(NotExpression));
+Expression.registerExpression(klass.opName, base.parse);

+ 43 - 49
test/lib/pipeline/expressions/IfNullExpression.js

@@ -1,58 +1,52 @@
 "use strict";
 var assert = require("assert"),
-		IfNullExpression = require("../../../../lib/pipeline/expressions/IfNullExpression"),
-		Expression = require("../../../../lib/pipeline/expressions/Expression");
+	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");
-						}
-
-				},
-
-				"#evaluateInternal()": {
-
-						"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"]
-								}).evaluateInternal({
-										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"]
-								}).evaluateInternal({
-										a: null,
-										b: 2
-								}), 2);
-								assert.strictEqual(Expression.parseOperand({
-										$ifNull: ["$a", "$b"]
-								}).evaluateInternal({
-										b: 2
-								}), 2);
-						}
-				}
-
+	"IfNullExpression": {
+
+		"constructor()": {
+
+			"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();
+				});
+			}
+		},
+
+		"#getOpName()": {
+
+			"should return the correct op name; $ifNull": function() {
+				assert.equal(new IfNullExpression().getOpName(), "$ifNull");
+			}
+
+		},
+
+		"#evaluateInternal()": {
+			beforeEach: function () {
+				this.expr = {$ifNull:["$a", "$b"]};
+				this.parsed = Expression.parseOperand(this.expr, {});
+			},
+
+			"should return the left hand side if the left hand side is not null or undefined": function() {
+				assert.strictEqual(this.parsed.evaluateInternal({a: 1, b: 2}), 1);
+			},
+			"should return the right hand side if the left hand side is null": function() {
+				assert.strictEqual(this.parsed.evaluateInternal({a: null, b: 2}), 2);
+			},
+			"should return the right hand side if the left hand side is undefined": function() {
+				assert.strictEqual(this.parsed.evaluateInternal({b: 2}), 2);
+			}
 		}
-
+	}
 };
 
-if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);
+if (!module.parent)(new (require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);

+ 0 - 0
test/lib/pipeline/expressions/NotExpression.js → test/lib/pipeline/expressions/NotExpression_test.js