Debian Codegraph @

debian/rust-vendor/regex-lite/tests/string.rs

raw
1 use {
2 anyhow::Result,
3 regex_lite::{Regex, RegexBuilder},
4 regex_test::{
5 CompiledRegex, Match, RegexTest, Span, TestResult, TestRunner,
6 },
7 };
8
9 /// Tests the default configuration of the hybrid NFA/DFA.
10 #[test]
11 fn default() -> Result<()> {
12 let mut runner = TestRunner::new()?;
13 runner
14 .expand(&["is_match", "find", "captures"], |test| test.compiles())
15 .blacklist_iter(super::BLACKLIST)
16 .test_iter(crate::suite()?.iter(), compiler)
17 .assert();
18 Ok(())
19 }
20
21 fn run_test(re: &Regex, test: &RegexTest) -> TestResult {
22 let hay = match std::str::from_utf8(test.haystack()) {
23 Ok(hay) => hay,
24 Err(err) => {
25 return TestResult::fail(&format!(
26 "haystack is not valid UTF-8: {err}",
27 ));
28 }
29 };
30 match test.additional_name() {
31 "is_match" => TestResult::matched(re.is_match(hay)),
32 "find" => TestResult::matches(
33 re.find_iter(hay)
34 .take(test.match_limit().unwrap_or(std::usize::MAX))
35 .map(|m| Match {
36 id: 0,
37 span: Span { start: m.start(), end: m.end() },
38 }),
39 ),
40 "captures" => {
41 let it = re
42 .captures_iter(hay)
43 .take(test.match_limit().unwrap_or(std::usize::MAX))
44 .map(|caps| testify_captures(&caps));
45 TestResult::captures(it)
46 }
47 name => TestResult::fail(&format!("unrecognized test name: {name}")),
48 }
49 }
50
51 /// Converts the given regex test to a closure that searches with a
52 /// `bytes::Regex`. If the test configuration is unsupported, then a
53 /// `CompiledRegex` that skips the test is returned.
54 fn compiler(
55 test: &RegexTest,
56 _patterns: &[String],
57 ) -> anyhow::Result<CompiledRegex> {
58 let Some(pattern) = skip_or_get_pattern(test) else {
59 return Ok(CompiledRegex::skip());
60 };
61 let re = RegexBuilder::new(pattern)
62 .case_insensitive(test.case_insensitive())
63 .build()?;
64 Ok(CompiledRegex::compiled(move |test| run_test(&re, test)))
65 }
66
67 /// Whether we should skip the given test or not. If not, return the single
68 /// pattern from the given test.
69 fn skip_or_get_pattern(test: &RegexTest) -> Option<&str> {
70 // We're only testing Regex here, which supports one pattern only.
71 let pattern = match test.regexes().len() {
72 1 => &test.regexes()[0],
73 _ => return None,
74 };
75 // If the test name contains 'regex-lite', then we ALWAYS run it. Because
76 // those tests are specifically designed for regex-lite. So if they fail,
77 // then something needs attention.
78 if test.full_name().contains("regex-lite/") {
79 return Some(pattern);
80 }
81 // If the pattern has a \p in it, then we almost certainly don't support
82 // it. This probably skips more than we intend, but there are likely very
83 // few tests that contain a \p that isn't also a Unicode class.
84 if pattern.contains(r"\p") || pattern.contains(r"\P") {
85 return None;
86 }
87 // Similar deal for Perl classes, but we can abide them if the haystack
88 // is ASCII-only.
89 if !test.haystack().is_ascii() {
90 if pattern.contains(r"\d") || pattern.contains(r"\D") {
91 return None;
92 }
93 if pattern.contains(r"\s") || pattern.contains(r"\S") {
94 return None;
95 }
96 if pattern.contains(r"\w") || pattern.contains(r"\W") {
97 return None;
98 }
99 }
100 // And also same deal for word boundaries.
101 if !test.haystack().is_ascii() {
102 if pattern.contains(r"\b") || pattern.contains(r"\B") {
103 return None;
104 }
105 }
106 // We only test is_match, find_iter and captures_iter. All of those are
107 // leftmost searches.
108 if !matches!(test.search_kind(), regex_test::SearchKind::Leftmost) {
109 return None;
110 }
111 // The top-level single-pattern regex API always uses leftmost-first.
112 if !matches!(test.match_kind(), regex_test::MatchKind::LeftmostFirst) {
113 return None;
114 }
115 // The top-level regex API always runs unanchored searches. ... But we can
116 // handle tests that are anchored but have only one match.
117 if test.anchored() && test.match_limit() != Some(1) {
118 return None;
119 }
120 // We don't support tests with explicit search bounds. We could probably
121 // support this by using the 'find_at' (and such) APIs.
122 let bounds = test.bounds();
123 if !(bounds.start == 0 && bounds.end == test.haystack().len()) {
124 return None;
125 }
126 // The Regex API specifically does not support disabling UTF-8 mode because
127 // it can only search &str which is always valid UTF-8.
128 if !test.utf8() {
129 return None;
130 }
131 // regex-lite doesn't support Unicode-aware case insensitive matching.
132 if test.case_insensitive()
133 && (!pattern.is_ascii() || !test.haystack().is_ascii())
134 {
135 return None;
136 }
137 Some(pattern)
138 }
139
140 /// Convert `Captures` into the test suite's capture values.
141 fn testify_captures(caps: &regex_lite::Captures<'_>) -> regex_test::Captures {
142 let spans = caps.iter().map(|group| {
143 group.map(|m| regex_test::Span { start: m.start(), end: m.end() })
144 });
145 // This unwrap is OK because we assume our 'caps' represents a match, and
146 // a match always gives a non-zero number of groups with the first group
147 // being non-None.
148 regex_test::Captures::new(0, spans).unwrap()
149 }