Browse Source

EAGLESIX-2651: ToUpper: better sync w/ 2.6.5 tests, minor formatting to code

Kyle P Davis 11 years ago
parent
commit
b4c9d99836

+ 8 - 17
lib/pipeline/expressions/ToUpperExpression.js

@@ -2,36 +2,27 @@
 
 /**
  * A $toUpper pipeline expression.
- * @see evaluateInternal
  * @class ToUpperExpression
  * @namespace mungedb-aggregate.pipeline.expressions
  * @module mungedb-aggregate
  * @constructor
- **/
+ */
 var ToUpperExpression = module.exports = function ToUpperExpression() {
 	if (arguments.length !== 0) throw new Error(klass.name + ": args expected: value");
 	base.call(this);
 }, klass = ToUpperExpression, base = require("./FixedArityExpressionT")(ToUpperExpression, 1), proto = klass.prototype = Object.create(base.prototype, {constructor: {value: klass }});
 
-// DEPENDENCIES
 var Value = require("../Value"),
 	Expression = require("./Expression");
 
-klass.opName = "$toUpper";
-
-// 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.toUpperCase();
 };
 
-/** Register Expression */
-Expression.registerExpression(klass.opName, base.parse);
+Expression.registerExpression("$toUpper", base.parse);
+
+proto.getOpName = function getOpName() {
+	return "$toUpper";
+};

+ 72 - 46
test/lib/pipeline/expressions/ToUpperExpression_test.js

@@ -3,64 +3,90 @@ var assert = require("assert"),
 	ToUpperExpression = require("../../../../lib/pipeline/expressions/ToUpperExpression"),
 	VariablesParseState = require("../../../../lib/pipeline/expressions/VariablesParseState"),
 	VariablesIdGenerator = require("../../../../lib/pipeline/expressions/VariablesIdGenerator"),
-	Expression = require("../../../../lib/pipeline/expressions/Expression");
+	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 {$toUpper:[this.str]};
+		};
+		return klass;
+	})();
 
-	"ToUpperExpression": {
-		
-		beforeEach: function(){
-			this.vps = new VariablesParseState(new VariablesIdGenerator());
+exports.ToUpperExpression = {
+
+	"constructor()": {
+
+		"should construct instance": function() {
+			assert(new ToUpperExpression() instanceof ToUpperExpression);
+			assert(new ToUpperExpression() instanceof Expression);
 		},
 
-		"constructor()": {
+		"should error if given args": function() {
+			assert.throws(function() {
+				new ToUpperExpression("bad stuff");
+			});
+		},
 
-			"should not throw Error when constructing without args": function testConstructor() {
-				assert.doesNotThrow(function () {
-					new ToUpperExpression();
-				});
-			},
+	},
 
-			"should throw Error when constructing with args": function testConstructor() {
-				assert.throws(function () {
-					new ToUpperExpression(1);
-				});
-			}
+	"#getOpName()": {
 
-		},
+		"should return the correct op name; $toUpper": function() {
+			assert.equal(new ToUpperExpression().getOpName(), "$toUpper");
+		}
 
-		"#getOpName()": {
+	},
 
-			"should return the correct op name; $toUpper": function testOpName() {
-				assert.equal(new ToUpperExpression().getOpName(), "$toUpper");
-			}
+	"#evaluate()": {
 
+		"should return the uppercase 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();
 		},
 
-		"#evaluateInternal()": {
-			"should uppercase a string": function(){
-				assert.strictEqual(Expression.parseOperand({$toUpper: "$a"}, this.vps).evaluate({a: "now is the time"}), "NOW IS THE TIME");
-			},
-			"should not change symbols": function(){
-				var symbs = "!@#$%^&*()_+{}[]:\";'<>?/.,;";
-				assert.strictEqual(Expression.parseOperand({$toUpper: "$a"}, this.vps).evaluate({a: symbs}), symbs);
-			},
-			"should not change uppercase": function(){
-				var symbs = "NOW IS THE TIME FOR ALL GOOD MEN TO COME FROM THE AID OF THEIR COMPUTERS";
-				assert.strictEqual(Expression.parseOperand({$toUpper: "$a"}, this.vps).evaluate({a: symbs}), symbs);
-			},
-			"should return the uppercase version of the string if there is a null character in the middle of the string": function testStuff() {
-				assert.strictEqual(Expression.parseOperand({$toUpper: "$a"}, this.vps).evaluate({a: "a\0B"}), "A\0B");
-			},
-			"should return the uppercase version of the string if there is a null character at the beginning of the string": function testStuff() {
-				assert.strictEqual(Expression.parseOperand({$toUpper: "$a"}, this.vps).evaluate({a: "\0aB"}), "\0AB");
-			},
-			"should return the uppercase version of the string if there is a null character at the end of the string": function testStuff() {
-				assert.strictEqual(Expression.parseOperand({$toUpper: "$a"}, this.vps).evaluate({a: "aB\0"}), "AB\0");
-			}
-		}
-	}
+		"should return the uppercase 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 uppercase 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);