aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_db
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_db')
-rw-r--r--crates/ide_db/src/helpers/insert_use/tests.rs109
1 files changed, 77 insertions, 32 deletions
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 @@
1use super::*; 1use super::*;
2 2
3use hir::PrefixKind; 3use hir::PrefixKind;
4use test_utils::assert_eq_text; 4use test_utils::{assert_eq_text, extract_range_or_offset, CURSOR_MARKER};
5
6#[test]
7fn respects_cfg_attr_fn() {
8 check(
9 r"bar::Bar",
10 r#"
11#[cfg(test)]
12fn foo() {$0}
13"#,
14 r#"
15#[cfg(test)]
16fn foo() {
17use bar::Bar;
18}
19"#,
20 ImportGranularity::Crate,
21 );
22}
23
24#[test]
25fn respects_cfg_attr_const() {
26 check(
27 r"bar::Bar",
28 r#"
29#[cfg(test)]
30const FOO: Bar = {$0};
31"#,
32 r#"
33#[cfg(test)]
34const FOO: Bar = {
35use bar::Bar;
36};
37"#,
38 ImportGranularity::Crate,
39 );
40}
5 41
6#[test] 42#[test]
7fn insert_skips_lone_glob_imports() { 43fn insert_skips_lone_glob_imports() {
@@ -15,15 +51,13 @@ use foo::bar::*;
15use foo::baz::A; 51use 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]
24fn insert_not_group() { 58fn 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"
29use std::bar::B; 63use std::bar::B;
@@ -38,24 +72,32 @@ use crate::bar::A;
38use self::bar::A; 72use self::bar::A;
39use super::bar::A; 73use super::bar::A;
40use external_crate2::bar::A;", 74use 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]
48fn insert_not_group_empty() { 86fn 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"{ 340mod x {$0}
341",
342 r"
343mod 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
844fn check_crate(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { 889fn 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
848fn check_module(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { 893fn 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
852fn check_none(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) { 897fn 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
856fn check_merge_only_fail(ra_fixture0: &str, ra_fixture1: &str, mb: MergeBehavior) { 901fn check_merge_only_fail(ra_fixture0: &str, ra_fixture1: &str, mb: MergeBehavior) {