test diagnostics

This commit is contained in:
Aleksey Kladov 2019-03-24 11:39:47 +03:00
parent c7ffd939f6
commit 4c4a714328
3 changed files with 61 additions and 1 deletions

View File

@ -9,7 +9,7 @@ use relative_path::RelativePathBuf;
use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset};
use rustc_hash::FxHashMap;
use crate::{db, HirInterner};
use crate::{db, HirInterner, diagnostics::DiagnosticSink, DefDatabase};
pub const WORKSPACE: SourceRootId = SourceRootId(0);
@ -70,6 +70,24 @@ impl MockDatabase {
self.set_crate_graph(Arc::new(crate_graph))
}
pub fn diagnostics(&self) -> String {
let mut buf = String::from("\n");
let mut files: Vec<FileId> = self.files.values().map(|&it| it).collect();
files.sort();
for file in files {
let module = crate::source_binder::module_from_file_id(self, file).unwrap();
module.diagnostics(
self,
&mut DiagnosticSink::new(|d| {
let source_file = self.hir_parse(d.file());
let syntax_node = d.syntax_node().to_node(&source_file);
buf += &format!("{:?}: {}\n", syntax_node.text(), d.message());
}),
)
}
buf
}
fn from_fixture(fixture: &str) -> (MockDatabase, Option<FilePosition>) {
let mut db = MockDatabase::default();

View File

@ -552,3 +552,21 @@ foo: v
"###
);
}
#[test]
fn unresolved_module_diagnostics() {
let diagnostics = MockDatabase::with_files(
r"
//- /lib.rs
mod foo;
mod bar;
//- /foo.rs
",
)
.diagnostics();
assert_snapshot_matches!(diagnostics, @r###"
"mod bar;": unresolved module
"###
);
}

View File

@ -2319,3 +2319,27 @@ fn typing_whitespace_inside_a_function_should_not_invalidate_types() {
assert!(!format!("{:?}", events).contains("infer"), "{:#?}", events)
}
}
#[test]
fn no_such_field_diagnostics() {
let diagnostics = MockDatabase::with_files(
r"
//- /lib.rs
struct S { foo: i32, bar: () }
impl S {
fn new() -> S {
S {
foo: 92,
baz: 62,
}
}
}
",
)
.diagnostics();
assert_snapshot_matches!(diagnostics, @r###"
"baz: 62": no such field
"###
);
}