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