1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
/**
* Tests regular expressions and the use of various UCP verbs.
*/
(function() {
"use strict";
const coll = db.getCollection("regex_backtracking_verbs");
coll.drop();
const docA = {
_id: 0,
text: "a"
};
const docB = {
_id: 1,
text: "b"
};
[docA, docB].forEach(doc => assert.commandWorked(coll.insert(doc)));
/**
* Helper function that asserts that a find command with a filter on the "text" field using
* 'regex' returns 'expected' when sorting by _id ascending.
*/
function assertFindResultsEq(regex, expected) {
const res = coll.find({text: {$regex: regex}}).sort({_id: 1}).toArray();
const errfn = `Regex query ${tojson(regex)} returned ${tojson(res)} ` +
`but expected ${tojson(expected)}`;
assert.eq(res, expected, errfn);
}
const assertMatchesEverything = (regex) => assertFindResultsEq(regex, [docA, docB]);
const assertMatchesNothing = (regex) => assertFindResultsEq(regex, []);
// On encountering FAIL, the pattern immediately does not match.
assertMatchesNothing("(*FAIL)");
assertMatchesNothing("a(*FAIL)");
assertMatchesNothing("(*FAIL)b");
// On encountering ACCEPT, the pattern immediately matches.
assertMatchesEverything("(*ACCEPT)");
assertMatchesEverything("(*ACCEPT)a");
assertMatchesEverything("(*ACCEPT)c");
assertFindResultsEq("b(*ACCEPT)", [docB]);
// The following tests simply assert that the backtracking verbs are accepted and do not
// influence matching.
["COMMIT", "PRUNE", "PRUNE:FOO", "SKIP", "SKIP:BAR", "THEN", "THEN:BAZ"].forEach(verb => {
// Verb by itself is the same as an empty regex and matches everything.
assertMatchesEverything(`(*${verb})`);
// Verb with pattern does not affect the "matchiness" of the pattern.
assertFindResultsEq(`(*${verb})a`, [docA]);
assertFindResultsEq(`(*${verb})[Bb]`, [docB]);
});
}());
|