diff options
author | Aleksey Kladov <[email protected]> | 2019-03-14 08:53:40 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-03-17 09:49:07 +0000 |
commit | 71e5adf694a4b253bc5bb48be96bb6ba08002d8c (patch) | |
tree | 27208afed0ec41b07f8fffa05659259f13fc027c /crates/ra_hir/src/nameres/crate_def_map/tests.rs | |
parent | 2195d1db6d70d64383bec82819fab02891d09744 (diff) |
move tests over to crate-def-map
Diffstat (limited to 'crates/ra_hir/src/nameres/crate_def_map/tests.rs')
-rw-r--r-- | crates/ra_hir/src/nameres/crate_def_map/tests.rs | 411 |
1 files changed, 337 insertions, 74 deletions
diff --git a/crates/ra_hir/src/nameres/crate_def_map/tests.rs b/crates/ra_hir/src/nameres/crate_def_map/tests.rs index 742a19e5c..36c1d74ce 100644 --- a/crates/ra_hir/src/nameres/crate_def_map/tests.rs +++ b/crates/ra_hir/src/nameres/crate_def_map/tests.rs | |||
@@ -1,3 +1,7 @@ | |||
1 | mod macros; | ||
2 | mod globs; | ||
3 | mod incremental; | ||
4 | |||
1 | use std::sync::Arc; | 5 | use std::sync::Arc; |
2 | 6 | ||
3 | use ra_db::SourceDatabase; | 7 | use ra_db::SourceDatabase; |
@@ -62,6 +66,8 @@ fn crate_def_map_smoke_test() { | |||
62 | //- /lib.rs | 66 | //- /lib.rs |
63 | mod foo; | 67 | mod foo; |
64 | struct S; | 68 | struct S; |
69 | use crate::foo::bar::E; | ||
70 | use self::E::V; | ||
65 | 71 | ||
66 | //- /foo/mod.rs | 72 | //- /foo/mod.rs |
67 | pub mod bar; | 73 | pub mod bar; |
@@ -74,9 +80,13 @@ fn crate_def_map_smoke_test() { | |||
74 | ); | 80 | ); |
75 | assert_snapshot_matches!(map, @r###" | 81 | assert_snapshot_matches!(map, @r###" |
76 | crate | 82 | crate |
83 | V: t v | ||
84 | E: t | ||
85 | foo: t | ||
77 | S: t v | 86 | S: t v |
78 | 87 | ||
79 | crate::foo | 88 | crate::foo |
89 | bar: t | ||
80 | f: v | 90 | f: v |
81 | 91 | ||
82 | crate::foo::bar | 92 | crate::foo::bar |
@@ -87,91 +97,96 @@ E: t | |||
87 | } | 97 | } |
88 | 98 | ||
89 | #[test] | 99 | #[test] |
90 | fn macro_rules_are_globally_visible() { | 100 | fn use_as() { |
91 | let map = def_map( | 101 | let map = def_map( |
92 | " | 102 | " |
93 | //- /lib.rs | 103 | //- /lib.rs |
94 | macro_rules! structs { | 104 | mod foo; |
95 | ($($i:ident),*) => { | 105 | |
96 | $(struct $i { field: u32 } )* | 106 | use crate::foo::Baz as Foo; |
97 | } | ||
98 | } | ||
99 | structs!(Foo); | ||
100 | mod nested; | ||
101 | 107 | ||
102 | //- /nested.rs | 108 | //- /foo/mod.rs |
103 | structs!(Bar, Baz); | 109 | pub struct Baz; |
104 | ", | 110 | ", |
105 | ); | 111 | ); |
106 | assert_snapshot_matches!(map, @r###" | 112 | assert_snapshot_matches!(map, |
113 | @r###" | ||
107 | crate | 114 | crate |
108 | Foo: t v | 115 | Foo: t v |
116 | foo: t | ||
109 | 117 | ||
110 | crate::nested | 118 | crate::foo |
111 | Bar: t v | ||
112 | Baz: t v | 119 | Baz: t v |
113 | "###); | 120 | "### |
121 | ); | ||
114 | } | 122 | } |
115 | 123 | ||
116 | #[test] | 124 | #[test] |
117 | fn macro_rules_can_define_modules() { | 125 | fn use_trees() { |
118 | let map = def_map( | 126 | let map = def_map( |
119 | " | 127 | " |
120 | //- /lib.rs | 128 | //- /lib.rs |
121 | macro_rules! m { | 129 | mod foo; |
122 | ($name:ident) => { mod $name; } | 130 | |
123 | } | 131 | use crate::foo::bar::{Baz, Quux}; |
124 | m!(n1); | 132 | |
133 | //- /foo/mod.rs | ||
134 | pub mod bar; | ||
125 | 135 | ||
126 | //- /n1.rs | 136 | //- /foo/bar.rs |
127 | m!(n2) | 137 | pub struct Baz; |
128 | //- /n1/n2.rs | 138 | pub enum Quux {}; |
129 | struct X; | ||
130 | ", | 139 | ", |
131 | ); | 140 | ); |
132 | assert_snapshot_matches!(map, @r###" | 141 | assert_snapshot_matches!(map, |
142 | @r###" | ||
133 | crate | 143 | crate |
144 | Quux: t | ||
145 | Baz: t v | ||
146 | foo: t | ||
134 | 147 | ||
135 | crate::n1 | 148 | crate::foo |
149 | bar: t | ||
136 | 150 | ||
137 | crate::n1::n2 | 151 | crate::foo::bar |
138 | X: t v | 152 | Quux: t |
139 | "###); | 153 | Baz: t v |
154 | "### | ||
155 | ); | ||
140 | } | 156 | } |
141 | 157 | ||
142 | #[test] | 158 | #[test] |
143 | fn macro_rules_from_other_crates_are_visible() { | 159 | fn re_exports() { |
144 | let map = def_map_with_crate_graph( | 160 | let map = def_map( |
145 | " | 161 | " |
146 | //- /main.rs | 162 | //- /lib.rs |
147 | foo::structs!(Foo, Bar) | 163 | mod foo; |
148 | mod bar; | ||
149 | 164 | ||
150 | //- /bar.rs | 165 | use self::foo::Baz; |
151 | use crate::*; | ||
152 | 166 | ||
153 | //- /lib.rs | 167 | //- /foo/mod.rs |
154 | #[macro_export] | 168 | pub mod bar; |
155 | macro_rules! structs { | 169 | |
156 | ($($i:ident),*) => { | 170 | pub use self::bar::Baz; |
157 | $(struct $i { field: u32 } )* | 171 | |
158 | } | 172 | //- /foo/bar.rs |
159 | } | 173 | pub struct Baz; |
160 | ", | 174 | ", |
161 | crate_graph! { | ||
162 | "main": ("/main.rs", ["foo"]), | ||
163 | "foo": ("/lib.rs", []), | ||
164 | }, | ||
165 | ); | 175 | ); |
166 | assert_snapshot_matches!(map, @r###" | 176 | assert_snapshot_matches!(map, |
177 | @r###" | ||
167 | crate | 178 | crate |
168 | Foo: t v | 179 | Baz: t v |
169 | Bar: t v | 180 | foo: t |
170 | 181 | ||
171 | crate::bar | 182 | crate::foo |
172 | Foo: t v | 183 | bar: t |
173 | Bar: t v | 184 | Baz: t v |
174 | "###); | 185 | |
186 | crate::foo::bar | ||
187 | Baz: t v | ||
188 | "### | ||
189 | ); | ||
175 | } | 190 | } |
176 | 191 | ||
177 | #[test] | 192 | #[test] |
@@ -203,12 +218,152 @@ Baz: t v | |||
203 | } | 218 | } |
204 | 219 | ||
205 | #[test] | 220 | #[test] |
206 | fn glob_across_crates() { | 221 | fn can_import_enum_variant() { |
207 | covers!(glob_across_crates); | 222 | covers!(can_import_enum_variant); |
223 | let map = def_map( | ||
224 | " | ||
225 | //- /lib.rs | ||
226 | enum E { V } | ||
227 | use self::E::V; | ||
228 | ", | ||
229 | ); | ||
230 | assert_snapshot_matches!(map, @r###" | ||
231 | crate | ||
232 | V: t v | ||
233 | E: t | ||
234 | "### | ||
235 | ); | ||
236 | } | ||
237 | |||
238 | #[test] | ||
239 | fn edition_2015_imports() { | ||
240 | let map = def_map_with_crate_graph( | ||
241 | " | ||
242 | //- /main.rs | ||
243 | mod foo; | ||
244 | mod bar; | ||
245 | |||
246 | //- /bar.rs | ||
247 | struct Bar; | ||
248 | |||
249 | //- /foo.rs | ||
250 | use bar::Bar; | ||
251 | use other_crate::FromLib; | ||
252 | |||
253 | //- /lib.rs | ||
254 | struct FromLib; | ||
255 | ", | ||
256 | crate_graph! { | ||
257 | "main": ("/main.rs", "2015", ["other_crate"]), | ||
258 | "other_crate": ("/lib.rs", "2018", []), | ||
259 | }, | ||
260 | ); | ||
261 | |||
262 | assert_snapshot_matches!(map, | ||
263 | @r###" | ||
264 | crate | ||
265 | bar: t | ||
266 | foo: t | ||
267 | |||
268 | crate::bar | ||
269 | Bar: t v | ||
270 | |||
271 | crate::foo | ||
272 | FromLib: t v | ||
273 | Bar: t v | ||
274 | "### | ||
275 | ); | ||
276 | } | ||
277 | |||
278 | #[test] | ||
279 | fn module_resolution_works_for_non_standard_filenames() { | ||
280 | let map = def_map_with_crate_graph( | ||
281 | " | ||
282 | //- /my_library.rs | ||
283 | mod foo; | ||
284 | use self::foo::Bar; | ||
285 | |||
286 | //- /foo/mod.rs | ||
287 | pub struct Bar; | ||
288 | ", | ||
289 | crate_graph! { | ||
290 | "my_library": ("/my_library.rs", []), | ||
291 | }, | ||
292 | ); | ||
293 | |||
294 | assert_snapshot_matches!(map, | ||
295 | @r###" | ||
296 | crate | ||
297 | Bar: t v | ||
298 | foo: t | ||
299 | |||
300 | crate::foo | ||
301 | Bar: t v | ||
302 | "### | ||
303 | ); | ||
304 | } | ||
305 | |||
306 | #[test] | ||
307 | fn name_res_works_for_broken_modules() { | ||
308 | covers!(name_res_works_for_broken_modules); | ||
309 | let map = def_map( | ||
310 | " | ||
311 | //- /lib.rs | ||
312 | mod foo // no `;`, no body | ||
313 | |||
314 | use self::foo::Baz; | ||
315 | |||
316 | //- /foo/mod.rs | ||
317 | pub mod bar; | ||
318 | |||
319 | pub use self::bar::Baz; | ||
320 | |||
321 | //- /foo/bar.rs | ||
322 | pub struct Baz; | ||
323 | ", | ||
324 | ); | ||
325 | assert_snapshot_matches!(map, | ||
326 | @r###" | ||
327 | crate | ||
328 | Baz: _ | ||
329 | "### | ||
330 | ); | ||
331 | } | ||
332 | |||
333 | #[test] | ||
334 | fn item_map_using_self() { | ||
335 | let map = def_map( | ||
336 | " | ||
337 | //- /lib.rs | ||
338 | mod foo; | ||
339 | use crate::foo::bar::Baz::{self}; | ||
340 | //- /foo/mod.rs | ||
341 | pub mod bar; | ||
342 | //- /foo/bar.rs | ||
343 | pub struct Baz; | ||
344 | ", | ||
345 | ); | ||
346 | assert_snapshot_matches!(map, | ||
347 | @r###" | ||
348 | crate | ||
349 | Baz: t v | ||
350 | foo: t | ||
351 | |||
352 | crate::foo | ||
353 | bar: t | ||
354 | |||
355 | crate::foo::bar | ||
356 | Baz: t v | ||
357 | "### | ||
358 | ); | ||
359 | } | ||
360 | |||
361 | #[test] | ||
362 | fn item_map_across_crates() { | ||
208 | let map = def_map_with_crate_graph( | 363 | let map = def_map_with_crate_graph( |
209 | " | 364 | " |
210 | //- /main.rs | 365 | //- /main.rs |
211 | use test_crate::*; | 366 | use test_crate::Baz; |
212 | 367 | ||
213 | //- /lib.rs | 368 | //- /lib.rs |
214 | pub struct Baz; | 369 | pub struct Baz; |
@@ -218,7 +373,9 @@ fn glob_across_crates() { | |||
218 | "test_crate": ("/lib.rs", []), | 373 | "test_crate": ("/lib.rs", []), |
219 | }, | 374 | }, |
220 | ); | 375 | ); |
221 | assert_snapshot_matches!(map, @r###" | 376 | |
377 | assert_snapshot_matches!(map, | ||
378 | @r###" | ||
222 | crate | 379 | crate |
223 | Baz: t v | 380 | Baz: t v |
224 | "### | 381 | "### |
@@ -226,40 +383,146 @@ Baz: t v | |||
226 | } | 383 | } |
227 | 384 | ||
228 | #[test] | 385 | #[test] |
229 | fn item_map_enum_importing() { | 386 | fn extern_crate_rename() { |
230 | covers!(item_map_enum_importing); | 387 | let map = def_map_with_crate_graph( |
231 | let map = def_map( | ||
232 | " | 388 | " |
389 | //- /main.rs | ||
390 | extern crate alloc as alloc_crate; | ||
391 | |||
392 | mod alloc; | ||
393 | mod sync; | ||
394 | |||
395 | //- /sync.rs | ||
396 | use alloc_crate::Arc; | ||
397 | |||
233 | //- /lib.rs | 398 | //- /lib.rs |
234 | enum E { V } | 399 | struct Arc; |
235 | use self::E::V; | ||
236 | ", | 400 | ", |
401 | crate_graph! { | ||
402 | "main": ("/main.rs", ["alloc"]), | ||
403 | "alloc": ("/lib.rs", []), | ||
404 | }, | ||
237 | ); | 405 | ); |
238 | assert_snapshot_matches!(map, @r###" | 406 | |
407 | assert_snapshot_matches!(map, | ||
408 | @r###" | ||
239 | crate | 409 | crate |
240 | V: t v | 410 | Arc: t v |
241 | E: t | ||
242 | "### | 411 | "### |
243 | ); | 412 | ); |
244 | } | 413 | } |
245 | 414 | ||
246 | #[test] | 415 | #[test] |
247 | fn glob_enum() { | 416 | fn extern_crate_rename_2015_edition() { |
248 | covers!(glob_enum); | 417 | let map = def_map_with_crate_graph( |
249 | let map = def_map( | 418 | " |
419 | //- /main.rs | ||
420 | extern crate alloc as alloc_crate; | ||
421 | |||
422 | mod alloc; | ||
423 | mod sync; | ||
424 | |||
425 | //- /sync.rs | ||
426 | use alloc_crate::Arc; | ||
427 | |||
428 | //- /lib.rs | ||
429 | struct Arc; | ||
430 | ", | ||
431 | crate_graph! { | ||
432 | "main": ("/main.rs", "2015", ["alloc"]), | ||
433 | "alloc": ("/lib.rs", []), | ||
434 | }, | ||
435 | ); | ||
436 | |||
437 | assert_snapshot_matches!(map, | ||
438 | @r###" | ||
439 | crate | ||
440 | Arc: t v | ||
441 | "### | ||
442 | ); | ||
443 | } | ||
444 | |||
445 | #[test] | ||
446 | fn import_across_source_roots() { | ||
447 | let map = def_map_with_crate_graph( | ||
250 | " | 448 | " |
251 | //- /lib.rs | 449 | //- /lib.rs |
252 | enum Foo { | 450 | pub mod a { |
253 | Bar, Baz | 451 | pub mod b { |
452 | pub struct C; | ||
453 | } | ||
254 | } | 454 | } |
255 | use self::Foo::*; | 455 | |
456 | //- root /main/ | ||
457 | |||
458 | //- /main/main.rs | ||
459 | use test_crate::a::b::C; | ||
256 | ", | 460 | ", |
461 | crate_graph! { | ||
462 | "main": ("/main/main.rs", ["test_crate"]), | ||
463 | "test_crate": ("/lib.rs", []), | ||
464 | }, | ||
257 | ); | 465 | ); |
258 | assert_snapshot_matches!(map, @r###" | 466 | |
467 | assert_snapshot_matches!(map, | ||
468 | @r###" | ||
469 | crate | ||
470 | C: t v | ||
471 | "### | ||
472 | ); | ||
473 | } | ||
474 | |||
475 | #[test] | ||
476 | fn reexport_across_crates() { | ||
477 | let map = def_map_with_crate_graph( | ||
478 | " | ||
479 | //- /main.rs | ||
480 | use test_crate::Baz; | ||
481 | |||
482 | //- /lib.rs | ||
483 | pub use foo::Baz; | ||
484 | |||
485 | mod foo; | ||
486 | |||
487 | //- /foo.rs | ||
488 | pub struct Baz; | ||
489 | ", | ||
490 | crate_graph! { | ||
491 | "main": ("/main.rs", ["test_crate"]), | ||
492 | "test_crate": ("/lib.rs", []), | ||
493 | }, | ||
494 | ); | ||
495 | |||
496 | assert_snapshot_matches!(map, | ||
497 | @r###" | ||
259 | crate | 498 | crate |
260 | Foo: t | ||
261 | Bar: t v | ||
262 | Baz: t v | 499 | Baz: t v |
263 | "### | 500 | "### |
264 | ); | 501 | ); |
265 | } | 502 | } |
503 | |||
504 | #[test] | ||
505 | fn values_dont_shadow_extern_crates() { | ||
506 | let map = def_map_with_crate_graph( | ||
507 | " | ||
508 | //- /main.rs | ||
509 | fn foo() {} | ||
510 | use foo::Bar; | ||
511 | |||
512 | //- /foo/lib.rs | ||
513 | pub struct Bar; | ||
514 | ", | ||
515 | crate_graph! { | ||
516 | "main": ("/main.rs", ["foo"]), | ||
517 | "foo": ("/foo/lib.rs", []), | ||
518 | }, | ||
519 | ); | ||
520 | |||
521 | assert_snapshot_matches!(map, | ||
522 | @r###" | ||
523 | crate | ||
524 | Bar: t v | ||
525 | foo: v | ||
526 | "### | ||
527 | ); | ||
528 | } | ||