Browse Source

Refs #3423. Ported SizeMatchExpression tests and fixed failing tests.

Spencer Rathbun 12 years ago
parent
commit
8e187a5ee3

+ 4 - 1
lib/pipeline/matcher/ArrayMatchingMatchExpression.js

@@ -73,7 +73,7 @@ proto._matches = function _matches(doc, path, details){
 	// File: expression_array.cpp lines: 34-53
 	var element, k, item,
 		curr = doc;
-	for (k in path) {
+	for (k = 0; k < path.length; k++) {
 		item = curr[path[k]];
 		if (item instanceof Object && item.constructor === Object) {
 			curr = item;
@@ -82,6 +82,9 @@ proto._matches = function _matches(doc, path, details){
 			if (k == path.length-1) {
 				curr = item;
 				break; // this is the end of the path, so check this array
+			} else if (!(isNaN(parseInt(path[k+1], 10)))) {
+				curr = item;
+				continue; // the *next* path section is an item in the array so we don't check this whole array
 			}
 			// otherwise, check each item in the array against the rest of the path
 			for(var ii = 0, il = item.length; ii < il; ii++){

+ 11 - 11
lib/pipeline/matcher/SizeMatchExpression.js

@@ -16,20 +16,20 @@ var SizeMatchExpression = module.exports = function SizeMatchExpression(){
 proto._size = undefined;
 
 /**
- * 
+ *
  * This documentation was automatically generated. Please update when you touch this function.
  * @method debugString
  * @param
  *
  */
-proto.debugString = function debugString( level ) { //  StringBuilder& debug, int level 
+proto.debugString = function debugString( level ) { //  StringBuilder& debug, int level
 // File: expression_array.cpp lines: 259-261
-	return this._debugAddSpace( level ) + this.path() + " $size : " + str(this._size) + "\n";
+	return this._debugAddSpace( level ) + this.path() + " $size : " + this._size.toString() + "\n";
 };
 
 
 /**
- * 
+ *
  * This documentation was automatically generated. Please update when you touch this function.
  * @method equivalent
  * @param
@@ -40,12 +40,12 @@ proto.equivalent = function equivalent( other ) { //  const MatchExpression* oth
 	if(other._matchType != 'SIZE') {
 		return false;
 	}
-	return this._size == other._size && this._path == other._size;
+	return this._size == other._size && this._path == other._path;
 };
 
 
 /**
- * 
+ *
  * This documentation was automatically generated. Please update when you touch this function.
  * @method getData
  * @param
@@ -58,7 +58,7 @@ proto.getData = function getData( /*  */ ){
 
 
 /**
- * 
+ *
  * This documentation was automatically generated. Please update when you touch this function.
  * @method init
  * @param
@@ -72,23 +72,23 @@ proto.init = function init( path,size ) { //  const StringData& path, int size
 
 
 /**
- * 
+ *
  * This documentation was automatically generated. Please update when you touch this function.
  * @method matchesArray
  * @param
  *
  */
-proto.matchesArray = function matchesArray( anArray,details ) { //  const BSONObj& anArray, MatchDetails* details
+proto.matchesArray = function matchesArray(anArray, details) { //  const BSONObj& anArray, MatchDetails* details
 // File: expression_array.cpp lines: 253-256
 	if(this._size < 0) {
 		return false;
 	}
-	return anArray.length === this._size;
+	return anArray.length == this._size;
 };
 
 
 /**
- * 
+ *
  * This documentation was automatically generated. Please update when you touch this function.
  * @method shallowClone
  * @param

+ 11 - 11
test/lib/pipeline/matcher/SizeMatchExpression.js

@@ -12,8 +12,8 @@ module.exports = {
 				size = new SizeMatchExpression();
 
 			assert.strictEqual(size.init("", 2).code, 'OK');
-			assert.ok(size.matchesSingleElement(match.firstElement()));
-			assert.ok(!size.matchesSingleElement(notMatch.firstElement()));
+			assert.ok(size.matchesSingleElement(match.a));
+			assert.ok(!size.matchesSingleElement(notMatch.a));
 		},
 
 		"Should not match non array": function() {
@@ -24,18 +24,18 @@ module.exports = {
 				size = new SizeMatchExpression();
 
 			assert.strictEqual(size.init("", 0).code, 'OK');
-			assert.ok(!size.matchesSingleElement(stringValue.firstElement()));
-			assert.ok(!size.matchesSingleElement(numberValue.firstElement()));
-			assert.ok(size.matchesSingleElement(arrayValue.firstElement()));
+			assert.ok(!size.matchesSingleElement(stringValue.a));
+			assert.ok(!size.matchesSingleElement(numberValue.a));
+			assert.ok(size.matchesSingleElement(arrayValue.a));
 		},
 
 		"Should match an array": function() {
 			var size = new SizeMatchExpression();
 
 			assert.strictEqual(size.init("a", 2).code, 'OK');
-			assert.strictEqual(size.matches({"a":[4, 5.5]}, null));
+			assert.ok(size.matches({"a":[4, 5.5]}, null));
 			// Arrays are not unwound to look for matching subarrays.
-			assert.strictEqual(!size.matches({"a":[4, 5.5, [1,2]]}, null));
+			assert.ok(!size.matches({"a":[4, 5.5, [1,2]]}, null));
 		},
 
 		"Should match a nested array": function() {
@@ -43,7 +43,7 @@ module.exports = {
 
 			assert.strictEqual(size.init("a.2", 2).code, 'OK');
 			// A numerically referenced nested array is matched.
-			assert.strictEqual(size.matches({"a":[4, 5.5, [1, 2]]}, null));
+			assert.ok(size.matches({"a":[4, 5.5, [1, 2]]}, null));
 		},
 
 		"ElemMatchKey should return the appropriate results": function() {
@@ -52,11 +52,11 @@ module.exports = {
 
 			assert.strictEqual(size.init("a.b", 3).code, 'OK');
 			details.requestElemMatchKey();
-			assert.strictEqual(!size.matches({"a":1}, details));
+			assert.ok(!size.matches({"a":1}, details));
 			assert.ok(!details.hasElemMatchKey());
-			assert.strictEqual(size.matches({"a":{"b":[1, 2, 3]}}, details));
+			assert.ok(size.matches({"a":{"b":[1, 2, 3]}}, details));
 			assert.ok(!details.hasElemMatchKey());
-			assert.strictEqual(size.matches({"a":[2, {"b":[1, 2, 3]}]}, details));
+			assert.ok(size.matches({"a":[2, {"b":[1, 2, 3]}]}, details));
 			assert.ok(details.hasElemMatchKey());
 			assert.strictEqual("1", details.elemMatchKey());
 		},