From 4a8679824b9ddf950e6754231755d5d065692c47 Mon Sep 17 00:00:00 2001 From: David Lattimore Date: Fri, 3 Jul 2020 13:15:00 +1000 Subject: SSR: Improve error reporting when a test fails --- crates/ra_ssr/src/lib.rs | 30 +++++++++++++++--------------- crates/ra_ssr/src/tests.rs | 27 +++++++++++++++++++++------ 2 files changed, 36 insertions(+), 21 deletions(-) (limited to 'crates/ra_ssr') diff --git a/crates/ra_ssr/src/lib.rs b/crates/ra_ssr/src/lib.rs index d6862356d..cca4576ce 100644 --- a/crates/ra_ssr/src/lib.rs +++ b/crates/ra_ssr/src/lib.rs @@ -201,9 +201,8 @@ impl<'db> MatchFinder<'db> { ); } } - } 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.matched { + Ok(_) => writeln!(f, "Node matched")?, + Err(reason) => writeln!(f, "Node failed to match because: {}", reason.reason)?, + } + writeln!( + f, + "============ AST ===========\n\ + {:#?}", + self.node + )?; + writeln!(f, "========= PATTERN ==========")?; match &self.pattern { Ok(pattern) => { - write!(f, "{:#?}", pattern)?; + writeln!(f, "{:#?}", pattern)?; } Err(err) => { - write!(f, "{}", err.reason)?; + writeln!(f, "{}", err.reason)?; } } - write!( - f, - "\n============ AST ===========\n\ - {:#?}\n============================\n", - self.node - )?; - match &self.matched { - Ok(_) => write!(f, "Node matched")?, - Err(reason) => write!(f, "Node failed to match because: {}", reason.reason)?, - } + writeln!(f, "============================")?; Ok(()) } } diff --git a/crates/ra_ssr/src/tests.rs b/crates/ra_ssr/src/tests.rs index 9568d4432..9628dcbac 100644 --- a/crates/ra_ssr/src/tests.rs +++ b/crates/ra_ssr/src/tests.rs @@ -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) { -- cgit v1.2.3