Skip to content

Commit 32ee060

Browse files
bors[bot]matklad
andauthored
Merge #5127
5127: Update the rest of the tests r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 82ce579 + bbc4dc9 commit 32ee060

File tree

12 files changed

+434
-565
lines changed

12 files changed

+434
-565
lines changed

crates/ra_hir_ty/src/test_db.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,19 @@ impl TestDB {
154154
});
155155
(buf, count)
156156
}
157+
158+
pub fn all_files(&self) -> Vec<FileId> {
159+
let mut res = Vec::new();
160+
let crate_graph = self.crate_graph();
161+
for krate in crate_graph.iter() {
162+
let crate_def_map = self.crate_def_map(krate);
163+
for (module_id, _) in crate_def_map.modules.iter() {
164+
let file_id = crate_def_map[module_id].origin.file_id();
165+
res.extend(file_id)
166+
}
167+
}
168+
res
169+
}
157170
}
158171

159172
impl TestDB {

crates/ra_hir_ty/src/tests.rs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@ use hir_def::{
1717
item_scope::ItemScope,
1818
keys,
1919
nameres::CrateDefMap,
20-
AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId, ModuleId,
20+
AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId,
2121
};
2222
use hir_expand::{db::AstDatabase, InFile};
2323
use insta::assert_snapshot;
24-
use ra_db::{fixture::WithFixture, salsa::Database, FilePosition, SourceDatabase};
24+
use ra_db::{fixture::WithFixture, salsa::Database, FileRange, SourceDatabase};
2525
use ra_syntax::{
2626
algo,
2727
ast::{self, AstNode},
2828
SyntaxNode,
2929
};
3030
use stdx::format_to;
31+
use test_utils::extract_annotations;
3132

3233
use crate::{
3334
db::HirDatabase, display::HirDisplay, infer::TypeMismatch, test_db::TestDB, InferenceResult, Ty,
@@ -37,21 +38,38 @@ use crate::{
3738
// against snapshots of the expected results using insta. Use cargo-insta to
3839
// update the snapshots.
3940

40-
fn type_at_pos(db: &TestDB, pos: FilePosition) -> String {
41-
type_at_pos_displayed(db, pos, |ty, _| ty.display(db).to_string())
41+
fn check_types(ra_fixture: &str) {
42+
check_types_impl(ra_fixture, false)
4243
}
4344

44-
fn displayed_source_at_pos(db: &TestDB, pos: FilePosition) -> String {
45-
type_at_pos_displayed(db, pos, |ty, module_id| ty.display_source_code(db, module_id).unwrap())
45+
fn check_types_source_code(ra_fixture: &str) {
46+
check_types_impl(ra_fixture, true)
4647
}
4748

48-
fn type_at_pos_displayed(
49-
db: &TestDB,
50-
pos: FilePosition,
51-
display_fn: impl FnOnce(&Ty, ModuleId) -> String,
52-
) -> String {
49+
fn check_types_impl(ra_fixture: &str, display_source: bool) {
50+
let db = TestDB::with_files(ra_fixture);
51+
let mut checked_one = false;
52+
for file_id in db.all_files() {
53+
let text = db.parse(file_id).syntax_node().to_string();
54+
let annotations = extract_annotations(&text);
55+
for (range, expected) in annotations {
56+
let ty = type_at_range(&db, FileRange { file_id, range });
57+
let actual = if display_source {
58+
let module = db.module_for_file(file_id);
59+
ty.display_source_code(&db, module).unwrap()
60+
} else {
61+
ty.display(&db).to_string()
62+
};
63+
assert_eq!(expected, actual);
64+
checked_one = true;
65+
}
66+
}
67+
assert!(checked_one, "no `//^` annotations found");
68+
}
69+
70+
fn type_at_range(db: &TestDB, pos: FileRange) -> Ty {
5371
let file = db.parse(pos.file_id).ok().unwrap();
54-
let expr = algo::find_node_at_offset::<ast::Expr>(file.syntax(), pos.offset).unwrap();
72+
let expr = algo::find_node_at_range::<ast::Expr>(file.syntax(), pos.range).unwrap();
5573
let fn_def = expr.syntax().ancestors().find_map(ast::FnDef::cast).unwrap();
5674
let module = db.module_for_file(pos.file_id);
5775
let func = *module.child_by_source(db)[keys::FUNCTION]
@@ -61,17 +79,11 @@ fn type_at_pos_displayed(
6179
let (_body, source_map) = db.body_with_source_map(func.into());
6280
if let Some(expr_id) = source_map.node_expr(InFile::new(pos.file_id.into(), &expr)) {
6381
let infer = db.infer(func.into());
64-
let ty = &infer[expr_id];
65-
return display_fn(ty, module);
82+
return infer[expr_id].clone();
6683
}
6784
panic!("Can't find expression")
6885
}
6986

70-
fn type_at(ra_fixture: &str) -> String {
71-
let (db, file_pos) = TestDB::with_position(ra_fixture);
72-
type_at_pos(&db, file_pos)
73-
}
74-
7587
fn infer(ra_fixture: &str) -> String {
7688
infer_with_mismatches(ra_fixture, false)
7789
}

crates/ra_hir_ty/src/tests/coercion.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use super::infer_with_mismatches;
21
use insta::assert_snapshot;
32
use test_utils::mark;
43

4+
use super::infer_with_mismatches;
5+
56
// Infer with some common definitions and impls.
67
fn infer(source: &str) -> String {
78
let defs = r#"
Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,41 @@
1-
use super::displayed_source_at_pos;
2-
use crate::test_db::TestDB;
3-
use ra_db::fixture::WithFixture;
1+
use super::check_types_source_code;
42

53
#[test]
64
fn qualify_path_to_submodule() {
7-
let (db, pos) = TestDB::with_position(
5+
check_types_source_code(
86
r#"
9-
//- /main.rs
10-
117
mod foo {
128
pub struct Foo;
139
}
1410
1511
fn bar() {
1612
let foo: foo::Foo = foo::Foo;
17-
foo<|>
18-
}
13+
foo
14+
} //^ foo::Foo
1915
2016
"#,
2117
);
22-
assert_eq!("foo::Foo", displayed_source_at_pos(&db, pos));
2318
}
2419

2520
#[test]
2621
fn omit_default_type_parameters() {
27-
let (db, pos) = TestDB::with_position(
28-
r"
29-
//- /main.rs
30-
struct Foo<T = u8> { t: T }
31-
fn main() {
32-
let foo = Foo { t: 5u8 };
33-
foo<|>;
34-
}
35-
",
22+
check_types_source_code(
23+
r#"
24+
struct Foo<T = u8> { t: T }
25+
fn main() {
26+
let foo = Foo { t: 5u8 };
27+
foo;
28+
} //^ Foo
29+
"#,
3630
);
37-
assert_eq!("Foo", displayed_source_at_pos(&db, pos));
3831

39-
let (db, pos) = TestDB::with_position(
40-
r"
41-
//- /main.rs
42-
struct Foo<K, T = u8> { k: K, t: T }
43-
fn main() {
44-
let foo = Foo { k: 400, t: 5u8 };
45-
foo<|>;
46-
}
47-
",
32+
check_types_source_code(
33+
r#"
34+
struct Foo<K, T = u8> { k: K, t: T }
35+
fn main() {
36+
let foo = Foo { k: 400, t: 5u8 };
37+
foo;
38+
} //^ Foo<i32>
39+
"#,
4840
);
49-
assert_eq!("Foo<i32>", displayed_source_at_pos(&db, pos));
5041
}

0 commit comments

Comments
 (0)