瀏覽代碼

Fixes #975. SubstrExpression and tests completed.

http://source.rd.rcg.local/trac/eagle6/changeset/1292/Eagle6_SVN
Kyle Davis 12 年之前
父節點
當前提交
111f2b2f86
共有 2 個文件被更改,包括 98 次插入3 次删除
  1. 7 3
      lib/pipeline/expressions/SubstrExpression.js
  2. 91 0
      test/lib/pipeline/expressions/SubstrExpression.js

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

@@ -1,6 +1,8 @@
 var SubstrExpression = module.exports = (function(){
 	// CONSTRUCTOR
-	/** A $substr pipeline expression. @see evaluate **/
+	/**
+	* A $substr pipeline expression. @see evaluate
+	**/
 	var klass = function SubstrExpression(){
 		if(arguments.length !== 0) throw new Error("zero args expected");
 		base.call(this);
@@ -16,10 +18,12 @@ var SubstrExpression = module.exports = (function(){
 
 	proto.addOperand = function addOperand(expr) {
 		this.checkArgLimit(3);
-		base.addOperand(expr);
+		base.prototype.addOperand.call(this, expr);
 	};
 
-	/** Takes a string and two numbers. The first number represents the number of bytes in the string to skip, and the second number specifies the number of bytes to return from the string. **/
+	/**
+	* Takes a string and two numbers. The first number represents the number of bytes in the string to skip, and the second number specifies the number of bytes to return from the string.
+	**/
 	proto.evaluate = function evaluate(doc) {
 		this.checkArgCount(3);
 		var val = this.operands[0].evaluate(doc),

+ 91 - 0
test/lib/pipeline/expressions/SubstrExpression.js

@@ -0,0 +1,91 @@
+var assert = require("assert"),
+	SubstrExpression = require("../../../../lib/pipeline/expressions/SubstrExpression"),
+	Expression = require("../../../../lib/pipeline/expressions/Expression");
+
+module.exports = {
+
+	"SubstrExpression": {
+
+		"constructor()": {
+
+			"should not throw Error when constructing without args": function testConstructor(){
+				assert.doesNotThrow(function(){
+					new SubstrExpression();
+				});
+			}
+
+		},
+
+		"#getOpName()": {
+
+			"should return the correct op name; $substr": function testOpName(){
+				assert.equal(new SubstrExpression().getOpName(), "$substr");
+			}
+
+		},
+
+		"#evaluate()": {
+
+			"Should fail if no end argument is given": function testMissing3rdArg(){
+				var s = "mystring",
+				start = 0,
+				end = s.length;
+				assert.throws(function(){Expression.parseOperand({$substr:["$s", "$start"]}).evaluate({s:s, start:start});});
+			},
+
+			"Should return entire string when called with 0 and length": function testWholeString(){
+				var s = "mystring",
+				start = 0,
+				end = s.length;
+				assert.strictEqual(Expression.parseOperand({$substr:["$s", "$start", "$end"]}).evaluate({s:s, start:start, end:end}), "mystring");
+			},
+
+			"Should return entire string less the last character when called with 0 and length-1": function testLastCharacter(){
+				var s = "mystring",
+				start = 0,
+				end = s.length;
+				assert.strictEqual(Expression.parseOperand({$substr:["$s", "$start", "$end"]}).evaluate({s:s, start:start, end:end-1}), "mystrin");
+			},
+
+			"Should return empty string when 0 and 0 are given as indexes": function test00Indexes(){
+				var s = "mystring",
+				start = 0,
+				end = 0;
+				assert.strictEqual(Expression.parseOperand({$substr:["$s", "$start", "$end"]}).evaluate({s:s, start:start, end:end}), "");
+			},
+
+			"Should first character when 0 and 1 are given as indexes": function testFirstCharacter(){
+				var s = "mystring",
+				start = 0,
+				end = 1;
+				assert.strictEqual(Expression.parseOperand({$substr:["$s", "$start", "$end"]}).evaluate({s:s, start:start, end:end}), "m");
+			},
+
+			"Should return empty string when empty string is given": function testEmptyString(){
+				var s = "",
+				start = 0,
+				end = 0;
+				assert.strictEqual(Expression.parseOperand({$substr:["$s", "$start", "$end"]}).evaluate({s:s, start:start, end:end}), "");
+			},
+
+			"Should fail if end is before begin": function testUnorderedIndexes(){
+				var s = "mystring",
+				start = s.length,
+				end = 0;
+				assert.throws(function(){Expression.parseOperand({$substr:["$s", "$start"]}).evaluate({s:s, start:start, end:end});});
+			},
+
+			"Should fail if end is greater than length": function testIndexTooLarge(){
+				var s = "mystring",
+				start = 0,
+				end = s.length+1;
+				assert.throws(function(){Expression.parseOperand({$substr:["$s", "$start"]}).evaluate({s:s, start:start, end:end});});
+			}
+
+		}
+
+	}
+
+};
+
+if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);