| 1 |
//! Run tests from Oniguruma's test suite, see `oniguruma/README.md` |
| 2 |
|
| 3 |
use std::collections::HashMap; |
| 4 |
use std::panic; |
| 5 |
|
| 6 |
use regex::Regex; |
| 7 |
|
| 8 |
use fancy_regex::RegexBuilder; |
| 9 |
|
| 10 |
#[derive(Debug, Eq, Hash, PartialEq)] |
| 11 |
struct Test { |
| 12 |
source: String, |
| 13 |
pattern: String, |
| 14 |
text: String, |
| 15 |
assertion: Assertion, |
| 16 |
} |
| 17 |
|
| 18 |
#[derive(Debug, Eq, Hash, PartialEq)] |
| 19 |
enum Assertion { |
| 20 |
Match { |
| 21 |
group: usize, |
| 22 |
start: usize, |
| 23 |
end: usize, |
| 24 |
}, |
| 25 |
NoMatch, |
| 26 |
} |
| 27 |
|
| 28 |
/// Extract tests from the C source file (or the ignore file). |
| 29 |
/// |
| 30 |
/// Returns a vec of tuple of the test data and the comment for the test. |
| 31 |
fn parse_tests(test_source: &str) -> Vec<(Test, String)> { |
| 32 |
let mut tests = Vec::new(); |
| 33 |
|
| 34 |
let c_string = r#""((?:\\\\|\\"|[^"])*)""#; |
| 35 |
let re = Regex::new(&format!( |
| 36 |
r"(?m)((?:^ //.*\n)*)^\s*((x2|x3|n)\({},\s*{},?([^\)]+)\);)", |
| 37 |
c_string, c_string |
| 38 |
)) |
| 39 |
.unwrap(); |
| 40 |
for caps in re.captures_iter(test_source) { |
| 41 |
let comment = caps |
| 42 |
.get(1) |
| 43 |
.unwrap() |
| 44 |
.as_str() |
| 45 |
.replace(" // ", "") |
| 46 |
.trim() |
| 47 |
.to_string(); |
| 48 |
let source = caps.get(2).unwrap().as_str().to_string(); |
| 49 |
let kind = caps.get(3).unwrap().as_str(); |
| 50 |
let pattern = unescape(caps.get(4).unwrap().as_str()); |
| 51 |
let text = unescape(caps.get(5).unwrap().as_str()); |
| 52 |
let args: Vec<usize> = caps |
| 53 |
.get(6) |
| 54 |
.unwrap() |
| 55 |
.as_str() |
| 56 |
.split(",") |
| 57 |
.map(|s| s.trim().parse().unwrap()) |
| 58 |
.collect(); |
| 59 |
|
| 60 |
let assertion = match kind { |
| 61 |
"x2" => Assertion::Match { |
| 62 |
start: args[0], |
| 63 |
end: args[1], |
| 64 |
group: 0, |
| 65 |
}, |
| 66 |
"x3" => Assertion::Match { |
| 67 |
start: args[0], |
| 68 |
end: args[1], |
| 69 |
group: args[2], |
| 70 |
}, |
| 71 |
"n" => Assertion::NoMatch, |
| 72 |
_ => { |
| 73 |
panic!("Unexpected test type {}", kind); |
| 74 |
} |
| 75 |
}; |
| 76 |
|
| 77 |
let test = Test { |
| 78 |
source, |
| 79 |
pattern, |
| 80 |
text, |
| 81 |
assertion, |
| 82 |
}; |
| 83 |
|
| 84 |
tests.push((test, comment)); |
| 85 |
} |
| 86 |
tests |
| 87 |
} |
| 88 |
|
| 89 |
/// Unescape a string as it appears in C source. This is probably not a perfect implementation, but |
| 90 |
/// it's good enough for these tests. |
| 91 |
fn unescape(escaped: &str) -> String { |
| 92 |
let mut s: Vec<u8> = Vec::new(); |
| 93 |
let mut chars = escaped.chars(); |
| 94 |
while let Some(c) = chars.next() { |
| 95 |
match c { |
| 96 |
'\\' => { |
| 97 |
let next = chars.next().expect("Expected character after backslash"); |
| 98 |
match next { |
| 99 |
'\\' => { |
| 100 |
s.push(b'\\'); |
| 101 |
} |
| 102 |
'"' => { |
| 103 |
s.push(b'"'); |
| 104 |
} |
| 105 |
'?' => { |
| 106 |
// '?' has to be escaped in C to avoid trigraphs |
| 107 |
s.push(b'?'); |
| 108 |
} |
| 109 |
'n' => { |
| 110 |
s.push(b'\n'); |
| 111 |
} |
| 112 |
'r' => { |
| 113 |
s.push(b'\r'); |
| 114 |
} |
| 115 |
'0' => { |
| 116 |
// octal escape, e.g. \001 |
| 117 |
let mut octal = String::new(); |
| 118 |
octal.push(chars.next().expect("Expected character after \\0")); |
| 119 |
octal.push(chars.next().expect("Expected second character after \\0")); |
| 120 |
let num = |
| 121 |
u8::from_str_radix(&octal, 8).expect("Error parsing octal number"); |
| 122 |
s.push(num); |
| 123 |
} |
| 124 |
'x' => { |
| 125 |
// hex escape, e.g. \x1f |
| 126 |
let mut hex = String::new(); |
| 127 |
hex.push(chars.next().expect("Expected character after \\x")); |
| 128 |
hex.push(chars.next().expect("Expected second character after \\x")); |
| 129 |
let num = u8::from_str_radix(&hex, 16).expect("Error parsing hex number"); |
| 130 |
s.push(num); |
| 131 |
} |
| 132 |
_ => { |
| 133 |
unimplemented!("Unknown escaped character {} in {}", next, escaped); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
_ => { |
| 138 |
s.append(&mut c.to_string().into_bytes()); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
// Some strings in the test are invalid UTF-8. We handle them via ignores. |
| 143 |
String::from_utf8_lossy(&s).to_string() |
| 144 |
} |
| 145 |
|
| 146 |
fn run_test(test: &Test) -> Option<String> { |
| 147 |
let Test { |
| 148 |
pattern, |
| 149 |
text, |
| 150 |
assertion, |
| 151 |
.. |
| 152 |
} = test; |
| 153 |
|
| 154 |
let compile_result = RegexBuilder::new(pattern) |
| 155 |
.multi_line(true) |
| 156 |
.oniguruma_mode(true) |
| 157 |
.ignore_numbered_groups_when_named_groups_exist(true) |
| 158 |
.build(); |
| 159 |
let Ok(regex) = compile_result else { |
| 160 |
let error = format!("{:?}", compile_result.unwrap_err()); |
| 161 |
return Some(format!("Compile failed: {}", error)); |
| 162 |
}; |
| 163 |
|
| 164 |
match *assertion { |
| 165 |
Assertion::Match { group, start, end } => { |
| 166 |
let captures_result = regex.captures(&text).unwrap(); |
| 167 |
|
| 168 |
if let Some(captures) = captures_result { |
| 169 |
let Some(m) = captures.get(group) else { |
| 170 |
return Some("Expected group to exist".to_owned()); |
| 171 |
}; |
| 172 |
if m.start() != start || m.end() != end { |
| 173 |
Some(format!( |
| 174 |
"Match found at start {} and end {} (expected {} and {})", |
| 175 |
m.start(), |
| 176 |
m.end(), |
| 177 |
start, |
| 178 |
end |
| 179 |
)) |
| 180 |
} else { |
| 181 |
None |
| 182 |
} |
| 183 |
} else { |
| 184 |
Some("No match found".to_string()) |
| 185 |
} |
| 186 |
} |
| 187 |
Assertion::NoMatch => { |
| 188 |
let result = regex.find(text).unwrap(); |
| 189 |
if result.is_some() { |
| 190 |
Some("Match found".to_string()) |
| 191 |
} else { |
| 192 |
// We expected it not to match and it didn't -> good |
| 193 |
None |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
#[test] |
| 200 |
fn oniguruma() { |
| 201 |
let tests: Vec<Test> = parse_tests(include_str!("oniguruma/test_utf8.c")) |
| 202 |
.into_iter() |
| 203 |
.map(|(test, _comment)| test) |
| 204 |
.collect(); |
| 205 |
let ignore: HashMap<Test, String> = parse_tests(include_str!("oniguruma/test_utf8_ignore.c")) |
| 206 |
.into_iter() |
| 207 |
.collect(); |
| 208 |
|
| 209 |
let mut ignored = 0; |
| 210 |
let mut success = 0; |
| 211 |
|
| 212 |
for test in tests { |
| 213 |
let result = run_test(&test); |
| 214 |
|
| 215 |
if let Some(expected_failure) = ignore.get(&test) { |
| 216 |
assert!(result.is_some(), |
| 217 |
"Expected ignored test to fail, but it succeeded. Remove it from the ignore file: {}", &test.source); |
| 218 |
let failure = result.unwrap(); |
| 219 |
assert!(failure.starts_with(expected_failure), |
| 220 |
"Expected failure differed for test, change it in the ignore file: {}\nExpected: {}\nActual : {}\n", |
| 221 |
&test.source, &expected_failure, &failure |
| 222 |
); |
| 223 |
ignored += 1; |
| 224 |
} else { |
| 225 |
if let Some(failure) = result { |
| 226 |
// This is a weird way to do the assertions, but the nice thing about it is that we |
| 227 |
// can run the tests without an "ignore" file and instead of failing, print the |
| 228 |
// content for the ignore file. To do that, disable the assert and enable the print: |
| 229 |
|
| 230 |
// println!(" // {}\n {}\n", failure, test.source); |
| 231 |
assert!(false, "Test {} failed: {}", &test.source, failure); |
| 232 |
} else { |
| 233 |
// println!("Success: {}", test.source); |
| 234 |
success += 1; |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
println!( |
| 240 |
"{} successful Oniguruma tests, {} ignored", |
| 241 |
success, ignored |
| 242 |
); |
| 243 |
} |