diff options
Diffstat (limited to 'crates/ide_db/src/helpers/insert_use')
-rw-r--r-- | crates/ide_db/src/helpers/insert_use/tests.rs | 172 |
1 files changed, 134 insertions, 38 deletions
diff --git a/crates/ide_db/src/helpers/insert_use/tests.rs b/crates/ide_db/src/helpers/insert_use/tests.rs index 70b11bf81..01894630a 100644 --- a/crates/ide_db/src/helpers/insert_use/tests.rs +++ b/crates/ide_db/src/helpers/insert_use/tests.rs | |||
@@ -1,12 +1,63 @@ | |||
1 | use super::*; | 1 | use super::*; |
2 | 2 | ||
3 | use hir::PrefixKind; | 3 | use hir::PrefixKind; |
4 | use test_utils::assert_eq_text; | 4 | use test_utils::{assert_eq_text, extract_range_or_offset, CURSOR_MARKER}; |
5 | |||
6 | #[test] | ||
7 | fn respects_cfg_attr_fn() { | ||
8 | check( | ||
9 | r"bar::Bar", | ||
10 | r#" | ||
11 | #[cfg(test)] | ||
12 | fn foo() {$0} | ||
13 | "#, | ||
14 | r#" | ||
15 | #[cfg(test)] | ||
16 | fn foo() { | ||
17 | use bar::Bar; | ||
18 | } | ||
19 | "#, | ||
20 | ImportGranularity::Crate, | ||
21 | ); | ||
22 | } | ||
23 | |||
24 | #[test] | ||
25 | fn respects_cfg_attr_const() { | ||
26 | check( | ||
27 | r"bar::Bar", | ||
28 | r#" | ||
29 | #[cfg(test)] | ||
30 | const FOO: Bar = {$0}; | ||
31 | "#, | ||
32 | r#" | ||
33 | #[cfg(test)] | ||
34 | const FOO: Bar = { | ||
35 | use bar::Bar; | ||
36 | }; | ||
37 | "#, | ||
38 | ImportGranularity::Crate, | ||
39 | ); | ||
40 | } | ||
41 | |||
42 | #[test] | ||
43 | fn insert_skips_lone_glob_imports() { | ||
44 | check( | ||
45 | "use foo::baz::A", | ||
46 | r" | ||
47 | use foo::bar::*; | ||
48 | ", | ||
49 | r" | ||
50 | use foo::bar::*; | ||
51 | use foo::baz::A; | ||
52 | ", | ||
53 | ImportGranularity::Crate, | ||
54 | ); | ||
55 | } | ||
5 | 56 | ||
6 | #[test] | 57 | #[test] |
7 | fn insert_not_group() { | 58 | fn insert_not_group() { |
8 | cov_mark::check!(insert_no_grouping_last); | 59 | cov_mark::check!(insert_no_grouping_last); |
9 | check( | 60 | check_with_config( |
10 | "use external_crate2::bar::A", | 61 | "use external_crate2::bar::A", |
11 | r" | 62 | r" |
12 | use std::bar::B; | 63 | use std::bar::B; |
@@ -21,24 +72,32 @@ use crate::bar::A; | |||
21 | use self::bar::A; | 72 | use self::bar::A; |
22 | use super::bar::A; | 73 | use super::bar::A; |
23 | use external_crate2::bar::A;", | 74 | use external_crate2::bar::A;", |
24 | ImportGranularity::Item, | 75 | &InsertUseConfig { |
25 | false, | 76 | granularity: ImportGranularity::Item, |
26 | false, | 77 | enforce_granularity: true, |
78 | prefix_kind: PrefixKind::Plain, | ||
79 | group: false, | ||
80 | skip_glob_imports: true, | ||
81 | }, | ||
27 | ); | 82 | ); |
28 | } | 83 | } |
29 | 84 | ||
30 | #[test] | 85 | #[test] |
31 | fn insert_not_group_empty() { | 86 | fn insert_not_group_empty() { |
32 | cov_mark::check!(insert_no_grouping_last2); | 87 | cov_mark::check!(insert_no_grouping_last2); |
33 | check( | 88 | check_with_config( |
34 | "use external_crate2::bar::A", | 89 | "use external_crate2::bar::A", |
35 | r"", | 90 | r"", |
36 | r"use external_crate2::bar::A; | 91 | r"use external_crate2::bar::A; |
37 | 92 | ||
38 | ", | 93 | ", |
39 | ImportGranularity::Item, | 94 | &InsertUseConfig { |
40 | false, | 95 | granularity: ImportGranularity::Item, |
41 | false, | 96 | enforce_granularity: true, |
97 | prefix_kind: PrefixKind::Plain, | ||
98 | group: false, | ||
99 | skip_glob_imports: true, | ||
100 | }, | ||
42 | ); | 101 | ); |
43 | } | 102 | } |
44 | 103 | ||
@@ -277,13 +336,15 @@ fn insert_empty_module() { | |||
277 | cov_mark::check!(insert_group_empty_module); | 336 | cov_mark::check!(insert_group_empty_module); |
278 | check( | 337 | check( |
279 | "foo::bar", | 338 | "foo::bar", |
280 | "mod x {}", | 339 | r" |
281 | r"{ | 340 | mod x {$0} |
341 | ", | ||
342 | r" | ||
343 | mod x { | ||
282 | use foo::bar; | 344 | use foo::bar; |
283 | }", | 345 | } |
346 | ", | ||
284 | ImportGranularity::Item, | 347 | ImportGranularity::Item, |
285 | true, | ||
286 | true, | ||
287 | ) | 348 | ) |
288 | } | 349 | } |
289 | 350 | ||
@@ -511,13 +572,14 @@ use std::io; | |||
511 | } | 572 | } |
512 | 573 | ||
513 | #[test] | 574 | #[test] |
514 | #[ignore] // FIXME: Support this | ||
515 | fn split_out_merge() { | 575 | fn split_out_merge() { |
576 | // FIXME: This is suboptimal, we want to get `use std::fmt::{self, Result}` | ||
577 | // instead. | ||
516 | check_module( | 578 | check_module( |
517 | "std::fmt::Result", | 579 | "std::fmt::Result", |
518 | r"use std::{fmt, io};", | 580 | r"use std::{fmt, io};", |
519 | r"use std::fmt::{self, Result}; | 581 | r"use std::fmt::Result; |
520 | use std::io;", | 582 | use std::{fmt, io};", |
521 | ) | 583 | ) |
522 | } | 584 | } |
523 | 585 | ||
@@ -533,17 +595,35 @@ fn merge_groups_self() { | |||
533 | 595 | ||
534 | #[test] | 596 | #[test] |
535 | fn merge_mod_into_glob() { | 597 | fn merge_mod_into_glob() { |
536 | check_crate( | 598 | check_with_config( |
537 | "token::TokenKind", | 599 | "token::TokenKind", |
538 | r"use token::TokenKind::*;", | 600 | r"use token::TokenKind::*;", |
539 | r"use token::TokenKind::{*, self};", | 601 | r"use token::TokenKind::{*, self};", |
602 | &InsertUseConfig { | ||
603 | granularity: ImportGranularity::Crate, | ||
604 | enforce_granularity: true, | ||
605 | prefix_kind: PrefixKind::Plain, | ||
606 | group: false, | ||
607 | skip_glob_imports: false, | ||
608 | }, | ||
540 | ) | 609 | ) |
541 | // FIXME: have it emit `use token::TokenKind::{self, *}`? | 610 | // FIXME: have it emit `use token::TokenKind::{self, *}`? |
542 | } | 611 | } |
543 | 612 | ||
544 | #[test] | 613 | #[test] |
545 | fn merge_self_glob() { | 614 | fn merge_self_glob() { |
546 | check_crate("self", r"use self::*;", r"use self::{*, self};") | 615 | check_with_config( |
616 | "self", | ||
617 | r"use self::*;", | ||
618 | r"use self::{*, self};", | ||
619 | &InsertUseConfig { | ||
620 | granularity: ImportGranularity::Crate, | ||
621 | enforce_granularity: true, | ||
622 | prefix_kind: PrefixKind::Plain, | ||
623 | group: false, | ||
624 | skip_glob_imports: false, | ||
625 | }, | ||
626 | ) | ||
547 | // FIXME: have it emit `use {self, *}`? | 627 | // FIXME: have it emit `use {self, *}`? |
548 | } | 628 | } |
549 | 629 | ||
@@ -756,19 +836,24 @@ use foo::bar::qux; | |||
756 | ); | 836 | ); |
757 | } | 837 | } |
758 | 838 | ||
759 | fn check( | 839 | fn check_with_config( |
760 | path: &str, | 840 | path: &str, |
761 | ra_fixture_before: &str, | 841 | ra_fixture_before: &str, |
762 | ra_fixture_after: &str, | 842 | ra_fixture_after: &str, |
763 | granularity: ImportGranularity, | 843 | config: &InsertUseConfig, |
764 | module: bool, | ||
765 | group: bool, | ||
766 | ) { | 844 | ) { |
767 | let mut syntax = ast::SourceFile::parse(ra_fixture_before).tree().syntax().clone(); | 845 | let (text, pos) = if ra_fixture_before.contains(CURSOR_MARKER) { |
768 | if module { | 846 | let (range_or_offset, text) = extract_range_or_offset(ra_fixture_before); |
769 | syntax = syntax.descendants().find_map(ast::Module::cast).unwrap().syntax().clone(); | 847 | (text, Some(range_or_offset)) |
770 | } | 848 | } else { |
771 | let file = super::ImportScope::from(syntax.clone_for_update()).unwrap(); | 849 | (ra_fixture_before.to_owned(), None) |
850 | }; | ||
851 | let syntax = ast::SourceFile::parse(&text).tree().syntax().clone_for_update(); | ||
852 | let file = pos | ||
853 | .and_then(|pos| syntax.token_at_offset(pos.expect_offset()).next()?.parent()) | ||
854 | .and_then(|it| super::ImportScope::find_insert_use_container(&it)) | ||
855 | .or_else(|| super::ImportScope::from(syntax)) | ||
856 | .unwrap(); | ||
772 | let path = ast::SourceFile::parse(&format!("use {};", path)) | 857 | let path = ast::SourceFile::parse(&format!("use {};", path)) |
773 | .tree() | 858 | .tree() |
774 | .syntax() | 859 | .syntax() |
@@ -776,30 +861,41 @@ fn check( | |||
776 | .find_map(ast::Path::cast) | 861 | .find_map(ast::Path::cast) |
777 | .unwrap(); | 862 | .unwrap(); |
778 | 863 | ||
779 | insert_use( | 864 | insert_use(&file, path, config); |
780 | &file, | 865 | let result = file.as_syntax_node().ancestors().last().unwrap().to_string(); |
866 | assert_eq_text!(ra_fixture_after, &result); | ||
867 | } | ||
868 | |||
869 | fn check( | ||
870 | path: &str, | ||
871 | ra_fixture_before: &str, | ||
872 | ra_fixture_after: &str, | ||
873 | granularity: ImportGranularity, | ||
874 | ) { | ||
875 | check_with_config( | ||
781 | path, | 876 | path, |
782 | InsertUseConfig { | 877 | ra_fixture_before, |
878 | ra_fixture_after, | ||
879 | &InsertUseConfig { | ||
783 | granularity, | 880 | granularity, |
784 | enforce_granularity: true, | 881 | enforce_granularity: true, |
785 | prefix_kind: PrefixKind::Plain, | 882 | prefix_kind: PrefixKind::Plain, |
786 | group, | 883 | group: true, |
884 | skip_glob_imports: true, | ||
787 | }, | 885 | }, |
788 | ); | 886 | ) |
789 | let result = file.as_syntax_node().to_string(); | ||
790 | assert_eq_text!(ra_fixture_after, &result); | ||
791 | } | 887 | } |
792 | 888 | ||
793 | fn check_crate(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { | 889 | fn check_crate(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { |
794 | check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Crate, false, true) | 890 | check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Crate) |
795 | } | 891 | } |
796 | 892 | ||
797 | fn check_module(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { | 893 | fn check_module(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { |
798 | check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Module, false, true) | 894 | check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Module) |
799 | } | 895 | } |
800 | 896 | ||
801 | fn check_none(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { | 897 | fn check_none(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { |
802 | check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Item, false, true) | 898 | check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Item) |
803 | } | 899 | } |
804 | 900 | ||
805 | fn check_merge_only_fail(ra_fixture0: &str, ra_fixture1: &str, mb: MergeBehavior) { | 901 | fn check_merge_only_fail(ra_fixture0: &str, ra_fixture1: &str, mb: MergeBehavior) { |