Fix dotted path selector compiler, fixes #71.

Add dotted path test suite.
This commit is contained in:
matt debergalis
2012-04-22 13:06:18 -07:00
parent c1badda2f0
commit 753d139325
2 changed files with 27 additions and 7 deletions

View File

@@ -520,12 +520,33 @@ Tinytest.add("minimongo - selector_compiler", function (test) {
match({x: {$not: /a/}}, {x: "dog"});
nomatch({x: {$not: /a/}}, {x: "cat"});
match({x: {$not: /a/}}, {x: ["dog", "puppy"]});
nomatch({x: {$not: /a/}}, {x: ["kitten", "cat"]})
nomatch({x: {$not: /a/}}, {x: ["kitten", "cat"]});
// dotted keypaths: bare values
match({"a.b": 1}, {a: {b: 1}});
nomatch({"a.b": 1}, {a: {b: 2}});
match({"a.b": [1,2,3]}, {a: {b: [1,2,3]}});
nomatch({"a.b": [1,2,3]}, {a: {b: [4]}});
match({"a.b": /a/}, {a: {b: "cat"}});
nomatch({"a.b": /a/}, {a: {b: "dog"}});
// dotted keypaths: literal objects
match({"a.b": {c: 1}}, {a: {b: {c: 1}}});
nomatch({"a.b": {c: 1}}, {a: {b: {c: 2}}});
nomatch({"a.b": {c: 1}}, {a: {b: 2}});
match({"a.b": {c: 1, d: 2}}, {a: {b: {c: 1, d: 2}}});
nomatch({"a.b": {c: 1, d: 2}}, {a: {b: {c: 1, d: 1}}});
nomatch({"a.b": {c: 1, d: 2}}, {a: {b: {d: 2}}});
// dotted keypaths: $ operators
match({"a.b": {$in: [1, 2, 3]}}, {a: {b: [2]}}); // tested against mongodb
match({"a.b": {$in: [{x: 1}, {x: 2}, {x: 3}]}}, {a: {b: [{x: 2}]}});
match({"a.b": {$in: [1, 2, 3]}}, {a: {b: [4, 2]}});
nomatch({"a.b": {$in: [1, 2, 3]}}, {a: {b: [4]}});
// XXX still needs tests:
// - $or, $and, $nor, $where
// - $elemMatch
// - dotted keypaths
// - people.2.name
// - non-scalar arguments to $gt, $lt, etc
});

View File

@@ -356,10 +356,9 @@ LocalCollection._exprForKeypathPredicate = function (keypath, value, literals) {
var predcode = '';
if (value instanceof RegExp) {
predcode = LocalCollection._exprForOperatorTest(value, literals);
} else if (
!(typeof value === 'object') ||
value === null ||
value instanceof Array) {
} else if ( !(typeof value === 'object')
|| value === null
|| value instanceof Array) {
// it's something like {x.y: 12} or {x.y: [12]}
predcode = LocalCollection._exprForValueTest(value, literals);
} else {
@@ -395,7 +394,7 @@ LocalCollection._exprForKeypathPredicate = function (keypath, value, literals) {
// for all but the innermost level of a dotted expression,
// if the runtime type is an array, search it
ret = 'f._matches(' + formal + '[' + JSON.stringify(part) +
'], function(x){return ' + ret + ';}';
'], function(x){return ' + ret + ';})';
}
}