]> git.lizzy.rs Git - rust.git/commitdiff
Macro tests
authorAleksey Kladov <aleksey.kladov@gmail.com>
Sat, 4 Jul 2020 13:04:57 +0000 (15:04 +0200)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Sat, 4 Jul 2020 13:04:57 +0000 (15:04 +0200)
crates/ra_ide/src/completion/complete_macro_in_item_position.rs
crates/ra_ide/src/completion/presentation.rs

index 4c33f41d4f9913a47482146df68baf93056bc6b6..d6613ed7bd8bcfe91a9801c0dc779bae5082c6ed 100644 (file)
@@ -15,130 +15,27 @@ pub(super) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &Compl
 
 #[cfg(test)]
 mod tests {
-    use insta::assert_debug_snapshot;
+    use expect::{expect, Expect};
 
-    use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
+    use crate::completion::{test_utils::completion_list, CompletionKind};
 
-    fn do_reference_completion(code: &str) -> Vec<CompletionItem> {
-        do_completion(code, CompletionKind::Reference)
+    fn check(ra_fixture: &str, expect: Expect) {
+        let actual = completion_list(ra_fixture, CompletionKind::Reference);
+        expect.assert_eq(&actual)
     }
 
     #[test]
     fn completes_macros_as_item() {
-        assert_debug_snapshot!(
-            do_reference_completion(
-                "
-                //- /main.rs
-                macro_rules! foo {
-                    () => {}
-                }
-
-                fn foo() {}
-
-                <|>
-                "
-            ),
-            @r###"
-        [
-            CompletionItem {
-                label: "foo!(…)",
-                source_range: 48..48,
-                delete: 48..48,
-                insert: "foo!($0)",
-                kind: Macro,
-                detail: "macro_rules! foo",
-            },
-        ]
-        "###
-        );
-    }
-
-    #[test]
-    fn completes_vec_macros_with_square_brackets() {
-        assert_debug_snapshot!(
-            do_reference_completion(
-                "
-                //- /main.rs
-                /// Creates a [`Vec`] containing the arguments.
-                ///
-                /// - Create a [`Vec`] containing a given list of elements:
-                ///
-                /// ```
-                /// let v = vec![1, 2, 3];
-                /// assert_eq!(v[0], 1);
-                /// assert_eq!(v[1], 2);
-                /// assert_eq!(v[2], 3);
-                /// ```
-                macro_rules! vec {
-                    () => {}
-                }
-
-                fn foo() {}
-
-                <|>
-                "
-            ),
-            @r###"
-        [
-            CompletionItem {
-                label: "vec![…]",
-                source_range: 282..282,
-                delete: 282..282,
-                insert: "vec![$0]",
-                kind: Macro,
-                detail: "macro_rules! vec",
-                documentation: Documentation(
-                    "Creates a [`Vec`] containing the arguments.\n\n- Create a [`Vec`] containing a given list of elements:\n\n```\nlet v = vec![1, 2, 3];\nassert_eq!(v[0], 1);\nassert_eq!(v[1], 2);\nassert_eq!(v[2], 3);\n```",
-                ),
-            },
-        ]
-        "###
-        );
-    }
-
-    #[test]
-    fn completes_macros_braces_guessing() {
-        assert_debug_snapshot!(
-            do_reference_completion(
-                "
-                //- /main.rs
-                /// Foo
-                ///
-                /// Not call `fooo!()` `fooo!()`, or `_foo![]` `_foo![]`.
-                /// Call as `let _=foo!  { hello world };`
-                macro_rules! foo {
-                    () => {}
-                }
-
-                fn main() {
-                    <|>
-                }
-                "
-            ),
-            @r###"
-        [
-            CompletionItem {
-                label: "foo! {…}",
-                source_range: 164..164,
-                delete: 164..164,
-                insert: "foo! {$0}",
-                kind: Macro,
-                detail: "macro_rules! foo",
-                documentation: Documentation(
-                    "Foo\n\nNot call `fooo!()` `fooo!()`, or `_foo![]` `_foo![]`.\nCall as `let _=foo!  { hello world };`",
-                ),
-            },
-            CompletionItem {
-                label: "main()",
-                source_range: 164..164,
-                delete: 164..164,
-                insert: "main()$0",
-                kind: Function,
-                lookup: "main",
-                detail: "fn main()",
-            },
-        ]
-        "###
-        );
+        check(
+            r#"
+macro_rules! foo { () => {} }
+fn foo() {}
+
+<|>
+"#,
+            expect![[r#"
+                ma foo!(…) macro_rules! foo
+            "#]],
+        )
     }
 }
index dc391c46bc1d24c27030100a8da3c70f87eebeb6..946bbef7c94a09c7f06767ba86b100c15bdc9cba 100644 (file)
@@ -174,6 +174,7 @@ pub(crate) fn add_macro(
                 builder
                     .insert_snippet(cap, format!("{}!{}$0{}", name, bra, ket))
                     .label(format!("{}!{}…{}", name, bra, ket))
+                    .lookup_by(format!("{}!", name))
             }
             None if needs_bang => builder.insert_text(format!("{}!", name)),
             _ => {
@@ -1079,4 +1080,57 @@ fn go(…) []
             "#]],
         );
     }
+
+    #[test]
+    fn guesses_macro_braces() {
+        check_edit(
+            "vec!",
+            r#"
+/// Creates a [`Vec`] containing the arguments.
+///
+/// ```
+/// let v = vec![1, 2, 3];
+/// assert_eq!(v[0], 1);
+/// assert_eq!(v[1], 2);
+/// assert_eq!(v[2], 3);
+/// ```
+macro_rules! vec { () => {} }
+
+fn fn main() { v<|> }
+"#,
+            r#"
+/// Creates a [`Vec`] containing the arguments.
+///
+/// ```
+/// let v = vec![1, 2, 3];
+/// assert_eq!(v[0], 1);
+/// assert_eq!(v[1], 2);
+/// assert_eq!(v[2], 3);
+/// ```
+macro_rules! vec { () => {} }
+
+fn fn main() { vec![$0] }
+"#,
+        );
+
+        check_edit(
+            "foo!",
+            r#"
+/// Foo
+///
+/// Don't call `fooo!()` `fooo!()`, or `_foo![]` `_foo![]`,
+/// call as `let _=foo!  { hello world };`
+macro_rules! foo { () => {} }
+fn main() { <|> }
+"#,
+            r#"
+/// Foo
+///
+/// Don't call `fooo!()` `fooo!()`, or `_foo![]` `_foo![]`,
+/// call as `let _=foo!  { hello world };`
+macro_rules! foo { () => {} }
+fn main() { foo! {$0} }
+"#,
+        )
+    }
 }