]> git.lizzy.rs Git - rust.git/blob - crates/ide-completion/src/tests/pattern.rs
Auto merge of #11983 - jhpratt:remove-rustc_deprecated, r=lnicola
[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     )
343 }
344
345 #[test]
346 fn completes_in_fn_param() {
347     check_empty(
348         r#"
349 struct Foo { bar: Bar }
350 struct Bar(u32);
351 fn foo($0) {}
352 "#,
353         expect![[r#"
354             st Bar
355             st Foo
356             bn Bar Bar($1): Bar$0
357             bn Foo Foo { bar$1 }: Foo$0
358             kw mut
359             kw ref
360         "#]],
361     )
362 }
363
364 #[test]
365 fn completes_in_closure_param() {
366     check_empty(
367         r#"
368 struct Foo { bar: Bar }
369 struct Bar(u32);
370 fn foo() {
371     |$0| {};
372 }
373 "#,
374         expect![[r#"
375             st Bar
376             st Foo
377             bn Bar Bar($1)$0
378             bn Foo Foo { bar$1 }$0
379             kw mut
380             kw ref
381         "#]],
382     )
383 }
384
385 #[test]
386 fn completes_no_delims_if_existing() {
387     check_empty(
388         r#"
389 struct Bar(u32);
390 fn foo() {
391     match Bar(0) {
392         B$0(b) => {}
393     }
394 }
395 "#,
396         expect![[r#"
397             fn foo()   fn()
398             st Bar
399             bt u32
400             kw crate::
401             kw self::
402             kw super::
403         "#]],
404     );
405     check_empty(
406         r#"
407 struct Foo { bar: u32 }
408 fn foo() {
409     match (Foo { bar: 0 }) {
410         F$0 { bar } => {}
411     }
412 }
413 "#,
414         expect![[r#"
415             fn foo()   fn()
416             st Foo
417             bt u32
418             kw crate::
419             kw self::
420             kw super::
421         "#]],
422     );
423     check_empty(
424         r#"
425 enum Enum {
426     TupleVariant(u32)
427 }
428 fn foo() {
429     match Enum::TupleVariant(0) {
430         Enum::T$0(b) => {}
431     }
432 }
433 "#,
434         expect![[r#"
435             ev TupleVariant(…) TupleVariant
436         "#]],
437     );
438     check_empty(
439         r#"
440 enum Enum {
441     RecordVariant { field: u32 }
442 }
443 fn foo() {
444     match (Enum::RecordVariant { field: 0 }) {
445         Enum::RecordV$0 { field } => {}
446     }
447 }
448 "#,
449         expect![[r#"
450             ev RecordVariant {…} RecordVariant
451         "#]],
452     );
453 }
454
455 #[test]
456 fn completes_associated_const() {
457     check_empty(
458         r#"
459 #[derive(PartialEq, Eq)]
460 struct Ty(u8);
461
462 impl Ty {
463     const ABC: Self = Self(0);
464 }
465
466 fn f(t: Ty) {
467     match t {
468         Ty::$0 => {}
469         _ => {}
470     }
471 }
472 "#,
473         expect![[r#"
474             ct ABC const ABC: Self
475         "#]],
476     );
477
478     check_empty(
479         r#"
480 enum MyEnum {}
481
482 impl MyEnum {
483     pub const A: i32 = 123;
484     pub const B: i32 = 456;
485 }
486
487 fn f(e: MyEnum) {
488     match e {
489         MyEnum::$0 => {}
490         _ => {}
491     }
492 }
493 "#,
494         expect![[r#"
495             ct A pub const A: i32
496             ct B pub const B: i32
497         "#]],
498     );
499
500     check_empty(
501         r#"
502 union U {
503     i: i32,
504     f: f32,
505 }
506
507 impl U {
508     pub const C: i32 = 123;
509     pub const D: i32 = 456;
510 }
511
512 fn f(u: U) {
513     match u {
514         U::$0 => {}
515         _ => {}
516     }
517 }
518 "#,
519         expect![[r#"
520             ct C pub const C: i32
521             ct D pub const D: i32
522         "#]],
523     );
524
525     check_empty(
526         r#"
527 #[lang = "u32"]
528 impl u32 {
529     pub const MIN: Self = 0;
530 }
531
532 fn f(v: u32) {
533     match v {
534         u32::$0
535     }
536 }
537         "#,
538         expect![[r#"
539             ct MIN pub const MIN: Self
540         "#]],
541     );
542 }