aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_db/src/helpers/insert_use
diff options
context:
space:
mode:
authorLukas Tobias Wirth <[email protected]>2021-05-18 19:21:47 +0100
committerLukas Tobias Wirth <[email protected]>2021-05-19 14:57:10 +0100
commitb4fe479236f592fcbfa1422dda54253b77d8b0e1 (patch)
tree0ce721228484783514b8948a0a64e3e4ca38ea95 /crates/ide_db/src/helpers/insert_use
parent5fd9f6c7b9944638e4781e3d9384638942f84456 (diff)
Replace ImportGranularity::Guess with guessing boolean flag
Diffstat (limited to 'crates/ide_db/src/helpers/insert_use')
-rw-r--r--crates/ide_db/src/helpers/insert_use/tests.rs115
1 files changed, 114 insertions, 1 deletions
diff --git a/crates/ide_db/src/helpers/insert_use/tests.rs b/crates/ide_db/src/helpers/insert_use/tests.rs
index f99857a89..f795bbf00 100644
--- a/crates/ide_db/src/helpers/insert_use/tests.rs
+++ b/crates/ide_db/src/helpers/insert_use/tests.rs
@@ -631,6 +631,104 @@ fn merge_last_fail3() {
631 ); 631 );
632} 632}
633 633
634#[test]
635fn guess_empty() {
636 check_guess("", ImportGranularityGuess::Unknown);
637}
638
639#[test]
640fn guess_single() {
641 check_guess(r"use foo::{baz::{qux, quux}, bar};", ImportGranularityGuess::Crate);
642 check_guess(r"use foo::bar;", ImportGranularityGuess::Unknown);
643 check_guess(r"use foo::bar::{baz, qux};", ImportGranularityGuess::CrateOrModule);
644}
645
646#[test]
647fn guess_unknown() {
648 check_guess(
649 r"
650use foo::bar::baz;
651use oof::rab::xuq;
652",
653 ImportGranularityGuess::Unknown,
654 );
655}
656
657#[test]
658fn guess_item() {
659 check_guess(
660 r"
661use foo::bar::baz;
662use foo::bar::qux;
663",
664 ImportGranularityGuess::Item,
665 );
666}
667
668#[test]
669fn guess_module() {
670 check_guess(
671 r"
672use foo::bar::baz;
673use foo::bar::{qux, quux};
674",
675 ImportGranularityGuess::Module,
676 );
677 // this is a rather odd case, technically this file isn't following any style properly.
678 check_guess(
679 r"
680use foo::bar::baz;
681use foo::{baz::{qux, quux}, bar};
682",
683 ImportGranularityGuess::Module,
684 );
685}
686
687#[test]
688fn guess_crate_or_module() {
689 check_guess(
690 r"
691use foo::bar::baz;
692use oof::bar::{qux, quux};
693",
694 ImportGranularityGuess::CrateOrModule,
695 );
696}
697
698#[test]
699fn guess_crate() {
700 check_guess(
701 r"
702use frob::bar::baz;
703use foo::{baz::{qux, quux}, bar};
704",
705 ImportGranularityGuess::Crate,
706 );
707}
708
709#[test]
710fn guess_skips_differing_vis() {
711 check_guess(
712 r"
713use foo::bar::baz;
714pub use foo::bar::qux;
715",
716 ImportGranularityGuess::Unknown,
717 );
718}
719
720#[test]
721fn guess_grouping_matters() {
722 check_guess(
723 r"
724use foo::bar::baz;
725use oof::bar::baz;
726use foo::bar::qux;
727",
728 ImportGranularityGuess::Unknown,
729 );
730}
731
634fn check( 732fn check(
635 path: &str, 733 path: &str,
636 ra_fixture_before: &str, 734 ra_fixture_before: &str,
@@ -651,7 +749,16 @@ fn check(
651 .find_map(ast::Path::cast) 749 .find_map(ast::Path::cast)
652 .unwrap(); 750 .unwrap();
653 751
654 insert_use(&file, path, InsertUseConfig { granularity, prefix_kind: PrefixKind::Plain, group }); 752 insert_use(
753 &file,
754 path,
755 InsertUseConfig {
756 granularity,
757 enforce_granularity: true,
758 prefix_kind: PrefixKind::Plain,
759 group,
760 },
761 );
655 let result = file.as_syntax_node().to_string(); 762 let result = file.as_syntax_node().to_string();
656 assert_eq_text!(ra_fixture_after, &result); 763 assert_eq_text!(ra_fixture_after, &result);
657} 764}
@@ -686,3 +793,9 @@ fn check_merge_only_fail(ra_fixture0: &str, ra_fixture1: &str, mb: MergeBehavior
686 let result = try_merge_imports(&use0, &use1, mb); 793 let result = try_merge_imports(&use0, &use1, mb);
687 assert_eq!(result.map(|u| u.to_string()), None); 794 assert_eq!(result.map(|u| u.to_string()), None);
688} 795}
796
797fn check_guess(ra_fixture: &str, expected: ImportGranularityGuess) {
798 let syntax = ast::SourceFile::parse(ra_fixture).tree().syntax().clone();
799 let file = super::ImportScope::from(syntax).unwrap();
800 assert_eq!(file.guess_granularity_from_scope(), expected);
801}