diff options
Diffstat (limited to 'crates/ra_hir/src/module/nameres')
-rw-r--r-- | crates/ra_hir/src/module/nameres/tests.rs | 66 |
1 files changed, 55 insertions, 11 deletions
diff --git a/crates/ra_hir/src/module/nameres/tests.rs b/crates/ra_hir/src/module/nameres/tests.rs index 03ea5c1d6..ca20f064f 100644 --- a/crates/ra_hir/src/module/nameres/tests.rs +++ b/crates/ra_hir/src/module/nameres/tests.rs | |||
@@ -2,8 +2,8 @@ use std::sync::Arc; | |||
2 | 2 | ||
3 | use salsa::Database; | 3 | use salsa::Database; |
4 | use ra_db::{FilesDatabase, CrateGraph}; | 4 | use ra_db::{FilesDatabase, CrateGraph}; |
5 | use ra_syntax::SmolStr; | ||
6 | use relative_path::RelativePath; | 5 | use relative_path::RelativePath; |
6 | use test_utils::assert_eq_text; | ||
7 | 7 | ||
8 | use crate::{ | 8 | use crate::{ |
9 | self as hir, | 9 | self as hir, |
@@ -21,6 +21,35 @@ fn item_map(fixture: &str) -> (Arc<hir::ItemMap>, hir::ModuleId) { | |||
21 | (db.item_map(source_root).unwrap(), module_id) | 21 | (db.item_map(source_root).unwrap(), module_id) |
22 | } | 22 | } |
23 | 23 | ||
24 | fn check_module_item_map(map: &hir::ItemMap, module_id: hir::ModuleId, expected: &str) { | ||
25 | let mut lines = map.per_module[&module_id] | ||
26 | .items | ||
27 | .iter() | ||
28 | .map(|(name, res)| format!("{}: {}", name, dump_resolution(res))) | ||
29 | .collect::<Vec<_>>(); | ||
30 | lines.sort(); | ||
31 | let actual = lines.join("\n"); | ||
32 | let expected = expected | ||
33 | .trim() | ||
34 | .lines() | ||
35 | .map(|it| it.trim()) | ||
36 | .collect::<Vec<_>>() | ||
37 | .join("\n"); | ||
38 | assert_eq_text!(&actual, &expected); | ||
39 | |||
40 | fn dump_resolution(resolution: &hir::Resolution) -> &'static str { | ||
41 | match ( | ||
42 | resolution.def_id.types.is_some(), | ||
43 | resolution.def_id.values.is_some(), | ||
44 | ) { | ||
45 | (true, true) => "t v", | ||
46 | (true, false) => "t", | ||
47 | (false, true) => "v", | ||
48 | (false, false) => "_", | ||
49 | } | ||
50 | } | ||
51 | } | ||
52 | |||
24 | #[test] | 53 | #[test] |
25 | fn item_map_smoke_test() { | 54 | fn item_map_smoke_test() { |
26 | let (item_map, module_id) = item_map( | 55 | let (item_map, module_id) = item_map( |
@@ -38,13 +67,18 @@ fn item_map_smoke_test() { | |||
38 | pub struct Baz; | 67 | pub struct Baz; |
39 | ", | 68 | ", |
40 | ); | 69 | ); |
41 | let name = SmolStr::from("Baz"); | 70 | check_module_item_map( |
42 | let resolution = &item_map.per_module[&module_id].items[&name]; | 71 | &item_map, |
43 | assert!(resolution.def_id.take_types().is_some()); | 72 | module_id, |
73 | " | ||
74 | Baz: t v | ||
75 | foo: t | ||
76 | ", | ||
77 | ); | ||
44 | } | 78 | } |
45 | 79 | ||
46 | #[test] | 80 | #[test] |
47 | fn test_self() { | 81 | fn item_map_using_self() { |
48 | let (item_map, module_id) = item_map( | 82 | let (item_map, module_id) = item_map( |
49 | " | 83 | " |
50 | //- /lib.rs | 84 | //- /lib.rs |
@@ -57,9 +91,14 @@ fn test_self() { | |||
57 | pub struct Baz; | 91 | pub struct Baz; |
58 | ", | 92 | ", |
59 | ); | 93 | ); |
60 | let name = SmolStr::from("Baz"); | 94 | check_module_item_map( |
61 | let resolution = &item_map.per_module[&module_id].items[&name]; | 95 | &item_map, |
62 | assert!(resolution.def_id.take_types().is_some()); | 96 | module_id, |
97 | " | ||
98 | Baz: t v | ||
99 | foo: t | ||
100 | ", | ||
101 | ); | ||
63 | } | 102 | } |
64 | 103 | ||
65 | #[test] | 104 | #[test] |
@@ -90,9 +129,14 @@ fn item_map_across_crates() { | |||
90 | let module_id = module.module_id; | 129 | let module_id = module.module_id; |
91 | let item_map = db.item_map(source_root).unwrap(); | 130 | let item_map = db.item_map(source_root).unwrap(); |
92 | 131 | ||
93 | let name = SmolStr::from("Baz"); | 132 | check_module_item_map( |
94 | let resolution = &item_map.per_module[&module_id].items[&name]; | 133 | &item_map, |
95 | assert!(resolution.def_id.take_types().is_some()); | 134 | module_id, |
135 | " | ||
136 | Baz: t v | ||
137 | test_crate: t | ||
138 | ", | ||
139 | ); | ||
96 | } | 140 | } |
97 | 141 | ||
98 | #[test] | 142 | #[test] |