diff options
Diffstat (limited to 'crates/ra_hir/src/module/nameres/tests.rs')
-rw-r--r-- | crates/ra_hir/src/module/nameres/tests.rs | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/crates/ra_hir/src/module/nameres/tests.rs b/crates/ra_hir/src/module/nameres/tests.rs new file mode 100644 index 000000000..060683e27 --- /dev/null +++ b/crates/ra_hir/src/module/nameres/tests.rs | |||
@@ -0,0 +1,94 @@ | |||
1 | use std::sync::Arc; | ||
2 | |||
3 | use salsa::Database; | ||
4 | use ra_db::FilesDatabase; | ||
5 | use ra_syntax::SmolStr; | ||
6 | |||
7 | use crate::{ | ||
8 | self as hir, | ||
9 | db::HirDatabase, | ||
10 | mock::MockDatabase, | ||
11 | }; | ||
12 | |||
13 | fn item_map(fixture: &str) -> (Arc<hir::ItemMap>, hir::ModuleId) { | ||
14 | let (db, pos) = MockDatabase::with_position(fixture); | ||
15 | let source_root = db.file_source_root(pos.file_id); | ||
16 | let module = hir::source_binder::module_from_position(&db, pos) | ||
17 | .unwrap() | ||
18 | .unwrap(); | ||
19 | let module_id = module.module_id; | ||
20 | (db.item_map(source_root).unwrap(), module_id) | ||
21 | } | ||
22 | |||
23 | #[test] | ||
24 | fn test_item_map() { | ||
25 | let (item_map, module_id) = item_map( | ||
26 | " | ||
27 | //- /lib.rs | ||
28 | mod foo; | ||
29 | |||
30 | use crate::foo::bar::Baz; | ||
31 | <|> | ||
32 | |||
33 | //- /foo/mod.rs | ||
34 | pub mod bar; | ||
35 | |||
36 | //- /foo/bar.rs | ||
37 | pub struct Baz; | ||
38 | ", | ||
39 | ); | ||
40 | let name = SmolStr::from("Baz"); | ||
41 | let resolution = &item_map.per_module[&module_id].items[&name]; | ||
42 | assert!(resolution.def_id.is_some()); | ||
43 | } | ||
44 | |||
45 | #[test] | ||
46 | fn typing_inside_a_function_should_not_invalidate_item_map() { | ||
47 | let (mut db, pos) = MockDatabase::with_position( | ||
48 | " | ||
49 | //- /lib.rs | ||
50 | mod foo;<|> | ||
51 | |||
52 | use crate::foo::bar::Baz; | ||
53 | |||
54 | fn foo() -> i32 { | ||
55 | 1 + 1 | ||
56 | } | ||
57 | //- /foo/mod.rs | ||
58 | pub mod bar; | ||
59 | |||
60 | //- /foo/bar.rs | ||
61 | pub struct Baz; | ||
62 | ", | ||
63 | ); | ||
64 | let source_root = db.file_source_root(pos.file_id); | ||
65 | { | ||
66 | let events = db.log_executed(|| { | ||
67 | db.item_map(source_root).unwrap(); | ||
68 | }); | ||
69 | assert!(format!("{:?}", events).contains("item_map")) | ||
70 | } | ||
71 | |||
72 | let new_text = " | ||
73 | mod foo; | ||
74 | |||
75 | use crate::foo::bar::Baz; | ||
76 | |||
77 | fn foo() -> i32 { 92 } | ||
78 | " | ||
79 | .to_string(); | ||
80 | |||
81 | db.query_mut(ra_db::FileTextQuery) | ||
82 | .set(pos.file_id, Arc::new(new_text)); | ||
83 | |||
84 | { | ||
85 | let events = db.log_executed(|| { | ||
86 | db.item_map(source_root).unwrap(); | ||
87 | }); | ||
88 | assert!( | ||
89 | !format!("{:?}", events).contains("_item_map"), | ||
90 | "{:#?}", | ||
91 | events | ||
92 | ) | ||
93 | } | ||
94 | } | ||