aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/nameres/tests.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def/src/nameres/tests.rs')
-rw-r--r--crates/ra_hir_def/src/nameres/tests.rs522
1 files changed, 522 insertions, 0 deletions
diff --git a/crates/ra_hir_def/src/nameres/tests.rs b/crates/ra_hir_def/src/nameres/tests.rs
new file mode 100644
index 000000000..52bd0aa91
--- /dev/null
+++ b/crates/ra_hir_def/src/nameres/tests.rs
@@ -0,0 +1,522 @@
1mod globs;
2mod incremental;
3mod macros;
4mod mod_resolution;
5mod primitives;
6
7use std::sync::Arc;
8
9use insta::assert_snapshot;
10use ra_db::{fixture::WithFixture, SourceDatabase};
11use test_utils::covers;
12
13use crate::{db::DefDatabase2, nameres::*, test_db::TestDB, CrateModuleId};
14
15fn def_map(fixtute: &str) -> String {
16 let dm = compute_crate_def_map(fixtute);
17 render_crate_def_map(&dm)
18}
19
20fn compute_crate_def_map(fixture: &str) -> Arc<CrateDefMap> {
21 let db = TestDB::with_files(fixture);
22 let krate = db.crate_graph().iter().next().unwrap();
23 db.crate_def_map(krate)
24}
25
26fn render_crate_def_map(map: &CrateDefMap) -> String {
27 let mut buf = String::new();
28 go(&mut buf, map, "\ncrate", map.root());
29 return buf.trim().to_string();
30
31 fn go(buf: &mut String, map: &CrateDefMap, path: &str, module: CrateModuleId) {
32 *buf += path;
33 *buf += "\n";
34
35 let mut entries = map.modules[module]
36 .scope
37 .items
38 .iter()
39 .map(|(name, res)| (name, res.def))
40 .collect::<Vec<_>>();
41 entries.sort_by_key(|(name, _)| *name);
42
43 for (name, res) in entries {
44 *buf += &format!("{}:", name);
45
46 if res.types.is_some() {
47 *buf += " t";
48 }
49 if res.values.is_some() {
50 *buf += " v";
51 }
52 if res.macros.is_some() {
53 *buf += " m";
54 }
55 if res.is_none() {
56 *buf += " _";
57 }
58
59 *buf += "\n";
60 }
61
62 for (name, child) in map.modules[module].children.iter() {
63 let path = path.to_string() + &format!("::{}", name);
64 go(buf, map, &path, *child);
65 }
66 }
67}
68
69#[test]
70fn crate_def_map_smoke_test() {
71 let map = def_map(
72 "
73 //- /lib.rs
74 mod foo;
75 struct S;
76 use crate::foo::bar::E;
77 use self::E::V;
78
79 //- /foo/mod.rs
80 pub mod bar;
81 fn f() {}
82
83 //- /foo/bar.rs
84 pub struct Baz;
85 enum E { V }
86 ",
87 );
88 assert_snapshot!(map, @r###"
89 ⋮crate
90 ⋮E: t
91 ⋮S: t v
92 ⋮V: t v
93 ⋮foo: t
94
95 ⋮crate::foo
96 ⋮bar: t
97 ⋮f: v
98
99 ⋮crate::foo::bar
100 ⋮Baz: t v
101 ⋮E: t
102 "###)
103}
104
105#[test]
106fn bogus_paths() {
107 covers!(bogus_paths);
108 let map = def_map(
109 "
110 //- /lib.rs
111 mod foo;
112 struct S;
113 use self;
114
115 //- /foo/mod.rs
116 use super;
117 use crate;
118
119 ",
120 );
121 assert_snapshot!(map, @r###"
122 ⋮crate
123 ⋮S: t v
124 ⋮foo: t
125
126 ⋮crate::foo
127 "###
128 )
129}
130
131#[test]
132fn use_as() {
133 let map = def_map(
134 "
135 //- /lib.rs
136 mod foo;
137
138 use crate::foo::Baz as Foo;
139
140 //- /foo/mod.rs
141 pub struct Baz;
142 ",
143 );
144 assert_snapshot!(map,
145 @r###"
146 ⋮crate
147 ⋮Foo: t v
148 ⋮foo: t
149
150 ⋮crate::foo
151 ⋮Baz: t v
152 "###
153 );
154}
155
156#[test]
157fn use_trees() {
158 let map = def_map(
159 "
160 //- /lib.rs
161 mod foo;
162
163 use crate::foo::bar::{Baz, Quux};
164
165 //- /foo/mod.rs
166 pub mod bar;
167
168 //- /foo/bar.rs
169 pub struct Baz;
170 pub enum Quux {};
171 ",
172 );
173 assert_snapshot!(map, @r###"
174 ⋮crate
175 ⋮Baz: t v
176 ⋮Quux: t
177 ⋮foo: t
178
179 ⋮crate::foo
180 ⋮bar: t
181
182 ⋮crate::foo::bar
183 ⋮Baz: t v
184 ⋮Quux: t
185 "###);
186}
187
188#[test]
189fn re_exports() {
190 let map = def_map(
191 "
192 //- /lib.rs
193 mod foo;
194
195 use self::foo::Baz;
196
197 //- /foo/mod.rs
198 pub mod bar;
199
200 pub use self::bar::Baz;
201
202 //- /foo/bar.rs
203 pub struct Baz;
204 ",
205 );
206 assert_snapshot!(map, @r###"
207 ⋮crate
208 ⋮Baz: t v
209 ⋮foo: t
210
211 ⋮crate::foo
212 ⋮Baz: t v
213 ⋮bar: t
214
215 ⋮crate::foo::bar
216 ⋮Baz: t v
217 "###);
218}
219
220#[test]
221fn std_prelude() {
222 covers!(std_prelude);
223 let map = def_map(
224 "
225 //- /main.rs crate:main deps:test_crate
226 use Foo::*;
227
228 //- /lib.rs crate:test_crate
229 mod prelude;
230 #[prelude_import]
231 use prelude::*;
232
233 //- /prelude.rs
234 pub enum Foo { Bar, Baz };
235 ",
236 );
237 assert_snapshot!(map, @r###"
238 ⋮crate
239 ⋮Bar: t v
240 ⋮Baz: t v
241 "###);
242}
243
244#[test]
245fn can_import_enum_variant() {
246 covers!(can_import_enum_variant);
247 let map = def_map(
248 "
249 //- /lib.rs
250 enum E { V }
251 use self::E::V;
252 ",
253 );
254 assert_snapshot!(map, @r###"
255 ⋮crate
256 ⋮E: t
257 ⋮V: t v
258 "###
259 );
260}
261
262#[test]
263fn edition_2015_imports() {
264 let map = def_map(
265 "
266 //- /main.rs crate:main deps:other_crate edition:2015
267 mod foo;
268 mod bar;
269
270 //- /bar.rs
271 struct Bar;
272
273 //- /foo.rs
274 use bar::Bar;
275 use other_crate::FromLib;
276
277 //- /lib.rs crate:other_crate edition:2018
278 struct FromLib;
279 ",
280 );
281
282 assert_snapshot!(map, @r###"
283 ⋮crate
284 ⋮bar: t
285 ⋮foo: t
286
287 ⋮crate::bar
288 ⋮Bar: t v
289
290 ⋮crate::foo
291 ⋮Bar: t v
292 ⋮FromLib: t v
293 "###);
294}
295
296#[test]
297fn item_map_using_self() {
298 let map = def_map(
299 "
300 //- /lib.rs
301 mod foo;
302 use crate::foo::bar::Baz::{self};
303 //- /foo/mod.rs
304 pub mod bar;
305 //- /foo/bar.rs
306 pub struct Baz;
307 ",
308 );
309 assert_snapshot!(map, @r###"
310 ⋮crate
311 ⋮Baz: t v
312 ⋮foo: t
313
314 ⋮crate::foo
315 ⋮bar: t
316
317 ⋮crate::foo::bar
318 ⋮Baz: t v
319 "###);
320}
321
322#[test]
323fn item_map_across_crates() {
324 let map = def_map(
325 "
326 //- /main.rs crate:main deps:test_crate
327 use test_crate::Baz;
328
329 //- /lib.rs crate:test_crate
330 pub struct Baz;
331 ",
332 );
333
334 assert_snapshot!(map, @r###"
335 ⋮crate
336 ⋮Baz: t v
337 "###);
338}
339
340#[test]
341fn extern_crate_rename() {
342 let map = def_map(
343 "
344 //- /main.rs crate:main deps:alloc
345 extern crate alloc as alloc_crate;
346
347 mod alloc;
348 mod sync;
349
350 //- /sync.rs
351 use alloc_crate::Arc;
352
353 //- /lib.rs crate:alloc
354 struct Arc;
355 ",
356 );
357
358 assert_snapshot!(map, @r###"
359 ⋮crate
360 ⋮alloc_crate: t
361 ⋮sync: t
362
363 ⋮crate::sync
364 ⋮Arc: t v
365 "###);
366}
367
368#[test]
369fn extern_crate_rename_2015_edition() {
370 let map = def_map(
371 "
372 //- /main.rs crate:main deps:alloc edition:2015
373 extern crate alloc as alloc_crate;
374
375 mod alloc;
376 mod sync;
377
378 //- /sync.rs
379 use alloc_crate::Arc;
380
381 //- /lib.rs crate:alloc
382 struct Arc;
383 ",
384 );
385
386 assert_snapshot!(map,
387 @r###"
388 ⋮crate
389 ⋮alloc_crate: t
390 ⋮sync: t
391
392 ⋮crate::sync
393 ⋮Arc: t v
394 "###
395 );
396}
397
398#[test]
399fn import_across_source_roots() {
400 let map = def_map(
401 "
402 //- /main.rs crate:main deps:test_crate
403 use test_crate::a::b::C;
404
405 //- root /test_crate/
406
407 //- /test_crate/lib.rs crate:test_crate
408 pub mod a {
409 pub mod b {
410 pub struct C;
411 }
412 }
413
414 ",
415 );
416
417 assert_snapshot!(map, @r###"
418 ⋮crate
419 ⋮C: t v
420 "###);
421}
422
423#[test]
424fn reexport_across_crates() {
425 let map = def_map(
426 "
427 //- /main.rs crate:main deps:test_crate
428 use test_crate::Baz;
429
430 //- /lib.rs crate:test_crate
431 pub use foo::Baz;
432
433 mod foo;
434
435 //- /foo.rs
436 pub struct Baz;
437 ",
438 );
439
440 assert_snapshot!(map, @r###"
441 ⋮crate
442 ⋮Baz: t v
443 "###);
444}
445
446#[test]
447fn values_dont_shadow_extern_crates() {
448 let map = def_map(
449 "
450 //- /main.rs crate:main deps:foo
451 fn foo() {}
452 use foo::Bar;
453
454 //- /foo/lib.rs crate:foo
455 pub struct Bar;
456 ",
457 );
458
459 assert_snapshot!(map, @r###"
460 ⋮crate
461 ⋮Bar: t v
462 ⋮foo: v
463 "###);
464}
465
466#[test]
467fn cfg_not_test() {
468 let map = def_map(
469 r#"
470 //- /main.rs crate:main deps:std
471 use {Foo, Bar, Baz};
472
473 //- /lib.rs crate:std
474 #[prelude_import]
475 pub use self::prelude::*;
476 mod prelude {
477 #[cfg(test)]
478 pub struct Foo;
479 #[cfg(not(test))]
480 pub struct Bar;
481 #[cfg(all(not(any()), feature = "foo", feature = "bar", opt = "42"))]
482 pub struct Baz;
483 }
484 "#,
485 );
486
487 assert_snapshot!(map, @r###"
488 ⋮crate
489 ⋮Bar: t v
490 ⋮Baz: _
491 ⋮Foo: _
492 "###);
493}
494
495#[test]
496fn cfg_test() {
497 let map = def_map(
498 r#"
499 //- /main.rs crate:main deps:std
500 use {Foo, Bar, Baz};
501
502 //- /lib.rs crate:std cfg:test,feature=foo,feature=bar,opt=42
503 #[prelude_import]
504 pub use self::prelude::*;
505 mod prelude {
506 #[cfg(test)]
507 pub struct Foo;
508 #[cfg(not(test))]
509 pub struct Bar;
510 #[cfg(all(not(any()), feature = "foo", feature = "bar", opt = "42"))]
511 pub struct Baz;
512 }
513 "#,
514 );
515
516 assert_snapshot!(map, @r###"
517 ⋮crate
518 ⋮Bar: _
519 ⋮Baz: t v
520 ⋮Foo: t v
521 "###);
522}