diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_hir/src/lib.rs | 1 | ||||
-rw-r--r-- | crates/ra_hir/src/mock.rs | 35 | ||||
-rw-r--r-- | crates/ra_hir/src/nameres/tests.rs | 116 |
3 files changed, 81 insertions, 71 deletions
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index a9cd955cf..8ed8c4bdc 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs | |||
@@ -18,6 +18,7 @@ macro_rules! impl_froms { | |||
18 | } | 18 | } |
19 | 19 | ||
20 | pub mod db; | 20 | pub mod db; |
21 | #[macro_use] | ||
21 | pub mod mock; | 22 | pub mod mock; |
22 | mod query_definitions; | 23 | mod query_definitions; |
23 | mod path; | 24 | mod path; |
diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs index 29b696afa..5ca870867 100644 --- a/crates/ra_hir/src/mock.rs +++ b/crates/ra_hir/src/mock.rs | |||
@@ -55,6 +55,24 @@ impl MockDatabase { | |||
55 | } | 55 | } |
56 | } | 56 | } |
57 | 57 | ||
58 | pub fn set_crate_graph_from_fixture(&mut self, graph: CrateGraphFixture) { | ||
59 | let mut ids = FxHashMap::default(); | ||
60 | let mut crate_graph = CrateGraph::default(); | ||
61 | for (crate_name, (crate_root, _)) in graph.0.iter() { | ||
62 | let crate_root = self.file_id_of(&crate_root); | ||
63 | let crate_id = crate_graph.add_crate_root(crate_root); | ||
64 | ids.insert(crate_name, crate_id); | ||
65 | } | ||
66 | for (crate_name, (_, deps)) in graph.0.iter() { | ||
67 | let from = ids[crate_name]; | ||
68 | for dep in deps { | ||
69 | let to = ids[dep]; | ||
70 | crate_graph.add_dep(from, dep.as_str().into(), to).unwrap(); | ||
71 | } | ||
72 | } | ||
73 | self.set_crate_graph(Arc::new(crate_graph)) | ||
74 | } | ||
75 | |||
58 | fn from_fixture(fixture: &str) -> (MockDatabase, Option<FilePosition>) { | 76 | fn from_fixture(fixture: &str) -> (MockDatabase, Option<FilePosition>) { |
59 | let mut db = MockDatabase::default(); | 77 | let mut db = MockDatabase::default(); |
60 | 78 | ||
@@ -212,3 +230,20 @@ impl MockDatabase { | |||
212 | .collect() | 230 | .collect() |
213 | } | 231 | } |
214 | } | 232 | } |
233 | |||
234 | #[derive(Default)] | ||
235 | pub struct CrateGraphFixture(pub FxHashMap<String, (String, Vec<String>)>); | ||
236 | |||
237 | #[macro_export] | ||
238 | macro_rules! crate_graph { | ||
239 | ($($crate_name:literal: ($crate_path:literal, [$($dep:literal),*]),)*) => {{ | ||
240 | let mut res = $crate::mock::CrateGraphFixture::default(); | ||
241 | $( | ||
242 | res.0.insert( | ||
243 | $crate_name.to_string(), | ||
244 | ($crate_path.to_string(), vec![$($dep.to_string()),*]) | ||
245 | ); | ||
246 | )* | ||
247 | res | ||
248 | }} | ||
249 | } | ||
diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index 905c3accf..9b621fbc2 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use std::sync::Arc; | 1 | use std::sync::Arc; |
2 | 2 | ||
3 | use ra_db::{CrateGraph, SourceDatabase}; | 3 | use ra_db::SourceDatabase; |
4 | use test_utils::{assert_eq_text, covers}; | 4 | use test_utils::{assert_eq_text, covers}; |
5 | 5 | ||
6 | use crate::{ | 6 | use crate::{ |
@@ -19,20 +19,6 @@ fn item_map(fixture: &str) -> (Arc<ItemMap>, ModuleId) { | |||
19 | (db.item_map(krate), module_id) | 19 | (db.item_map(krate), module_id) |
20 | } | 20 | } |
21 | 21 | ||
22 | /// Sets the crate root to the file of the cursor marker | ||
23 | fn item_map_custom_crate_root(fixture: &str) -> (Arc<ItemMap>, ModuleId) { | ||
24 | let (mut db, pos) = MockDatabase::with_position(fixture); | ||
25 | |||
26 | let mut crate_graph = CrateGraph::default(); | ||
27 | crate_graph.add_crate_root(pos.file_id); | ||
28 | db.set_crate_graph(Arc::new(crate_graph)); | ||
29 | |||
30 | let module = crate::source_binder::module_from_position(&db, pos).unwrap(); | ||
31 | let krate = module.krate(&db).unwrap(); | ||
32 | let module_id = module.module_id; | ||
33 | (db.item_map(krate), module_id) | ||
34 | } | ||
35 | |||
36 | fn check_module_item_map(map: &ItemMap, module_id: ModuleId, expected: &str) { | 22 | fn check_module_item_map(map: &ItemMap, module_id: ModuleId, expected: &str) { |
37 | let mut lines = map[module_id] | 23 | let mut lines = map[module_id] |
38 | .items | 24 | .items |
@@ -258,17 +244,13 @@ fn glob_across_crates() { | |||
258 | 244 | ||
259 | //- /lib.rs | 245 | //- /lib.rs |
260 | pub struct Baz; | 246 | pub struct Baz; |
261 | ", | 247 | ", |
262 | ); | 248 | ); |
249 | db.set_crate_graph_from_fixture(crate_graph! { | ||
250 | "main": ("/main.rs", ["test_crate"]), | ||
251 | "test_crate": ("/lib.rs", []), | ||
252 | }); | ||
263 | let main_id = db.file_id_of("/main.rs"); | 253 | let main_id = db.file_id_of("/main.rs"); |
264 | let lib_id = db.file_id_of("/lib.rs"); | ||
265 | |||
266 | let mut crate_graph = CrateGraph::default(); | ||
267 | let main_crate = crate_graph.add_crate_root(main_id); | ||
268 | let lib_crate = crate_graph.add_crate_root(lib_id); | ||
269 | crate_graph.add_dep(main_crate, "test_crate".into(), lib_crate).unwrap(); | ||
270 | |||
271 | db.set_crate_graph(Arc::new(crate_graph)); | ||
272 | 254 | ||
273 | let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); | 255 | let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); |
274 | let krate = module.krate(&db).unwrap(); | 256 | let krate = module.krate(&db).unwrap(); |
@@ -285,22 +267,31 @@ fn glob_across_crates() { | |||
285 | 267 | ||
286 | #[test] | 268 | #[test] |
287 | fn module_resolution_works_for_non_standard_filenames() { | 269 | fn module_resolution_works_for_non_standard_filenames() { |
288 | let (item_map, module_id) = item_map_custom_crate_root( | 270 | let mut db = MockDatabase::with_files( |
289 | " | 271 | " |
290 | //- /my_library.rs | 272 | //- /my_library.rs |
291 | mod foo; | 273 | mod foo; |
292 | use self::foo::Bar; | 274 | use self::foo::Bar; |
293 | <|> | 275 | |
294 | //- /foo/mod.rs | 276 | //- /foo/mod.rs |
295 | pub struct Bar; | 277 | pub struct Bar; |
296 | ", | 278 | ", |
297 | ); | 279 | ); |
280 | db.set_crate_graph_from_fixture(crate_graph! { | ||
281 | "my_library": ("/my_library.rs", []), | ||
282 | }); | ||
283 | let file_id = db.file_id_of("/my_library.rs"); | ||
284 | |||
285 | let module = crate::source_binder::module_from_file_id(&db, file_id).unwrap(); | ||
286 | let krate = module.krate(&db).unwrap(); | ||
287 | let module_id = module.module_id; | ||
288 | let item_map = db.item_map(krate); | ||
298 | check_module_item_map( | 289 | check_module_item_map( |
299 | &item_map, | 290 | &item_map, |
300 | module_id, | 291 | module_id, |
301 | " | 292 | " |
302 | Bar: t v | 293 | Bar: t v |
303 | foo: t | 294 | foo: t |
304 | ", | 295 | ", |
305 | ); | 296 | ); |
306 | } | 297 | } |
@@ -417,17 +408,13 @@ fn item_map_across_crates() { | |||
417 | 408 | ||
418 | //- /lib.rs | 409 | //- /lib.rs |
419 | pub struct Baz; | 410 | pub struct Baz; |
420 | ", | 411 | ", |
421 | ); | 412 | ); |
413 | db.set_crate_graph_from_fixture(crate_graph! { | ||
414 | "main": ("/main.rs", ["test_crate"]), | ||
415 | "test_crate": ("/lib.rs", []), | ||
416 | }); | ||
422 | let main_id = db.file_id_of("/main.rs"); | 417 | let main_id = db.file_id_of("/main.rs"); |
423 | let lib_id = db.file_id_of("/lib.rs"); | ||
424 | |||
425 | let mut crate_graph = CrateGraph::default(); | ||
426 | let main_crate = crate_graph.add_crate_root(main_id); | ||
427 | let lib_crate = crate_graph.add_crate_root(lib_id); | ||
428 | crate_graph.add_dep(main_crate, "test_crate".into(), lib_crate).unwrap(); | ||
429 | |||
430 | db.set_crate_graph(Arc::new(crate_graph)); | ||
431 | 418 | ||
432 | let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); | 419 | let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); |
433 | let krate = module.krate(&db).unwrap(); | 420 | let krate = module.krate(&db).unwrap(); |
@@ -437,7 +424,7 @@ fn item_map_across_crates() { | |||
437 | &item_map, | 424 | &item_map, |
438 | module.module_id, | 425 | module.module_id, |
439 | " | 426 | " |
440 | Baz: t v | 427 | Baz: t v |
441 | ", | 428 | ", |
442 | ); | 429 | ); |
443 | } | 430 | } |
@@ -457,18 +444,13 @@ fn extern_crate_rename() { | |||
457 | 444 | ||
458 | //- /lib.rs | 445 | //- /lib.rs |
459 | struct Arc; | 446 | struct Arc; |
460 | ", | 447 | ", |
461 | ); | 448 | ); |
462 | let main_id = db.file_id_of("/main.rs"); | 449 | db.set_crate_graph_from_fixture(crate_graph! { |
450 | "main": ("/main.rs", ["alloc"]), | ||
451 | "alloc": ("/lib.rs", []), | ||
452 | }); | ||
463 | let sync_id = db.file_id_of("/sync.rs"); | 453 | let sync_id = db.file_id_of("/sync.rs"); |
464 | let lib_id = db.file_id_of("/lib.rs"); | ||
465 | |||
466 | let mut crate_graph = CrateGraph::default(); | ||
467 | let main_crate = crate_graph.add_crate_root(main_id); | ||
468 | let lib_crate = crate_graph.add_crate_root(lib_id); | ||
469 | crate_graph.add_dep(main_crate, "alloc".into(), lib_crate).unwrap(); | ||
470 | |||
471 | db.set_crate_graph(Arc::new(crate_graph)); | ||
472 | 454 | ||
473 | let module = crate::source_binder::module_from_file_id(&db, sync_id).unwrap(); | 455 | let module = crate::source_binder::module_from_file_id(&db, sync_id).unwrap(); |
474 | let krate = module.krate(&db).unwrap(); | 456 | let krate = module.krate(&db).unwrap(); |
@@ -478,7 +460,7 @@ fn extern_crate_rename() { | |||
478 | &item_map, | 460 | &item_map, |
479 | module.module_id, | 461 | module.module_id, |
480 | " | 462 | " |
481 | Arc: t v | 463 | Arc: t v |
482 | ", | 464 | ", |
483 | ); | 465 | ); |
484 | } | 466 | } |
@@ -494,21 +476,17 @@ fn import_across_source_roots() { | |||
494 | } | 476 | } |
495 | } | 477 | } |
496 | 478 | ||
497 | //- root /test_crate/ | 479 | //- root /main/ |
498 | 480 | ||
499 | //- /test_crate/main.rs | 481 | //- /main/main.rs |
500 | use test_crate::a::b::C; | 482 | use test_crate::a::b::C; |
501 | ", | 483 | ", |
502 | ); | 484 | ); |
503 | let lib_id = db.file_id_of("/lib.rs"); | 485 | db.set_crate_graph_from_fixture(crate_graph! { |
504 | let main_id = db.file_id_of("/test_crate/main.rs"); | 486 | "main": ("/main/main.rs", ["test_crate"]), |
505 | 487 | "test_crate": ("/lib.rs", []), | |
506 | let mut crate_graph = CrateGraph::default(); | 488 | }); |
507 | let main_crate = crate_graph.add_crate_root(main_id); | 489 | let main_id = db.file_id_of("/main/main.rs"); |
508 | let lib_crate = crate_graph.add_crate_root(lib_id); | ||
509 | crate_graph.add_dep(main_crate, "test_crate".into(), lib_crate).unwrap(); | ||
510 | |||
511 | db.set_crate_graph(Arc::new(crate_graph)); | ||
512 | 490 | ||
513 | let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); | 491 | let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); |
514 | let krate = module.krate(&db).unwrap(); | 492 | let krate = module.krate(&db).unwrap(); |
@@ -537,17 +515,13 @@ fn reexport_across_crates() { | |||
537 | 515 | ||
538 | //- /foo.rs | 516 | //- /foo.rs |
539 | pub struct Baz; | 517 | pub struct Baz; |
540 | ", | 518 | ", |
541 | ); | 519 | ); |
520 | db.set_crate_graph_from_fixture(crate_graph! { | ||
521 | "main": ("/main.rs", ["test_crate"]), | ||
522 | "test_crate": ("/lib.rs", []), | ||
523 | }); | ||
542 | let main_id = db.file_id_of("/main.rs"); | 524 | let main_id = db.file_id_of("/main.rs"); |
543 | let lib_id = db.file_id_of("/lib.rs"); | ||
544 | |||
545 | let mut crate_graph = CrateGraph::default(); | ||
546 | let main_crate = crate_graph.add_crate_root(main_id); | ||
547 | let lib_crate = crate_graph.add_crate_root(lib_id); | ||
548 | crate_graph.add_dep(main_crate, "test_crate".into(), lib_crate).unwrap(); | ||
549 | |||
550 | db.set_crate_graph(Arc::new(crate_graph)); | ||
551 | 525 | ||
552 | let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); | 526 | let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); |
553 | let krate = module.krate(&db).unwrap(); | 527 | let krate = module.krate(&db).unwrap(); |
@@ -557,7 +531,7 @@ fn reexport_across_crates() { | |||
557 | &item_map, | 531 | &item_map, |
558 | module.module_id, | 532 | module.module_id, |
559 | " | 533 | " |
560 | Baz: t v | 534 | Baz: t v |
561 | ", | 535 | ", |
562 | ); | 536 | ); |
563 | } | 537 | } |