]> git.lizzy.rs Git - rust.git/blob - crates/ide-completion/src/tests/pattern.rs
fix: variants rendering in pattern path
[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             kw mut
168             kw ref
169         "#]],
170     );
171 }
172
173 #[test]
174 fn in_param() {
175     check(
176         r#"
177 fn foo(a$0) {
178 }
179 "#,
180         expect![[r#"
181             ma makro!(…)  macro_rules! makro
182             md module
183             st Record
184             st Tuple
185             st Unit
186             bn Record {…} Record { field$1 }: Record$0
187             bn Tuple(…)   Tuple($1): Tuple$0
188             kw mut
189             kw ref
190         "#]],
191     );
192     check(
193         r#"
194 fn foo(a$0: Tuple) {
195 }
196 "#,
197         expect![[r#"
198             ma makro!(…)  macro_rules! makro
199             md module
200             st Record
201             st Tuple
202             st Unit
203             bn Record {…} Record { field$1 }$0
204             bn Tuple(…)   Tuple($1)$0
205             kw mut
206             kw ref
207         "#]],
208     );
209 }
210
211 #[test]
212 fn only_fn_like_macros() {
213     check_empty(
214         r#"
215 macro_rules! m { ($e:expr) => { $e } }
216
217 #[rustc_builtin_macro]
218 macro Clone {}
219
220 fn foo() {
221     let x$0
222 }
223 "#,
224         expect![[r#"
225             ma m!(…) macro_rules! m
226             kw mut
227             kw ref
228         "#]],
229     );
230 }
231
232 #[test]
233 fn in_simple_macro_call() {
234     check_empty(
235         r#"
236 macro_rules! m { ($e:expr) => { $e } }
237 enum E { X }
238
239 fn foo() {
240    m!(match E::X { a$0 })
241 }
242 "#,
243         expect![[r#"
244             en E
245             ma m!(…) macro_rules! m
246             kw mut
247             kw ref
248         "#]],
249     );
250 }
251
252 #[test]
253 fn omits_private_fields_pat() {
254     check_empty(
255         r#"
256 mod foo {
257     pub struct Record { pub field: i32, _field: i32 }
258     pub struct Tuple(pub u32, u32);
259     pub struct Invisible(u32, u32);
260 }
261 use foo::*;
262
263 fn outer() {
264     if let a$0
265 }
266 "#,
267         expect![[r#"
268             md foo
269             st Invisible
270             st Record
271             st Tuple
272             bn Record {…} Record { field$1, .. }$0
273             bn Tuple(…)   Tuple($1, ..)$0
274             kw mut
275             kw ref
276         "#]],
277     )
278 }
279
280 #[test]
281 fn completes_self_pats() {
282     check_empty(
283         r#"
284 struct Foo(i32);
285 impl Foo {
286     fn foo() {
287         match Foo(0) {
288             a$0
289         }
290     }
291 }
292     "#,
293         expect![[r#"
294             sp Self
295             st Foo
296             bn Foo(…)  Foo($1)$0
297             bn Self(…) Self($1)$0
298             kw mut
299             kw ref
300         "#]],
301     )
302 }
303
304 #[test]
305 fn enum_qualified() {
306     check(
307         r#"
308 impl Enum {
309     type AssocType = ();
310     const ASSOC_CONST: () = ();
311     fn assoc_fn() {}
312 }
313 fn func() {
314     if let Enum::$0 = unknown {}
315 }
316 "#,
317         expect![[r#"
318             ct ASSOC_CONST const ASSOC_CONST: ()
319             bn RecordV {…} RecordV { field$1 }$0
320             bn TupleV(…)   TupleV($1)$0
321         "#]],
322     );
323 }
324
325 #[test]
326 fn completes_in_record_field_pat() {
327     check_empty(
328         r#"
329 struct Foo { bar: Bar }
330 struct Bar(u32);
331 fn outer(Foo { bar: $0 }: Foo) {}
332 "#,
333         expect![[r#"
334             st Bar
335             st Foo
336             bn Bar(…)  Bar($1)$0
337             bn Foo {…} Foo { bar$1 }$0
338             kw mut
339             kw ref
340         "#]],
341     )
342 }
343
344 #[test]
345 fn skips_in_record_field_pat_name() {
346     check_empty(
347         r#"
348 struct Foo { bar: Bar }
349 struct Bar(u32);
350 fn outer(Foo { bar$0 }: Foo) {}
351 "#,
352         expect![[r#"
353             kw mut
354             kw ref
355         "#]],
356     )
357 }
358
359 #[test]
360 fn completes_in_fn_param() {
361     check_empty(
362         r#"
363 struct Foo { bar: Bar }
364 struct Bar(u32);
365 fn foo($0) {}
366 "#,
367         expect![[r#"
368             st Bar
369             st Foo
370             bn Bar(…)  Bar($1): Bar$0
371             bn Foo {…} Foo { bar$1 }: Foo$0
372             kw mut
373             kw ref
374         "#]],
375     )
376 }
377
378 #[test]
379 fn completes_in_closure_param() {
380     check_empty(
381         r#"
382 struct Foo { bar: Bar }
383 struct Bar(u32);
384 fn foo() {
385     |$0| {};
386 }
387 "#,
388         expect![[r#"
389             st Bar
390             st Foo
391             bn Bar(…)  Bar($1)$0
392             bn Foo {…} Foo { bar$1 }$0
393             kw mut
394             kw ref
395         "#]],
396     )
397 }
398
399 #[test]
400 fn completes_no_delims_if_existing() {
401     check_empty(
402         r#"
403 struct Bar(u32);
404 fn foo() {
405     match Bar(0) {
406         B$0(b) => {}
407     }
408 }
409 "#,
410         expect![[r#"
411             st Bar
412             kw crate::
413             kw self::
414             kw super::
415         "#]],
416     );
417     check_empty(
418         r#"
419 struct Foo { bar: u32 }
420 fn foo() {
421     match (Foo { bar: 0 }) {
422         F$0 { bar } => {}
423     }
424 }
425 "#,
426         expect![[r#"
427             st Foo
428             kw crate::
429             kw self::
430             kw super::
431         "#]],
432     );
433     check_empty(
434         r#"
435 enum Enum {
436     TupleVariant(u32)
437 }
438 fn foo() {
439     match Enum::TupleVariant(0) {
440         Enum::T$0(b) => {}
441     }
442 }
443 "#,
444         expect![[r#"
445             bn TupleVariant(…) TupleVariant($1)$0
446         "#]],
447     );
448     check_empty(
449         r#"
450 enum Enum {
451     RecordVariant { field: u32 }
452 }
453 fn foo() {
454     match (Enum::RecordVariant { field: 0 }) {
455         Enum::RecordV$0 { field } => {}
456     }
457 }
458 "#,
459         expect![[r#"
460             bn RecordVariant {…} RecordVariant { field$1 }$0
461         "#]],
462     );
463 }
464
465 #[test]
466 fn completes_enum_variant_pat() {
467     cov_mark::check!(enum_variant_pattern_path);
468     check_edit(
469         "RecordVariant {…}",
470         r#"
471 enum Enum {
472     RecordVariant { field: u32 }
473 }
474 fn foo() {
475     match (Enum::RecordVariant { field: 0 }) {
476         Enum::RecordV$0
477     }
478 }
479 "#,
480         r#"
481 enum Enum {
482     RecordVariant { field: u32 }
483 }
484 fn foo() {
485     match (Enum::RecordVariant { field: 0 }) {
486         Enum::RecordVariant { field$1 }$0
487     }
488 }
489 "#,
490     );
491 }
492
493 #[test]
494 fn completes_associated_const() {
495     check_empty(
496         r#"
497 #[derive(PartialEq, Eq)]
498 struct Ty(u8);
499
500 impl Ty {
501     const ABC: Self = Self(0);
502 }
503
504 fn f(t: Ty) {
505     match t {
506         Ty::$0 => {}
507         _ => {}
508     }
509 }
510 "#,
511         expect![[r#"
512             ct ABC const ABC: Self
513         "#]],
514     );
515
516     check_empty(
517         r#"
518 enum MyEnum {}
519
520 impl MyEnum {
521     pub const A: i32 = 123;
522     pub const B: i32 = 456;
523 }
524
525 fn f(e: MyEnum) {
526     match e {
527         MyEnum::$0 => {}
528         _ => {}
529     }
530 }
531 "#,
532         expect![[r#"
533             ct A pub const A: i32
534             ct B pub const B: i32
535         "#]],
536     );
537
538     check_empty(
539         r#"
540 union U {
541     i: i32,
542     f: f32,
543 }
544
545 impl U {
546     pub const C: i32 = 123;
547     pub const D: i32 = 456;
548 }
549
550 fn f(u: U) {
551     match u {
552         U::$0 => {}
553         _ => {}
554     }
555 }
556 "#,
557         expect![[r#"
558             ct C pub const C: i32
559             ct D pub const D: i32
560         "#]],
561     );
562
563     check_empty(
564         r#"
565 #[lang = "u32"]
566 impl u32 {
567     pub const MIN: Self = 0;
568 }
569
570 fn f(v: u32) {
571     match v {
572         u32::$0
573     }
574 }
575         "#,
576         expect![[r#"
577             ct MIN pub const MIN: Self
578         "#]],
579     );
580 }