]> git.lizzy.rs Git - rust.git/blob - crates/ide-completion/src/tests/pattern.rs
fix: escape for enum variant
[rust.git] / crates / ide-completion / src / tests / pattern.rs
1 //! Completion tests for pattern position.
2 use expect_test::{expect, Expect};
3
4 use crate::tests::{check_edit, completion_list, BASE_ITEMS_FIXTURE};
5
6 fn check_empty(ra_fixture: &str, expect: Expect) {
7     let actual = completion_list(ra_fixture);
8     expect.assert_eq(&actual)
9 }
10
11 fn check(ra_fixture: &str, expect: Expect) {
12     let actual = completion_list(&format!("{}\n{}", BASE_ITEMS_FIXTURE, ra_fixture));
13     expect.assert_eq(&actual)
14 }
15
16 #[test]
17 fn wildcard() {
18     check(
19         r#"
20 fn quux() {
21     let _$0
22 }
23 "#,
24         expect![""],
25     );
26 }
27
28 #[test]
29 fn ident_rebind_pat() {
30     check_empty(
31         r#"
32 fn quux() {
33     let en$0 @ x
34 }
35 "#,
36         expect![[r#"
37             kw mut
38             kw ref
39         "#]],
40     );
41 }
42
43 #[test]
44 fn ident_ref_pat() {
45     check_empty(
46         r#"
47 fn quux() {
48     let ref en$0
49 }
50 "#,
51         expect![[r#"
52             kw mut
53         "#]],
54     );
55     check_empty(
56         r#"
57 fn quux() {
58     let ref en$0 @ x
59 }
60 "#,
61         expect![[r#"
62             kw mut
63         "#]],
64     );
65 }
66
67 #[test]
68 fn ident_ref_mut_pat() {
69     check_empty(
70         r#"
71 fn quux() {
72     let ref mut en$0
73 }
74 "#,
75         expect![[r#""#]],
76     );
77     check_empty(
78         r#"
79 fn quux() {
80     let ref mut en$0 @ x
81 }
82 "#,
83         expect![[r#""#]],
84     );
85 }
86
87 #[test]
88 fn ref_pat() {
89     check_empty(
90         r#"
91 fn quux() {
92     let &en$0
93 }
94 "#,
95         expect![[r#"
96             kw mut
97         "#]],
98     );
99     check_empty(
100         r#"
101 fn quux() {
102     let &mut en$0
103 }
104 "#,
105         expect![[r#""#]],
106     );
107     check_empty(
108         r#"
109 fn foo() {
110     for &$0 in () {}
111 }
112 "#,
113         expect![[r#"
114             kw mut
115         "#]],
116     );
117 }
118
119 #[test]
120 fn refutable() {
121     check(
122         r#"
123 fn foo() {
124     if let a$0
125 }
126 "#,
127         expect![[r#"
128             ct CONST
129             en Enum
130             ma makro!(…)  macro_rules! makro
131             md module
132             st Record
133             st Tuple
134             st Unit
135             ev TupleV
136             bn Record {…} Record { field$1 }$0
137             bn Tuple(…)   Tuple($1)$0
138             bn TupleV(…)  TupleV($1)$0
139             kw mut
140             kw ref
141         "#]],
142     );
143 }
144
145 #[test]
146 fn irrefutable() {
147     check(
148         r#"
149 enum SingleVariantEnum {
150     Variant
151 }
152 use SingleVariantEnum::Variant;
153 fn foo() {
154    let a$0
155 }
156 "#,
157         expect![[r#"
158             en SingleVariantEnum
159             ma makro!(…)         macro_rules! makro
160             md module
161             st Record
162             st Tuple
163             st Unit
164             ev Variant
165             bn Record {…}        Record { field$1 }$0
166             bn Tuple(…)          Tuple($1)$0
167             bn Variant           Variant$0
168             kw mut
169             kw ref
170         "#]],
171     );
172 }
173
174 #[test]
175 fn in_param() {
176     check(
177         r#"
178 fn foo(a$0) {
179 }
180 "#,
181         expect![[r#"
182             ma makro!(…)  macro_rules! makro
183             md module
184             st Record
185             st Tuple
186             st Unit
187             bn Record {…} Record { field$1 }: Record$0
188             bn Tuple(…)   Tuple($1): Tuple$0
189             kw mut
190             kw ref
191         "#]],
192     );
193     check(
194         r#"
195 fn foo(a$0: Tuple) {
196 }
197 "#,
198         expect![[r#"
199             ma makro!(…)  macro_rules! makro
200             md module
201             st Record
202             st Tuple
203             st Unit
204             bn Record {…} Record { field$1 }$0
205             bn Tuple(…)   Tuple($1)$0
206             kw mut
207             kw ref
208         "#]],
209     );
210 }
211
212 #[test]
213 fn only_fn_like_macros() {
214     check_empty(
215         r#"
216 macro_rules! m { ($e:expr) => { $e } }
217
218 #[rustc_builtin_macro]
219 macro Clone {}
220
221 fn foo() {
222     let x$0
223 }
224 "#,
225         expect![[r#"
226             ma m!(…) macro_rules! m
227             kw mut
228             kw ref
229         "#]],
230     );
231 }
232
233 #[test]
234 fn in_simple_macro_call() {
235     check_empty(
236         r#"
237 macro_rules! m { ($e:expr) => { $e } }
238 enum E { X }
239
240 fn foo() {
241    m!(match E::X { a$0 })
242 }
243 "#,
244         expect![[r#"
245             en E
246             ma m!(…) macro_rules! m
247             bn E::X  E::X$0
248             kw mut
249             kw ref
250         "#]],
251     );
252 }
253
254 #[test]
255 fn omits_private_fields_pat() {
256     check_empty(
257         r#"
258 mod foo {
259     pub struct Record { pub field: i32, _field: i32 }
260     pub struct Tuple(pub u32, u32);
261     pub struct Invisible(u32, u32);
262 }
263 use foo::*;
264
265 fn outer() {
266     if let a$0
267 }
268 "#,
269         expect![[r#"
270             md foo
271             st Invisible
272             st Record
273             st Tuple
274             bn Record {…} Record { field$1, .. }$0
275             bn Tuple(…)   Tuple($1, ..)$0
276             kw mut
277             kw ref
278         "#]],
279     )
280 }
281
282 #[test]
283 fn completes_self_pats() {
284     check_empty(
285         r#"
286 struct Foo(i32);
287 impl Foo {
288     fn foo() {
289         match Foo(0) {
290             a$0
291         }
292     }
293 }
294     "#,
295         expect![[r#"
296             sp Self
297             st Foo
298             bn Foo(…)  Foo($1)$0
299             bn Self(…) Self($1)$0
300             kw mut
301             kw ref
302         "#]],
303     )
304 }
305
306 #[test]
307 fn enum_qualified() {
308     check(
309         r#"
310 impl Enum {
311     type AssocType = ();
312     const ASSOC_CONST: () = ();
313     fn assoc_fn() {}
314 }
315 fn func() {
316     if let Enum::$0 = unknown {}
317 }
318 "#,
319         expect![[r#"
320             ct ASSOC_CONST const ASSOC_CONST: ()
321             bn RecordV {…} RecordV { field$1 }$0
322             bn TupleV(…)   TupleV($1)$0
323             bn UnitV       UnitV$0
324         "#]],
325     );
326 }
327
328 #[test]
329 fn completes_in_record_field_pat() {
330     check_empty(
331         r#"
332 struct Foo { bar: Bar }
333 struct Bar(u32);
334 fn outer(Foo { bar: $0 }: Foo) {}
335 "#,
336         expect![[r#"
337             st Bar
338             st Foo
339             bn Bar(…)  Bar($1)$0
340             bn Foo {…} Foo { bar$1 }$0
341             kw mut
342             kw ref
343         "#]],
344     )
345 }
346
347 #[test]
348 fn skips_in_record_field_pat_name() {
349     check_empty(
350         r#"
351 struct Foo { bar: Bar }
352 struct Bar(u32);
353 fn outer(Foo { bar$0 }: Foo) {}
354 "#,
355         expect![[r#"
356             kw mut
357             kw ref
358         "#]],
359     )
360 }
361
362 #[test]
363 fn completes_in_fn_param() {
364     check_empty(
365         r#"
366 struct Foo { bar: Bar }
367 struct Bar(u32);
368 fn foo($0) {}
369 "#,
370         expect![[r#"
371             st Bar
372             st Foo
373             bn Bar(…)  Bar($1): Bar$0
374             bn Foo {…} Foo { bar$1 }: Foo$0
375             kw mut
376             kw ref
377         "#]],
378     )
379 }
380
381 #[test]
382 fn completes_in_closure_param() {
383     check_empty(
384         r#"
385 struct Foo { bar: Bar }
386 struct Bar(u32);
387 fn foo() {
388     |$0| {};
389 }
390 "#,
391         expect![[r#"
392             st Bar
393             st Foo
394             bn Bar(…)  Bar($1)$0
395             bn Foo {…} Foo { bar$1 }$0
396             kw mut
397             kw ref
398         "#]],
399     )
400 }
401
402 #[test]
403 fn completes_no_delims_if_existing() {
404     check_empty(
405         r#"
406 struct Bar(u32);
407 fn foo() {
408     match Bar(0) {
409         B$0(b) => {}
410     }
411 }
412 "#,
413         expect![[r#"
414             st Bar
415             kw crate::
416             kw self::
417             kw super::
418         "#]],
419     );
420     check_empty(
421         r#"
422 struct Foo { bar: u32 }
423 fn foo() {
424     match (Foo { bar: 0 }) {
425         F$0 { bar } => {}
426     }
427 }
428 "#,
429         expect![[r#"
430             st Foo
431             kw crate::
432             kw self::
433             kw super::
434         "#]],
435     );
436     check_empty(
437         r#"
438 enum Enum {
439     TupleVariant(u32)
440 }
441 fn foo() {
442     match Enum::TupleVariant(0) {
443         Enum::T$0(b) => {}
444     }
445 }
446 "#,
447         expect![[r#"
448             bn TupleVariant(…) TupleVariant($1)$0
449         "#]],
450     );
451     check_empty(
452         r#"
453 enum Enum {
454     RecordVariant { field: u32 }
455 }
456 fn foo() {
457     match (Enum::RecordVariant { field: 0 }) {
458         Enum::RecordV$0 { field } => {}
459     }
460 }
461 "#,
462         expect![[r#"
463             bn RecordVariant {…} RecordVariant { field$1 }$0
464         "#]],
465     );
466 }
467
468 #[test]
469 fn completes_enum_variant_pat() {
470     cov_mark::check!(enum_variant_pattern_path);
471     check_edit(
472         "RecordVariant {…}",
473         r#"
474 enum Enum {
475     RecordVariant { field: u32 }
476 }
477 fn foo() {
478     match (Enum::RecordVariant { field: 0 }) {
479         Enum::RecordV$0
480     }
481 }
482 "#,
483         r#"
484 enum Enum {
485     RecordVariant { field: u32 }
486 }
487 fn foo() {
488     match (Enum::RecordVariant { field: 0 }) {
489         Enum::RecordVariant { field$1 }$0
490     }
491 }
492 "#,
493     );
494 }
495
496 #[test]
497 fn completes_enum_variant_pat_escape() {
498     cov_mark::check!(enum_variant_pattern_path);
499     check_empty(
500         r#"
501 enum Enum {
502     A,
503     B { r#type: i32 },
504     r#type,
505     r#struct { r#type: i32 },
506 }
507 fn foo() {
508     match (Enum::A) {
509         $0
510     }
511 }
512 "#,
513         expect![[r#"
514             en Enum
515             bn Enum::A          Enum::A$0
516             bn Enum::B {…}      Enum::B { r#type$1 }$0
517             bn Enum::struct {…} Enum::r#struct { r#type$1 }$0
518             bn Enum::type       Enum::r#type$0
519             kw mut
520             kw ref
521         "#]],
522     );
523
524     check_empty(
525         r#"
526 enum Enum {
527     A,
528     B { r#type: i32 },
529     r#type,
530     r#struct { r#type: i32 },
531 }
532 fn foo() {
533     match (Enum::A) {
534         Enum::$0
535     }
536 }
537 "#,
538         expect![[r#"
539             bn A          A$0
540             bn B {…}      B { r#type$1 }$0
541             bn struct {…} r#struct { r#type$1 }$0
542             bn type       r#type$0
543         "#]],
544     );
545 }
546
547 #[test]
548 fn completes_associated_const() {
549     check_empty(
550         r#"
551 #[derive(PartialEq, Eq)]
552 struct Ty(u8);
553
554 impl Ty {
555     const ABC: Self = Self(0);
556 }
557
558 fn f(t: Ty) {
559     match t {
560         Ty::$0 => {}
561         _ => {}
562     }
563 }
564 "#,
565         expect![[r#"
566             ct ABC const ABC: Self
567         "#]],
568     );
569
570     check_empty(
571         r#"
572 enum MyEnum {}
573
574 impl MyEnum {
575     pub const A: i32 = 123;
576     pub const B: i32 = 456;
577 }
578
579 fn f(e: MyEnum) {
580     match e {
581         MyEnum::$0 => {}
582         _ => {}
583     }
584 }
585 "#,
586         expect![[r#"
587             ct A pub const A: i32
588             ct B pub const B: i32
589         "#]],
590     );
591
592     check_empty(
593         r#"
594 union U {
595     i: i32,
596     f: f32,
597 }
598
599 impl U {
600     pub const C: i32 = 123;
601     pub const D: i32 = 456;
602 }
603
604 fn f(u: U) {
605     match u {
606         U::$0 => {}
607         _ => {}
608     }
609 }
610 "#,
611         expect![[r#"
612             ct C pub const C: i32
613             ct D pub const D: i32
614         "#]],
615     );
616
617     check_empty(
618         r#"
619 #[lang = "u32"]
620 impl u32 {
621     pub const MIN: Self = 0;
622 }
623
624 fn f(v: u32) {
625     match v {
626         u32::$0
627     }
628 }
629         "#,
630         expect![[r#"
631             ct MIN pub const MIN: Self
632         "#]],
633     );
634 }