]> git.lizzy.rs Git - rust.git/blob - crates/ide-completion/src/tests/pattern.rs
Auto merge of #12520 - Veykril:flycheck-cancel, r=Veykril
[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::{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             ev RecordV {…} RecordV { field: u32 }
320             ev TupleV(…)   TupleV(u32)
321             ev UnitV       UnitV
322         "#]],
323     );
324 }
325
326 #[test]
327 fn completes_in_record_field_pat() {
328     check_empty(
329         r#"
330 struct Foo { bar: Bar }
331 struct Bar(u32);
332 fn outer(Foo { bar: $0 }: Foo) {}
333 "#,
334         expect![[r#"
335             st Bar
336             st Foo
337             bn Bar Bar($1)$0
338             bn Foo Foo { bar$1 }$0
339             kw mut
340             kw ref
341         "#]],
342     )
343 }
344
345 #[test]
346 fn skips_in_record_field_pat_name() {
347     check_empty(
348         r#"
349 struct Foo { bar: Bar }
350 struct Bar(u32);
351 fn outer(Foo { bar$0 }: Foo) {}
352 "#,
353         expect![[r#"
354             kw mut
355             kw ref
356         "#]],
357     )
358 }
359
360 #[test]
361 fn completes_in_fn_param() {
362     check_empty(
363         r#"
364 struct Foo { bar: Bar }
365 struct Bar(u32);
366 fn foo($0) {}
367 "#,
368         expect![[r#"
369             st Bar
370             st Foo
371             bn Bar Bar($1): Bar$0
372             bn Foo Foo { bar$1 }: Foo$0
373             kw mut
374             kw ref
375         "#]],
376     )
377 }
378
379 #[test]
380 fn completes_in_closure_param() {
381     check_empty(
382         r#"
383 struct Foo { bar: Bar }
384 struct Bar(u32);
385 fn foo() {
386     |$0| {};
387 }
388 "#,
389         expect![[r#"
390             st Bar
391             st Foo
392             bn Bar Bar($1)$0
393             bn Foo Foo { bar$1 }$0
394             kw mut
395             kw ref
396         "#]],
397     )
398 }
399
400 #[test]
401 fn completes_no_delims_if_existing() {
402     check_empty(
403         r#"
404 struct Bar(u32);
405 fn foo() {
406     match Bar(0) {
407         B$0(b) => {}
408     }
409 }
410 "#,
411         expect![[r#"
412             st Bar
413             kw crate::
414             kw self::
415             kw super::
416         "#]],
417     );
418     check_empty(
419         r#"
420 struct Foo { bar: u32 }
421 fn foo() {
422     match (Foo { bar: 0 }) {
423         F$0 { bar } => {}
424     }
425 }
426 "#,
427         expect![[r#"
428             st Foo
429             kw crate::
430             kw self::
431             kw super::
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             ev 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             ev RecordVariant RecordVariant
462         "#]],
463     );
464 }
465
466 #[test]
467 fn completes_associated_const() {
468     check_empty(
469         r#"
470 #[derive(PartialEq, Eq)]
471 struct Ty(u8);
472
473 impl Ty {
474     const ABC: Self = Self(0);
475 }
476
477 fn f(t: Ty) {
478     match t {
479         Ty::$0 => {}
480         _ => {}
481     }
482 }
483 "#,
484         expect![[r#"
485             ct ABC const ABC: Self
486         "#]],
487     );
488
489     check_empty(
490         r#"
491 enum MyEnum {}
492
493 impl MyEnum {
494     pub const A: i32 = 123;
495     pub const B: i32 = 456;
496 }
497
498 fn f(e: MyEnum) {
499     match e {
500         MyEnum::$0 => {}
501         _ => {}
502     }
503 }
504 "#,
505         expect![[r#"
506             ct A pub const A: i32
507             ct B pub const B: i32
508         "#]],
509     );
510
511     check_empty(
512         r#"
513 union U {
514     i: i32,
515     f: f32,
516 }
517
518 impl U {
519     pub const C: i32 = 123;
520     pub const D: i32 = 456;
521 }
522
523 fn f(u: U) {
524     match u {
525         U::$0 => {}
526         _ => {}
527     }
528 }
529 "#,
530         expect![[r#"
531             ct C pub const C: i32
532             ct D pub const D: i32
533         "#]],
534     );
535
536     check_empty(
537         r#"
538 #[lang = "u32"]
539 impl u32 {
540     pub const MIN: Self = 0;
541 }
542
543 fn f(v: u32) {
544     match v {
545         u32::$0
546     }
547 }
548         "#,
549         expect![[r#"
550             ct MIN pub const MIN: Self
551         "#]],
552     );
553 }