]> git.lizzy.rs Git - rust.git/blob - crates/ide-completion/src/tests/pattern.rs
minor: Sort ide-completions test outputs for less disruptive diffs
[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             kw crate::
398             kw self::
399             kw super::
400         "#]],
401     );
402     check_empty(
403         r#"
404 struct Foo { bar: u32 }
405 fn foo() {
406     match Foo { bar: 0 } {
407         F$0 { bar } => {}
408     }
409 }
410 "#,
411         expect![[r#"
412             fn foo()  fn()
413             st Foo
414             bt u32
415             kw crate
416             kw return
417             kw self
418             kw super
419         "#]],
420     );
421     check_empty(
422         r#"
423 enum Enum {
424     TupleVariant(u32)
425 }
426 fn foo() {
427     match Enum::TupleVariant(0) {
428         Enum::T$0(b) => {}
429     }
430 }
431 "#,
432         expect![[r#"
433             ev TupleVariant(…) TupleVariant
434         "#]],
435     );
436     check_empty(
437         r#"
438 enum Enum {
439     RecordVariant { field: u32 }
440 }
441 fn foo() {
442     match (Enum::RecordVariant { field: 0 }) {
443         Enum::RecordV$0 { field } => {}
444     }
445 }
446 "#,
447         expect![[r#"
448             ev RecordVariant {…} RecordVariant
449         "#]],
450     );
451 }
452
453 #[test]
454 fn completes_associated_const() {
455     check_empty(
456         r#"
457 #[derive(PartialEq, Eq)]
458 struct Ty(u8);
459
460 impl Ty {
461     const ABC: Self = Self(0);
462 }
463
464 fn f(t: Ty) {
465     match t {
466         Ty::$0 => {}
467         _ => {}
468     }
469 }
470 "#,
471         expect![[r#"
472             ct ABC const ABC: Self
473         "#]],
474     );
475
476     check_empty(
477         r#"
478 enum MyEnum {}
479
480 impl MyEnum {
481     pub const A: i32 = 123;
482     pub const B: i32 = 456;
483 }
484
485 fn f(e: MyEnum) {
486     match e {
487         MyEnum::$0 => {}
488         _ => {}
489     }
490 }
491 "#,
492         expect![[r#"
493             ct A pub const A: i32
494             ct B pub const B: i32
495         "#]],
496     );
497
498     check_empty(
499         r#"
500 union U {
501     i: i32,
502     f: f32,
503 }
504
505 impl U {
506     pub const C: i32 = 123;
507     pub const D: i32 = 456;
508 }
509
510 fn f(u: U) {
511     match u {
512         U::$0 => {}
513         _ => {}
514     }
515 }
516 "#,
517         expect![[r#"
518             ct C pub const C: i32
519             ct D pub const D: i32
520         "#]],
521     );
522
523     check_empty(
524         r#"
525 #[lang = "u32"]
526 impl u32 {
527     pub const MIN: Self = 0;
528 }
529
530 fn f(v: u32) {
531     match v {
532         u32::$0
533     }
534 }
535         "#,
536         expect![[r#"
537             ct MIN pub const MIN: Self
538         "#]],
539     );
540 }