diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-06-19 21:33:54 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2021-06-19 21:33:54 +0100 |
commit | 9d60d75e1400ba82ae8d41496d8a0fb9a8edec0c (patch) | |
tree | 349e09755a03480e318d32acd322bbee78787abd | |
parent | c2aa7782d65c4f2765f68b99f00f4203e1f143c1 (diff) | |
parent | 2113c467970ed80155bd4c6d8c5efa7dc0c114d1 (diff) |
Merge #9339
9339: minor: Cleanup insert_use tests r=Veykril a=Veykril
bors r+
Co-authored-by: Lukas Wirth <[email protected]>
-rw-r--r-- | crates/base_db/src/fixture.rs | 2 | ||||
-rw-r--r-- | crates/ide_assists/src/handlers/auto_import.rs | 58 | ||||
-rw-r--r-- | crates/ide_db/src/helpers/insert_use/tests.rs | 109 |
3 files changed, 78 insertions, 91 deletions
diff --git a/crates/base_db/src/fixture.rs b/crates/base_db/src/fixture.rs index d0c946d83..6ce377710 100644 --- a/crates/base_db/src/fixture.rs +++ b/crates/base_db/src/fixture.rs | |||
@@ -106,7 +106,7 @@ impl ChangeFixture { | |||
106 | let (range_or_offset, text) = extract_range_or_offset(&entry.text); | 106 | let (range_or_offset, text) = extract_range_or_offset(&entry.text); |
107 | assert!(file_position.is_none()); | 107 | assert!(file_position.is_none()); |
108 | file_position = Some((file_id, range_or_offset)); | 108 | file_position = Some((file_id, range_or_offset)); |
109 | text.to_string() | 109 | text |
110 | } | 110 | } |
111 | } else { | 111 | } else { |
112 | entry.text.clone() | 112 | entry.text.clone() |
diff --git a/crates/ide_assists/src/handlers/auto_import.rs b/crates/ide_assists/src/handlers/auto_import.rs index 8df8b060d..accc345fc 100644 --- a/crates/ide_assists/src/handlers/auto_import.rs +++ b/crates/ide_assists/src/handlers/auto_import.rs | |||
@@ -995,62 +995,4 @@ const _: () = { | |||
995 | "#, | 995 | "#, |
996 | ); | 996 | ); |
997 | } | 997 | } |
998 | |||
999 | #[test] | ||
1000 | fn respects_cfg_attr() { | ||
1001 | check_assist( | ||
1002 | auto_import, | ||
1003 | r#" | ||
1004 | mod bar { | ||
1005 | pub struct Bar; | ||
1006 | } | ||
1007 | |||
1008 | #[cfg(test)] | ||
1009 | fn foo() { | ||
1010 | Bar$0 | ||
1011 | } | ||
1012 | "#, | ||
1013 | r#" | ||
1014 | mod bar { | ||
1015 | pub struct Bar; | ||
1016 | } | ||
1017 | |||
1018 | #[cfg(test)] | ||
1019 | fn foo() { | ||
1020 | use bar::Bar; | ||
1021 | |||
1022 | Bar | ||
1023 | } | ||
1024 | "#, | ||
1025 | ); | ||
1026 | } | ||
1027 | |||
1028 | #[test] | ||
1029 | fn respects_cfg_attr2() { | ||
1030 | check_assist( | ||
1031 | auto_import, | ||
1032 | r#" | ||
1033 | mod bar { | ||
1034 | pub struct Bar; | ||
1035 | } | ||
1036 | |||
1037 | #[cfg(test)] | ||
1038 | const FOO: Bar = { | ||
1039 | Bar$0 | ||
1040 | } | ||
1041 | "#, | ||
1042 | r#" | ||
1043 | mod bar { | ||
1044 | pub struct Bar; | ||
1045 | } | ||
1046 | |||
1047 | #[cfg(test)] | ||
1048 | const FOO: Bar = { | ||
1049 | use bar::Bar; | ||
1050 | |||
1051 | Bar | ||
1052 | } | ||
1053 | "#, | ||
1054 | ); | ||
1055 | } | ||
1056 | } | 998 | } |
diff --git a/crates/ide_db/src/helpers/insert_use/tests.rs b/crates/ide_db/src/helpers/insert_use/tests.rs index 263edcdc9..01894630a 100644 --- a/crates/ide_db/src/helpers/insert_use/tests.rs +++ b/crates/ide_db/src/helpers/insert_use/tests.rs | |||
@@ -1,7 +1,43 @@ | |||
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 | } | ||
5 | 41 | ||
6 | #[test] | 42 | #[test] |
7 | fn insert_skips_lone_glob_imports() { | 43 | fn insert_skips_lone_glob_imports() { |
@@ -15,15 +51,13 @@ use foo::bar::*; | |||
15 | use foo::baz::A; | 51 | use foo::baz::A; |
16 | ", | 52 | ", |
17 | ImportGranularity::Crate, | 53 | ImportGranularity::Crate, |
18 | false, | ||
19 | false, | ||
20 | ); | 54 | ); |
21 | } | 55 | } |
22 | 56 | ||
23 | #[test] | 57 | #[test] |
24 | fn insert_not_group() { | 58 | fn insert_not_group() { |
25 | cov_mark::check!(insert_no_grouping_last); | 59 | cov_mark::check!(insert_no_grouping_last); |
26 | check( | 60 | check_with_config( |
27 | "use external_crate2::bar::A", | 61 | "use external_crate2::bar::A", |
28 | r" | 62 | r" |
29 | use std::bar::B; | 63 | use std::bar::B; |
@@ -38,24 +72,32 @@ use crate::bar::A; | |||
38 | use self::bar::A; | 72 | use self::bar::A; |
39 | use super::bar::A; | 73 | use super::bar::A; |
40 | use external_crate2::bar::A;", | 74 | use external_crate2::bar::A;", |
41 | ImportGranularity::Item, | 75 | &InsertUseConfig { |
42 | false, | 76 | granularity: ImportGranularity::Item, |
43 | false, | 77 | enforce_granularity: true, |
78 | prefix_kind: PrefixKind::Plain, | ||
79 | group: false, | ||
80 | skip_glob_imports: true, | ||
81 | }, | ||
44 | ); | 82 | ); |
45 | } | 83 | } |
46 | 84 | ||
47 | #[test] | 85 | #[test] |
48 | fn insert_not_group_empty() { | 86 | fn insert_not_group_empty() { |
49 | cov_mark::check!(insert_no_grouping_last2); | 87 | cov_mark::check!(insert_no_grouping_last2); |
50 | check( | 88 | check_with_config( |
51 | "use external_crate2::bar::A", | 89 | "use external_crate2::bar::A", |
52 | r"", | 90 | r"", |
53 | r"use external_crate2::bar::A; | 91 | r"use external_crate2::bar::A; |
54 | 92 | ||
55 | ", | 93 | ", |
56 | ImportGranularity::Item, | 94 | &InsertUseConfig { |
57 | false, | 95 | granularity: ImportGranularity::Item, |
58 | false, | 96 | enforce_granularity: true, |
97 | prefix_kind: PrefixKind::Plain, | ||
98 | group: false, | ||
99 | skip_glob_imports: true, | ||
100 | }, | ||
59 | ); | 101 | ); |
60 | } | 102 | } |
61 | 103 | ||
@@ -294,13 +336,15 @@ fn insert_empty_module() { | |||
294 | cov_mark::check!(insert_group_empty_module); | 336 | cov_mark::check!(insert_group_empty_module); |
295 | check( | 337 | check( |
296 | "foo::bar", | 338 | "foo::bar", |
297 | "mod x {}", | 339 | r" |
298 | r"{ | 340 | mod x {$0} |
341 | ", | ||
342 | r" | ||
343 | mod x { | ||
299 | use foo::bar; | 344 | use foo::bar; |
300 | }", | 345 | } |
346 | ", | ||
301 | ImportGranularity::Item, | 347 | ImportGranularity::Item, |
302 | true, | ||
303 | true, | ||
304 | ) | 348 | ) |
305 | } | 349 | } |
306 | 350 | ||
@@ -555,7 +599,6 @@ fn merge_mod_into_glob() { | |||
555 | "token::TokenKind", | 599 | "token::TokenKind", |
556 | r"use token::TokenKind::*;", | 600 | r"use token::TokenKind::*;", |
557 | r"use token::TokenKind::{*, self};", | 601 | r"use token::TokenKind::{*, self};", |
558 | false, | ||
559 | &InsertUseConfig { | 602 | &InsertUseConfig { |
560 | granularity: ImportGranularity::Crate, | 603 | granularity: ImportGranularity::Crate, |
561 | enforce_granularity: true, | 604 | enforce_granularity: true, |
@@ -573,7 +616,6 @@ fn merge_self_glob() { | |||
573 | "self", | 616 | "self", |
574 | r"use self::*;", | 617 | r"use self::*;", |
575 | r"use self::{*, self};", | 618 | r"use self::{*, self};", |
576 | false, | ||
577 | &InsertUseConfig { | 619 | &InsertUseConfig { |
578 | granularity: ImportGranularity::Crate, | 620 | granularity: ImportGranularity::Crate, |
579 | enforce_granularity: true, | 621 | enforce_granularity: true, |
@@ -798,14 +840,20 @@ fn check_with_config( | |||
798 | path: &str, | 840 | path: &str, |
799 | ra_fixture_before: &str, | 841 | ra_fixture_before: &str, |
800 | ra_fixture_after: &str, | 842 | ra_fixture_after: &str, |
801 | module: bool, | ||
802 | config: &InsertUseConfig, | 843 | config: &InsertUseConfig, |
803 | ) { | 844 | ) { |
804 | let mut syntax = ast::SourceFile::parse(ra_fixture_before).tree().syntax().clone(); | 845 | let (text, pos) = if ra_fixture_before.contains(CURSOR_MARKER) { |
805 | if module { | 846 | let (range_or_offset, text) = extract_range_or_offset(ra_fixture_before); |
806 | syntax = syntax.descendants().find_map(ast::Module::cast).unwrap().syntax().clone(); | 847 | (text, Some(range_or_offset)) |
807 | } | 848 | } else { |
808 | 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(); | ||
809 | let path = ast::SourceFile::parse(&format!("use {};", path)) | 857 | let path = ast::SourceFile::parse(&format!("use {};", path)) |
810 | .tree() | 858 | .tree() |
811 | .syntax() | 859 | .syntax() |
@@ -814,7 +862,7 @@ fn check_with_config( | |||
814 | .unwrap(); | 862 | .unwrap(); |
815 | 863 | ||
816 | insert_use(&file, path, config); | 864 | insert_use(&file, path, config); |
817 | let result = file.as_syntax_node().to_string(); | 865 | let result = file.as_syntax_node().ancestors().last().unwrap().to_string(); |
818 | assert_eq_text!(ra_fixture_after, &result); | 866 | assert_eq_text!(ra_fixture_after, &result); |
819 | } | 867 | } |
820 | 868 | ||
@@ -823,34 +871,31 @@ fn check( | |||
823 | ra_fixture_before: &str, | 871 | ra_fixture_before: &str, |
824 | ra_fixture_after: &str, | 872 | ra_fixture_after: &str, |
825 | granularity: ImportGranularity, | 873 | granularity: ImportGranularity, |
826 | module: bool, | ||
827 | group: bool, | ||
828 | ) { | 874 | ) { |
829 | check_with_config( | 875 | check_with_config( |
830 | path, | 876 | path, |
831 | ra_fixture_before, | 877 | ra_fixture_before, |
832 | ra_fixture_after, | 878 | ra_fixture_after, |
833 | module, | ||
834 | &InsertUseConfig { | 879 | &InsertUseConfig { |
835 | granularity, | 880 | granularity, |
836 | enforce_granularity: true, | 881 | enforce_granularity: true, |
837 | prefix_kind: PrefixKind::Plain, | 882 | prefix_kind: PrefixKind::Plain, |
838 | group, | 883 | group: true, |
839 | skip_glob_imports: true, | 884 | skip_glob_imports: true, |
840 | }, | 885 | }, |
841 | ) | 886 | ) |
842 | } | 887 | } |
843 | 888 | ||
844 | 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) { |
845 | check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Crate, false, true) | 890 | check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Crate) |
846 | } | 891 | } |
847 | 892 | ||
848 | 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) { |
849 | check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Module, false, true) | 894 | check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Module) |
850 | } | 895 | } |
851 | 896 | ||
852 | 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) { |
853 | check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Item, false, true) | 898 | check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Item) |
854 | } | 899 | } |
855 | 900 | ||
856 | 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) { |