浏览代码

EAGLESIX-2699 Filled out test cases for 'object' implementation of $cond.

Tony Ennis 11 年之前
父节点
当前提交
2d9afc2b68
共有 1 个文件被更改,包括 45 次插入20 次删除
  1. 45 20
      test/lib/pipeline/expressions/CondExpression_test.js

+ 45 - 20
test/lib/pipeline/expressions/CondExpression_test.js

@@ -10,28 +10,17 @@ module.exports = {
 
 		"constructor()": {
 
-			"should throw Error when constructing without args": function testConstructor(){
-				assert.throws(function(){
+			"should not throw an Error when constructing without args": function testConstructor(){
+				assert.doesNotThrow(function(){
 					new CondExpression();
 				});
 			},
 
 			"should throw Error when constructing with 1 arg": function testConstructor1(){
 				assert.throws(function(){
-					new CondExpression({if:true === true});
+					new CondExpression(1);
 				});
-			},
-			"should throw Error when constructing with 2 args": function testConstructor2(){
-				assert.throws(function(){
-					new CondExpression(true === true,1);
-				});
-			},
-			"should now throw Error when constructing with 3 args": function testConstructor3(){
-				assert.doesNotThrow(function(){
-					//new CondExpression({$cond:[{"if":"true === true"},{"then":"1"},{"else":"0"}]});
-					new CondExpression({$cond:[ true === true, 1, 0 ]});
-				});
-			},
+			}
 		},
 
 		"#getOpName()": {
@@ -44,24 +33,60 @@ module.exports = {
 
 		"#evaluateInternal()": {
 
-			"should evaluate boolean expression as true, then return 1; [ true === true, 1, 0 ]": function testStuff(){
+			"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 testStuff(){
+			"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 testStuff(){
+			"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 evaluate boolean expression as false, then return 0; [ (false === true) && true, 1, 0 ]": function testStuff(){
+			"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 complex boolean expression as true, then return 1; [ ( 1 > 0 ) && (( 'a' == 'b' ) || ( 3 <= 5 )), 1, 0 ]": function testStuff(){
+			"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);
+			},
+
+			"object style": {
+				beforeEach: function(){
+					this.shouldFail = function(expr) {
+						assert.throws(function(){
+							Expression.parseOperand(expr, {});
+						});
+					}
+				},
+				"should fail because the $cond is missing": function(){
+					this.shouldFail({$zoot:[true, 1, 0 ]}, {});
+				},
+				"should fail because of missing if": function(){
+					this.shouldFail({$cond:{xif:1, then:2, else:3}});
+				},
+				"should fail because of missing then": function(){
+					this.shouldFail({$cond:{if:1, xthen:2, else:3}});
+				},
+				"should fail because of missing else": function(){
+					this.shouldFail({$cond:{if:1, then:2, xelse:3}});
+				},
+				"should fail because of mystery args": function(){
+					this.shouldFail({$cond:{if:1, then:2, else:3, zoot:4}});
+				},
+				"should evaluate true": function(){
+					assert.strictEqual(
+						Expression.parseOperand({$cond:{ if: $a, then: 1, else: 0}}, {}).evaluate({$a: 1}),
+						1);
+				},
+				"should evaluate false": function(){
+					assert.strictEqual(
+						Expression.parseOperand({$cond:{ if: $a, then: 0, else: 1}}, {}).evaluate({$a: 0}),
+						1);
+				}
+
 			}
 		}
 	}