Browse Source

EAGLESIX-2714: all test cases pass

Jake Delaney 11 years ago
parent
commit
106e06a330

+ 25 - 0
lib/pipeline/expressions/Helpers.js

@@ -0,0 +1,25 @@
+module.exports = {
+	//Returns an object containing unique values. All keys are the same as the corresponding value.
+	arrayToSet : function arrayToSet(array){
+
+		var set = {};
+
+		// This ensures no duplicates.
+		array.forEach(function (element) {
+			var elementString = JSON.stringify(element);
+			set[elementString] = element;
+		});
+
+		return set;
+	},
+
+	setToArray: function setToArray(set){
+		var array = [];
+
+		Object.keys(set).forEach(function (key) {
+			array.push(set[key]);
+		});
+
+		return array;
+	}
+}

+ 26 - 11
lib/pipeline/expressions/SetEqualsExpression.js

@@ -9,7 +9,9 @@
  * @constructor
  **/
 var SetEqualsExpression = module.exports = function SetEqualsExpression() {
+
 	if (arguments.length !== 0) throw new Error("Zero arguments expected. Got " + arguments.length);
+
 	this.nargs = 2;
 
 	base.call(this);
@@ -17,28 +19,41 @@ var SetEqualsExpression = module.exports = function SetEqualsExpression() {
 
 // DEPENDENCIES
 var Value = require("../Value"),
-	Expression = require("./Expression");
+	Expression = require("./Expression"),
+	Helpers = require("./Helpers");
 
 // PROTOTYPE MEMBERS
 proto.getOpName = function getOpName() {
-	return "$setequals";
+	return "$setEquals";
 };
 
 proto.validateArguments = function validateArguments(args) {
-	if (arguments.length < 2) throw new Error("Two or more arguments requird. Got " + arguments.length);
+	if (args.length < 2) throw new Error("Two or more arguments requird. Got " + arguments.length);
 };
 /**
- * Takes 2 arrays. Assigns the second array to the first array.
+ * Takes arrays. Returns true if the arrays have the same values (after duplicates are removed). Returns false otherwise.
  * @method evaluateInternal
  **/
 proto.evaluateInternal = function evaluateInternal(vars) {
-	var array1 = this.operands[0].evaluateInternal(vars),
-		array2 = this.operands[1].evaluateInternal(vars);
-	if (!array1 instanceof Array) throw new Error(this.getOpName() + ": object 1 must be an array. Got a(n) " + typeof array1);
-	if (!array2 instanceof Array) throw new Error(this.getOpName() + ": object 2 must be an array. Got a(n) " + typeof array1);
-	array1 = array2;
-	return array1;
+	var n = this.operands.length,
+		lhs = [];
+
+	for (var i = 0; i < n; i++){
+		var nextEntry = this.operands[i].evaluateInternal(vars);
+
+		if(!(nextEntry instanceof Array)) throw new Error("All operands of " + this.getOpName() +" must be arrays. One argument is of type: " + typeof nextEntry);
+
+		if(i == 0){
+			lhs = Helpers.arrayToSet(nextEntry);
+		} else {
+			var rhs = Helpers.arrayToSet(nextEntry);
+			if (JSON.stringify(lhs) !== JSON.stringify(rhs)){
+				return false;
+			}
+		}
+	}
+	return true;
 };
 
 /** Register Expression */
-Expression.registerExpression("$setequals", base.parse);
+Expression.registerExpression("$setEquals", base.parse);

+ 103 - 75
test/lib/pipeline/expressions/SetEqualsExpression.js

@@ -1,87 +1,115 @@
 "use strict";
 var assert = require("assert"),
-		SetEqualsExpression = require("../../../../lib/pipeline/expressions/SetEqualsExpression"),
-		Expression = require("../../../../lib/pipeline/expressions/Expression");
+	VariablesIdGenerator = require("../../../../lib/pipeline/expressions/VariablesIdGenerator"),
+	VariablesParseState = require("../../../../lib/pipeline/expressions/VariablesParseState"),
+	SetEqualsExpression = require("../../../../lib/pipeline/expressions/SetEqualsExpression"),
+	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 = {
 
-		"SetEqualsExpression": {
-
-				"constructor()": {
-
-						"should throw Error when constructing without args": function testConstructor() {
-								assert.throws(function() {
-										new SetEqualsExpression();
-								});
-						}
-
-				},
-
-				"#getOpName()": {
-
-						"should return the correct op name; $setequals": function testOpName() {
-								assert.equal(new SetEqualsExpression([1,2,3],[4,5,6]).getOpName(), "$setequals");
-						}
-
-				},
-
-				"#evaluateInternal()": {
-
-						"Should fail if array1 is not an array": function testArg1() {
-								var array1 = "not an array",
-										array2 = [6, 7, 8, 9];
-								assert.throws(function() {
-										Expression.parseOperand({
-												$setequals: ["$array1", "$array2"]
-										}).evaluateInternal({
-												array1: array1,
-												array2: array2
-										});
-								});
-						},
-
-						"Should fail if array2 is not an array": function testArg2() {
-								var array1 = [1, 2, 3, 4],
-										array2 = "not an array";
-								assert.throws(function() {
-										Expression.parseOperand({
-												$setequals: ["$array1", "$array2"]
-										}).evaluateInternal({
-												array1: array1,
-												array2: array2
-										});
-								});
-						},
-
-						"Should fail if both are not an array": function testArg1andArg2() {
-								var array1 = "not an array",
-										array2 = "not an array";
-								assert.throws(function() {
-										Expression.parseOperand({
-												$setequals: ["$array1", "$array2"]
-										}).evaluateInternal({
-												array1: array1,
-												array2: array2
-										});
-								});
-						},
-
-						"Should pass and array1 should equal array2": function testBasicAssignment() {
-								var array1 = [1, 2, 3, 4],
-										array2 = [6, 7, 8, 9];
-								assert.strictEqual(Expression.parseOperand({
-										$setequals: ["$array1", "$array2"]
-								}).evaluateInternal({
-										array1: array1,
-										array2: array2
-								}), [6, 7, 8, 9]);
-						},
-
-				}
+	"SetEqualsExpression": {
+
+		"constructor()": {
+
+			"should not throw Error when constructing without args": function testConstructor() {
+				assert.doesNotThrow(function() {
+					new SetEqualsExpression();
+				});
+			},
+
+			"should throw Error when constructing with args": function testConstructor() {
+				assert.throws(function() {
+						new SetEqualsExpression("someArg");
+				});
+			}
+
+		},
+
+		"#getOpName()": {
+
+			"should return the correct op name; $setEquals": function testOpName() {
+				assert.equal(new SetEqualsExpression().getOpName(), "$setEquals");
+			}
+
+		},
+
+		"#evaluateInternal()": {
+
+			"Should fail if array1 is not an array": function testArg1() {
+				var array1 = "not an array",
+					array2 = [6, 7, 8, 9],
+					input = [array1,array2],
+					idGenerator = new VariablesIdGenerator(),
+					vps = new VariablesParseState(idGenerator),
+					expr = Expression.parseExpression("$setEquals", input, vps);
+				assert.throws(function() {
+						expr.evaluate({});
+				});
+			},
+
+			"Should fail if array2 is not an array": function testArg2() {
+				var array1 = [1, 2, 3, 4],
+					array2 = "not an array",
+					input = [array1,array2],
+					idGenerator = new VariablesIdGenerator(),
+					vps = new VariablesParseState(idGenerator),
+					expr = Expression.parseExpression("$setEquals", input, vps);
+				assert.throws(function() {
+						expr.evaluate({});
+				});
+			},
+
+			"Should fail if both are not an array": function testArg1andArg2() {
+				var array1 = "not an array",
+					array2 = "not an array",
+					input = [array1,array2],
+					idGenerator = new VariablesIdGenerator(),
+					vps = new VariablesParseState(idGenerator),
+					expr = Expression.parseExpression("$setEquals", input, vps);
+				assert.throws(function() {
+						expr.evaluate({});
+				});
+			},
+
+			"Should pass and array1 should equal array2": function testBasicAssignment(){
+				var array1 = [1, 2, 3, 5, 4],
+					array2 = [1, 3, 2, 4, 5],
+					input = [array1,array2],
+					idGenerator = new VariablesIdGenerator(),
+					vps = new VariablesParseState(idGenerator),
+					expr = Expression.parseExpression("$setEquals", input, vps),
+					result = expr.evaluate({}),
+					expected = true,
+					msg = errMsg("$setEquals", input, expr.serialize(false), expected, result);
+				assert.equal(result, expected, msg);
+			},
+
+			"Should pass and array1 should not equal array2": function testBasicAssignment(){
+				var array1 = [1, 2, 3, 4],
+					array2 = [1, 3, 2, 4, 5],
+					input = [array1,array2],
+					idGenerator = new VariablesIdGenerator(),
+					vps = new VariablesParseState(idGenerator),
+					expr = Expression.parseExpression("$setEquals", input, vps),
+					result = expr.evaluate({}),
+					expected = false,
+					msg = errMsg("$setEquals", input, expr.serialize(false), expected, result);
+				assert.equal(result, expected, msg);
+			},
 
 		}
 
+	}
+
 };
 
 if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);