aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/module
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/module')
-rw-r--r--crates/ra_hir/src/module/nameres/tests.rs67
1 files changed, 55 insertions, 12 deletions
diff --git a/crates/ra_hir/src/module/nameres/tests.rs b/crates/ra_hir/src/module/nameres/tests.rs
index 165ac81c8..ca20f064f 100644
--- a/crates/ra_hir/src/module/nameres/tests.rs
+++ b/crates/ra_hir/src/module/nameres/tests.rs
@@ -2,14 +2,13 @@ use std::sync::Arc;
2 2
3use salsa::Database; 3use salsa::Database;
4use ra_db::{FilesDatabase, CrateGraph}; 4use ra_db::{FilesDatabase, CrateGraph};
5use ra_syntax::SmolStr;
6use relative_path::RelativePath; 5use relative_path::RelativePath;
6use test_utils::assert_eq_text;
7 7
8use crate::{ 8use crate::{
9 self as hir, 9 self as hir,
10 db::HirDatabase, 10 db::HirDatabase,
11 mock::MockDatabase, 11 mock::MockDatabase,
12 Name,
13}; 12};
14 13
15fn item_map(fixture: &str) -> (Arc<hir::ItemMap>, hir::ModuleId) { 14fn item_map(fixture: &str) -> (Arc<hir::ItemMap>, hir::ModuleId) {
@@ -22,6 +21,35 @@ fn item_map(fixture: &str) -> (Arc<hir::ItemMap>, hir::ModuleId) {
22 (db.item_map(source_root).unwrap(), module_id) 21 (db.item_map(source_root).unwrap(), module_id)
23} 22}
24 23
24fn 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
25#[test] 53#[test]
26fn item_map_smoke_test() { 54fn item_map_smoke_test() {
27 let (item_map, module_id) = item_map( 55 let (item_map, module_id) = item_map(
@@ -39,13 +67,18 @@ fn item_map_smoke_test() {
39 pub struct Baz; 67 pub struct Baz;
40 ", 68 ",
41 ); 69 );
42 let name = Name::new(SmolStr::from("Baz")); 70 check_module_item_map(
43 let resolution = &item_map.per_module[&module_id].items[&name]; 71 &item_map,
44 assert!(resolution.def_id.take_types().is_some()); 72 module_id,
73 "
74 Baz: t v
75 foo: t
76 ",
77 );
45} 78}
46 79
47#[test] 80#[test]
48fn test_self() { 81fn item_map_using_self() {
49 let (item_map, module_id) = item_map( 82 let (item_map, module_id) = item_map(
50 " 83 "
51 //- /lib.rs 84 //- /lib.rs
@@ -58,9 +91,14 @@ fn test_self() {
58 pub struct Baz; 91 pub struct Baz;
59 ", 92 ",
60 ); 93 );
61 let name = Name::new(SmolStr::from("Baz")); 94 check_module_item_map(
62 let resolution = &item_map.per_module[&module_id].items[&name]; 95 &item_map,
63 assert!(resolution.def_id.take_types().is_some()); 96 module_id,
97 "
98 Baz: t v
99 foo: t
100 ",
101 );
64} 102}
65 103
66#[test] 104#[test]
@@ -91,9 +129,14 @@ fn item_map_across_crates() {
91 let module_id = module.module_id; 129 let module_id = module.module_id;
92 let item_map = db.item_map(source_root).unwrap(); 130 let item_map = db.item_map(source_root).unwrap();
93 131
94 let name = Name::new(SmolStr::from("Baz")); 132 check_module_item_map(
95 let resolution = &item_map.per_module[&module_id].items[&name]; 133 &item_map,
96 assert!(resolution.def_id.take_types().is_some()); 134 module_id,
135 "
136 Baz: t v
137 test_crate: t
138 ",
139 );
97} 140}
98 141
99#[test] 142#[test]