Browse Source

EAGLESIX-2699 more enhancements to the $cond code.

Tony Ennis 11 years ago
parent
commit
f7f59c2495

+ 3 - 1
lib/pipeline/expressions/CondExpression.js

@@ -45,7 +45,9 @@ klass.parse = function parse(expr, vps) {
 	// 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);
-
+// what is args?  If it is an array of scalars (array mode) or an array of objects (object mode), we're ok.
+// In the latter case, I am very not ok with the assuming if, then, and else are in a known order which is
+// what the original code implies.
     if (args[0] !== "if")
 		throw new Error("Missing 'if' parameter to $cond");
     if (args[1] !== "then")

+ 12 - 16
test/lib/pipeline/expressions/CondExpression_test.js

@@ -32,25 +32,21 @@ module.exports = {
 		},
 
 		"#evaluateInternal()": {
+			"array style": {
 
-			"should evaluate boolean expression as true, then return 1; [ true === true, 1, 0 ]": function(){
-				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(){
-				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(){
-				assert.strictEqual(Expression.parseOperand({$cond:[ (true === true) && true , 1, 0 ]}, {}).evaluateInternal({}), 1);
-			},
+				"should fail if there aren't enough arguments": function() {
+					assert.throws(function(){
+						Expression.parseOperand({$cond:[1,2]}, {});
+					})
+				},
 
-			"should evaluate boolean expression as false, then return 0; [ (false === true) && true, 1, 0 ]": function(){
-				assert.strictEqual(Expression.parseOperand({$cond:[ (false === true) && true, 1, 0 ]}, {}).evaluateInternal({}), 0);
-			},
+				"should evaluate boolean expression as true, then return 1; [ true === true, 1, 0 ]": function () {
+					assert.strictEqual(Expression.parseOperand({$cond: [ true, 1, 0 ]}, {}).evaluateInternal({}), 1);
+				},
 
-			"should evaluate complex boolean expression as true, then return 1; [ ( 1 > 0 ) && (( 'a' == 'b' ) || ( 3 <= 5 )), 1, 0 ]": function(){
-				assert.strictEqual(Expression.parseOperand({$cond:[ ( 1 > 0 ) && (( 'a' == 'b' ) || ( 3 <= 5 )), 1, 0 ]}, {}).evaluate({}), 1);
+				"should evaluate boolean expression as false, then return 0; [ false === true, 1, 0 ]": function () {
+					assert.strictEqual(Expression.parseOperand({$cond: [ false, 1, 0 ]}, {}).evaluateInternal({}), 0);
+				}
 			},
 
 			"object style": {