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