5368: Compress match checking tests r=matklad a=matklad



bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-07-14 11:13:58 +00:00 committed by GitHub
commit 735baad6ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 543 additions and 1299 deletions

View file

@ -244,3 +244,28 @@ impl AstDiagnostic for MismatchedArgCount {
ast::CallExpr::cast(node).unwrap() ast::CallExpr::cast(node).unwrap()
} }
} }
#[cfg(test)]
fn check_diagnostics(ra_fixture: &str) {
use ra_db::{fixture::WithFixture, FileId};
use ra_syntax::TextRange;
use rustc_hash::FxHashMap;
use crate::test_db::TestDB;
let db = TestDB::with_files(ra_fixture);
let annotations = db.extract_annotations();
let mut actual: FxHashMap<FileId, Vec<(TextRange, String)>> = FxHashMap::default();
db.diag(|d| {
// FXIME: macros...
let file_id = d.source().file_id.original_file(&db);
let range = d.syntax_node(&db).text_range();
// FIXME: support multi-line messages in annotations
let message = d.message().lines().next().unwrap().to_owned();
actual.entry(file_id).or_default().push((range, message));
});
actual.values_mut().for_each(|diags| diags.sort_by_key(|it| it.0.start()));
assert_eq!(annotations, actual);
}

View file

@ -376,146 +376,117 @@ pub fn record_pattern_missing_fields(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use expect::{expect, Expect}; use crate::diagnostics::check_diagnostics;
use ra_db::fixture::WithFixture;
use crate::{diagnostics::MismatchedArgCount, test_db::TestDB};
fn check_diagnostic(ra_fixture: &str, expect: Expect) {
let msg = TestDB::with_single_file(ra_fixture).0.diagnostic::<MismatchedArgCount>().0;
expect.assert_eq(&msg);
}
fn check_no_diagnostic(ra_fixture: &str) {
let (s, diagnostic_count) =
TestDB::with_single_file(ra_fixture).0.diagnostic::<MismatchedArgCount>();
assert_eq!(0, diagnostic_count, "expected no diagnostic, found one: {}", s);
}
#[test] #[test]
fn simple_free_fn_zero() { fn simple_free_fn_zero() {
check_diagnostic( check_diagnostics(
r" r#"
fn zero() {} fn zero() {}
fn f() { zero(1); } fn f() { zero(1); }
", //^^^^^^^ Expected 0 arguments, found 1
expect![["\"zero(1)\": Expected 0 arguments, found 1\n"]], "#,
); );
check_no_diagnostic( check_diagnostics(
r" r#"
fn zero() {} fn zero() {}
fn f() { zero(); } fn f() { zero(); }
", "#,
); );
} }
#[test] #[test]
fn simple_free_fn_one() { fn simple_free_fn_one() {
check_diagnostic( check_diagnostics(
r" r#"
fn one(arg: u8) {} fn one(arg: u8) {}
fn f() { one(); } fn f() { one(); }
", //^^^^^ Expected 1 argument, found 0
expect![["\"one()\": Expected 1 argument, found 0\n"]], "#,
); );
check_no_diagnostic( check_diagnostics(
r" r#"
fn one(arg: u8) {} fn one(arg: u8) {}
fn f() { one(1); } fn f() { one(1); }
", "#,
); );
} }
#[test] #[test]
fn method_as_fn() { fn method_as_fn() {
check_diagnostic( check_diagnostics(
r" r#"
struct S; struct S;
impl S { impl S { fn method(&self) {} }
fn method(&self) {}
}
fn f() { fn f() {
S::method(); S::method();
} } //^^^^^^^^^^^ Expected 1 argument, found 0
", "#,
expect![["\"S::method()\": Expected 1 argument, found 0\n"]],
); );
check_no_diagnostic( check_diagnostics(
r" r#"
struct S; struct S;
impl S { impl S { fn method(&self) {} }
fn method(&self) {}
}
fn f() { fn f() {
S::method(&S); S::method(&S);
S.method(); S.method();
} }
", "#,
); );
} }
#[test] #[test]
fn method_with_arg() { fn method_with_arg() {
check_diagnostic( check_diagnostics(
r" r#"
struct S; struct S;
impl S { impl S { fn method(&self, arg: u8) {} }
fn method(&self, arg: u8) {}
}
fn f() { fn f() {
S.method(); S.method();
} } //^^^^^^^^^^ Expected 1 argument, found 0
", "#,
expect![["\"S.method()\": Expected 1 argument, found 0\n"]],
); );
check_no_diagnostic( check_diagnostics(
r" r#"
struct S; struct S;
impl S { impl S { fn method(&self, arg: u8) {} }
fn method(&self, arg: u8) {}
}
fn f() { fn f() {
S::method(&S, 0); S::method(&S, 0);
S.method(1); S.method(1);
} }
", "#,
); );
} }
#[test] #[test]
fn tuple_struct() { fn tuple_struct() {
check_diagnostic( check_diagnostics(
r" r#"
struct Tup(u8, u16); struct Tup(u8, u16);
fn f() { fn f() {
Tup(0); Tup(0);
} } //^^^^^^ Expected 2 arguments, found 1
", "#,
expect![["\"Tup(0)\": Expected 2 arguments, found 1\n"]],
) )
} }
#[test] #[test]
fn enum_variant() { fn enum_variant() {
check_diagnostic( check_diagnostics(
r" r#"
enum En { enum En { Variant(u8, u16), }
Variant(u8, u16),
}
fn f() { fn f() {
En::Variant(0); En::Variant(0);
} } //^^^^^^^^^^^^^^ Expected 2 arguments, found 1
", "#,
expect![["\"En::Variant(0)\": Expected 2 arguments, found 1\n"]],
) )
} }
} }

