Debian Codegraph @

debian/missing-sources/regex/tests/suite_string.rs

raw
1 use {
2 anyhow::Result,
3 regex::{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 skip = Ok(CompiledRegex::skip());
59
60 // We're only testing bytes::Regex here, which supports one pattern only.
61 let pattern = match test.regexes().len() {
62 1 => &test.regexes()[0],
63 _ => return skip,
64 };
65 // We only test is_match, find_iter and captures_iter. All of those are
66 // leftmost searches.
67 if !matches!(test.search_kind(), regex_test::SearchKind::Leftmost) {
68 return skip;
69 }
70 // The top-level single-pattern regex API always uses leftmost-first.
71 if !matches!(test.match_kind(), regex_test::MatchKind::LeftmostFirst) {
72 return skip;
73 }
74 // The top-level regex API always runs unanchored searches. ... But we can
75 // handle tests that are anchored but have only one match.
76 if test.anchored() && test.match_limit() != Some(1) {
77 return skip;
78 }
79 // We don't support tests with explicit search bounds. We could probably
80 // support this by using the 'find_at' (and such) APIs.
81 let bounds = test.bounds();
82 if !(bounds.start == 0 && bounds.end == test.haystack().len()) {
83 return skip;
84 }
85 // The Regex API specifically does not support disabling UTF-8 mode because
86 // it can only search &str which is always valid UTF-8.
87 if !test.utf8() {
88 return skip;
89 }
90 // If the test requires Unicode but the Unicode feature isn't enabled,
91 // skip it. This is a little aggressive, but the test suite doesn't
92 // have any easy way of communicating which Unicode features are needed.
93 if test.unicode() && !cfg!(feature = "unicode") {
94 return skip;
95 }
96 let re = RegexBuilder::new(pattern)
97 .case_insensitive(test.case_insensitive())
98 .unicode(test.unicode())
99 .line_terminator(test.line_terminator())
100 .build()?;
101 Ok(CompiledRegex::compiled(move |test| run_test(&re, test)))
102 }
103
104 /// Convert `Captures` into the test suite's capture values.
105 fn testify_captures(caps: &regex::Captures<'_>) -> regex_test::Captures {
106 let spans = caps.iter().map(|group| {
107 group.map(|m| regex_test::Span { start: m.start(), end: m.end() })
108 });
109 // This unwrap is OK because we assume our 'caps' represents a match, and
110 // a match always gives a non-zero number of groups with the first group
111 // being non-None.
112 regex_test::Captures::new(0, spans).unwrap()
113 }