Repository: rust-analyzer/rust-analyzer
Pull request #5214 (2020-07-03 21:15:03)

Cleanup presentation tests


bors r+
🤖


Patch:
diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs
index bd274bd743..f472b95295 100644
--- a/crates/ra_ide/src/completion/presentation.rs
+++ b/crates/ra_ide/src/completion/presentation.rs
@@ -460,7 +460,7 @@ mod tests {
     use test_utils::mark;
 
     use crate::completion::{
-        test_utils::{do_completion, do_completion_with_options},
+        test_utils::{check_edit, do_completion, do_completion_with_options},
         CompletionConfig, CompletionItem, CompletionKind,
     };
 
@@ -636,150 +636,59 @@ fn foo() {
     #[test]
     fn inserts_parens_for_function_calls() {
         mark::check!(inserts_parens_for_function_calls);
-        assert_debug_snapshot!(
-            do_reference_completion(
-                r"
-                fn no_args() {}
-                fn main() { no_<|> }
-                "
-            ),
-            @r###"
-        [
-            CompletionItem {
-                label: "main()",
-                source_range: 28..31,
-                delete: 28..31,
-                insert: "main()$0",
-                kind: Function,
-                lookup: "main",
-                detail: "fn main()",
-            },
-            CompletionItem {
-                label: "no_args()",
-                source_range: 28..31,
-                delete: 28..31,
-                insert: "no_args()$0",
-                kind: Function,
-                lookup: "no_args",
-                detail: "fn no_args()",
-            },
-        ]
-        "###
-        );
-        assert_debug_snapshot!(
-            do_reference_completion(
-                r"
-                fn with_args(x: i32, y: String) {}
-                fn main() { with_<|> }
-                "
-            ),
-            @r###"
-        [
-            CompletionItem {
-                label: "main()",
-                source_range: 47..52,
-                delete: 47..52,
-                insert: "main()$0",
-                kind: Function,
-                lookup: "main",
-                detail: "fn main()",
-            },
-            CompletionItem {
-                label: "with_args(…)",
-                source_range: 47..52,
-                delete: 47..52,
-                insert: "with_args(${1:x}, ${2:y})$0",
-                kind: Function,
-                lookup: "with_args",
-                detail: "fn with_args(x: i32, y: String)",
-                trigger_call_info: true,
-            },
-        ]
-        "###
+        check_edit(
+            "no_args",
+            r#"
+fn no_args() {}
+fn main() { no_<|> }
+"#,
+            r#"
+fn no_args() {}
+fn main() { no_args()$0 }
+"#,
         );
-        assert_debug_snapshot!(
-            do_reference_completion(
-                r"
-                fn with_ignored_args(_foo: i32, ___bar: bool, ho_ge_: String) {}
-                fn main() { with_<|> }
-                "
-            ),
-            @r###"
-        [
-            CompletionItem {
-                label: "main()",
-                source_range: 77..82,
-                delete: 77..82,
-                insert: "main()$0",
-                kind: Function,
-                lookup: "main",
-                detail: "fn main()",
-            },
-            CompletionItem {
-                label: "with_ignored_args(…)",
-                source_range: 77..82,
-                delete: 77..82,
-                insert: "with_ignored_args(${1:foo}, ${2:bar}, ${3:ho_ge_})$0",
-                kind: Function,
-                lookup: "with_ignored_args",
-                detail: "fn with_ignored_args(_foo: i32, ___bar: bool, ho_ge_: String)",
-                trigger_call_info: true,
-            },
-        ]
-        "###
+        check_edit(
+            "with_args",
+            r#"
+fn with_args(x: i32, y: String) {}
+fn main() { with_<|> }
+"#,
+            r#"
+fn with_args(x: i32, y: String) {}
+fn main() { with_args(${1:x}, ${2:y})$0 }
+"#,
         );
-        assert_debug_snapshot!(
-            do_reference_completion(
-                r"
-                struct S {}
-                impl S {
-                    fn foo(&self) {}
-                }
-                fn bar(s: &S) {
-                    s.f<|>
-                }
-                "
-            ),
-            @r###"
-        [
-            CompletionItem {
-                label: "foo()",
-                source_range: 66..67,
-                delete: 66..67,
-                insert: "foo()$0",
-                kind: Method,
-                lookup: "foo",
-                detail: "fn foo(&self)",
-            },
-        ]
-        "###
+        check_edit(
+            "foo",
+            r#"
+struct S;
+impl S {
+    fn foo(&self) {}
+}
+fn bar(s: &S) { s.f<|> }
+"#,
+            r#"
+struct S;
+impl S {
+    fn foo(&self) {}
+}
+fn bar(s: &S) { s.foo()$0 }
+"#,
         );
-        assert_debug_snapshot!(
-            do_reference_completion(
-                r"
-                struct S {}
-                impl S {
-                    fn foo_ignored_args(&self, _a: bool, b: i32) {}
-                }
-                fn bar(s: &S) {
-                    s.f<|>
-                }
-                "
-            ),
-            @r###"
-        [
-            CompletionItem {
-                label: "foo_ignored_args(…)",
-                source_range: 97..98,
-                delete: 97..98,
-                insert: "foo_ignored_args(${1:a}, ${2:b})$0",
-                kind: Method,
-                lookup: "foo_ignored_args",
-                detail: "fn foo_ignored_args(&self, _a: bool, b: i32)",
-                trigger_call_info: true,
-            },
-        ]
-        "###
+    }
+
+    #[test]
+    fn strips_underscores_from_args() {
+        check_edit(
+            "foo",
+            r#"
+fn foo(_foo: i32, ___bar: bool, ho_ge_: String) {}
+fn main() { f<|> }
+"#,
+            r#"
+fn foo(_foo: i32, ___bar: bool, ho_ge_: String) {}
+fn main() { foo(${1:foo}, ${2:bar}, ${3:ho_ge_})$0 }
+"#,
         );
     }
 


Test patch:
diff --git a/crates/ra_ide/src/completion/test_utils.rs b/crates/ra_ide/src/completion/test_utils.rs
index 9c036eac7f..f251902411 100644
--- a/crates/ra_ide/src/completion/test_utils.rs
+++ b/crates/ra_ide/src/completion/test_utils.rs
@@ -3,7 +3,7 @@
 use hir::Semantics;
 use itertools::Itertools;
 use ra_syntax::{AstNode, NodeOrToken, SyntaxElement};
-use stdx::format_to;
+use stdx::{format_to, trim_indent};
 use test_utils::assert_eq_text;
 
 use crate::{
@@ -57,14 +57,18 @@ pub(crate) fn completion_list_with_options(
 }
 
 pub(crate) fn check_edit(what: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
+    let ra_fixture_after = trim_indent(ra_fixture_after);
     let (analysis, position) = analysis_and_position(ra_fixture_before);
     let completions: Vec<CompletionItem> =
         analysis.completions(&CompletionConfig::default(), position).unwrap().unwrap().into();
-    let (completion,) =
-        completions.into_iter().filter(|it| it.label() == what).collect_tuple().unwrap();
+    let (completion,) = completions
+        .iter()
+        .filter(|it| it.lookup() == what)
+        .collect_tuple()
+        .unwrap_or_else(|| panic!("can't find {:?} completion in {:#?}", what, completions));
     let mut actual = analysis.file_text(position.file_id).unwrap().to_string();
     completion.text_edit().apply(&mut actual);
-    assert_eq_text!(ra_fixture_after, &actual)
+    assert_eq_text!(&ra_fixture_after, &actual)
 }
 
 pub(crate) fn check_pattern_is_applicable(code: &str, check: fn(SyntaxElement) -> bool) {