Skip to content

Commit 0b51fc5

Browse files
Add unit tests for look-behind assertions
1 parent ee10459 commit 0b51fc5

File tree

6 files changed

+93
-3
lines changed

6 files changed

+93
-3
lines changed

regex-automata/tests/dfa/onepass/suite.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ fn compiler(
7979
// Since our error types are all generally opaque, we just
8080
// look for an error string. Not great, but not the end of the
8181
// world.
82-
if test.compiles() && msg.contains("not one-pass") {
82+
if test.compiles()
83+
&& (msg.contains("not one-pass")
84+
|| msg.contains("look-around"))
85+
{
8386
return Ok(CompiledRegex::skip());
8487
}
8588
return Err(err.into());

regex-automata/tests/dfa/suite.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,17 @@ fn compiler(
292292
if !configure_regex_builder(test, &mut builder) {
293293
return Ok(CompiledRegex::skip());
294294
}
295-
create_matcher(&builder, pre, builder.build_many(&regexes)?)
295+
let re = match builder.build_many(regexes) {
296+
Ok(re) => re,
297+
Err(err)
298+
if test.compiles()
299+
&& format!("{err}").contains("look-around") =>
300+
{
301+
return Ok(CompiledRegex::skip());
302+
}
303+
Err(err) => return Err(err.into()),
304+
};
305+
create_matcher(&builder, pre, re)
296306
}
297307
}
298308

regex-automata/tests/hybrid/suite.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,16 @@ fn compiler(
183183
if !configure_regex_builder(test, &mut builder) {
184184
return Ok(CompiledRegex::skip());
185185
}
186-
let re = builder.build_many(&regexes)?;
186+
let re = match builder.build_many(regexes) {
187+
Ok(re) => re,
188+
Err(err)
189+
if test.compiles()
190+
&& format!("{err}").contains("look-around") =>
191+
{
192+
return Ok(CompiledRegex::skip());
193+
}
194+
Err(err) => return Err(err.into()),
195+
};
187196
let mut cache = re.create_cache();
188197
Ok(CompiledRegex::compiled(move |test| -> TestResult {
189198
run_test(&re, &mut cache, test)

regex-automata/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ fn suite() -> anyhow::Result<regex_test::RegexTests> {
6565
load!("fowler/basic");
6666
load!("fowler/nullsubexpr");
6767
load!("fowler/repetition");
68+
load!("lookaround");
6869

6970
Ok(tests)
7071
}

regex-automata/tests/nfa/thompson/backtrack/suite.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ fn min_visited_capacity() -> Result<()> {
7474
.configure(config_thompson(test))
7575
.syntax(config_syntax(test))
7676
.build_many(&regexes)?;
77+
// TODO: remove once look-around is supported.
78+
if nfa.lookaround_count() > 0 {
79+
return Ok(CompiledRegex::skip());
80+
}
7781
let mut builder = BoundedBacktracker::builder();
7882
if !configure_backtrack_builder(test, &mut builder) {
7983
return Ok(CompiledRegex::skip());
@@ -105,6 +109,10 @@ fn compiler(
105109
return Ok(CompiledRegex::skip());
106110
}
107111
let re = builder.build_many(&regexes)?;
112+
// TODO: remove once look-around is supported.
113+
if re.get_nfa().lookaround_count() > 0 {
114+
return Ok(CompiledRegex::skip());
115+
}
108116
let mut cache = re.create_cache();
109117
Ok(CompiledRegex::compiled(move |test| -> TestResult {
110118
run_test(&re, &mut cache, test)

testdata/lookaround.toml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
[[test]]
2+
name = "basic lookbehind positive"
3+
regex = "(?<=b)a"
4+
haystack = "ba"
5+
matches = [[1, 2]]
6+
7+
[[test]]
8+
name = "basic lookbehind negative"
9+
regex = "(?<!c)a"
10+
haystack = "ba"
11+
matches = [[1, 2]]
12+
13+
[[test]]
14+
name = "basic lookbehind positive no match"
15+
regex = "(?<=c)a"
16+
haystack = "ba"
17+
matches = []
18+
19+
[[test]]
20+
name = "basic lookbehind negative no match"
21+
regex = "(?<!b)a"
22+
haystack = "ba"
23+
matches = []
24+
25+
[[test]]
26+
name = "lookbehind in quantifier non-repeating"
27+
regex = "(?:(?<=c)a)+"
28+
haystack = "badacacaea"
29+
matches = [[5,6], [7,8]]
30+
31+
[[test]]
32+
name = "lookbehind in quantifier repeating"
33+
regex = "(?:(?<=a)a)+"
34+
haystack = "babaabaaabaaaac"
35+
matches = [[4,5], [7,9], [11,14]]
36+
37+
[[test]]
38+
name = "lookbehind with quantifier"
39+
regex = "(?<=cb+)a"
40+
haystack = "acabacbacbbaea"
41+
matches = [[7,8], [11,12]]
42+
43+
[[test]]
44+
name = "nested lookbehind"
45+
regex = "(?<=c[def]+(?<!fed))a"
46+
haystack = "cdaceacfeeacfedeacfeda"
47+
matches = [[2,3], [5,6], [10,11], [16,17]]
48+
49+
[[test]]
50+
name = "lookbehind with alternation"
51+
regex = "(?<=def|abc)a"
52+
haystack = "defaabcadefbca"
53+
matches = [[3,4], [7,8]]
54+
55+
[[test]]
56+
name = "lookbehind in alternation"
57+
regex = "(?<=c+)a|(?<=d+)a"
58+
haystack = "aabacadaccaddaea"
59+
matches = [[5,6], [7,8], [10,11], [13,14]]

0 commit comments

Comments
 (0)