Skip to content

Commit

Permalink
update: check_diagnostics_ignore_syntax_error
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-Flash committed Nov 27, 2023
1 parent cab9148 commit ece1195
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
5 changes: 2 additions & 3 deletions crates/ide-diagnostics/src/handlers/missing_match_arms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub(crate) fn missing_match_arms(

#[cfg(test)]
mod tests {
use crate::tests::check_diagnostics;
use crate::tests::{check_diagnostics, check_diagnostics_ignore_syntax_error};

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

#[test]
fn empty_body() {
check_diagnostics_no_bails(
check_diagnostics_ignore_syntax_error(
r#"
fn main() {
match 0;
//^ error: Syntax Error: expected `{`
}
"#,
);
Expand Down
59 changes: 59 additions & 0 deletions crates/ide-diagnostics/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,65 @@ pub(crate) fn check_diagnostics_with_config(config: DiagnosticsConfig, ra_fixtur
}
}

#[track_caller]
pub(crate) fn check_diagnostics_ignore_syntax_error(ra_fixture: &str) {
let (db, files) = RootDatabase::with_many_files(ra_fixture);
let mut config = DiagnosticsConfig::test_sample();
config.disabled.insert("inactive-code".to_string());
for file_id in files {
let line_index = db.line_index(file_id);
let diagnostics = super::diagnostics(&db, &config, &AssistResolveStrategy::All, file_id);

let expected = extract_annotations(&db.file_text(file_id));
let mut actual = diagnostics
.into_iter()
.filter_map(|d| {
if d.message.contains("Syntax Error: ") {
return None;
}
let mut annotation = String::new();
if let Some(fixes) = &d.fixes {
assert!(!fixes.is_empty());
annotation.push_str("💡 ")
}
annotation.push_str(match d.severity {
Severity::Error => "error",
Severity::WeakWarning => "weak",
Severity::Warning => "warn",
Severity::Allow => "allow",
});
annotation.push_str(": ");
annotation.push_str(&d.message);
Some((d.range, annotation))
})
.collect::<Vec<_>>();
actual.sort_by_key(|(range, _)| range.start());
if expected.is_empty() {
// makes minicore smoke test debugable
for (e, _) in &actual {
eprintln!(
"Code in range {e:?} = {}",
&db.file_text(file_id)[usize::from(e.start())..usize::from(e.end())]
)
}
}
if expected != actual {
let fneg = expected
.iter()
.filter(|x| !actual.contains(x))
.map(|(range, s)| (line_index.line_col(range.start()), range, s))
.collect::<Vec<_>>();
let fpos = actual
.iter()
.filter(|x| !expected.contains(x))
.map(|(range, s)| (line_index.line_col(range.start()), range, s))
.collect::<Vec<_>>();

panic!("Diagnostic test failed.\nFalse negatives: {fneg:?}\nFalse positives: {fpos:?}");
}
}
}

#[test]
fn test_disabled_diagnostics() {
let mut config = DiagnosticsConfig::test_sample();
Expand Down

0 comments on commit ece1195

Please sign in to comment.