Debian Codegraph @

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

raw
1 use regex::{bytes, regex, Regex};
2
3 #[test]
4 fn unclosed_group_error() {
5 let err = Regex::new(r"(").unwrap_err();
6 let msg = err.to_string();
7 assert!(msg.contains("unclosed group"), "error message: {msg:?}");
8 }
9
10 #[test]
11 fn regex_string() {
12 assert_eq!(r"[a-zA-Z0-9]+", regex!(r"[a-zA-Z0-9]+").as_str());
13 assert_eq!(r"[a-zA-Z0-9]+", &format!("{}", regex!(r"[a-zA-Z0-9]+")));
14 assert_eq!(
15 r#"Regex("[a-zA-Z0-9]+")"#,
16 &format!("{:?}", regex!(r"[a-zA-Z0-9]+"))
17 );
18 }
19
20 #[test]
21 fn capture_names() {
22 let re = regex!(r"(.)(?P<a>.)");
23 assert_eq!(3, re.captures_len());
24 assert_eq!((3, Some(3)), re.capture_names().size_hint());
25 assert_eq!(
26 vec![None, None, Some("a")],
27 re.capture_names().collect::<Vec<_>>()
28 );
29 }
30
31 #[test]
32 fn capture_index() {
33 let re = regex!(r"^(?P<name>.+)$");
34 let cap = re.captures("abc").unwrap();
35 assert_eq!(&cap[0], "abc");
36 assert_eq!(&cap[1], "abc");
37 assert_eq!(&cap["name"], "abc");
38 }
39
40 #[test]
41 #[should_panic]
42 fn capture_index_panic_usize() {
43 let re = regex!(r"^(?P<name>.+)$");
44 let cap = re.captures("abc").unwrap();
45 let _ = cap[2];
46 }
47
48 #[test]
49 #[should_panic]
50 fn capture_index_panic_name() {
51 let re = regex!(r"^(?P<name>.+)$");
52 let cap = re.captures("abc").unwrap();
53 let _ = cap["bad name"];
54 }
55
56 #[test]
57 fn capture_index_lifetime() {
58 // This is a test of whether the types on `caps["..."]` are general
59 // enough. If not, this will fail to typecheck.
60 fn inner(s: &str) -> usize {
61 let re = regex!(r"(?P<number>[0-9]+)");
62 let caps = re.captures(s).unwrap();
63 caps["number"].len()
64 }
65 assert_eq!(3, inner("123"));
66 }
67
68 #[test]
69 fn capture_misc() {
70 let re = regex!(r"(.)(?P<a>a)?(.)(?P<b>.)");
71 let cap = re.captures("abc").unwrap();
72
73 assert_eq!(5, cap.len());
74
75 assert_eq!((0, 3), {
76 let m = cap.get(0).unwrap();
77 (m.start(), m.end())
78 });
79 assert_eq!(None, cap.get(2));
80 assert_eq!((2, 3), {
81 let m = cap.get(4).unwrap();
82 (m.start(), m.end())
83 });
84
85 assert_eq!("abc", cap.get(0).unwrap().as_str());
86 assert_eq!(None, cap.get(2));
87 assert_eq!("c", cap.get(4).unwrap().as_str());
88
89 assert_eq!(None, cap.name("a"));
90 assert_eq!("c", cap.name("b").unwrap().as_str());
91 }
92
93 #[test]
94 fn sub_capture_matches() {
95 let re = regex!(r"([a-z])(([a-z])|([0-9]))");
96 let cap = re.captures("a5").unwrap();
97 let subs: Vec<_> = cap.iter().collect();
98
99 assert_eq!(5, subs.len());
100 assert!(subs[0].is_some());
101 assert!(subs[1].is_some());
102 assert!(subs[2].is_some());
103 assert!(subs[3].is_none());
104 assert!(subs[4].is_some());
105
106 assert_eq!("a5", subs[0].unwrap().as_str());
107 assert_eq!("a", subs[1].unwrap().as_str());
108 assert_eq!("5", subs[2].unwrap().as_str());
109 assert_eq!("5", subs[4].unwrap().as_str());
110 }
111
112 // Test that the DFA can handle pathological cases. (This should result in the
113 // DFA's cache being flushed too frequently, which should cause it to quit and
114 // fall back to the NFA algorithm.)
115 #[test]
116 fn dfa_handles_pathological_case() {
117 fn ones_and_zeroes(count: usize) -> String {
118 let mut s = String::new();
119 for i in 0..count {
120 if i % 3 == 0 {
121 s.push('1');
122 } else {
123 s.push('0');
124 }
125 }
126 s
127 }
128
129 let re = regex!(r"[01]*1[01]{20}$");
130 let text = {
131 let mut pieces = ones_and_zeroes(100_000);
132 pieces.push('1');
133 pieces.push_str(&ones_and_zeroes(20));
134 pieces
135 };
136 assert!(re.is_match(&text));
137 }
138
139 #[test]
140 fn re_macro() {
141 // >1 regex! should work in a scope since the static is in a block
142 assert!(regex!("foo").is_match("foo"));
143 assert!(regex!("bar").is_match("bar"));
144
145 let re: &Regex = regex!("foo");
146 ensure_static(re);
147
148 fn ensure_static(_re: &'static Regex) {}
149 }
150
151 #[test]
152 fn re_bytes_macro() {
153 // >1 regex! should work in a scope since the static is in a block
154 assert!(bytes::regex!("foo").is_match(b"foo"));
155 assert!(bytes::regex!("bar").is_match(b"bar"));
156
157 let re: &bytes::Regex = bytes::regex!("foo");
158 ensure_static(re);
159
160 fn ensure_static(_re: &'static bytes::Regex) {}
161 }