SSR: Improve error reporting when a test fails

This commit is contained in:
David Lattimore 2020-07-03 13:15:00 +10:00
parent a5ef644a16
commit 4a8679824b
2 changed files with 37 additions and 22 deletions

View File

@ -201,9 +201,8 @@ fn output_debug_for_nodes_at_range(
); );
} }
} }
} else {
self.output_debug_for_nodes_at_range(&node, range, restrict_range, out);
} }
self.output_debug_for_nodes_at_range(&node, range, restrict_range, out);
} }
} }
} }
@ -218,25 +217,26 @@ pub struct MatchDebugInfo {
impl std::fmt::Debug for MatchDebugInfo { impl std::fmt::Debug for MatchDebugInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "========= PATTERN ==========\n")?; match &self.matched {
match &self.pattern { Ok(_) => writeln!(f, "Node matched")?,
Ok(pattern) => { Err(reason) => writeln!(f, "Node failed to match because: {}", reason.reason)?,
write!(f, "{:#?}", pattern)?;
} }
Err(err) => { writeln!(
write!(f, "{}", err.reason)?;
}
}
write!(
f, f,
"\n============ AST ===========\n\ "============ AST ===========\n\
{:#?}\n============================\n", {:#?}",
self.node self.node
)?; )?;
match &self.matched { writeln!(f, "========= PATTERN ==========")?;
Ok(_) => write!(f, "Node matched")?, match &self.pattern {
Err(reason) => write!(f, "Node failed to match because: {}", reason.reason)?, Ok(pattern) => {
writeln!(f, "{:#?}", pattern)?;
} }
Err(err) => {
writeln!(f, "{}", err.reason)?;
}
}
writeln!(f, "============================")?;
Ok(()) Ok(())
} }
} }

View File

@ -91,6 +91,18 @@ fn assert_ssr_transforms(rules: &[&str], input: &str, result: &str) {
} }
} }
fn print_match_debug_info(match_finder: &MatchFinder, file_id: FileId, snippet: &str) {
let debug_info = match_finder.debug_where_text_equal(file_id, snippet);
println!(
"Match debug info: {} nodes had text exactly equal to '{}'",
debug_info.len(),
snippet
);
for (index, d) in debug_info.iter().enumerate() {
println!("Node #{}\n{:#?}\n", index, d);
}
}
fn assert_matches(pattern: &str, code: &str, expected: &[&str]) { fn assert_matches(pattern: &str, code: &str, expected: &[&str]) {
let (db, file_id) = single_file(code); let (db, file_id) = single_file(code);
let mut match_finder = MatchFinder::new(&db); let mut match_finder = MatchFinder::new(&db);
@ -103,17 +115,20 @@ fn assert_matches(pattern: &str, code: &str, expected: &[&str]) {
.map(|m| m.matched_text()) .map(|m| m.matched_text())
.collect(); .collect();
if matched_strings != expected && !expected.is_empty() { if matched_strings != expected && !expected.is_empty() {
let debug_info = match_finder.debug_where_text_equal(file_id, &expected[0]); print_match_debug_info(&match_finder, file_id, &expected[0]);
eprintln!("Test is about to fail. Some possibly useful info: {} nodes had text exactly equal to '{}'", debug_info.len(), &expected[0]);
for d in debug_info {
eprintln!("{:#?}", d);
}
} }
assert_eq!(matched_strings, expected); assert_eq!(matched_strings, expected);
} }
fn assert_no_match(pattern: &str, code: &str) { fn assert_no_match(pattern: &str, code: &str) {
assert_matches(pattern, code, &[]); let (db, file_id) = single_file(code);
let mut match_finder = MatchFinder::new(&db);
match_finder.add_search_pattern(pattern.parse().unwrap());
let matches = match_finder.find_matches_in_file(file_id).flattened().matches;
if !matches.is_empty() {
print_match_debug_info(&match_finder, file_id, &matches[0].matched_text());
panic!("Got {} matches when we expected none: {:#?}", matches.len(), matches);
}
} }
fn assert_match_failure_reason(pattern: &str, code: &str, snippet: &str, expected_reason: &str) { fn assert_match_failure_reason(pattern: &str, code: &str, snippet: &str, expected_reason: &str) {