Browse Source

EAGLESIX-2696 First pass at test cases and minor code changes for ConstantExpression.

Tony Ennis 11 years ago
parent
commit
b9eab995fa

+ 4 - 4
lib/pipeline/expressions/ConstantExpression.js

@@ -11,7 +11,7 @@ var ConstantExpression = module.exports = function ConstantExpression(value){
     if (arguments.length !== 1) throw new Error("args expected: value");
     this.value = value; //TODO: actually make read-only in terms of JS?
     base.call(this);
-}, klass = ConstantExpression, base = require("./Expression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
+}, klass = ConstantExpression, base = require("./FixedArityExpressionT")(klass,1), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
 
 // DEPENDENCIES
@@ -37,7 +37,7 @@ proto.addDependencies = function addDependencies(deps, path) {
 };
 
 klass.parse = function parse(expr, vps){
-    return new ConstantExpression(expr);
+    return new ConstantExpression(expr); //NOTE: DEVIATION FROM MONGO: in the c++ code, it uses "new ExpressionConstant(Value(exprElement))"
 };
 
 /**
@@ -60,5 +60,5 @@ proto.serialize = function(rawValue){
 //TODO: proto.addToBsonArray
 
 /** Register Expression */
-Expression.registerExpression("$const",klass.parse(ConstantExpression));
-Expression.registerExpression("$literal", klass.parse(ConstantExpression)); // alias
+Expression.registerExpression("$const", klass.parse);
+Expression.registerExpression("$literal", klass.parse); // alias

+ 0 - 55
test/lib/pipeline/expressions/ConstantExpression.js

@@ -1,55 +0,0 @@
-"use strict";
-var assert = require("assert"),
-	ConstantExpression = require("../../../../lib/pipeline/expressions/ConstantExpression");
-
-
-module.exports = {
-
-	"ConstantExpression": {
-
-		"constructor() / #evaluate": {
-
-			"should be able to construct from a value type": function testCreate(){
-				assert.strictEqual(new ConstantExpression(5).evaluateInternal({}), 5);
-			}
-
-			//TODO: CreateFromBsonElement ? ?? ???
-
-		},
-
-// TODO: the constructor() tests this so not really needed here
-//		"#evaluate()": {
-//		},
-
-		"#optimize()": {
-
-			"should not optimize anything": function testOptimize(){
-				var expr = new ConstantExpression(5);
-				assert.strictEqual(expr, expr.optimize());
-			}
-
-		},
-
-		"#addDependencies()": {
-
-			"should return nothing": function testDependencies(){
-				assert.strictEqual(new ConstantExpression(5).addDependencies(), undefined);
-			}
-
-		},
-
-		"#toJSON()": {
-
-			"should output proper JSON": function testJson(){
-				var expr = new ConstantExpression(5);
-				assert.strictEqual(expr.serialize(), 5);
-				assert.deepEqual(expr.serialize(true), {$const:5});
-			}
-
-		}
-
-	}
-
-};
-
-if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);

+ 68 - 0
test/lib/pipeline/expressions/ConstantExpression_test.js

@@ -0,0 +1,68 @@
+"use strict";
+var assert = require("assert"),
+	ConstantExpression = require("../../../../lib/pipeline/expressions/ConstantExpression");
+
+
+module.exports = {
+
+	"ConstantExpression": {
+
+		"constructor()": {
+
+			"should accept one argument": function () {
+				new ConstantExpression(5);
+			},
+			"should not accept 0 arguments": function () {
+				assert.throws(new function () {
+					new ConstantExpression();
+				});
+			},
+			"should not accept 2 arguments": function () {
+				assert.throws(new function () {
+					new ConstantExpression(1, 2);
+				});
+			}
+		},
+
+
+		"#evaluate()": {
+			"should do what comes natural with an int": function () {
+				var c = 567;
+				var expr = new ConstantExpression(c);
+				assert.deepEqual(expr.evaluate(), c);
+			},
+			"should do what comes natural with a float": function () {
+				var c = 567.123;
+				var expr = new ConstantExpression(c);
+				assert.deepEqual(expr.evaluate(), c);
+			},
+			"should do what comes natural with a String": function () {
+				var c = "Quoth the raven";
+				var expr = new ConstantExpression(c);
+				assert.deepEqual(expr.evaluate(), c);
+			},
+			"should do what comes natural with a date": function () {
+				var c = new Date();
+				var expr = new ConstantExpression(c);
+				assert.deepEqual(expr.evaluate(), c);
+			}
+		},
+
+		"#optimize()": {
+			"should not optimize anything": function testOptimize() {
+				var expr = new ConstantExpression(5);
+				assert.strictEqual(expr, expr.optimize());
+			}
+
+		},
+
+		"#addDependencies()": {
+			"should return nothing": function testDependencies() {
+				assert.strictEqual(new ConstantExpression(5).addDependencies(), undefined);
+			}
+
+		}
+	}
+};
+
+if (!module.parent)(new (require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);