use crate::compilation_state::CompilationState;
use crate::diagnostics::{Diagnostic, DiagnosticLevel};
use crate::slice_options::SliceOptions;
#[must_use]
pub fn diagnostics_from_compilation_state(state: CompilationState, options: &SliceOptions) -> Vec<Diagnostic> {
let mut diagnostics = state.into_diagnostics(options);
diagnostics.retain(|diagnostic| diagnostic.level() != DiagnosticLevel::Allowed);
diagnostics
}
pub fn check_diagnostics<const L: usize>(diagnostics: Vec<Diagnostic>, expected: [impl Into<Diagnostic>; L]) {
if expected.len() != diagnostics.len() {
eprintln!(
"Expected {} diagnostics, but got {}.",
expected.len(),
diagnostics.len()
);
eprintln!("The emitted diagnostics were:");
for diagnostic in diagnostics {
eprintln!("\t{diagnostic:?}");
}
eprintln!();
panic!("test failure");
}
for (expect, diagnostic) in expected.into_iter().zip(diagnostics) {
let expect: Diagnostic = expect.into();
let mut failed = false;
if expect.code() != diagnostic.code() {
eprintln!("diagnostic codes didn't match:");
eprintln!("\texpected '{:?}', but got '{:?}'", expect.code(), diagnostic.code());
failed = true;
}
if expect.message() != diagnostic.message() {
eprintln!("diagnostic messages didn't match:");
eprintln!("\texpected: \"{}\"", expect.message());
eprintln!("\t but got: \"{}\"", diagnostic.message());
failed = true;
}
if expect.span().is_some() && expect.span() != diagnostic.span() {
eprintln!("diagnostic spans didn't match:");
eprintln!("\texpected: \"{:?}\"", expect.span());
eprintln!("\t but got: \"{:?}\"", diagnostic.span());
failed = true;
}
if !expect.notes().is_empty() {
let expected_notes = expect.notes();
let emitted_notes = diagnostic.notes();
if expected_notes.len() != emitted_notes.len() {
eprintln!(
"Expected {} notes, but got {}.",
expected_notes.len(),
emitted_notes.len()
);
eprintln!("The emitted notes were:");
for note in emitted_notes {
eprintln!("\t{note:?}");
}
failed = true;
} else {
for (expected_note, emitted_note) in expected_notes.iter().zip(emitted_notes) {
if expected_note.message != emitted_note.message {
eprintln!("note messages didn't match:");
eprintln!("\texpected: \"{}\"", expected_note.message);
eprintln!("\t but got: \"{}\"", emitted_note.message);
failed = true;
}
if expected_note.span.is_some() && expected_note.span != emitted_note.span {
eprintln!("note spans didn't match:");
eprintln!("\texpected: \"{:?}\"", expected_note.span);
eprintln!("\t but got: \"{:?}\"", emitted_note.span);
failed = true;
}
}
}
}
if failed {
eprintln!();
panic!("test failure");
}
}
}