Quellcode durchsuchen

ref #3359: Fix more testcases to pass

Brennan Chesley vor 12 Jahren
Ursprung
Commit
fb977d3512

+ 6 - 2
lib/pipeline/matcher/ArrayFilterEntries.js

@@ -90,8 +90,12 @@ proto.addRegex = function addRegex( expr ) { //  RegexMatchExpression* expr
 proto.contains = function contains( elem ) { //  const BSONElement& elem
 // File: expression_leaf.h lines: 249-248
 	for (var i = 0; i < this._equalities.length; i++) {
-		if(Value.compare(elem, this._equalities[i]) === 0) {
-			return true;
+		if(typeof(elem) == typeof(this._equalities[i])){
+			if(Value.compare(elem, this._equalities[i]) === 0) {
+				return true;
+			}
+		} else {
+			return false;
 		}
 	}
 	return false;

+ 1 - 1
lib/pipeline/matcher/InMatchExpression.js

@@ -33,7 +33,7 @@ proto._matchesRealElement = function _matchesRealElement( e ) { //  const BSONEl
 	}
 
 	for (var i = 0; i < this._arrayEntries.numRegexes(); i++) {
-		if(e.match && e.match(this._arrayEntries.regex(i))) {
+		if(e.match && e.match(this._arrayEntries.regex(i)._regex)) {
 			return true;
 		}
 	}

+ 40 - 38
lib/pipeline/matcher/MatchExpressionParser.js

@@ -192,16 +192,16 @@ proto._parseAll = function _parseAll(name, element){
 				// $all : [ { $elemMatch : ... }, 5 ]
 				return {code:ErrorCodes.BAD_VALUE, description:"$all/$elemMatch has to be consistent"};
 			}
-
-			if ("$elemMatch" != hopefullyElemMatchElement.keys()[0]) {
+			
+			if ("$elemMatch" != Object.keys(hopefullyElemMatchElement)[0]) {
 				// $all : [ { $elemMatch : ... }, { x : 5 } ]
 				return {code:ErrorCodes.BAD_VALUE, description:"$all/$elemMatch has to be consistent"};
 			}
 
-			status = this._parseElemMatch("", hopefullyElemMatchElement[hopefullyElemMatchElement.keys()[0]]); // TODO: wrong way to do this?
+			status = this._parseElemMatch("",  hopefullyElemMatchElement['$elemMatch'] ); // TODO: wrong way to do this?
 			if (status.code != ErrorCodes.OK)
 				return status;
-			temp.add(new ArrayMatchingMatchExpression(status.result));
+			temp.add(status.result);
 		}
 
 		return {code:ErrorCodes.OK, result:temp};
@@ -346,20 +346,26 @@ proto._parseElemMatch = function _parseElemMatch(name, element){
  */
 proto._parseMOD = function _parseMOD(name, element){
 	// File: expression_parser.cpp lines: 360-387
-
+	var d,r;
 	if (!(element instanceof Array))
 		return {code:ErrorCodes.BAD_VALUE, result:"malformed mod, needs to be an array"};
 	if (element.length < 2)
 		return {code:ErrorCodes.BAD_VALUE, result:"malformed mod, not enough elements"};
 	if (element.length > 2)
 		return {code:ErrorCodes.BAD_VALUE, result:"malformed mod, too many elements"};
-	if (!(element[0] instanceof Number))
+	if (!(element[0] instanceof Number || typeof(element[0]) == 'number')) {
 		return {code:ErrorCodes.BAD_VALUE, result:"malformed mod, divisor not a number"};
-	if (!(element[1] instanceof Number))
-		return {code:ErrorCodes.BAD_VALUE, result:"malformed mod, remainder not a number"};
+	} else {
+		d = element[0];
+	}
+	if (!(element[1] instanceof Number || typeof(element[1]) == 'number')) {
+		r = 0;
+	} else {
+		r = element[1];
+	}
 
 	var temp = new ModMatchExpression();
-	var status = temp.init( name, element[0], element[1]);
+	var status = temp.init( name, d, r);
 	if (status.code != ErrorCodes.OK)
 		return status;
 
@@ -422,40 +428,36 @@ proto._parseNot = function _parseNot(name, element){
  */
 proto._parseRegexDocument = function _parseRegexDocument(name, doc){
 	// File: expression_parser.cpp lines: 402-442
-	var regex, regexOptions, e;
-
-	for (var i = 0; i < doc.length; i++) {
-		e = doc[i];
-		switch (e.getGtLtOp()) {
-		case 'opREGEX':
-			if (e instanceof String) {
-				regex = e;
-			}
-			else if (e instanceof RegExp) {
-				var str = e.toString(),
-					flagIndex = 0;
-				for (var c = str.length; c > 0; c--){
-					if (str[c] == '/') {
-						flagIndex = c;
-						break;
-					}
+	var regex = '', regexOptions = '', e;
+	debugger;
+	if(doc['$regex']) {
+		if(doc['$regex'] instanceof RegExp) {
+			e = doc['$regex'];
+			var str = e.toString(),
+				flagIndex = 0;
+			for (var c = str.length; c > 0; c--){
+				if (str[c] == '/') {
+					flagIndex = c;
+					break;
 				}
-				regex = str.substr(1, flagIndex-1);
-				regexOptions = str.substr(flagIndex, str.length);
-			}
-			else {
-				return {code:ErrorCodes.BAD_VALUE, description:"$regex has to be a string"};
 			}
+			regex = (flagIndex? str : str.substr(1, flagIndex-1));
+			regexOptions = str.substr(flagIndex, str.length);
+		} else if (e instanceof String) {
+			regex = e;
+		} else {
+			return {code:ErrorCodes.BAD_VALUE, description:"$regex has to be a string"};
+		}
+	}
 
-			break;
-		case 'opOPTIONS':
-			if (!(e instanceof String))
-				return {code:ErrorCodes.BAD_VALUE, description:"$options has to be a string"};
+	if(doc['$options']) {
+		e = doc['$options'];
+		if(typeof(e) == 'string') {
 			regexOptions = e;
-			break;
-		default:
-			break;
+		} else {
+			return {code:ErrorCodes.BAD_VALUE, description:"$options has to be a string"};
 		}
+
 	}
 
 	var temp = new RegexMatchExpression();

+ 1 - 1
lib/pipeline/matcher/ModMatchExpression.js

@@ -102,7 +102,7 @@ proto.init = function init( path,divisor,remainder ) { //  const StringData& pat
  */
 proto.matchesSingleElement = function matchesSingleElement( e ) { //  const BSONElement& e 
 // File: expression_leaf.cpp lines: 247-250
-	if(typeof(e) == 'number') {
+	if(!(typeof(e) == 'number')) {
 		return false;
 	}
 	

+ 2 - 2
lib/pipeline/matcher/RegexMatchExpression.js

@@ -42,7 +42,7 @@ proto._regex = undefined;
  */
 proto.debugString = function debugString( level ) { //  StringBuilder& debug, int level
 // File: expression_leaf.cpp lines: 225-234
-	return this._debugAddSpace( level ) + this.path() + " regex /" + this._regex + "/" + this._flags + (this.getTags() ? ' ' + this.getTags().debugString : '') + "\n";
+	return this._debugAddSpace( level ) + this.path() + " regex /" + this._regex + "/" + this._flags + (this.getTag() ? ' ' + this.getTag().debugString : '') + "\n";
 };
 
 
@@ -94,7 +94,7 @@ proto.getString = function getString( /*  */ ){
  */
 proto.init = function init( path,regex,flags ) { //  const StringData& path, const StringData& regex, const StringData& options
 // File: expression_leaf.cpp lines: 196-205
-	if(regex.length > klass.MaxPatternSize){
+	if(regex.toString().length > klass.MaxPatternSize){
 		return {'code':'BAD_VALUE', 'desc':'Regular Expression too long.'};
 	}
 

+ 5 - 6
test/lib/pipeline/matcher/MatchExpressionParser.js

@@ -99,7 +99,7 @@ module.exports = {
 		"Should parse and match $all:[1,2]" : function() {
 			var parser = new MatchExpressionParser();
 			var q = {'x':{'$all':[1,2]}};
-			debugger;
+			
 			var res = parser.parse( q );
 			assert.strictEqual( res.code,'OK',res.description );
 			assert.ok( ! res.result.matches({'x':1}) );
@@ -119,7 +119,7 @@ module.exports = {
 		"Should not allow large regex patterns": function () {
 			var parser = new MatchExpressionParser();
 			var q = {'x':{'$all':[new RegExp((new Array(50*1000+1)).join('z'))] }};
-
+			
 			var res = parser.parse( q );
 			assert.strictEqual( res.code, 'BAD_VALUE' );	
 		},
@@ -170,7 +170,7 @@ module.exports = {
 		},
 		"Should properly not parse bad $all $elemMatch queries": function() {
 			var parser = new MatchExpressionParser();
-			var q = {'x':{'$all':[{'$elemMatch':{'x':1,'y':2}}]}};
+			var q = {'x':{'$all':[{'$elemMatch':{'x':1,'y':2}}, 5]}};
 
 			var res = parser.parse( q );
 			assert.strictEqual( res.code, 'BAD_VALUE' );
@@ -327,7 +327,7 @@ module.exports = {
 			var a = /^a/;
 			var b = /B/i;
 			var q = {'a': {'$in': [a,b,"2",4]}};
-
+			debugger;
 			var res = parser.parse( q );
 			assert.strictEqual( res.code,'OK',res.description );
 			assert.ok( res.result.matches({'a':'ax'}) );
@@ -450,7 +450,7 @@ module.exports = {
 		"Should parse but not match a type beyond typemax in $type": function() {
 			var parser = new MatchExpressionParser();
 			var q = {'x':{'$type': 1000}};
-			debugger;
+			
 			var res = parser.parse( q );
 			assert.strictEqual( res.code,'OK',res.description );
 			assert.ok( ! res.result.matches({'x':5}) );
@@ -522,7 +522,6 @@ module.exports = {
 			var parser = new MatchExpressionParser();
 			var a = /abc/i;
 			var q = {'x':{'$not': a}};
-			debugger;
 			var res = parser.parse( q );
 			assert.strictEqual( res.code,'OK',res.description );
 			assert.ok( ! res.result.matches({'x':'abc'}) );