Debian Codegraph @

.cargo/vendor/regex-automata/tests/meta/suite.rs

raw
1 use {
2 anyhow::Result,
3 regex_automata::{
4 meta::{self, Regex},
5 util::syntax,
6 MatchKind, PatternSet,
7 },
8 regex_test::{
9 CompiledRegex, Match, RegexTest, SearchKind, Span, TestResult,
10 TestRunner,
11 },
12 };
13
14 use crate::{create_input, suite, testify_captures};
15
16 const BLACKLIST: &[&str] = &[
17 // These 'earliest' tests are blacklisted because the meta searcher doesn't
18 // give the same offsets that the test expects. This is legal because the
19 // 'earliest' routines don't guarantee a particular match offset other
20 // than "the earliest the regex engine can report a match." Some regex
21 // engines will quit earlier than others. The backtracker, for example,
22 // can't really quit before finding the full leftmost-first match. Many of
23 // the literal searchers also don't have the ability to quit fully or it's
24 // otherwise not worth doing. (A literal searcher not quitting as early as
25 // possible usually means looking at a few more bytes. That's no biggie.)
26 "earliest/",
27 ];
28
29 /// Tests the default configuration of the meta regex engine.
30 #[test]
31 fn default() -> Result<()> {
32 let builder = Regex::builder();
33 let mut runner = TestRunner::new()?;
34 runner
35 .expand(&["is_match", "find", "captures"], |test| test.compiles())
36 .blacklist_iter(BLACKLIST)
37 .test_iter(suite()?.iter(), compiler(builder))
38 .assert();
39 Ok(())
40 }
41
42 /// Tests the default configuration minus the full DFA.
43 #[test]
44 fn no_dfa() -> Result<()> {
45 let mut builder = Regex::builder();
46 builder.configure(Regex::config().dfa(false));
47 let mut runner = TestRunner::new()?;
48 runner
49 .expand(&["is_match", "find", "captures"], |test| test.compiles())
50 .blacklist_iter(BLACKLIST)
51 .test_iter(suite()?.iter(), compiler(builder))
52 .assert();
53 Ok(())
54 }
55
56 /// Tests the default configuration minus the full DFA and lazy DFA.
57 #[test]
58 fn no_dfa_hybrid() -> Result<()> {
59 let mut builder = Regex::builder();
60 builder.configure(Regex::config().dfa(false).hybrid(false));
61 let mut runner = TestRunner::new()?;
62 runner
63 .expand(&["is_match", "find", "captures"], |test| test.compiles())
64 .blacklist_iter(BLACKLIST)
65 .test_iter(suite()?.iter(), compiler(builder))
66 .assert();
67 Ok(())
68 }
69
70 /// Tests the default configuration minus the full DFA, lazy DFA and one-pass
71 /// DFA.
72 #[test]
73 fn no_dfa_hybrid_onepass() -> Result<()> {
74 let mut builder = Regex::builder();
75 builder.configure(Regex::config().dfa(false).hybrid(false).onepass(false));
76 let mut runner = TestRunner::new()?;
77 runner
78 .expand(&["is_match", "find", "captures"], |test| test.compiles())
79 .blacklist_iter(BLACKLIST)
80 .test_iter(suite()?.iter(), compiler(builder))
81 .assert();
82 Ok(())
83 }
84
85 /// Tests the default configuration minus the full DFA, lazy DFA, one-pass
86 /// DFA and backtracker.
87 #[test]
88 fn no_dfa_hybrid_onepass_backtrack() -> Result<()> {
89 let mut builder = Regex::builder();
90 builder.configure(
91 Regex::config()
92 .dfa(false)
93 .hybrid(false)
94 .onepass(false)
95 .backtrack(false),
96 );
97 let mut runner = TestRunner::new()?;
98 runner
99 .expand(&["is_match", "find", "captures"], |test| test.compiles())
100 .blacklist_iter(BLACKLIST)
101 .test_iter(suite()?.iter(), compiler(builder))
102 .assert();
103 Ok(())
104 }
105
106 fn compiler(
107 mut builder: meta::Builder,
108 ) -> impl FnMut(&RegexTest, &[String]) -> Result<CompiledRegex> {
109 move |test, regexes| {
110 if !configure_meta_builder(test, &mut builder) {
111 return Ok(CompiledRegex::skip());
112 }
113 let re = builder.build_many(&regexes)?;
114 Ok(CompiledRegex::compiled(move |test| -> TestResult {
115 run_test(&re, test)
116 }))
117 }
118 }
119
120 fn run_test(re: &Regex, test: &RegexTest) -> TestResult {
121 let input = create_input(test);
122 match test.additional_name() {
123 "is_match" => TestResult::matched(re.is_match(input)),
124 "find" => match test.search_kind() {
125 SearchKind::Earliest => TestResult::matches(
126 re.find_iter(input.earliest(true))
127 .take(test.match_limit().unwrap_or(std::usize::MAX))
128 .map(|m| Match {
129 id: m.pattern().as_usize(),
130 span: Span { start: m.start(), end: m.end() },
131 }),
132 ),
133 SearchKind::Leftmost => TestResult::matches(
134 re.find_iter(input)
135 .take(test.match_limit().unwrap_or(std::usize::MAX))
136 .map(|m| Match {
137 id: m.pattern().as_usize(),
138 span: Span { start: m.start(), end: m.end() },
139 }),
140 ),
141 SearchKind::Overlapping => {
142 let mut patset = PatternSet::new(re.pattern_len());
143 re.which_overlapping_matches(&input, &mut patset);
144 TestResult::which(patset.iter().map(|p| p.as_usize()))
145 }
146 },
147 "captures" => match test.search_kind() {
148 SearchKind::Earliest => {
149 let it = re
150 .captures_iter(input.earliest(true))
151 .take(test.match_limit().unwrap_or(std::usize::MAX))
152 .map(|caps| testify_captures(&caps));
153 TestResult::captures(it)
154 }
155 SearchKind::Leftmost => {
156 let it = re
157 .captures_iter(input)
158 .take(test.match_limit().unwrap_or(std::usize::MAX))
159 .map(|caps| testify_captures(&caps));
160 TestResult::captures(it)
161 }
162 SearchKind::Overlapping => {
163 // There is no overlapping regex API that supports captures.
164 TestResult::skip()
165 }
166 },
167 name => TestResult::fail(&format!("unrecognized test name: {}", name)),
168 }
169 }
170
171 /// Configures the given regex builder with all relevant settings on the given
172 /// regex test.
173 ///
174 /// If the regex test has a setting that is unsupported, then this returns
175 /// false (implying the test should be skipped).
176 fn configure_meta_builder(
177 test: &RegexTest,
178 builder: &mut meta::Builder,
179 ) -> bool {
180 let match_kind = match test.match_kind() {
181 regex_test::MatchKind::All => MatchKind::All,
182 regex_test::MatchKind::LeftmostFirst => MatchKind::LeftmostFirst,
183 regex_test::MatchKind::LeftmostLongest => return false,
184 };
185 let meta_config = Regex::config()
186 .match_kind(match_kind)
187 .utf8_empty(test.utf8())
188 .line_terminator(test.line_terminator());
189 builder.configure(meta_config).syntax(config_syntax(test));
190 true
191 }
192
193 /// Configuration of the regex parser from a regex test.
194 fn config_syntax(test: &RegexTest) -> syntax::Config {
195 syntax::Config::new()
196 .case_insensitive(test.case_insensitive())
197 .unicode(test.unicode())
198 .utf8(test.utf8())
199 .line_terminator(test.line_terminator())
200 }