Browse Source

EAGLESIX-2699 This includes ifNull, Not, and Cond. The latter is turning out to be interesting - I am not sure the code we have works at all.

Tony Ennis 11 years ago
parent
commit
23a651f778

+ 24 - 21
lib/pipeline/expressions/CondExpression.js

@@ -8,44 +8,44 @@
  * @constructor
  **/
 var CondExpression = module.exports = function CondExpression(vars) {
-    this.nargs = 3;
-    this.pCond = this.evaluateInternal(vars);
-    this.idx = this.pCond.coerceToBool() ? 1 : 2;
-
-    if (arguments.length !== 3) throw new Error("three args expected");
-    base.call(this);
-}, klass = CondExpression,
-    base = require("./NaryExpression"),
-    proto = klass.prototype = Object.create(base.prototype, {
-	constructor: {
-	    value: klass
-	}
-    });
+		if (arguments.length !== 0) throw new Error("zero args expected");
+		base.call(this);
+	}, klass = CondExpression,
+	base = require("./FixedArityExpression")(klass, 3),
+	proto = klass.prototype = Object.create(base.prototype, {
+		constructor: {
+			value: klass
+		}
+	});
 
 // DEPENDENCIES
 var Value = require("../Value"),
     Expression = require("./Expression");
 
 // PROTOTYPE MEMBERS
