Skip to content

Commit 4a86798

Browse files
SSR: Improve error reporting when a test fails
1 parent a5ef644 commit 4a86798

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

crates/ra_ssr/src/lib.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,8 @@ impl<'db> MatchFinder<'db> {
201201
);
202202
}
203203
}
204-
} else {
205-
self.output_debug_for_nodes_at_range(&node, range, restrict_range, out);
206204
}
205+
self.output_debug_for_nodes_at_range(&node, range, restrict_range, out);
207206
}
208207
}
209208
}
@@ -218,25 +217,26 @@ pub struct MatchDebugInfo {
218217

219218
impl std::fmt::Debug for MatchDebugInfo {
220219
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
221-
write!(f, "========= PATTERN ==========\n")?;
220+
match &self.matched {
221+
Ok(_) => writeln!(f, "Node matched")?,
222+
Err(reason) => writeln!(f, "Node failed to match because: {}", reason.reason)?,
223+
}
224+
writeln!(
225+
f,
226+
"============ AST ===========\n\
227+
{:#?}",
228+
self.node
229+
)?;
230+
writeln!(f, "========= PATTERN ==========")?;
222231
match &self.pattern {
223232
Ok(pattern) => {
224-
write!(f, "{:#?}", pattern)?;
233+
writeln!(f, "{:#?}", pattern)?;
225234
}
226235
Err(err) => {
227-
write!(f, "{}", err.reason)?;
236+
writeln!(f, "{}", err.reason)?;
228237
}
229238
}
230-
write!(
231-
f,
232-
"\n============ AST ===========\n\
233-
{:#?}\n============================\n",
234-
self.node
235-
)?;
236-
match &self.matched {
237-
Ok(_) => write!(f, "Node matched")?,
238-
Err(reason) => write!(f, "Node failed to match because: {}", reason.reason)?,
239-
}
239+
writeln!(f, "============================")?;
240240
Ok(())
241241
}
242242
}

crates/ra_ssr/src/tests.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ fn assert_ssr_transforms(rules: &[&str], input: &str, result: &str) {
9191
}
9292
}
9393

94+
fn print_match_debug_info(match_finder: &MatchFinder, file_id: FileId, snippet: &str) {
95+
let debug_info = match_finder.debug_where_text_equal(file_id, snippet);
96+
println!(
97+
"Match debug info: {} nodes had text exactly equal to '{}'",
98+
debug_info.len(),
99+
snippet
100+
);
101+
for (index, d) in debug_info.iter().enumerate() {
102+
println!("Node #{}\n{:#?}\n", index, d);
103+
}
104+
}
105+
94106
fn assert_matches(pattern: &str, code: &str, expected: &[&str]) {
95107
let (db, file_id) = single_file(code);
96108
let mut match_finder = MatchFinder::new(&db);
@@ -103,17 +115,20 @@ fn assert_matches(pattern: &str, code: &str, expected: &[&str]) {
103115
.map(|m| m.matched_text())
104116
.collect();
105117
if matched_strings != expected && !expected.is_empty() {
106-
let debug_info = match_finder.debug_where_text_equal(file_id, &expected[0]);
107-
eprintln!("Test is about to fail. Some possibly useful info: {} nodes had text exactly equal to '{}'", debug_info.len(), &expected[0]);
108-
for d in debug_info {
109-
eprintln!("{:#?}", d);
110-
}
118+
print_match_debug_info(&match_finder, file_id, &expected[0]);
111119
}
112120
assert_eq!(matched_strings, expected);
113121
}
114122

115123
fn assert_no_match(pattern: &str, code: &str) {
116-
assert_matches(pattern, code, &[]);
124+
let (db, file_id) = single_file(code);
125+
let mut match_finder = MatchFinder::new(&db);
126+
match_finder.add_search_pattern(pattern.parse().unwrap());
127+
let matches = match_finder.find_matches_in_file(file_id).flattened().matches;
128+
if !matches.is_empty() {
129+
print_match_debug_info(&match_finder, file_id, &matches[0].matched_text());
130+
panic!("Got {} matches when we expected none: {:#?}", matches.len(), matches);
131+
}
117132
}
118133

119134
fn assert_match_failure_reason(pattern: &str, code: &str, snippet: &str, expected_reason: &str) {

0 commit comments

Comments
 (0)