Skip to content

Commit ece1195

Browse files
committed
update: check_diagnostics_ignore_syntax_error
1 parent cab9148 commit ece1195

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

crates/ide-diagnostics/src/handlers/missing_match_arms.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub(crate) fn missing_match_arms(
1717

1818
#[cfg(test)]
1919
mod tests {
20-
use crate::tests::check_diagnostics;
20+
use crate::tests::{check_diagnostics, check_diagnostics_ignore_syntax_error};
2121

2222
#[track_caller]
2323
fn check_diagnostics_no_bails(ra_fixture: &str) {
@@ -27,11 +27,10 @@ mod tests {
2727

2828
#[test]
2929
fn empty_body() {
30-
check_diagnostics_no_bails(
30+
check_diagnostics_ignore_syntax_error(
3131
r#"
3232
fn main() {
3333
match 0;
34-
//^ error: Syntax Error: expected `{`
3534
}
3635
"#,
3736
);

crates/ide-diagnostics/src/tests.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,65 @@ pub(crate) fn check_diagnostics_with_config(config: DiagnosticsConfig, ra_fixtur
154154
}
155155
}
156156

157+
#[track_caller]
158+
pub(crate) fn check_diagnostics_ignore_syntax_error(ra_fixture: &str) {
159+
let (db, files) = RootDatabase::with_many_files(ra_fixture);
160+
let mut config = DiagnosticsConfig::test_sample();
161+
config.disabled.insert("inactive-code".to_string());
162+
for file_id in files {
163+
let line_index = db.line_index(file_id);
164+
let diagnostics = super::diagnostics(&db, &config, &AssistResolveStrategy::All, file_id);
165+
166+
let expected = extract_annotations(&db.file_text(file_id));
167+
let mut actual = diagnostics
168+
.into_iter()
169+
.filter_map(|d| {
170+
if d.message.contains("Syntax Error: ") {
171+
return None;
172+
}
173+
let mut annotation = String::new();
174+
if let Some(fixes) = &d.fixes {
175+
assert!(!fixes.is_empty());
176+
annotation.push_str("💡 ")
177+
}
178+
annotation.push_str(match d.severity {
179+
Severity::Error => "error",
180+
Severity::WeakWarning => "weak",
181+
Severity::Warning => "warn",
182+
Severity::Allow => "allow",
183+
});
184+
annotation.push_str(": ");
185+
annotation.push_str(&d.message);
186+
Some((d.range, annotation))
187+
})
188+
.collect::<Vec<_>>();
189+
actual.sort_by_key(|(range, _)| range.start());
190+
if expected.is_empty() {
191+
// makes minicore smoke test debugable
192+
for (e, _) in &actual {
193+
eprintln!(
194+
"Code in range {e:?} = {}",
195+
&db.file_text(file_id)[usize::from(e.start())..usize::from(e.end())]
196+
)
197+
}
198+
}
199+
if expected != actual {
200+
let fneg = expected
201+
.iter()
202+
.filter(|x| !actual.contains(x))
203+
.map(|(range, s)| (line_index.line_col(range.start()), range, s))
204+
.collect::<Vec<_>>();
205+
let fpos = actual
206+
.iter()
207+
.filter(|x| !expected.contains(x))
208+
.map(|(range, s)| (line_index.line_col(range.start()), range, s))
209+
.collect::<Vec<_>>();
210+
211+
panic!("Diagnostic test failed.\nFalse negatives: {fneg:?}\nFalse positives: {fpos:?}");
212+
}
213+
}
214+
}
215+
157216
#[test]
158217
fn test_disabled_diagnostics() {
159218
let mut config = DiagnosticsConfig::test_sample();

0 commit comments

Comments
 (0)