فهرست منبع

EAGLESIX-2716 test cases complete. Ready for pull request.

Jake Delaney 11 سال پیش
والد
کامیت
a6d76c228a
2فایلهای تغییر یافته به همراه58 افزوده شده و 43 حذف شده
  1. 17 12
      lib/pipeline/expressions/SetIsSubsetExpression.js
  2. 41 31
      test/lib/pipeline/expressions/SetIsSubsetExpression.js

+ 17 - 12
lib/pipeline/expressions/SetIsSubsetExpression.js

@@ -10,7 +10,9 @@
  **/
 
 var SetIsSubsetExpression = module.exports = function SetIsSubsetExpression() {
-//	if (arguments.length !== 2) throw new Error("two args expected");
+
+	if (arguments.length != 0) throw new Error("SetIsSubsetExpression constructor must be called with no args");
+
 	base.call(this);
 }, klass = SetIsSubsetExpression,
 	FixedArityExpression = require("./FixedArityExpressionT")(klass, 2),
@@ -29,21 +31,22 @@ var Value = require("../Value"),
 
 // PROTOTYPE MEMBERS
 proto.getOpName = function getOpName() {
-	return "$setissubset";
+	return "$setIsSubset";
 };
 
 
 // lhs should be array, rhs should be set (object). See arrayToSet implementation.
 var setIsSubsetHelper = function setIsSubsetHelper(lhs, rhs){
-	var lset = Helpers.arrayToSet(lhs);
-		rkeys = Object.keys(rhs);
+
+	var rkeys = Object.keys(rhs);
+
 	// do not shortcircuit when lhs.size() > rhs.size()
 	// because lhs can have redundant entries
-	Object.keys(lset).forEach(function (lkey){
-		if (rkeys.indexOf(lkey) < 0){
-			return false;
+	for (var i = 0; i < lhs.length; i++){
+		if (rkeys.indexOf(JSON.stringify(lhs[i])) < 0) {
+			return false
 		}
-	});
+	}
 
 	return true;
 };
@@ -53,13 +56,15 @@ var setIsSubsetHelper = function setIsSubsetHelper(lhs, rhs){
  * @method evaluateInternal
  **/
 proto.evaluateInternal = function evaluateInternal(vars) {
+	if (this.operands.length !== 2) throw new Error("two args expected");
 	var lhs = this.operands[0].evaluateInternal(vars),
 		rhs = this.operands[1].evaluateInternal(vars);
 
-	if (!(lhs instanceof Array)) throw new Error("Both operands of " + this.getOpName() + ": be arrays. First argument is of type " + typeof lhs);
-	if (!(rhs instanceof Array)) throw new Error("Both operands of " + this.getOpName() + ": be arrays. First argument is of type " + typeof rhs);
+	if (!(lhs instanceof Array)) throw new Error("Both operands of " + this.getOpName() + ": must be arrays. First argument is of type " + typeof lhs);
+	if (!(rhs instanceof Array)) throw new Error("Both operands of " + this.getOpName() + ": must be arrays. First argument is of type " + typeof rhs);
 
 	return setIsSubsetHelper(lhs, Helpers.arrayToSet(rhs));
+
 };
 
 
@@ -79,7 +84,7 @@ Optimized.prototype.evaluateInternal = function evaluateInternal(vars){
 	lhs = this.operands[0].evaluateInternal(vars);
 
 	if (!(lhs instanceof Array)) throw new Error("uassert 17310: both operands of " + this.getOpName() + "  must be arrays. First argument is of type " + typeof lhs);
-	
+
 	return setIsSubsetHelper(lhs, this._cachedRhsSet);
 };
 
@@ -108,4 +113,4 @@ proto.optimize = function optimize(cachedRhsSet, operands) {
 };
 
 /** Register Expression */
-Expression.registerExpression("$setissubset", base.parse);
+Expression.registerExpression("$setIsSubset", base.parse);

+ 41 - 31
test/lib/pipeline/expressions/SetIsSubsetExpression.js

@@ -1,27 +1,43 @@
 "use strict";
 var assert = require("assert"),
+		VariablesIdGenerator = require("../../../../lib/pipeline/expressions/VariablesIdGenerator"),
+		VariablesParseState = require("../../../../lib/pipeline/expressions/VariablesParseState"),
 		SetIsSubsetExpression = require("../../../../lib/pipeline/expressions/SetIsSubsetExpression"),
 		Expression = require("../../../../lib/pipeline/expressions/Expression");
 
 
+function errMsg(expr, args, tree, expected, result) {
+	return 	"for expression " + expr +
+			" with argument " + args +
+			" full tree: " + JSON.stringify(tree) +
+			" expected: " + expected +
+			" result: " + result;
+}
+
 module.exports = {
 
 		"SetIsSubsetExpression": {
 
 				"constructor()": {
 
-						"should throw Error when constructing without args": function testConstructor() {
-								assert.throws(function() {
+						"should not throw Error when constructing without args": function testConstructor() {
+								assert.doesNotThrow(function() {
 										new SetIsSubsetExpression();
 								});
+						},
+
+						"should throw Error when constructing with args": function testConstructor() {
+								assert.throws(function() {
+										new SetIsSubsetExpression("someArg");
+								});
 						}
 
 				},
 
 				"#getOpName()": {
 
-						"should return the correct op name; $setissubset": function testOpName() {
-								assert.equal(new SetIsSubsetExpression([1,2,3],[4,5,6]).getOpName(), "$setissubset");
+						"should return the correct op name; $setIsSubset": function testOpName() {
+								assert.equal(new SetIsSubsetExpression().getOpName(), "$setIsSubset");
 						}
 
 				},
@@ -33,7 +49,7 @@ module.exports = {
 										array2 = [6, 7, 8, 9];
 								assert.throws(function() {
 										Expression.parseOperand({
-												$setissubset: ["$array1", "$array2"]
+												$setIsSubset: ["$array1", "$array2"]
 										}).evaluateInternal({
 												array1: array1,
 												array2: array2
@@ -46,7 +62,7 @@ module.exports = {
 										array2 = "not an array";
 								assert.throws(function() {
 										Expression.parseOperand({
-												$setissubset: ["$array1", "$array2"]
+												$setIsSubset: ["$array1", "$array2"]
 										}).evaluateInternal({
 												array1: array1,
 												array2: array2
@@ -59,7 +75,7 @@ module.exports = {
 										array2 = "not an array";
 								assert.throws(function() {
 										Expression.parseOperand({
-												$setissubset: ["$array1", "$array2"]
+												$setIsSubset: ["$array1", "$array2"]
 										}).evaluateInternal({
 												array1: array1,
 												array2: array2
@@ -67,37 +83,31 @@ module.exports = {
 								});
 						},
 
-						"Should pass and return a true": function testBasicAssignment() {
-								var array1 = [1, 2, 3, 4, 5],
-										array2 = [2,3];
-								assert.strictEqual(Expression.parseOperand({
-										$setissubset: ["$array1", "$array2"]
-								}).evaluateInternal({
-										array1: array1,
-										array2: array2
-								}), true);
-						},
 
-						"Should pass and return a true2": function testBasicAssignment(){
-							var array1 = [1, 2, 3, 4, 5],
-								array2 = [2,3],
-								input = ["$array1","$array2"],
-							 	expr = Expression.parseExpression("$setissubset", input),
+						"Should pass and return a true": function testBasicAssignment(){
+							var array1 = [2,3],
+								array2 = [1, 2, 3, 4, 5],
+								input = [array1,array2],
+								idGenerator = new VariablesIdGenerator(),
+								vps = new VariablesParseState(idGenerator),
+								expr = Expression.parseExpression("$setIsSubset", input, vps),
 								result = expr.evaluate({}),
 								expected = true,
-								msg = errMsg("$allElementsTrue", input, expr.serialize(false), expected, result);
+								msg = errMsg("$setIsSubset", input, expr.serialize(false), expected, result);
 							assert.equal(result, expected, msg);
 						},
 
 						"Should pass and return false": function testBasicAssignment() {
-								var array1 = [1, 2, 3, 4, 5],
-										array2 = [7, 8, 9];
-								assert.strictEqual(Expression.parseOperand({
-										$setissubset: ["$array1", "$array2"]
-								}).evaluateInternal({
-										array1: array1,
-										array2: array2
-								}), true);
+							var array1 = [1, 2, 3, 4, 5],
+								array2 = [7, 8, 9],
+								input = [array1, array2],
+								idGenerator = new VariablesIdGenerator(),
+								vps = new VariablesParseState(idGenerator),
+								expr = Expression.parseExpression("$setIsSubset", input, vps),
+								result = expr.evaluate({}),
+								expected = false,
+								msg = errMsg("$setIsSubset", input, expr.serialize(false), expected, result);
+							assert.equal(result, expected, msg);
 						},
 
 				}