]> git.lizzy.rs Git - rust.git/blobdiff - crates/ide-completion/src/tests/pattern.rs
fix: format literal lookup
[rust.git] / crates / ide-completion / src / tests / pattern.rs
index 3b592635288811eb639a6c3cae63b2c233c95baa..85c4dbd6625dfcfc891a073c885eab6eec98780b 100644 (file)
@@ -1,7 +1,7 @@
 //! Completion tests for pattern position.
 use expect_test::{expect, Expect};
 
-use crate::tests::{completion_list, BASE_ITEMS_FIXTURE};
+use crate::tests::{check_edit, completion_list, BASE_ITEMS_FIXTURE};
 
 fn check_empty(ra_fixture: &str, expect: Expect) {
     let actual = completion_list(ra_fixture);
@@ -13,6 +13,18 @@ fn check(ra_fixture: &str, expect: Expect) {
     expect.assert_eq(&actual)
 }
 
+#[test]
+fn wildcard() {
+    check(
+        r#"
+fn quux() {
+    let _$0
+}
+"#,
+        expect![""],
+    );
+}
+
 #[test]
 fn ident_rebind_pat() {
     check_empty(
@@ -22,8 +34,8 @@ fn quux() {
 }
 "#,
         expect![[r#"
-            kw ref
             kw mut
+            kw ref
         "#]],
     );
 }
@@ -113,19 +125,19 @@ fn foo() {
 }
 "#,
         expect![[r#"
-            kw ref
-            kw mut
+            ct CONST
             en Enum
-            bn Record    Record { field$1 }$0
+            ma makro!(…)  macro_rules! makro
+            md module
             st Record
-            bn Tuple     Tuple($1)$0
             st Tuple
-            md module
             st Unit
-            ma makro!(…) macro_rules! makro
-            bn TupleV    TupleV($1)$0
             ev TupleV
-            ct CONST
+            bn Record {…} Record { field$1 }$0
+            bn Tuple(…)   Tuple($1)$0
+            bn TupleV(…)  TupleV($1)$0
+            kw mut
+            kw ref
         "#]],
     );
 }
@@ -143,17 +155,18 @@ fn foo() {
 }
 "#,
         expect![[r#"
-            kw ref
-            kw mut
-            bn Record            Record { field$1 }$0
+            en SingleVariantEnum
+            ma makro!(…)         macro_rules! makro
+            md module
             st Record
-            bn Tuple             Tuple($1)$0
             st Tuple
-            ev Variant
-            md module
-            en SingleVariantEnum
             st Unit
-            ma makro!(…)         macro_rules! makro
+            ev Variant
+            bn Record {…}        Record { field$1 }$0
+            bn Tuple(…)          Tuple($1)$0
+            bn Variant           Variant$0
+            kw mut
+            kw ref
         "#]],
     );
 }
@@ -166,15 +179,15 @@ fn foo(a$0) {
 }
 "#,
         expect![[r#"
-            kw ref
-            kw mut
-            bn Record    Record { field$1 }: Record$0
+            ma makro!(…)  macro_rules! makro
+            md module
             st Record
-            bn Tuple     Tuple($1): Tuple$0
             st Tuple
-            md module
             st Unit
-            ma makro!(…) macro_rules! makro
+            bn Record {…} Record { field$1 }: Record$0
+            bn Tuple(…)   Tuple($1): Tuple$0
+            kw mut
+            kw ref
         "#]],
     );
     check(
@@ -183,15 +196,15 @@ fn foo(a$0: Tuple) {
 }
 "#,
         expect![[r#"
-            kw ref
-            kw mut
-            bn Record    Record { field$1 }$0
+            ma makro!(…)  macro_rules! makro
+            md module
             st Record
-            bn Tuple     Tuple($1)$0
             st Tuple
-            md module
             st Unit
-            ma makro!(…) macro_rules! makro
+            bn Record {…} Record { field$1 }$0
+            bn Tuple(…)   Tuple($1)$0
+            kw mut
+            kw ref
         "#]],
     );
 }
@@ -210,9 +223,9 @@ fn foo() {
 }
 "#,
         expect![[r#"
-            kw ref
-            kw mut
             ma m!(…) macro_rules! m
+            kw mut
+            kw ref
         "#]],
     );
 }
@@ -229,10 +242,11 @@ fn foo() {
 }
 "#,
         expect![[r#"
-            kw ref
-            kw mut
             en E
             ma m!(…) macro_rules! m
+            bn E::X  E::X$0
+            kw mut
+            kw ref
         "#]],
     );
 }
@@ -253,14 +267,14 @@ fn outer() {
 }
 "#,
         expect![[r#"
-            kw ref
-            kw mut
-            bn Record    Record { field$1, .. }$0
+            md foo
+            st Invisible
             st Record
-            bn Tuple     Tuple($1, ..)$0
             st Tuple
-            st Invisible
-            md foo
+            bn Record {…} Record { field$1, .. }$0
+            bn Tuple(…)   Tuple($1, ..)$0
+            kw mut
+            kw ref
         "#]],
     )
 }
@@ -279,12 +293,12 @@ fn foo() {
 }
     "#,
         expect![[r#"
-            kw ref
-            kw mut
-            bn Self Self($1)$0
             sp Self
-            bn Foo  Foo($1)$0
             st Foo
+            bn Foo(…)  Foo($1)$0
+            bn Self(…) Self($1)$0
+            kw mut
+            kw ref
         "#]],
     )
 }
@@ -303,10 +317,10 @@ fn func() {
 }
 "#,
         expect![[r#"
-            ev TupleV(…)   TupleV(u32)
-            ev RecordV {…} RecordV { field: u32 }
-            ev UnitV       UnitV
             ct ASSOC_CONST const ASSOC_CONST: ()
+            bn RecordV {…} RecordV { field$1 }$0
+            bn TupleV(…)   TupleV($1)$0
+            bn UnitV       UnitV$0
         "#]],
     );
 }
@@ -320,12 +334,12 @@ struct Foo { bar: Bar }
 fn outer(Foo { bar: $0 }: Foo) {}
 "#,
         expect![[r#"
-            kw ref
-            kw mut
-            bn Foo Foo { bar$1 }$0
-            st Foo
-            bn Bar Bar($1)$0
             st Bar
+            st Foo
+            bn Bar(…)  Bar($1)$0
+            bn Foo {…} Foo { bar$1 }$0
+            kw mut
+            kw ref
         "#]],
     )
 }
@@ -338,7 +352,10 @@ struct Foo { bar: Bar }
 struct Bar(u32);
 fn outer(Foo { bar$0 }: Foo) {}
 "#,
-        expect![[r#""#]],
+        expect![[r#"
+            kw mut
+            kw ref
+        "#]],
     )
 }
 
@@ -351,12 +368,12 @@ struct Foo { bar: Bar }
 fn foo($0) {}
 "#,
         expect![[r#"
-            kw ref
-            kw mut
-            bn Foo Foo { bar$1 }: Foo$0
-            st Foo
-            bn Bar Bar($1): Bar$0
             st Bar
+            st Foo
+            bn Bar(…)  Bar($1): Bar$0
+            bn Foo {…} Foo { bar$1 }: Foo$0
+            kw mut
+            kw ref
         "#]],
     )
 }
@@ -372,12 +389,12 @@ fn foo() {
 }
 "#,
         expect![[r#"
-            kw ref
-            kw mut
-            bn Foo Foo { bar$1 }$0
-            st Foo
-            bn Bar Bar($1)$0
             st Bar
+            st Foo
+            bn Bar(…)  Bar($1)$0
+            bn Foo {…} Foo { bar$1 }$0
+            kw mut
+            kw ref
         "#]],
     )
 }
@@ -394,28 +411,24 @@ fn foo() {
 }
 "#,
         expect![[r#"
-            kw self::
-            kw super::
+            st Bar
             kw crate::
+            kw self::
         "#]],
     );
     check_empty(
         r#"
 struct Foo { bar: u32 }
 fn foo() {
-    match Foo { bar: 0 } {
+    match (Foo { bar: 0 }) {
         F$0 { bar } => {}
     }
 }
 "#,
         expect![[r#"
-            kw return
-            kw self
-            kw super
-            kw crate
             st Foo
-            fn foo()  fn()
-            bt u32
+            kw crate::
+            kw self::
         "#]],
     );
     check_empty(
@@ -430,7 +443,7 @@ fn foo() {
 }
 "#,
         expect![[r#"
-            ev TupleVariant(…) TupleVariant
+            bn TupleVariant TupleVariant
         "#]],
     );
     check_empty(
@@ -445,7 +458,86 @@ fn foo() {
 }
 "#,
         expect![[r#"
-            ev RecordVariant {…} RecordVariant
+            bn RecordVariant RecordVariant
+        "#]],
+    );
+}
+
+#[test]
+fn completes_enum_variant_pat() {
+    cov_mark::check!(enum_variant_pattern_path);
+    check_edit(
+        "RecordVariant{}",
+        r#"
+enum Enum {
+    RecordVariant { field: u32 }
+}
+fn foo() {
+    match (Enum::RecordVariant { field: 0 }) {
+        Enum::RecordV$0
+    }
+}
+"#,
+        r#"
+enum Enum {
+    RecordVariant { field: u32 }
+}
+fn foo() {
+    match (Enum::RecordVariant { field: 0 }) {
+        Enum::RecordVariant { field$1 }$0
+    }
+}
+"#,
+    );
+}
+
+#[test]
+fn completes_enum_variant_pat_escape() {
+    cov_mark::check!(enum_variant_pattern_path);
+    check_empty(
+        r#"
+enum Enum {
+    A,
+    B { r#type: i32 },
+    r#type,
+    r#struct { r#type: i32 },
+}
+fn foo() {
+    match (Enum::A) {
+        $0
+    }
+}
+"#,
+        expect![[r#"
+            en Enum
+            bn Enum::A          Enum::A$0
+            bn Enum::B {…}      Enum::B { r#type$1 }$0
+            bn Enum::struct {…} Enum::r#struct { r#type$1 }$0
+            bn Enum::type       Enum::r#type$0
+            kw mut
+            kw ref
+        "#]],
+    );
+
+    check_empty(
+        r#"
+enum Enum {
+    A,
+    B { r#type: i32 },
+    r#type,
+    r#struct { r#type: i32 },
+}
+fn foo() {
+    match (Enum::A) {
+        Enum::$0
+    }
+}
+"#,
+        expect![[r#"
+            bn A          A$0
+            bn B {…}      B { r#type$1 }$0
+            bn struct {…} r#struct { r#type$1 }$0
+            bn type       r#type$0
         "#]],
     );
 }
@@ -538,3 +630,87 @@ fn f(v: u32) {
         "#]],
     );
 }
+
+#[test]
+fn in_method_param() {
+    check_empty(
+        r#"
+struct Ty(u8);
+
+impl Ty {
+    fn foo($0)
+}
+"#,
+        expect![[r#"
+            sp Self
+            st Ty
+            bn &mut self
+            bn &self
+            bn Self(…)   Self($1): Self$0
+            bn Ty(…)     Ty($1): Ty$0
+            bn mut self
+            bn self
+            kw mut
+            kw ref
+        "#]],
+    );
+    check_empty(
+        r#"
+struct Ty(u8);
+
+impl Ty {
+    fn foo(s$0)
+}
+"#,
+        expect![[r#"
+            sp Self
+            st Ty
+            bn &mut self
+            bn &self
+            bn Self(…)   Self($1): Self$0
+            bn Ty(…)     Ty($1): Ty$0
+            bn mut self
+            bn self
+            kw mut
+            kw ref
+        "#]],
+    );
+    check_empty(
+        r#"
+struct Ty(u8);
+
+impl Ty {
+    fn foo(s$0, foo: u8)
+}
+"#,
+        expect![[r#"
+            sp Self
+            st Ty
+            bn &mut self
+            bn &self
+            bn Self(…)   Self($1): Self$0
+            bn Ty(…)     Ty($1): Ty$0
+            bn mut self
+            bn self
+            kw mut
+            kw ref
+        "#]],
+    );
+    check_empty(
+        r#"
+struct Ty(u8);
+
+impl Ty {
+    fn foo(foo: u8, b$0)
+}
+"#,
+        expect![[r#"
+            sp Self
+            st Ty
+            bn Self(…) Self($1): Self$0
+            bn Ty(…)   Ty($1): Ty$0
+            kw mut
+            kw ref
+        "#]],
+    );
+}