Skip to content

Commit bbc4dc9

Browse files
committed
Update the rest of the tests
1 parent e805e8c commit bbc4dc9

File tree

9 files changed

+311
-446
lines changed

9 files changed

+311
-446
lines changed

crates/ra_hir_ty/src/tests.rs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ 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},
@@ -39,35 +39,37 @@ use crate::{
3939
// update the snapshots.
4040

4141
fn check_types(ra_fixture: &str) {
42+
check_types_impl(ra_fixture, false)
43+
}
44+
45+
fn check_types_source_code(ra_fixture: &str) {
46+
check_types_impl(ra_fixture, true)
47+
}
48+
49+
fn check_types_impl(ra_fixture: &str, display_source: bool) {
4250
let db = TestDB::with_files(ra_fixture);
4351
let mut checked_one = false;
4452
for file_id in db.all_files() {
4553
let text = db.parse(file_id).syntax_node().to_string();
4654
let annotations = extract_annotations(&text);
47-
for (offset, expected) in annotations {
48-
let actual = type_at_pos(&db, FilePosition { file_id, offset });
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+
};
4963
assert_eq!(expected, actual);
5064
checked_one = true;
5165
}
5266
}
5367
assert!(checked_one, "no `//^` annotations found");
5468
}
5569

56-
fn type_at_pos(db: &TestDB, pos: FilePosition) -> String {
57-
type_at_pos_displayed(db, pos, |ty, _| ty.display(db).to_string())
58-
}
59-
60-
fn displayed_source_at_pos(db: &TestDB, pos: FilePosition) -> String {
61-
type_at_pos_displayed(db, pos, |ty, module_id| ty.display_source_code(db, module_id).unwrap())
62-
}
63-
64-
fn type_at_pos_displayed(
65-
db: &TestDB,
66-
pos: FilePosition,
67-
display_fn: impl FnOnce(&Ty, ModuleId) -> String,
68-
) -> String {
70+
fn type_at_range(db: &TestDB, pos: FileRange) -> Ty {
6971
let file = db.parse(pos.file_id).ok().unwrap();
70-
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();
7173
let fn_def = expr.syntax().ancestors().find_map(ast::FnDef::cast).unwrap();
7274
let module = db.module_for_file(pos.file_id);
7375
let func = *module.child_by_source(db)[keys::FUNCTION]
@@ -77,17 +79,11 @@ fn type_at_pos_displayed(
7779
let (_body, source_map) = db.body_with_source_map(func.into());
7880
if let Some(expr_id) = source_map.node_expr(InFile::new(pos.file_id.into(), &expr)) {
7981
let infer = db.infer(func.into());
80-
let ty = &infer[expr_id];
81-
return display_fn(ty, module);
82+
return infer[expr_id].clone();
8283
}
8384
panic!("Can't find expression")
8485
}
8586

86-
fn type_at(ra_fixture: &str) -> String {
87-
let (db, file_pos) = TestDB::with_position(ra_fixture);
88-
type_at_pos(&db, file_pos)
89-
}
90-
9187
fn infer(ra_fixture: &str) -> String {
9288
infer_with_mismatches(ra_fixture, false)
9389
}

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)