]> git.lizzy.rs Git - rust.git/commitdiff
Cleanup insert_use tests
authorLukas Wirth <lukastw97@gmail.com>
Sat, 19 Jun 2021 20:31:24 +0000 (22:31 +0200)
committerLukas Wirth <lukastw97@gmail.com>
Sat, 19 Jun 2021 20:33:29 +0000 (22:33 +0200)
crates/base_db/src/fixture.rs
crates/ide_assists/src/handlers/auto_import.rs
crates/ide_db/src/helpers/insert_use/tests.rs

index d0c946d8335b0bf570dc3d902721c5c9b815b8df..6ce3777106ad91aef8bf0ce49ffece26cf88176b 100644 (file)
@@ -106,7 +106,7 @@ pub fn parse(ra_fixture: &str) -> ChangeFixture {
                     let (range_or_offset, text) = extract_range_or_offset(&entry.text);
                     assert!(file_position.is_none());
                     file_position = Some((file_id, range_or_offset));
-                    text.to_string()
+                    text
                 }
             } else {
                 entry.text.clone()
index 8df8b060d97b6894719433ddb41d2bcd4696467b..accc345fc62554f65be330609ebd462184168971 100644 (file)
@@ -992,64 +992,6 @@ mod foo {}
 const _: () = {
     Foo
 };
-"#,
-        );
-    }
-
-    #[test]
-    fn respects_cfg_attr() {
-        check_assist(
-            auto_import,
-            r#"
-mod bar {
-    pub struct Bar;
-}
-
-#[cfg(test)]
-fn foo() {
-    Bar$0
-}
-"#,
-            r#"
-mod bar {
-    pub struct Bar;
-}
-
-#[cfg(test)]
-fn foo() {
-use bar::Bar;
-
-    Bar
-}
-"#,
-        );
-    }
-
-    #[test]
-    fn respects_cfg_attr2() {
-        check_assist(
-            auto_import,
-            r#"
-mod bar {
-    pub struct Bar;
-}
-
-#[cfg(test)]
-const FOO: Bar = {
-    Bar$0
-}
-"#,
-            r#"
-mod bar {
-    pub struct Bar;
-}
-
-#[cfg(test)]
-const FOO: Bar = {
-use bar::Bar;
-
-    Bar
-}
 "#,
         );
     }
index 263edcdc92defdc7550ae60d358097dfb3827243..01894630a8f321d337b750a6ec3f19188ce61b8d 100644 (file)
@@ -1,7 +1,43 @@
 use super::*;
 
 use hir::PrefixKind;
