aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_hir/src/module/nameres/tests.rs67
-rw-r--r--crates/ra_hir/src/name.rs14
2 files changed, 62 insertions, 19 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]
diff --git a/crates/ra_hir/src/name.rs b/crates/ra_hir/src/name.rs
index cdad31be7..e4fc141a6 100644
--- a/crates/ra_hir/src/name.rs
+++ b/crates/ra_hir/src/name.rs
@@ -5,7 +5,7 @@ use ra_syntax::{ast, SmolStr};
5/// `Name` is a wrapper around string, which is used in hir for both references 5/// `Name` is a wrapper around string, which is used in hir for both references
6/// and declarations. In theory, names should also carry hygene info, but we are 6/// and declarations. In theory, names should also carry hygene info, but we are
7/// not there yet! 7/// not there yet!
8#[derive(Debug, Clone, PartialEq, Eq, Hash)] 8#[derive(Clone, PartialEq, Eq, Hash)]
9pub struct Name { 9pub struct Name {
10 text: SmolStr, 10 text: SmolStr,
11} 11}
@@ -16,6 +16,12 @@ impl fmt::Display for Name {
16 } 16 }
17} 17}
18 18
19impl fmt::Debug for Name {
20 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21 fmt::Debug::fmt(&self.text, f)
22 }
23}
24
19impl Name { 25impl Name {
20 pub(crate) fn as_known_name(&self) -> Option<KnownName> { 26 pub(crate) fn as_known_name(&self) -> Option<KnownName> {
21 let name = match self.text.as_str() { 27 let name = match self.text.as_str() {
@@ -38,15 +44,9 @@ impl Name {
38 Some(name) 44 Some(name)
39 } 45 }
40 46
41 #[cfg(not(test))]
42 fn new(text: SmolStr) -> Name { 47 fn new(text: SmolStr) -> Name {
43 Name { text } 48 Name { text }
44 } 49 }
45
46 #[cfg(test)]
47 pub(crate) fn new(text: SmolStr) -> Name {
48 Name { text }
49 }
50} 50}
51 51
52pub(crate) trait AsName { 52pub(crate) trait AsName {