Forráskód Böngészése

ref #3424: TypeMatchExpression tests ported

Brennan Chesley 12 éve
szülő
commit
bc6f7bcdb0
2 módosított fájl, 101 hozzáadás és 13 törlés
  1. 8 3
      lib/pipeline/Value.js
  2. 93 10
      test/lib/pipeline/matcher/TypeMatchExpression.js

+ 8 - 3
lib/pipeline/Value.js

@@ -105,9 +105,14 @@ klass.compare = function compare(l, r) {
 		return l < r ? -1 : l > r ? 1 : 0;
 	}
 	// Compare MinKey and MaxKey cases
-	if(l.constructor && l.constructor.name in ['MinKey','MaxKey'] ){
-		if(l.constructor.name == r.constructor.name) { return 0; }
-		return ['MinKey','$Jared','MaxKey'].indexOf(l.constructor.name) - 1;
+	if(l.constructor && l.constructor.name in {'MinKey':1,'MaxKey':1} ){
+		if(l.constructor.name == r.constructor.name) { 
+			return 0; 
+		} else if (l.constructor.name === 'MinKey'){
+			return -1;
+		} else {
+			return 1; // Must be MaxKey, which is greater than everything but MaxKey (which r cannot be)
+		}	
 	}
 	// hack: These should really get converted to their BSON type ids and then compared, we use int vs object in queries
 	if (lt === "number" && rt === "object"){

+ 93 - 10
test/lib/pipeline/matcher/TypeMatchExpression.js

@@ -6,31 +6,114 @@ var assert = require("assert"),
 module.exports = {
 	"TypeMatchExpression": {
 		"should match string type": function (){
-			
+			var e = new TypeMatchExpression();
+			var s = e.init('', 2 );
+
+			assert.strictEqual(s.code, 'OK');
+			assert.ok( e.matches('abc') );
+			assert.ok( ! e.matches(2) );
+
 		},
 		"should match null type": function() {
-			
+			var e = new TypeMatchExpression();
+			var s = e.init('',10 );
+
+			assert.strictEqual(s.code, 'OK');
+			assert.ok( e.matches(null) );
+			assert.ok( ! e.matches(10) );
+
 		},
 		"should match unknown type": function() {
-		
+			var e = new TypeMatchExpression();
+			var s = e.init('', 1024);
+	
+			assert.strictEqual(s.code, 'OK');
+			assert.ok( ! e.matches(1024) );
+			assert.ok( ! e.matches('abc') );
+
 		},
 		"should match bool type": function() {
-		
+			var e = new TypeMatchExpression();
+			var s = e.init('',8 );
+
+			assert.strictEqual(s.code, 'OK');
+			assert.ok( e.matches(true) );
+			assert.ok( ! e.matches(8) );
+
 		},
 		"should match number type": function() {
-		
+			var e = new TypeMatchExpression();
+			var s = e.init('a',1 );
+
+			assert.strictEqual(s.code, 'OK');
+			assert.ok( e.matches({'a':[4]}) );	
+			assert.ok( e.matches({'a':[4, 'a']}) );	
+			assert.ok( e.matches({'a':['a', 4]}) );
+			assert.ok( ! e.matches({'a':['a']}) );
+			assert.ok( ! e.matches({'a':[[4]]}) );
+
 		},
-		"should match array  type": function() {
-		
+		"should match array type": function() {
+			var e = new TypeMatchExpression();
+			var s = e.init('a', 4);
+
+			assert.strictEqual(s.code, 'OK');
+			assert.ok( ! e.matches({'a':[]}) );	
+			assert.ok( ! e.matches({'a':[4, 'a']}) );
+			assert.ok( e.matches({'a':[[2]]}) );
+			assert.ok( ! e.matches({'a':'bar'}) );
+
 		},
 		"should match null type more": function() {
-		
+			var e = new TypeMatchExpression();
+			var s = e.init('a', 10);
+
+			assert.strictEqual(s.code, 'OK');
+			assert.ok( e.matches({'a':null}) );
+			assert.ok( ! e.matches({'a':4}) );
+			assert.ok( ! e.matches({}));
+
 		},
 		"should match and preserve elemMatchKey": function() {
-		
+			var e = new TypeMatchExpression();
+			var s = e.init('a.b', 2);
+			var m = new MatchDetails();
+			m.requestElemMatchKey();
+
+			assert.strictEqual(s.code, 'OK');
+			
+			assert.ok( ! e.matches({'a':1}, m) );
+			assert.ok( ! m.hasElemMatchKey() );
+			
+			assert.ok( e.matches({'a':{'b':'string'}}), m );
+			assert.ok( ! m.hasElemMatchKey() );
+
+			assert.ok( e.matches({'a':{'b':['string']}}), m );
+			assert.ok( m.hasElemMatchKey() );
+			assert.strictEqual('0', m.elemMatchKey() );
+
+	
+			assert.ok( e.matches({'a':[2, {'b':['string']}]}), m );
+			assert.ok( m.hasElemMatchKey() );
+			assert.strictEqual('1', m.elemMatchKey() );
+
+
+
 		},
 		"should be equivalent": function() {
-		
+			var e = new TypeMatchExpression();
+			var s = e.init('a', 2);
+			var b = new TypeMatchExpression();
+			var c = new TypeMatchExpression();
+
+			assert.strictEqual(s.code, 'OK');
+			s = b.init('a', 1);	
+			assert.strictEqual(s.code, 'OK');
+			s = c.init('b', 2);
+			assert.strictEqual(s.code, 'OK');
+			assert.ok( e.equivalent(e) );
+			assert.ok( !e.equivalent(b) );
+			assert.ok( !e.equivalent(c) );
 		}
 
 	}