-use test_utils::assert_eq_text;
+use test_utils::{assert_eq_text, extract_range_or_offset, CURSOR_MARKER};
+
+#[test]
+fn respects_cfg_attr_fn() {
+    check(
+        r"bar::Bar",
+        r#"
+#[cfg(test)]
+fn foo() {$0}
+"#,
+        r#"
+#[cfg(test)]
+fn foo() {
+use bar::Bar;
+}
+"#,
+        ImportGranularity::Crate,
+    );
+}
+
+#[test]
+fn respects_cfg_attr_const() {
+    check(
+        r"bar::Bar",
+        r#"
+#[cfg(test)]
+const FOO: Bar = {$0};
+"#,
+        r#"
+#[cfg(test)]
+const FOO: Bar = {
+use bar::Bar;
+};
+"#,
+        ImportGranularity::Crate,
+    );
+}
 
 #[test]
 fn insert_skips_lone_glob_imports() {
@@ -15,15 +51,13 @@ fn insert_skips_lone_glob_imports() {
 use foo::baz::A;
 ",
         ImportGranularity::Crate,
-        false,
-        false,
     );
 }
 
 #[test]
 fn insert_not_group() {
     cov_mark::check!(insert_no_grouping_last);
-    check(
+    check_with_config(
         "use external_crate2::bar::A",
         r"
 use std::bar::B;
@@ -38,24 +72,32 @@ fn insert_not_group() {
 use self::bar::A;
 use super::bar::A;
 use external_crate2::bar::A;",
-        ImportGranularity::Item,
-        false,
-        false,
+        &InsertUseConfig {
+            granularity: ImportGranularity::Item,
+            enforce_granularity: true,
+            prefix_kind: PrefixKind::Plain,
+            group: false,
+            skip_glob_imports: true,
+        },
     );
 }
 
 #[test]
 fn insert_not_group_empty() {
     cov_mark::check!(insert_no_grouping_last2);
-    check(
+    check_with_config(
         "use external_crate2::bar::A",
         r"",
         r"use external_crate2::bar::A;
 
 ",
-        ImportGranularity::Item,
-        false,
-        false,
+        &InsertUseConfig {
+            granularity: ImportGranularity::Item,
+            enforce_granularity: true,
+            prefix_kind: PrefixKind::Plain,
+            group: false,
+            skip_glob_imports: true,
+        },
     );
 }
 
@@ -294,13 +336,15 @@ fn insert_empty_module() {
     cov_mark::check!(insert_group_empty_module);
     check(
         "foo::bar",
-        "mod x {}",
-        r"{
+        r"
+mod x {$0}
+",
+        r"
+mod x {
     use foo::bar;
-}",
+}
+",
         ImportGranularity::Item,
-        true,
-        true,
     )
 }
 
@@ -555,7 +599,6 @@ fn merge_mod_into_glob() {
         "token::TokenKind",
         r"use token::TokenKind::*;",
         r"use token::TokenKind::{*, self};",
-        false,
         &InsertUseConfig {
             granularity: ImportGranularity::Crate,
             enforce_granularity: true,
@@ -573,7 +616,6 @@ fn merge_self_glob() {
         "self",
         r"use self::*;",
         r"use self::{*, self};",
-        false,
         &InsertUseConfig {
             granularity: ImportGranularity::Crate,
             enforce_granularity: true,
@@ -798,14 +840,20 @@ fn check_with_config(
     path: &str,
     ra_fixture_before: &str,
     ra_fixture_after: &str,
-    module: bool,
     config: &InsertUseConfig,
 ) {
-    let mut syntax = ast::SourceFile::parse(ra_fixture_before).tree().syntax().clone();
-    if module {
-        syntax = syntax.descendants().find_map(ast::Module::cast).unwrap().syntax().clone();
-    }
-    let file = super::ImportScope::from(syntax.clone_for_update()).unwrap();
+    let (text, pos) = if ra_fixture_before.contains(CURSOR_MARKER) {
+        let (range_or_offset, text) = extract_range_or_offset(ra_fixture_before);
+        (text, Some(range_or_offset))
+    } else {
+        (ra_fixture_before.to_owned(), None)
+    };
+    let syntax = ast::SourceFile::parse(&text).tree().syntax().clone_for_update();
+    let file = pos
+        .and_then(|pos| syntax.token_at_offset(pos.expect_offset()).next()?.parent())
+        .and_then(|it| super::ImportScope::find_insert_use_container(&it))
+        .or_else(|| super::ImportScope::from(syntax))
+        .unwrap();
     let path = ast::SourceFile::parse(&format!("use {};", path))
         .tree()
         .syntax()
@@ -814,7 +862,7 @@ fn check_with_config(
         .unwrap();
 
     insert_use(&file, path, config);
-    let result = file.as_syntax_node().to_string();
+    let result = file.as_syntax_node().ancestors().last().unwrap().to_string();
     assert_eq_text!(ra_fixture_after, &result);
 }
 
@@ -823,34 +871,31 @@ fn check(
     ra_fixture_before: &str,
     ra_fixture_after: &str,
     granularity: ImportGranularity,
-    module: bool,
-    group: bool,
 ) {
     check_with_config(
         path,
         ra_fixture_before,
         ra_fixture_after,
-        module,
         &InsertUseConfig {
             granularity,
             enforce_granularity: true,
             prefix_kind: PrefixKind::Plain,
-            group,
+            group: true,
             skip_glob_imports: true,
         },
     )
 }
 
 fn check_crate(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
-    check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Crate, false, true)
+    check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Crate)
 }
 
 fn check_module(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
-    check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Module, false, true)
+    check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Module)
 }
 
 fn check_none(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
-    check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Item, false, true)
+    check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Item)
 }
 
 fn check_merge_only_fail(ra_fixture0: &str, ra_fixture1: &str, mb: MergeBehavior) {