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 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "========= PATTERN ==========\n")?;
match &self.pattern {
Ok(pattern) => {
write!(f, "{:#?}", pattern)?;
match &self.matched {
Ok(_) => writeln!(f, "Node matched")?,
Err(reason) => writeln!(f, "Node failed to match because: {}", reason.reason)?,
}
Err(err) => {
write!(f, "{}", err.reason)?;
}
}
write!(
writeln!(
f,
"\n============ AST ===========\n\
{:#?}\n============================\n",
"============ AST ===========\n\
{:#?}",
self.node
)?;
match &self.matched {
Ok(_) => write!(f, "Node matched")?,
Err(reason) => write!(f, "Node failed to match because: {}", reason.reason)?,
writeln!(f, "========= PATTERN ==========")?;
match &self.pattern {
Ok(pattern) => {
writeln!(f, "{:#?}", pattern)?;
}
Err(err) => {
writeln!(f, "{}", err.reason)?;
}
}
writeln!(f, "============================")?;
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]) {
let (db, file_id) = single_file(code);
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())
.collect();
if matched_strings != expected && !expected.is_empty() {
let debug_info = match_finder.debug_where_text_equal(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);
}
print_match_debug_info(&match_finder, file_id, &expected[0]);
}
assert_eq!(matched_strings, expected);
}
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) {