+klass.opName = "$cond";
 proto.getOpName = function getOpName() {
-    return "$cond";
+    return klass.opName;
 };
 
 klass.parse = function parse(expr, vps) {
-    this.checkArgLimit(3);
+	// There may only be one argument - an array of 3 items, or a hash containing 3 keys.
+    //this.checkArgLimit(3);
 
     // if not an object, return;
     if (typeof(expr) !== Object)
 		return Expression.parse(expr, vps);
 
-    // verify
-    if (Expression.parseOperand(expr) !== "$cond")
-		throw new Error("Invalid expression");
+	if(!(klass.opName in expr)) {
+		throw new Error("Invalid expression. Expected to see '"+klass.opName+"'");
+	}
 
     var ret = new CondExpression();
 
-    var ex = Expression.parseObject(expr);
+	// If this is an Object and not an array, verify all the bits are specified.
+	// If this is an Object that is an array, verify there are three bits.
     var args = Expression.parseOperand(expr, vps);
+
     if (args[0] !== "if")
 		throw new Error("Missing 'if' parameter to $cond");
     if (args[1] !== "then")
@@ -58,7 +58,10 @@ klass.parse = function parse(expr, vps) {
 };
 
 /**
- * Use the $cond operator with the following syntax:  { $cond: [ <boolean-expression>, <true-case>, <false-case> ] }
+ * Use the $cond operator with the following syntax:
+ * { $cond: { if: <boolean-expression>, then: <true-case>, else: <false-case-> } }
+ * -or-
+ * { $cond: [ <boolean-expression>, <true-case>, <false-case> ] }
  * @method evaluate
  **/
 proto.evaluateInternal = function evaluateInternal(vars) {
@@ -75,4 +78,4 @@ proto.evaluateInternal = function evaluateInternal(vars) {
 };
 
 /** Register Expression */
-Expression.registerExpression("$cond", klass.parse);
+Expression.registerExpression(klass.opName, klass.parse);

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

@@ -9,10 +9,10 @@
  * @constructor
  **/
 var NotExpression = module.exports = function NotExpression() {
-	this.nargs = 1;
-	base.call(this);
-}, klass = NotExpression,
-	base = require("./NaryExpression"),
+		if (arguments.length !== 0) throw new Error("zero args expected");
+		base.call(this);
+	}, klass = NotExpression,
+	base = require("./FixedArityExpression")(klass, 1),
 	proto = klass.prototype = Object.create(base.prototype, {
 		constructor: {
 			value: klass

+ 6 - 7
test/lib/pipeline/expressions/CondExpression.js → test/lib/pipeline/expressions/CondExpression_test.js

@@ -45,26 +45,25 @@ module.exports = {
 		"#evaluateInternal()": {
 
 			"should evaluate boolean expression as true, then return 1; [ true === true, 1, 0 ]": function testStuff(){
-				assert.strictEqual(Expression.parseOperand({$cond:[ true === true, 1, 0 ]}).evaluateInternal({}), 1);
+				assert.strictEqual(Expression.parseOperand({$cond:[ true === true, 1, 0 ]}, {}).evaluateInternal({}), 1);
 			},
 
 			"should evaluate boolean expression as false, then return 0; [ false === true, 1, 0 ]": function testStuff(){
-				assert.strictEqual(Expression.parseOperand({$cond:[ false === true, 1, 0 ]}).evaluateInternal({}), 0);
+				assert.strictEqual(Expression.parseOperand({$cond:[ false === true, 1, 0 ]}, {}).evaluateInternal({}), 0);
 			}, 
 
 			"should evaluate boolean expression as true, then return 1; [ (true === true) && true, 1, 0 ]": function testStuff(){
-				assert.strictEqual(Expression.parseOperand({$cond:[ (true === true) && true , 1, 0 ]}).evaluateInternal({}), 1);
+				assert.strictEqual(Expression.parseOperand({$cond:[ (true === true) && true , 1, 0 ]}, {}).evaluateInternal({}), 1);
 			},
 
 			"should evaluate boolean expression as false, then return 0; [ (false === true) && true, 1, 0 ]": function testStuff(){
-				assert.strictEqual(Expression.parseOperand({$cond:[ (false === true) && true, 1, 0 ]}).evaluateInternal({}), 0);
+				assert.strictEqual(Expression.parseOperand({$cond:[ (false === true) && true, 1, 0 ]}, {}).evaluateInternal({}), 0);
 			},
 
 			"should evaluate complex boolean expression as true, then return 1; [ ( 1 > 0 ) && (( 'a' == 'b' ) || ( 3 <= 5 )), 1, 0 ]": function testStuff(){
-				assert.strictEqual(Expression.parseOperand({$cond:[ ( 1 > 0 ) && (( 'a' == 'b' ) || ( 3 <= 5 )), 1, 0 ]}).evaluate({}), 1);
-			},
+				assert.strictEqual(Expression.parseOperand({$cond:[ ( 1 > 0 ) && (( 'a' == 'b' ) || ( 3 <= 5 )), 1, 0 ]}, {}).evaluate({}), 1);
+			}
 		}
-
 	}
 
 };

+ 0 - 0
test/lib/pipeline/expressions/IfNullExpression.js → test/lib/pipeline/expressions/IfNullExpression_test.js


+ 9 - 4
test/lib/pipeline/expressions/NotExpression_test.js

@@ -14,6 +14,11 @@ module.exports = {
 				assert.doesNotThrow(function(){
 					new NotExpression();
 				});
+			},
+			"should throw when constructing with args": function testConstructor(){
+				assert.throws(function(){
+					new NotExpression(1);
+				});
 			}
 
 		},
@@ -36,12 +41,12 @@ module.exports = {
 
 		"#evaluateInternal()": {
 
-			"should return false for a true input; false for true": function testStuff(){
-				assert.strictEqual(Expression.parseOperand({$not:true}).evaluateInternal({}), false);
+			"should return false for a true input; false for true": function(){
+				assert.strictEqual(Expression.parseOperand({$not:true}, {}).evaluateInternal({}), false);
 			},
 
-			"should return true for a false input; true for false": function testStuff(){
-				assert.strictEqual(Expression.parseOperand({$not:false}).evaluateInternal({}), true);
+			"should return true for a false input; true for false": function(){
+				assert.strictEqual(Expression.parseOperand({$not:false}, {}).evaluateInternal({}), true);
 			}
 
 		}