Debian Codegraph @

debian/rust-vendor/regex/tests/suite_string_set.rs

raw
1 use {
2 anyhow::Result,
3 regex::{RegexSet, RegexSetBuilder},
4 regex_test::{CompiledRegex, RegexTest, TestResult, TestRunner},
5 };
6
7 /// Tests the default configuration of the hybrid NFA/DFA.
8 #[test]
9 fn default() -> Result<()> {
10 let mut runner = TestRunner::new()?;
11 runner
12 .expand(&["is_match", "which"], |test| test.compiles())
13 .blacklist_iter(super::BLACKLIST)
14 .test_iter(crate::suite()?.iter(), compiler)
15 .assert();
16 Ok(())
17 }
18
19 fn run_test(re: &RegexSet, test: &RegexTest) -> TestResult {
20 let hay = match std::str::from_utf8(test.haystack()) {
21 Ok(hay) => hay,
22 Err(err) => {
23 return TestResult::fail(&format!(
24 "haystack is not valid UTF-8: {err}",
25 ));
26 }
27 };
28 match test.additional_name() {
29 "is_match" => TestResult::matched(re.is_match(hay)),
30 "which" => TestResult::which(re.matches(hay).iter()),
31 name => TestResult::fail(&format!("unrecognized test name: {name}")),
32 }
33 }
34
35 /// Converts the given regex test to a closure that searches with a
36 /// `bytes::Regex`. If the test configuration is unsupported, then a
37 /// `CompiledRegex` that skips the test is returned.
38 fn compiler(
39 test: &RegexTest,
40 _patterns: &[String],
41 ) -> anyhow::Result<CompiledRegex> {
42 let skip = Ok(CompiledRegex::skip());
43
44 // The top-level RegexSet API only supports "overlapping" semantics.
45 if !matches!(test.search_kind(), regex_test::SearchKind::Overlapping) {
46 return skip;
47 }
48 // The top-level RegexSet API only supports "all" semantics.
49 if !matches!(test.match_kind(), regex_test::MatchKind::All) {
50 return skip;
51 }
52 // The top-level RegexSet API always runs unanchored searches.
53 if test.anchored() {
54 return skip;
55 }
56 // We don't support tests with explicit search bounds.
57 let bounds = test.bounds();
58 if !(bounds.start == 0 && bounds.end == test.haystack().len()) {
59 return skip;
60 }
61 // The Regex API specifically does not support disabling UTF-8 mode because
62 // it can only search &str which is always valid UTF-8.
63 if !test.utf8() {
64 return skip;
65 }
66 // If the test requires Unicode but the Unicode feature isn't enabled,
67 // skip it. This is a little aggressive, but the test suite doesn't
68 // have any easy way of communicating which Unicode features are needed.
69 if test.unicode() && !cfg!(feature = "unicode") {
70 return skip;
71 }
72 let re = RegexSetBuilder::new(test.regexes())
73 .case_insensitive(test.case_insensitive())
74 .unicode(test.unicode())
75 .line_terminator(test.line_terminator())
76 .build()?;
77 Ok(CompiledRegex::compiled(move |test| run_test(&re, test)))
78 }