Repository navigation
Expand file tree
/
Copy pathquery_test.zig
More file actions
357 lines (324 loc) · 16.9 KB
/
Copy pathquery_test.zig
File metadata and controls
357 lines (324 loc) · 16.9 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//! Unit + oracle coverage for the shared compiled-query core (`query.zig`):
//! the compile shapes (literal vs linear vs PCRE2 vs escaped `-F -i`), the sound
//! trigram prefilter selection each face prunes by, the fail-closed compile
//! boundary, and the per-doc match/line-count kernels against hand-computed
//! answers. The `-P` rows exercise constructs the linear engine DECLINES
//! (lookaround, backreferences), so a passing match proves the PCRE2 arm does
//! the work — the expectations are hand-derived from PCRE semantics, never a
//! self-run of the engine under test.
const std = @import("std");
const testing = std.testing;
const q = @import("query.zig");
const CompiledQuery = q.CompiledQuery;
fn compile(spec: q.Spec) !CompiledQuery {
return CompiledQuery.compile(testing.allocator, spec);
}
/// Whether `pf` contains an exact literal (prefilter order is engine-internal).
fn pfHas(pf: []const []const u8, lit: []const u8) bool {
for (pf) |p| if (std.mem.eql(u8, p, lit)) return true;
return false;
}
test "compile: -F no-fold lowers to a literal body" {
var cq = try compile(.{ .pattern = "foobar", .fixed = true });
defer cq.deinit(testing.allocator);
try testing.expect(cq.body == .literal);
try testing.expect(!cq.caseless);
}
test "compile: plain pattern lowers to a linear engine body" {
var cq = try compile(.{ .pattern = "foo.*bar" });
defer cq.deinit(testing.allocator);
try testing.expect(cq.body == .engine);
try testing.expect(cq.body.engine == .linear);
}
test "compile: -F -i escapes the literal and folds via the regex body" {
var cq = try compile(.{ .pattern = "a.b+c", .fixed = true, .ignore_case = true });
defer cq.deinit(testing.allocator);
try testing.expect(cq.body == .engine);
try testing.expect(cq.caseless);
try testing.expect(cq.escaped != null); // the escaped `.`/`+` buffer is owned
var sc = try cq.scratch(testing.allocator);
defer sc.deinit();
// The `.`/`+` are LITERAL (escaped), and the match is case-insensitive.
try testing.expect(cq.docMatches("xx A.B+C yy", &sc));
try testing.expect(!cq.docMatches("aXbXc", &sc)); // `.`/`+` are not metachars here
}
test "compile: a pattern outside the linear-time syntax is Unsupported, not fatal" {
try testing.expectError(error.Unsupported, compile(.{ .pattern = "(foo" }));
}
// ── -P (PCRE2) rows: each uses a construct the linear engine declines, so a
// passing match is the PCRE2 arm at work. Expectations hand-derived from PCRE
// semantics. ──
test "compile: -P lowers to a PCRE2 engine body" {
var cq = try compile(.{ .pattern = "foo(?=bar)", .pcre = true });
defer cq.deinit(testing.allocator);
try testing.expect(cq.body == .engine);
try testing.expect(cq.body.engine == .pcre);
}
test "compile: -P is inert under -F (fixed wins → literal body)" {
var cq = try compile(.{ .pattern = "a.b", .fixed = true, .pcre = true });
defer cq.deinit(testing.allocator);
try testing.expect(cq.body == .literal); // a fixed string needs no engine
}
test "-P: lookahead matches only when the assertion holds (linear declines it)" {
// `foo(?=bar)` matches `foo` iff immediately followed by `bar`. The linear
// engine cannot express lookahead, so its compile declines — proving the
// warm hit below is the PCRE2 backend.
try testing.expectError(error.Unsupported, compile(.{ .pattern = "foo(?=bar)" }));
var cq = try compile(.{ .pattern = "foo(?=bar)", .pcre = true, .mode = .files });
defer cq.deinit(testing.allocator);
var sc = try cq.scratch(testing.allocator);
defer sc.deinit();
try testing.expect(cq.docMatches("x foobar y\n", &sc));
try testing.expect(!cq.docMatches("x foobaz y\n", &sc));
}
test "-P: backreference over rg's line model (count)" {
// `(\w+)\s+\1` = a doubled word (backrefs are PCRE2-only). Lines 1 and 3
// hold a repeat; line 2 does not.
var cq = try compile(.{ .pattern = "(\\w+)\\s+\\1", .pcre = true, .mode = .count });
defer cq.deinit(testing.allocator);
var sc = try cq.scratch(testing.allocator);
defer sc.deinit();
try testing.expectEqual(@as(u64, 2), cq.countLines("the the\nthe cat\ngo go now\n", &sc));
}
test "-P: collectSpans yields the PCRE2 leftmost non-overlapping spans" {
const a = testing.allocator;
var cq = try compile(.{ .pattern = "a(?=b)", .pcre = true });
defer cq.deinit(a);
var ms = try cq.matchScratch(a);
defer ms.deinit();
var spans: std.ArrayList(q.Span) = .empty;
defer spans.deinit(a);
// "abXab": `a` at 0 (b follows) and at 3 (b follows) match; the middle X
// breaks the run. Each span is the zero-consumed-lookahead `a` only.
try cq.collectSpans(a, "abXab", true, &ms, &spans);
try testing.expectEqual(@as(usize, 2), spans.items.len);
try testing.expectEqual(@as(usize, 0), spans.items[0].start);
try testing.expectEqual(@as(usize, 1), spans.items[0].end);
try testing.expectEqual(@as(usize, 3), spans.items[1].start);
try testing.expectEqual(@as(usize, 4), spans.items[1].end);
}
test "-P -w: the word rule composes with the PCRE2 engine" {
// `ru\w` (matches e.g. `run`) under -w: the span must be word-bounded both
// sides. ` run ` is valid; every `ru\w` inside `rerunning` is embedded.
var cq = try compile(.{ .pattern = "ru\\w", .pcre = true, .word = true, .mode = .files });
defer cq.deinit(testing.allocator);
var sc = try cq.scratch(testing.allocator);
defer sc.deinit();
try testing.expect(cq.docMatches("go run now\n", &sc));
try testing.expect(!cq.docMatches("rerunning\n", &sc));
}
test "-P: prefilter prunes by the backend required literal; caseless declines" {
var one: [1][]const u8 = undefined;
var cq = try compile(.{ .pattern = "foobar", .pcre = true });
defer cq.deinit(testing.allocator);
try testing.expect(pfHas(cq.prefilter(&one), "foobar"));
// Caseless -P soundly declines the prefilter (full warm scan — the same
// match set, unpruned; PCRE2 caseless variant-mining is deferred).
var ci = try compile(.{ .pattern = "foobar", .pcre = true, .ignore_case = true });
defer ci.deinit(testing.allocator);
try testing.expectEqual(@as(usize, 0), ci.prefilter(&one).len);
}
test "-P: an invalid PCRE2 pattern is Unsupported, not fatal" {
try testing.expectError(error.Unsupported, compile(.{ .pattern = "foo(", .pcre = true }));
}
test "prefilter: a >=3 literal prunes by itself; a short one declines" {
var big = try compile(.{ .pattern = "foobar", .fixed = true });
defer big.deinit(testing.allocator);
var one: [1][]const u8 = undefined;
const pf = big.prefilter(&one);
try testing.expectEqual(@as(usize, 1), pf.len);
try testing.expect(pfHas(pf, "foobar"));
var small = try compile(.{ .pattern = "ab", .fixed = true });
defer small.deinit(testing.allocator);
try testing.expectEqual(@as(usize, 0), small.prefilter(&one).len);
}
test "prefilter: a caseless query prunes by case variants; escape orbits decline" {
// A fold-closed literal prunes by the caseless variant OR-set (one window
// of the raw literal in every case spelling) — the warm twin of cold's
// `caselessFilter`.
var cq = try compile(.{ .pattern = "foobar", .ignore_case = true });
defer cq.deinit(testing.allocator);
var one: [1][]const u8 = undefined;
const pf = cq.prefilter(&one);
try testing.expect(pf.len > 0);
for (pf) |v| try testing.expect(std.ascii.eqlIgnoreCase(v, pf[0]));
// …and the whole-literal caseless SIMD gate is mined alongside it.
try testing.expectEqualStrings("foobar", cq.gate.?);
// Every window of "sks" holds a Kelvin/long-s escape orbit under Unicode
// fold, so both accelerations decline — the old engine-only path remains.
var risky = try compile(.{ .pattern = "sks", .ignore_case = true });
defer risky.deinit(testing.allocator);
try testing.expectEqual(@as(usize, 0), risky.prefilter(&one).len);
try testing.expect(risky.gate == null);
}
test "prefilter: a regex prunes by its required literal" {
var cq = try compile(.{ .pattern = "foobar.*baz" });
defer cq.deinit(testing.allocator);
var one: [1][]const u8 = undefined;
const pf = cq.prefilter(&one);
try testing.expect(pf.len >= 1);
try testing.expect(pfHas(pf, "foobar") or pfHas(pf, "baz"));
}
test "prefilter: an alternation prunes by its per-branch cover" {
var cq = try compile(.{ .pattern = "foobar|bazqux" });
defer cq.deinit(testing.allocator);
var one: [1][]const u8 = undefined;
const pf = cq.prefilter(&one);
try testing.expect(pfHas(pf, "foobar"));
try testing.expect(pfHas(pf, "bazqux"));
}
test "docMatches: literal is substring presence anywhere in the doc" {
var cq = try compile(.{ .pattern = "needle", .fixed = true });
defer cq.deinit(testing.allocator);
var sc = try cq.scratch(testing.allocator);
defer sc.deinit();
try testing.expect(cq.docMatches("a\nb needle c\nd", &sc));
try testing.expect(!cq.docMatches("no match here", &sc));
}
test "countLines: literal, rg line model (\\n terminates, no phantom final line)" {
var cq = try compile(.{ .pattern = "foo", .fixed = true, .mode = .count });
defer cq.deinit(testing.allocator);
var sc = try cq.scratch(testing.allocator);
defer sc.deinit();
// 4 lines; "foo" on lines 2 and 4.
try testing.expectEqual(@as(u64, 2), cq.countLines("bar\nfoo\nbaz\nfood\n", &sc));
// No trailing newline: the last line still counts once.
try testing.expectEqual(@as(u64, 1), cq.countLines("x\nfoo", &sc));
// A line matched twice is still one matching LINE.
try testing.expectEqual(@as(u64, 1), cq.countLines("foo foo foo\n", &sc));
}
test "countLines: regex over rg's line model" {
var cq = try compile(.{ .pattern = "fo+", .mode = .count });
defer cq.deinit(testing.allocator);
var sc = try cq.scratch(testing.allocator);
defer sc.deinit();
// "fo+" matches lines 1 (foo) and 3 (fooo), not line 2 (f).
try testing.expectEqual(@as(u64, 2), cq.countLines("foo\nf\nfooo\n", &sc));
}
// ── -w (word) rows: expectations hand-derived from ripgrep's post-match word
// rule as cold implements it (`output.zig::wordOk`/`nextSpan`) — never from a
// self-run of this engine. ──
test "word: literal occurrences filter by wordOk over the non-overlapping scan" {
var cq = try compile(.{ .pattern = "run", .fixed = true, .word = true });
defer cq.deinit(testing.allocator);
var sc = try cq.scratch(testing.allocator);
defer sc.deinit();
try testing.expect(cq.docMatches("run runner\n", &sc)); // valid at [0,3)
try testing.expect(cq.docMatches("rerun run\n", &sc)); // rejected at [2,5), valid at [6,9)
try testing.expect(!cq.docMatches("runner rerun\n", &sc)); // every occurrence word-rejected
try testing.expect(!cq.docMatches("\xc3\xa9run \xe4\xb8\xadrun\n", &sc)); // é / 中 neighbors reject
try testing.expectEqual(@as(u64, 2), cq.countLines("run runner\nrerun run\nrunner only\n", &sc));
}
test "word: -F adjacent repeats scan leftmost non-overlapping (aa in aaa)" {
var cq = try compile(.{ .pattern = "aa", .fixed = true, .word = true });
defer cq.deinit(testing.allocator);
var sc = try cq.scratch(testing.allocator);
defer sc.deinit();
// `aaa`: [0,2) is word-rejected; the scan resumes AT 2 (never 1), where no
// full occurrence remains — cold reports no match. `aaaa`: [0,2) and [2,4)
// are both rejected by their word neighbor. ` aa ` is valid.
try testing.expect(!cq.docMatches("aaa\n", &sc));
try testing.expect(!cq.docMatches("aaaa\n", &sc));
try testing.expect(cq.docMatches(" aa aaa\n", &sc));
try testing.expectEqual(@as(u64, 1), cq.countLines(" aa aaa\naaaa\n", &sc));
}
test "word: punctuation-only matches are word-valid; regex spans use nextSpan's rule" {
var cq = try compile(.{ .pattern = "\\.", .word = true });
defer cq.deinit(testing.allocator);
var sc = try cq.scratch(testing.allocator);
defer sc.deinit();
try testing.expect(cq.docMatches("a . b\n", &sc)); // `.` bounded by spaces — a word match
try testing.expect(!cq.docMatches(".dot\n", &sc)); // `d` after the span rejects it
try testing.expectEqual(@as(u64, 1), cq.countLines("a . b\n.dot\n", &sc));
}
test "word: a zero-width match carries a line only where non-word bytes bound it" {
var cq = try compile(.{ .pattern = "x*", .word = true, .mode = .count });
defer cq.deinit(testing.allocator);
var sc = try cq.scratch(testing.allocator);
defer sc.deinit();
// Without -w the boolean path counts every line (`x*` matches empty); under
// -w the empty match must still be bounded by non-word bytes on both sides.
// `rg -c -w 'x*'` ⇒ 1 over "x x\nyy\n": line 1 on its real x-runs, and "yy"
// not at all — every gap in it touches a `y`.
try testing.expectEqual(@as(u64, 1), cq.countLines("x x\nyy\n", &sc));
try testing.expect(!cq.docMatches("yy\n", &sc));
try testing.expect(cq.docMatches("x x\n", &sc));
// The discriminating case the line above cannot see, because there a
// non-empty match carries the line anyway. Here NO line holds an `x` at
// all, so a line can only be selected by a word-valid EMPTY match — and rg
// selects two of the three. Captured from ripgrep 15.2.0:
// rg -n -w 'x*' over "a b\nzz\n. .\n" ⇒ 1:a b / 3:. .
// Line 1 qualifies on the gap between its two spaces, line 3 at the line
// start before `.`; "zz" has no gap that isn't touching a word byte.
// Requiring a non-empty span here answered zero lines.
try testing.expectEqual(@as(u64, 2), cq.countLines("a b\nzz\n. .\n", &sc));
try testing.expect(cq.docMatches("a b\n", &sc));
try testing.expect(cq.docMatches(". .\n", &sc));
try testing.expect(!cq.docMatches("zz\n", &sc));
}
test "word: composes with the case fold; the word check reads original bytes" {
var cq = try compile(.{ .pattern = "run", .fixed = true, .ignore_case = true, .word = true });
defer cq.deinit(testing.allocator);
try testing.expect(cq.body == .engine); // -F -i escapes through the linear engine
var sc = try cq.scratch(testing.allocator);
defer sc.deinit();
try testing.expect(cq.docMatches("RUN loud\n", &sc));
try testing.expect(!cq.docMatches("RERUNNING\n", &sc)); // folded hit, still word-rejected
try testing.expectEqual(@as(u64, 1), cq.countLines("RUN loud\nrerunning\n", &sc));
}
test "word: collectSpans emits only word-valid spans with Rows' progress" {
const a = testing.allocator;
// Literal body: the rejected `rerun` occurrence is skipped, the later
// ` run` on the same line survives.
{
var cq = try compile(.{ .pattern = "run", .fixed = true, .word = true });
defer cq.deinit(a);
var ms = try cq.matchScratch(a);
defer ms.deinit();
var spans: std.ArrayList(q.Span) = .empty;
defer spans.deinit(a);
try cq.collectSpans(a, "rerun run", true, &ms, &spans);
try testing.expectEqual(@as(usize, 1), spans.items.len);
try testing.expectEqual(@as(usize, 6), spans.items[0].start);
try testing.expectEqual(@as(usize, 9), spans.items[0].end);
}
// Regex body: same rule through the span VM.
{
var cq = try compile(.{ .pattern = "ru.", .word = true });
defer cq.deinit(a);
var ms = try cq.matchScratch(a);
defer ms.deinit();
var spans: std.ArrayList(q.Span) = .empty;
defer spans.deinit(a);
try cq.collectSpans(a, "rerun run runt", true, &ms, &spans);
// `run` at [2,5) rejected (word char before); ` run` at [6,9) valid
// (space after); `run` in `runt` at [10,13) matches `ru.`+`n`… the
// span [10,13) is `run` inside `runt` → `t` after rejects it.
try testing.expectEqual(@as(usize, 1), spans.items.len);
try testing.expectEqual(@as(usize, 6), spans.items[0].start);
try testing.expectEqual(@as(usize, 9), spans.items[0].end);
}
// The prefilter is unchanged by -w: the literal still prunes.
{
var cq = try compile(.{ .pattern = "foobar", .fixed = true, .word = true });
defer cq.deinit(a);
var one: [1][]const u8 = undefined;
const pf = cq.prefilter(&one);
try testing.expectEqual(@as(usize, 1), pf.len);
try testing.expect(pfHas(pf, "foobar"));
}
}
test "docMatches vs countLines>0 agree on whether any line matches" {
const inputs = [_][]const u8{ "", "foo\n", "x\ny\nzfooz\n", "no\nmatch\n", "foo" };
for (inputs) |doc| {
var files_q = try compile(.{ .pattern = "foo", .fixed = true, .mode = .files });
defer files_q.deinit(testing.allocator);
var fs = try files_q.scratch(testing.allocator);
defer fs.deinit();
var count_q = try compile(.{ .pattern = "foo", .fixed = true, .mode = .count });
defer count_q.deinit(testing.allocator);
var cs = try count_q.scratch(testing.allocator);
defer cs.deinit();
try testing.expectEqual(files_q.docMatches(doc, &fs), count_q.countLines(doc, &cs) > 0);
}
}