]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-analyzer/crates/ide-completion/src/tests/pattern.rs
:arrow_up: rust-analyzer
[rust.git] / src / tools / rust-analyzer / 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         "#]],
418     );
419     check_empty(
420         r#"
421 struct Foo { bar: u32 }
422 fn foo() {
423     match (Foo { bar: 0 }) {
424         F$0 { bar } => {}
425     }
426 }
427 "#,
428         expect![[r#"
429             st Foo
430             kw crate::
431             kw self::
432         "#]],
433     );
434     check_empty(
435         r#"
436 enum Enum {
437     TupleVariant(u32)
438 }
439 fn foo() {
440     match Enum::TupleVariant(0) {
441         Enum::T$0(b) => {}
442     }
443 }
444 "#,
445         expect![[r#"
446             bn TupleVariant TupleVariant
447         "#]],
448     );
449     check_empty(
450         r#"
451 enum Enum {
452     RecordVariant { field: u32 }
453 }
454 fn foo() {
455     match (Enum::RecordVariant { field: 0 }) {
456         Enum::RecordV$0 { field } => {}
457     }
458 }
459 "#,
460         expect![[r#"
461             bn RecordVariant RecordVariant
462         "#]],
463     );
464 }
465
466 #[test]
467 fn completes_enum_variant_pat() {
468     cov_mark::check!(enum_variant_pattern_path);
469     check_edit(
470         "RecordVariant{}",
471         r#"
472 enum Enum {
473     RecordVariant { field: u32 }
474 }
475 fn foo() {
476     match (Enum::RecordVariant { field: 0 }) {
477         Enum::RecordV$0
478     }
479 }
480 "#,
481         r#"
482 enum Enum {
483     RecordVariant { field: u32 }
484 }
485 fn foo() {
486     match (Enum::RecordVariant { field: 0 }) {
487         Enum::RecordVariant { field$1 }$0
488     }
489 }
490 "#,
491     );
492 }
493
494 #[test]
495 fn completes_enum_variant_pat_escape() {
496     cov_mark::check!(enum_variant_pattern_path);
497     check_empty(
498         r#"
499 enum Enum {
500     A,
501     B { r#type: i32 },
502     r#type,
503     r#struct { r#type: i32 },
504 }
505 fn foo() {
506     match (Enum::A) {
507         $0
508     }
509 }
510 "#,
511         expect![[r#"
512             en Enum
513             bn Enum::A          Enum::A$0
514             bn Enum::B {…}      Enum::B { r#type$1 }$0
515             bn Enum::struct {…} Enum::r#struct { r#type$1 }$0
516             bn Enum::type       Enum::r#type$0
517             kw mut
518             kw ref
519         "#]],
520     );
521
522     check_empty(
523         r#"
524 enum Enum {
525     A,
526     B { r#type: i32 },
527     r#type,
528     r#struct { r#type: i32 },
529 }
530 fn foo() {
531     match (Enum::A) {
532         Enum::$0
533     }
534 }
535 "#,
536         expect![[r#"
537             bn A          A$0
538             bn B {…}      B { r#type$1 }$0
539             bn struct {…} r#struct { r#type$1 }$0
540             bn type       r#type$0
541         "#]],
542     );
543 }
544
545 #[test]
546 fn completes_associated_const() {
547     check_empty(
548         r#"
549 #[derive(PartialEq, Eq)]
550 struct Ty(u8);
551
552 impl Ty {
553     const ABC: Self = Self(0);
554 }
555
556 fn f(t: Ty) {
557     match t {
558         Ty::$0 => {}
559         _ => {}
560     }
561 }
562 "#,
563         expect![[r#"
564             ct ABC const ABC: Self
565         "#]],
566     );
567
568     check_empty(
569         r#"
570 enum MyEnum {}
571
572 impl MyEnum {
573     pub const A: i32 = 123;
574     pub const B: i32 = 456;
575 }
576
577 fn f(e: MyEnum) {
578     match e {
579         MyEnum::$0 => {}
580         _ => {}
581     }
582 }
583 "#,
584         expect![[r#"
585             ct A pub const A: i32
586             ct B pub const B: i32
587         "#]],
588     );
589
590     check_empty(
591         r#"
592 union U {
593     i: i32,
594     f: f32,
595 }
596
597 impl U {
598     pub const C: i32 = 123;
599     pub const D: i32 = 456;
600 }
601
602 fn f(u: U) {
603     match u {
604         U::$0 => {}
605         _ => {}
606     }
607 }
608 "#,
609         expect![[r#"
610             ct C pub const C: i32
611             ct D pub const D: i32
612         "#]],
613     );
614
615     check_empty(
616         r#"
617 #[lang = "u32"]
618 impl u32 {
619     pub const MIN: Self = 0;
620 }
621
622 fn f(v: u32) {
623     match v {
624         u32::$0
625     }
626 }
627         "#,
628         expect![[r#"
629             ct MIN pub const MIN: Self
630         "#]],
631     );
632 }
633
634 #[test]
635 fn in_method_param() {
636     check_empty(
637         r#"
638 struct Ty(u8);
639
640 impl Ty {
641     fn foo($0)
642 }
643 "#,
644         expect![[r#"
645             sp Self
646             st Ty
647             bn &mut self
648             bn &self
649             bn Self(…)   Self($1): Self$0
650             bn Ty(…)     Ty($1): Ty$0
651             bn mut self
652             bn self
653             kw mut
654             kw ref
655         "#]],
656     );
657     check_empty(
658         r#"
659 struct Ty(u8);
660
661 impl Ty {
662     fn foo(s$0)
663 }
664 "#,
665         expect![[r#"
666             sp Self
667             st Ty
668             bn &mut self
669             bn &self
670             bn Self(…)   Self($1): Self$0
671             bn Ty(…)     Ty($1): Ty$0
672             bn mut self
673             bn self
674             kw mut
675             kw ref
676         "#]],
677     );
678     check_empty(
679         r#"
680 struct Ty(u8);
681
682 impl Ty {
683     fn foo(s$0, foo: u8)
684 }
685 "#,
686         expect![[r#"
687             sp Self
688             st Ty
689             bn &mut self
690             bn &self
691             bn Self(…)   Self($1): Self$0
692             bn Ty(…)     Ty($1): Ty$0
693             bn mut self
694             bn self
695             kw mut
696             kw ref
697         "#]],
698     );
699     check_empty(
700         r#"
701 struct Ty(u8);
702
703 impl Ty {
704     fn foo(foo: u8, b$0)
705 }
706 "#,
707         expect![[r#"
708             sp Self
709             st Ty
710             bn Self(…) Self($1): Self$0
711             bn Ty(…)   Ty($1): Ty$0
712             kw mut
713             kw ref
714         "#]],
715     );
716 }