diff options
author | Aleksey Kladov <[email protected]> | 2020-06-29 16:22:47 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-06-29 16:23:01 +0100 |
commit | bbc4dc995612916a4c0a99396b5259a5fb1dda80 (patch) | |
tree | fac101a18e111a2a05bce14fbb68981c5bef3bfd /crates/ra_hir_ty/src/tests.rs | |
parent | e805e8c1d5bf26e9716fb855f97d950395129c20 (diff) |
Update the rest of the tests
Diffstat (limited to 'crates/ra_hir_ty/src/tests.rs')
-rw-r--r-- | crates/ra_hir_ty/src/tests.rs | 46 |
1 files changed, 21 insertions, 25 deletions
diff --git a/crates/ra_hir_ty/src/tests.rs b/crates/ra_hir_ty/src/tests.rs index 4d0dc3011..5424e6bb1 100644 --- a/crates/ra_hir_ty/src/tests.rs +++ b/crates/ra_hir_ty/src/tests.rs | |||
@@ -17,11 +17,11 @@ use hir_def::{ | |||
17 | item_scope::ItemScope, | 17 | item_scope::ItemScope, |
18 | keys, | 18 | keys, |
19 | nameres::CrateDefMap, | 19 | nameres::CrateDefMap, |
20 | AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId, ModuleId, | 20 | AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId, |
21 | }; | 21 | }; |
22 | use hir_expand::{db::AstDatabase, InFile}; | 22 | use hir_expand::{db::AstDatabase, InFile}; |
23 | use insta::assert_snapshot; | 23 | use insta::assert_snapshot; |
24 | use ra_db::{fixture::WithFixture, salsa::Database, FilePosition, SourceDatabase}; | 24 | use ra_db::{fixture::WithFixture, salsa::Database, FileRange, SourceDatabase}; |
25 | use ra_syntax::{ | 25 | use ra_syntax::{ |
26 | algo, | 26 | algo, |
27 | ast::{self, AstNode}, | 27 | ast::{self, AstNode}, |
@@ -39,13 +39,27 @@ use crate::{ | |||
39 | // update the snapshots. | 39 | // update the snapshots. |
40 | 40 | ||
41 | fn check_types(ra_fixture: &str) { | 41 | 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) { | ||
42 | let db = TestDB::with_files(ra_fixture); | 50 | let db = TestDB::with_files(ra_fixture); |
43 | let mut checked_one = false; | 51 | let mut checked_one = false; |
44 | for file_id in db.all_files() { | 52 | for file_id in db.all_files() { |
45 | let text = db.parse(file_id).syntax_node().to_string(); | 53 | let text = db.parse(file_id).syntax_node().to_string(); |
46 | let annotations = extract_annotations(&text); | 54 | let annotations = extract_annotations(&text); |
47 | for (offset, expected) in annotations { | 55 | for (range, expected) in annotations { |
48 | let actual = type_at_pos(&db, FilePosition { file_id, offset }); | 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 | }; | ||
49 | assert_eq!(expected, actual); | 63 | assert_eq!(expected, actual); |
50 | checked_one = true; | 64 | checked_one = true; |
51 | } | 65 | } |
@@ -53,21 +67,9 @@ fn check_types(ra_fixture: &str) { | |||
53 | assert!(checked_one, "no `//^` annotations found"); | 67 | assert!(checked_one, "no `//^` annotations found"); |
54 | } | 68 | } |
55 | 69 | ||
56 | fn type_at_pos(db: &TestDB, pos: FilePosition) -> String { | 70 | fn type_at_range(db: &TestDB, pos: FileRange) -> Ty { |
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 { | ||
69 | let file = db.parse(pos.file_id).ok().unwrap(); | 71 | 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(); |
71 | let fn_def = expr.syntax().ancestors().find_map(ast::FnDef::cast).unwrap(); | 73 | let fn_def = expr.syntax().ancestors().find_map(ast::FnDef::cast).unwrap(); |
72 | let module = db.module_for_file(pos.file_id); | 74 | let module = db.module_for_file(pos.file_id); |
73 | let func = *module.child_by_source(db)[keys::FUNCTION] | 75 | let func = *module.child_by_source(db)[keys::FUNCTION] |
@@ -77,17 +79,11 @@ fn type_at_pos_displayed( | |||
77 | let (_body, source_map) = db.body_with_source_map(func.into()); | 79 | let (_body, source_map) = db.body_with_source_map(func.into()); |
78 | if let Some(expr_id) = source_map.node_expr(InFile::new(pos.file_id.into(), &expr)) { | 80 | if let Some(expr_id) = source_map.node_expr(InFile::new(pos.file_id.into(), &expr)) { |
79 | let infer = db.infer(func.into()); | 81 | let infer = db.infer(func.into()); |
80 | let ty = &infer[expr_id]; | 82 | return infer[expr_id].clone(); |
81 | return display_fn(ty, module); | ||
82 | } | 83 | } |
83 | panic!("Can't find expression") | 84 | panic!("Can't find expression") |
84 | } | 85 | } |
85 | 86 | ||
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 | |||
91 | fn infer(ra_fixture: &str) -> String { | 87 | fn infer(ra_fixture: &str) -> String { |
92 | infer_with_mismatches(ra_fixture, false) | 88 | infer_with_mismatches(ra_fixture, false) |
93 | } | 89 | } |