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