瀏覽代碼

Fixed munge strcasecmp expression and added test cases. fixes #974

http://source.rd.rcg.local/trac/eagle6/changeset/1287/Eagle6_SVN
Philip Murray 12 年之前
父節點
當前提交
f42548795d

+ 7 - 3
lib/pipeline/expressions/StrcasecmpExpression.js

@@ -1,6 +1,8 @@
 var StrcasecmpExpression = module.exports = (function(){
 	// CONSTRUCTOR
-	/** A $strcasecmp pipeline expression. @see evaluate **/
+	/** 
+	* A $strcasecmp pipeline expression. @see evaluate 
+	**/
 	var klass = function StrcasecmpExpression(){
 		if(arguments.length !== 0) throw new Error("zero args expected");
 		base.call(this);
@@ -16,10 +18,12 @@ var Value = require("../Value"),
 
 	proto.addOperand = function addOperand(expr) {
 		this.checkArgLimit(2);
-		base.addOperand(expr);
+		base.prototype.addOperand.call(this, expr);
 	};
 
-	/** Takes in two strings. Returns a number. $strcasecmp is positive if the first string is “greater than” the second and negative if the first string is “less than” the second. $strcasecmp returns 0 if the strings are identical. **/
+	/** 
+	* Takes in two strings. Returns a number. $strcasecmp is positive if the first string is “greater than” the second and negative if the first string is “less than” the second. $strcasecmp returns 0 if the strings are identical. 
+	**/
 	proto.evaluate = function evaluate(doc){
 		this.checkArgCount(2);
 		var val1 = this.operands[0].evaluate(doc),

+ 58 - 0
test/lib/pipeline/expressions/StrcasecmpExpression.js

@@ -0,0 +1,58 @@
+var assert = require("assert"),
+	StrcasecmpExpression = require("../../../../lib/pipeline/expressions/StrcasecmpExpression"),
+	Expression = require("../../../../lib/pipeline/expressions/Expression");
+
+module.exports = {
+
+	"StrcasecmpExpression": {
+
+		"constructor()": {
+
+			"should not throw Error when constructing without args": function testConstructor(){
+				assert.doesNotThrow(function(){
+					new StrcasecmpExpression();
+				});
+			}
+
+		},
+
+		"#getOpName()": {
+
+			"should return the correct op name; $strcasecmp": function testOpName(){
+				assert.equal(new StrcasecmpExpression().getOpName(), "$strcasecmp");
+			}
+
+		},
+
+		"#getFactory()": {
+
+			"should return the constructor for this class": function factoryIsConstructor(){
+				assert.strictEqual(new StrcasecmpExpression().getFactory(), undefined);
+			}
+
+		},
+
+		"#evaluate()": {
+
+			"should return 0 if the strings are equivalent and begin with a null character": function testStuff(){
+				assert.strictEqual(Expression.parseOperand({$strcasecmp:["$a", "$b"]}).evaluate({a:"\0ab", b:"\0AB"}), 0);
+			},
+			"should return 0 if the strings are equivalent and end with a null character": function testStuff(){
+				assert.strictEqual(Expression.parseOperand({$strcasecmp:["$a", "$b"]}).evaluate({a:"ab\0", b:"AB\0"}), 0);
+			},
+			"should return -1 if the left hand side is less than the right hand side and both contain a null character": function testStuff(){
+				assert.strictEqual(Expression.parseOperand({$strcasecmp:["$a", "$b"]}).evaluate({a:"a\0a", b:"A\0B"}), -1);
+			},
+			"should return 0 if the strings are equivalent and both contain a null character": function testStuff(){
+				assert.strictEqual(Expression.parseOperand({$strcasecmp:["$a", "$b"]}).evaluate({a:"a\0b", b:"A\0B"}), 0);
+			},
+			"should return 1 if the left hand side is greater than the right hand side and both contain a null character": function testStuff(){
+				assert.strictEqual(Expression.parseOperand({$strcasecmp:["$a", "$b"]}).evaluate({a:"a\0c", b:"A\0B"}), 1);
+			}
+		}
+
+	}
+
+};
+
+if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);