test_error_messages: add back missing validation

Make sure to also look for the error part that occurs after the last format
specifier.

Still not great because it won't fail if there's unexpected output at the
beginning or end of the string.
This commit is contained in:
Johannes Altmanninger 2024-01-19 06:09:20 +01:00
parent 21b4a2e5d6
commit 7597288c18

View File

@ -17,7 +17,7 @@ fn test_error_messages() {
// Given a format string, returns a list of non-empty strings separated by format specifiers. The // Given a format string, returns a list of non-empty strings separated by format specifiers. The
// format specifiers themselves are omitted. // format specifiers themselves are omitted.
fn separate_by_format_specifiers(format: &wstr) -> Vec<&wstr> { fn separate_by_format_specifiers(format: &wstr) -> Vec<&wstr> {
let format_specifier_regex = Regex::new(L!(r"%l?[ds]").as_char_slice()).unwrap(); let format_specifier_regex = Regex::new(L!(r"%l?[cds]").as_char_slice()).unwrap();
let mut result = vec![]; let mut result = vec![];
let mut offset = 0; let mut offset = 0;
for mtch in format_specifier_regex.find_iter(format.as_char_slice()) { for mtch in format_specifier_regex.find_iter(format.as_char_slice()) {
@ -25,6 +25,7 @@ fn test_error_messages() {
result.push(&format[offset..mtch.start()]); result.push(&format[offset..mtch.start()]);
offset = mtch.end(); offset = mtch.end();
} }
result.push(&format[offset..]);
result result
} }
@ -33,6 +34,7 @@ fn test_error_messages() {
// that each of the remaining chunks is found (in order) in the string. // that each of the remaining chunks is found (in order) in the string.
fn string_matches_format(s: &wstr, format: &wstr) -> bool { fn string_matches_format(s: &wstr, format: &wstr) -> bool {
let components = separate_by_format_specifiers(format); let components = separate_by_format_specifiers(format);
assert!(!components.is_empty());
let mut idx = 0; let mut idx = 0;
for component in components { for component in components {
let Some(relpos) = s[idx..].find(component) else { let Some(relpos) = s[idx..].find(component) else {