Browse Source

Merge pull request #76 from RiveraGroup/feature/mongo_2.6.5_expressions_ToLower

Feature/mongo 2.6.5 expressions ToLower
Kyle P Davis 11 năm trước cách đây
mục cha
commit
670b657c31

+ 9 - 17
lib/pipeline/expressions/ToLowerExpression.js

@@ -2,35 +2,27 @@
 
 /**
  * A $toLower pipeline expression.
- * @see evaluateInternal
  * @class ToLowerExpression
  * @namespace mungedb-aggregate.pipeline.expressions
  * @module mungedb-aggregate
  * @constructor
- **/
+ */
 var ToLowerExpression = module.exports = function ToLowerExpression(){
+	if (arguments.length !== 0) throw new Error(klass.name + ": args expected: value");
 	base.call(this);
 }, klass = ToLowerExpression, base = require("./FixedArityExpressionT")(ToLowerExpression, 1), proto = klass.prototype = Object.create(base.prototype, {constructor: {value: klass}});
 
-// DEPENDENCIES
 var Value = require("../Value"),
 	Expression = require("./Expression");
 
-klass.opName = "$toLower";
-
-// PROTOTYPE MEMBERS
-proto.getOpName = function getOpName(){
-	return klass.opName;
-};
-
-/**
-* Takes a single string and converts that string to lowercase, returning the result. All uppercase letters become lowercase.
-**/
 proto.evaluateInternal = function evaluateInternal(vars) {
-	var val = this.operands[0].evaluateInternal(vars),
-		str = Value.coerceToString(val);
+	var pString = this.operands[0].evaluateInternal(vars),
+		str = Value.coerceToString(pString);
 	return str.toLowerCase();
 };
 
-/** Register Expression */
-Expression.registerExpression(klass.opName, base.parse);
+Expression.registerExpression("$toLower", base.parse);
+
+proto.getOpName = function getOpName(){
+	return "$toLower";
+};

+ 74 - 46
test/lib/pipeline/expressions/ToLowerExpression_test.js

@@ -1,64 +1,92 @@
 "use strict";
 var assert = require("assert"),
-		ToLowerExpression = require("../../../../lib/pipeline/expressions/ToLowerExpression"),
-		Expression = require("../../../../lib/pipeline/expressions/Expression");
+	ToLowerExpression = require("../../../../lib/pipeline/expressions/ToLowerExpression"),
+	VariablesParseState = require("../../../../lib/pipeline/expressions/VariablesParseState"),
+	VariablesIdGenerator = require("../../../../lib/pipeline/expressions/VariablesIdGenerator"),
+	Expression = require("../../../../lib/pipeline/expressions/Expression"),
+	utils = require("./utils"),
+	constify = utils.constify,
+	expressionToJson = utils.expressionToJson;
 
+// Mocha one-liner to make these tests self-hosted
+if(!module.parent)return(require.cache[__filename]=null,(new(require("mocha"))({ui:"exports",reporter:"spec",grep:process.env.TEST_GREP})).addFile(__filename).run(process.exit));
 
-module.exports = {
+var TestBase = function TestBase(overrides) {
+		//NOTE: DEVIATION FROM MONGO: using this base class to make things easier to initialize
+		for (var key in overrides)
+			this[key] = overrides[key];
+	},
+	ExpectedResultBase = (function() {
+		var klass = function ExpectedResultBase() {
+			base.apply(this, arguments);
+		}, base = TestBase, proto = klass.prototype = Object.create(base.prototype);
+		proto.run = function(){
+			var specElement = this.spec(),
+				idGenerator = new VariablesIdGenerator(),
+				vps = new VariablesParseState(idGenerator),
+				expr = Expression.parseOperand(specElement, vps);
+			assert.deepEqual(constify(specElement), expressionToJson(expr));
+			assert.strictEqual(this.expectedResult, expr.evaluate({}));
+		};
+		proto.spec = function() {
+			return {$toLower:[this.str]};
+		};
+		return klass;
+	})();
 
-		"ToLowerExpression": {
+exports.ToLowerExpression = {
 
-				"constructor()": {
+	"constructor()": {
 
-						"should not throw Error when constructing without args": function testConstructor() {
-								assert.doesNotThrow(function() {
-										new ToLowerExpression();
-								});
-						},
+		"should construct instance": function() {
+			assert(new ToLowerExpression() instanceof ToLowerExpression);
+			assert(new ToLowerExpression() instanceof Expression);
+		},
 
-					"should throw Error when constructing with args": function testConstructor(){
-						assert.throws(function(){
-							new ToLowerExpression(1);
-						});
-					}
+		"should error if given args": function() {
+			assert.throws(function() {
+				new ToLowerExpression("bad stuff");
+			});
+		},
 
-				},
+	},
 
-				"#getOpName()": {
+	"#getOpName()": {
 
-						"should return the correct op name; $toLower": function testOpName() {
-								assert.equal(new ToLowerExpression().getOpName(), "$toLower");
-						}
+		"should return the correct op name; $toLower": function() {
+			assert.equal(new ToLowerExpression().getOpName(), "$toLower");
+		},
 
-				},
+	},
 
-				"#evaluate()": {
+	"#evaluate()": {
 
-						"should return the lowercase version of the string if there is a null character in the middle of the string": function testStuff() {
-								assert.strictEqual(Expression.parseOperand({
-										$toLower: "$a"
-								}).evaluate({
-										a: "a\0B"
-								}), "a\0b");
-						},
-						"should return the lowercase version of the string if there is a null character at the beginning of the string": function testStuff() {
-								assert.strictEqual(Expression.parseOperand({
-										$toLower: "$a"
-								}).evaluate({
-										a: "\0aB"
-								}), "\0ab");
-						},
-						"should return the lowercase version of the string if there is a null character at the end of the string": function testStuff() {
-								assert.strictEqual(Expression.parseOperand({
-										$toLower: "$a"
-								}).evaluate({
-										a: "aB\0"
-								}), "ab\0");
-						}
-				}
+		"should return the lowercase version of the string if there is a null character at the beginning of the string": function NullBegin() {
+			/** String beginning with a null character. */
+			new ExpectedResultBase({
+				str: "\0aB",
+				expectedResult: "\0ab",
+			}).run();
+		},
 
-		}
+		"should return the lowercase version of the string if there is a null character in the middle of the string": function NullMiddle() {
+			/** String containing a null character. */
+			new ExpectedResultBase({
+				str: "a\0B",
+				expectedResult: "a\0b",
+			}).run();
+		},
+
+		"should return the lowercase version of the string if there is a null character at the end of the string": function NullEnd() {
+			/** String ending with a null character. */
+			new ExpectedResultBase({
+				str: "aB\0",
+				expectedResult: "ab\0",
+			}).run();
+		},
+
+	},
 
 };
 
-if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);
+if (!module.parent)(new (require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);