瀏覽代碼

EAGLESIX2716: further changes

Jake Delaney 11 年之前
父節點
當前提交
e80de50af1
共有 2 個文件被更改,包括 44 次插入30 次删除
  1. 33 30
      lib/pipeline/expressions/SetIsSubsetExpression.js
  2. 11 0
      test/lib/pipeline/expressions/SetIsSubsetExpression.js

+ 33 - 30
lib/pipeline/expressions/SetIsSubsetExpression.js

@@ -10,7 +10,7 @@
  **/
 
 var SetIsSubsetExpression = module.exports = function SetIsSubsetExpression() {
-	if (arguments.length !== 2) throw new Error("two args expected");
+//	if (arguments.length !== 2) throw new Error("two args expected");
 	base.call(this);
 }, klass = SetIsSubsetExpression,
 	FixedArityExpression = require("./FixedArityExpressionT")(klass, 2),
@@ -22,9 +22,19 @@ var SetIsSubsetExpression = module.exports = function SetIsSubsetExpression() {
 	});
 
 
+// DEPENDENCIES
+var Value = require("../Value"),
+	Expression = require("./Expression"),
+	Helpers = require("./Helpers");
+
+// PROTOTYPE MEMBERS
+proto.getOpName = function getOpName() {
+	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);
 	// do not shortcircuit when lhs.size() > rhs.size()
@@ -38,6 +48,22 @@ var setIsSubsetHelper = function setIsSubsetHelper(lhs, rhs){
 	return true;
 };
 
+/**
+ * Takes 2 arrays. Returns true if the first is a subset of the second. Returns false otherwise.
+ * @method evaluateInternal
+ **/
+proto.evaluateInternal = function evaluateInternal(vars) {
+	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);
+
+	return setIsSubsetHelper(lhs, Helpers.arrayToSet(rhs));
+};
+
+
+
 var Optimized = function Optimized(cachedRhsSet, operands) {
 	this.operands = operands;
 	this._cachedRhsSet = cachedRhsSet;
@@ -52,34 +78,24 @@ Optimized.prototype = Object.create(SetIsSubsetExpression.prototype, {
 Optimized.prototype.evaluateInternal = function evaluateInternal(vars){
 	lhs = this.operands[0].evaluateInternal(vars);
 
-	if (!(lhs instanceof Array)) throw new Error("uassert 17046: both operands of " + this.getOpName() + "  must be arrays. Second argument is of type " + typeof lhs);
+	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);
 };
 
-// DEPENDENCIES
-var Value = require("../Value"),
-	Expression = require("./Expression"),
-	Helpers = require("./Helpers");
-
-
-// PROTOTYPE MEMBERS
-proto.getOpName = function getOpName() {
-	return "$setissubset";
-};
 
 proto.optimize = function optimize(cachedRhsSet, operands) {
-
 	// perform basic optimizations
-	var optimized = base.optimize.call(this);
+	//var optimized = base.optimize.call(this);
+	var optimized = NaryExpression.optimize();
 
 	// if NaryExpression.optimize() created a new value, return it directly
 	if(optimized != this){
 		return optimized;
 	}
 
-	if (operands[1] instanceof ConstantExpression){
-		var ce = operands[1],
+	if (this.operands[1] instanceof ConstantExpression){
+		var ce = this.operands[1],
 			rhs = ce.getValue();
 
 		if (!(rhs instanceof Array)) throw new Error("uassert 17311: both operands of " + this.getOpName() + "  must be arrays. Second argument is of type " + typeof rhs);
@@ -91,18 +107,5 @@ proto.optimize = function optimize(cachedRhsSet, operands) {
 
 };
 
-/**
- * Takes 2 arrays. Returns true if the first is a subset of the second. Returns false otherwise.
- * @method evaluateInternal
- **/
-proto.evaluateInternal = function evaluateInternal(vars) {
-	var lhs = this.operands[0].evaluateInternal(vars),
-		rhs = this.operands[1].evaluateInternal(vars);
-	if (!(lhs instanceof Array)) throw new Error(this.getOpName() + ": object 1 must be an array. Got a(n) " + typeof lhs);
-	if (!(rhs instanceof Array)) throw new Error(this.getOpName() + ": object 2 must be an array. Got a(n) " + typeof rhs);
-
-	return setIsSubsetHelper(lhs, Helpers.arrayToSet(rhs));
-};
-
 /** Register Expression */
 Expression.registerExpression("$setissubset", base.parse);

+ 11 - 0
test/lib/pipeline/expressions/SetIsSubsetExpression.js

@@ -78,6 +78,17 @@ module.exports = {
 								}), 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),
+								result = expr.evaluate({}),
+								expected = true,
+								msg = errMsg("$allElementsTrue", 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];