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.rs144
1 files changed, 55 insertions, 89 deletions
diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs
index 9c0e4ef29..9b621fbc2 100644
--- a/crates/ra_hir/src/nameres/tests.rs
+++ b/crates/ra_hir/src/nameres/tests.rs
@@ -1,7 +1,6 @@
1use std::sync::Arc; 1use std::sync::Arc;
2 2
3use ra_db::{CrateGraph, SourceRootId, SourceDatabase}; 3use ra_db::SourceDatabase;
4use relative_path::RelativePath;
5use test_utils::{assert_eq_text, covers}; 4use test_utils::{assert_eq_text, covers};
6 5
7use crate::{ 6use crate::{
@@ -20,20 +19,6 @@ fn item_map(fixture: &str) -> (Arc<ItemMap>, ModuleId) {
20 (db.item_map(krate), module_id) 19 (db.item_map(krate), module_id)
21} 20}
22 21
23/// Sets the crate root to the file of the cursor marker
24fn item_map_custom_crate_root(fixture: &str) -> (Arc<ItemMap>, ModuleId) {
25 let (mut db, pos) = MockDatabase::with_position(fixture);
26
27 let mut crate_graph = CrateGraph::default();
28 crate_graph.add_crate_root(pos.file_id);
29 db.set_crate_graph(Arc::new(crate_graph));
30
31 let module = crate::source_binder::module_from_position(&db, pos).unwrap();
32 let krate = module.krate(&db).unwrap();
33 let module_id = module.module_id;
34 (db.item_map(krate), module_id)
35}
36
37fn check_module_item_map(map: &ItemMap, module_id: ModuleId, expected: &str) { 22fn check_module_item_map(map: &ItemMap, module_id: ModuleId, expected: &str) {
38 let mut lines = map[module_id] 23 let mut lines = map[module_id]
39 .items 24 .items
@@ -252,24 +237,20 @@ fn glob_enum() {
252#[test] 237#[test]
253fn glob_across_crates() { 238fn glob_across_crates() {
254 covers!(glob_across_crates); 239 covers!(glob_across_crates);
255 let (mut db, sr) = MockDatabase::with_files( 240 let mut db = MockDatabase::with_files(
256 " 241 "
257 //- /main.rs 242 //- /main.rs
258 use test_crate::*; 243 use test_crate::*;
259 244
260 //- /lib.rs 245 //- /lib.rs
261 pub struct Baz; 246 pub struct Baz;
262 ", 247 ",
263 ); 248 );
264 let main_id = sr.files[RelativePath::new("/main.rs")]; 249 db.set_crate_graph_from_fixture(crate_graph! {
265 let lib_id = sr.files[RelativePath::new("/lib.rs")]; 250 "main": ("/main.rs", ["test_crate"]),
266 251 "test_crate": ("/lib.rs", []),
267 let mut crate_graph = CrateGraph::default(); 252 });
268 let main_crate = crate_graph.add_crate_root(main_id); 253 let main_id = db.file_id_of("/main.rs");
269 let lib_crate = crate_graph.add_crate_root(lib_id);
270 crate_graph.add_dep(main_crate, "test_crate".into(), lib_crate).unwrap();
271
272 db.set_crate_graph(Arc::new(crate_graph));
273 254
274 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();
275 let krate = module.krate(&db).unwrap(); 256 let krate = module.krate(&db).unwrap();
@@ -286,22 +267,31 @@ fn glob_across_crates() {
286 267
287#[test] 268#[test]
288fn module_resolution_works_for_non_standard_filenames() { 269fn module_resolution_works_for_non_standard_filenames() {
289 let (item_map, module_id) = item_map_custom_crate_root( 270 let mut db = MockDatabase::with_files(
290 " 271 "
291 //- /my_library.rs 272 //- /my_library.rs
292 mod foo; 273 mod foo;
293 use self::foo::Bar; 274 use self::foo::Bar;
294 <|> 275
295 //- /foo/mod.rs 276 //- /foo/mod.rs
296 pub struct Bar; 277 pub struct Bar;
297 ", 278 ",
298 ); 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);
299 check_module_item_map( 289 check_module_item_map(
300 &item_map, 290 &item_map,
301 module_id, 291 module_id,
302 " 292 "
303 Bar: t v 293 Bar: t v
304 foo: t 294 foo: t
305 ", 295 ",
306 ); 296 );
307} 297}
@@ -411,24 +401,20 @@ fn item_map_enum_importing() {
411 401
412#[test] 402#[test]
413fn item_map_across_crates() { 403fn item_map_across_crates() {
414 let (mut db, sr) = MockDatabase::with_files( 404 let mut db = MockDatabase::with_files(
415 " 405 "
416 //- /main.rs 406 //- /main.rs
417 use test_crate::Baz; 407 use test_crate::Baz;
418 408
419 //- /lib.rs 409 //- /lib.rs
420 pub struct Baz; 410 pub struct Baz;
421 ", 411 ",
422 ); 412 );
423 let main_id = sr.files[RelativePath::new("/main.rs")]; 413 db.set_crate_graph_from_fixture(crate_graph! {
424 let lib_id = sr.files[RelativePath::new("/lib.rs")]; 414 "main": ("/main.rs", ["test_crate"]),
425 415 "test_crate": ("/lib.rs", []),
426 let mut crate_graph = CrateGraph::default(); 416 });
427 let main_crate = crate_graph.add_crate_root(main_id); 417 let main_id = db.file_id_of("/main.rs");
428 let lib_crate = crate_graph.add_crate_root(lib_id);
429 crate_graph.add_dep(main_crate, "test_crate".into(), lib_crate).unwrap();
430
431 db.set_crate_graph(Arc::new(crate_graph));
432 418
433 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();
434 let krate = module.krate(&db).unwrap(); 420 let krate = module.krate(&db).unwrap();
@@ -438,14 +424,14 @@ fn item_map_across_crates() {
438 &item_map, 424 &item_map,
439 module.module_id, 425 module.module_id,
440 " 426 "
441 Baz: t v 427 Baz: t v
442 ", 428 ",
443 ); 429 );
444} 430}
445 431
446#[test] 432#[test]
447fn extern_crate_rename() { 433fn extern_crate_rename() {
448 let (mut db, sr) = MockDatabase::with_files( 434 let mut db = MockDatabase::with_files(
449 " 435 "
450 //- /main.rs 436 //- /main.rs
451 extern crate alloc as alloc_crate; 437 extern crate alloc as alloc_crate;
@@ -458,18 +444,13 @@ fn extern_crate_rename() {
458 444
459 //- /lib.rs 445 //- /lib.rs
460 struct Arc; 446 struct Arc;
461 ", 447 ",
462 ); 448 );
463 let main_id = sr.files[RelativePath::new("/main.rs")]; 449 db.set_crate_graph_from_fixture(crate_graph! {
464 let sync_id = sr.files[RelativePath::new("/sync.rs")]; 450 "main": ("/main.rs", ["alloc"]),
465 let lib_id = sr.files[RelativePath::new("/lib.rs")]; 451 "alloc": ("/lib.rs", []),
466 452 });
467 let mut crate_graph = CrateGraph::default(); 453 let sync_id = db.file_id_of("/sync.rs");
468 let main_crate = crate_graph.add_crate_root(main_id);
469 let lib_crate = crate_graph.add_crate_root(lib_id);
470 crate_graph.add_dep(main_crate, "alloc".into(), lib_crate).unwrap();
471
472 db.set_crate_graph(Arc::new(crate_graph));
473 454
474 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();
475 let krate = module.krate(&db).unwrap(); 456 let krate = module.krate(&db).unwrap();
@@ -479,14 +460,14 @@ fn extern_crate_rename() {
479 &item_map, 460 &item_map,
480 module.module_id, 461 module.module_id,
481 " 462 "
482 Arc: t v 463 Arc: t v
483 ", 464 ",
484 ); 465 );
485} 466}
486 467
487#[test] 468#[test]
488fn import_across_source_roots() { 469fn import_across_source_roots() {
489 let (mut db, sr) = MockDatabase::with_files( 470 let mut db = MockDatabase::with_files(
490 " 471 "
491 //- /lib.rs 472 //- /lib.rs
492 pub mod a { 473 pub mod a {
@@ -494,29 +475,18 @@ fn import_across_source_roots() {
494 pub struct C; 475 pub struct C;
495 } 476 }
496 } 477 }
497 ",
498 );
499 let lib_id = sr.files[RelativePath::new("/lib.rs")];
500 478
501 let source_root = SourceRootId(1); 479 //- root /main/
502 480
503 let (sr2, pos) = db.add_fixture( 481 //- /main/main.rs
504 source_root,
505 "
506 //- /main.rs
507 use test_crate::a::b::C; 482 use test_crate::a::b::C;
508 ", 483 ",
509 ); 484 );
510 assert!(pos.is_none()); 485 db.set_crate_graph_from_fixture(crate_graph! {
511 486 "main": ("/main/main.rs", ["test_crate"]),
512 let main_id = sr2.files[RelativePath::new("/main.rs")]; 487 "test_crate": ("/lib.rs", []),
513 488 });
514 let mut crate_graph = CrateGraph::default(); 489 let main_id = db.file_id_of("/main/main.rs");
515 let main_crate = crate_graph.add_crate_root(main_id);
516 let lib_crate = crate_graph.add_crate_root(lib_id);
517 crate_graph.add_dep(main_crate, "test_crate".into(), lib_crate).unwrap();
518
519 db.set_crate_graph(Arc::new(crate_graph));
520 490
521 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();
522 let krate = module.krate(&db).unwrap(); 492 let krate = module.krate(&db).unwrap();
@@ -533,7 +503,7 @@ fn import_across_source_roots() {
533 503
534#[test] 504#[test]
535fn reexport_across_crates() { 505fn reexport_across_crates() {
536 let (mut db, sr) = MockDatabase::with_files( 506 let mut db = MockDatabase::with_files(
537 " 507 "
538 //- /main.rs 508 //- /main.rs
539 use test_crate::Baz; 509 use test_crate::Baz;
@@ -545,17 +515,13 @@ fn reexport_across_crates() {
545 515
546 //- /foo.rs 516 //- /foo.rs
547 pub struct Baz; 517 pub struct Baz;
548 ", 518 ",
549 ); 519 );
550 let main_id = sr.files[RelativePath::new("/main.rs")]; 520 db.set_crate_graph_from_fixture(crate_graph! {
551 let lib_id = sr.files[RelativePath::new("/lib.rs")]; 521 "main": ("/main.rs", ["test_crate"]),
552 522 "test_crate": ("/lib.rs", []),
553 let mut crate_graph = CrateGraph::default(); 523 });
554 let main_crate = crate_graph.add_crate_root(main_id); 524 let main_id = db.file_id_of("/main.rs");
555 let lib_crate = crate_graph.add_crate_root(lib_id);
556 crate_graph.add_dep(main_crate, "test_crate".into(), lib_crate).unwrap();
557
558 db.set_crate_graph(Arc::new(crate_graph));
559 525
560 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();
561 let krate = module.krate(&db).unwrap(); 527 let krate = module.krate(&db).unwrap();
@@ -565,7 +531,7 @@ fn reexport_across_crates() {
565 &item_map, 531 &item_map,
566 module.module_id, 532 module.module_id,
567 " 533 "
568 Baz: t v 534 Baz: t v
569 ", 535 ",
570 ); 536 );
571} 537}