File diff suppressed because it is too large Load diff

View file

@ -82,7 +82,7 @@ impl FileLoader for TestDB {
} }
impl TestDB { impl TestDB {
pub fn module_for_file(&self, file_id: FileId) -> ModuleId { pub(crate) fn module_for_file(&self, file_id: FileId) -> ModuleId {
for &krate in self.relevant_crates(file_id).iter() { for &krate in self.relevant_crates(file_id).iter() {
let crate_def_map = self.crate_def_map(krate); let crate_def_map = self.crate_def_map(krate);
for (local_id, data) in crate_def_map.modules.iter() { for (local_id, data) in crate_def_map.modules.iter() {
@ -94,7 +94,7 @@ impl TestDB {
panic!("Can't find module for file") panic!("Can't find module for file")
} }
fn diag<F: FnMut(&dyn Diagnostic)>(&self, mut cb: F) { pub(crate) fn diag<F: FnMut(&dyn Diagnostic)>(&self, mut cb: F) {
let crate_graph = self.crate_graph(); let crate_graph = self.crate_graph();
for krate in crate_graph.iter() { for krate in crate_graph.iter() {
let crate_def_map = self.crate_def_map(krate); let crate_def_map = self.crate_def_map(krate);
@ -124,7 +124,7 @@ impl TestDB {
} }
} }
pub fn diagnostics(&self) -> (String, u32) { pub(crate) fn diagnostics(&self) -> (String, u32) {
let mut buf = String::new(); let mut buf = String::new();
let mut count = 0; let mut count = 0;
self.diag(|d| { self.diag(|d| {
@ -134,22 +134,7 @@ impl TestDB {
(buf, count) (buf, count)
} }
/// Like `diagnostics`, but filtered for a single diagnostic. pub(crate) fn extract_annotations(&self) -> FxHashMap<FileId, Vec<(TextRange, String)>> {
pub fn diagnostic<D: Diagnostic>(&self) -> (String, u32) {
let mut buf = String::new();
let mut count = 0;
self.diag(|d| {
// We want to filter diagnostics by the particular one we are testing for, to
// avoid surprising results in tests.
if d.downcast_ref::<D>().is_some() {
format_to!(buf, "{:?}: {}\n", d.syntax_node(self).text(), d.message());
count += 1;
};
});
(buf, count)
}
pub fn extract_annotations(&self) -> FxHashMap<FileId, Vec<(TextRange, String)>> {
let mut files = Vec::new(); let mut files = Vec::new();
let crate_graph = self.crate_graph(); let crate_graph = self.crate_graph();
for krate in crate_graph.iter() { for krate in crate_graph.iter() {