]> git.lizzy.rs Git - rust.git/blob - crates/ide_completion/src/tests/pattern.rs
Fully render type alias completions from hir
[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         "#]],
27     );
28 }
29
30 #[test]
31 fn ident_ref_pat() {
32     check_empty(
33         r#"
34 fn quux() {
35     let ref en$0
36 }
37 "#,
38         expect![[r#"
39             kw mut
40         "#]],
41     );
42     check_empty(
43         r#"
44 fn quux() {
45     let ref en$0 @ x
46 }
47 "#,
48         expect![[r#"
49             kw mut
50         "#]],
51     );
52 }
53
54 #[test]
55 fn ident_ref_mut_pat() {
56     // FIXME mut is already here, don't complete it again
57     check_empty(
58         r#"
59 fn quux() {
60     let ref mut en$0
61 }
62 "#,
63         expect![[r#"
64             kw mut
65         "#]],
66     );
67     check_empty(
68         r#"
69 fn quux() {
70     let ref mut en$0 @ x
71 }
72 "#,
73         expect![[r#"
74             kw mut
75         "#]],
76     );
77 }
78
79 #[test]
80 fn ref_pat() {
81     check_empty(
82         r#"
83 fn quux() {
84     let &en$0
85 }
86 "#,
87         expect![[r#"
88             kw mut
89         "#]],
90     );
91     // FIXME mut is already here, don't complete it again
92     check_empty(
93         r#"
94 fn quux() {
95     let &mut en$0
96 }
97 "#,
98         expect![[r#"
99             kw mut
100         "#]],
101     );
102 }
103
104 #[test]
105 fn refutable() {
106     check(
107         r#"
108 fn foo() {
109     if let a$0
110 }
111 "#,
112         expect![[r##"
113             kw mut
114             en Enum
115             bn Record    Record { field$1 }$0
116             st Record
117             bn Tuple     Tuple($1)$0
118             st Tuple
119             md module
120             st Unit
121             ma makro!(…) #[macro_export] macro_rules! makro
122             bn TupleV    TupleV($1)$0
123             ev TupleV
124             ct CONST
125         "##]],
126     );
127 }
128
129 #[test]
130 fn irrefutable() {
131     check(
132         r#"
133 enum SingleVariantEnum {
134     Variant
135 }
136 use SingleVariantEnum::Variant;
137 fn foo() {
138    let a$0
139 }
140 "#,
141         expect![[r##"
142             kw mut
143             bn Record            Record { field$1 }$0
144             st Record
145             bn Tuple             Tuple($1)$0
146             st Tuple
147             ev Variant
148             en SingleVariantEnum
149             st Unit
150             ma makro!(…)         #[macro_export] macro_rules! makro
151         "##]],
152     );
153 }
154
155 #[test]
156 fn in_param() {
157     check(
158         r#"
159 fn foo(a$0) {
160 }
161 "#,
162         expect![[r##"
163             kw mut
164             bn Record    Record { field$1 }: Record$0
165             st Record
166             bn Tuple     Tuple($1): Tuple$0
167             st Tuple
168             st Unit
169             ma makro!(…) #[macro_export] macro_rules! makro
170         "##]],
171     );
172     check(
173         r#"
174 fn foo(a$0: Tuple) {
175 }
176 "#,
177         expect![[r##"
178             kw mut
179             bn Record    Record { field$1 }$0
180             st Record
181             bn Tuple     Tuple($1)$0
182             st Tuple
183             st Unit
184             ma makro!(…) #[macro_export] macro_rules! makro
185         "##]],
186     );
187 }
188
189 #[test]
190 fn only_fn_like_macros() {
191     check_empty(
192         r#"
193 macro_rules! m { ($e:expr) => { $e } }
194
195 #[rustc_builtin_macro]
196 macro Clone {}
197
198 fn foo() {
199     let x$0
200 }
201 "#,
202         expect![[r#"
203             kw mut
204             ma m!(…) macro_rules! m
205         "#]],
206     );
207 }
208
209 #[test]
210 fn in_simple_macro_call() {
211     check_empty(
212         r#"
213 macro_rules! m { ($e:expr) => { $e } }
214 enum E { X }
215
216 fn foo() {
217    m!(match E::X { a$0 })
218 }
219 "#,
220         expect![[r#"
221             kw mut
222             ev E::X  ()
223             en E
224             ma m!(…) macro_rules! m
225         "#]],
226     );
227 }
228
229 #[test]
230 fn omits_private_fields_pat() {
231     check_empty(
232         r#"
233 mod foo {
234     pub struct Record { pub field: i32, _field: i32 }
235     pub struct Tuple(pub u32, u32);
236     pub struct Invisible(u32, u32);
237 }
238 use foo::*;
239
240 fn outer() {
241     if let a$0
242 }
243 "#,
244         expect![[r#"
245             kw mut
246             bn Record    Record { field$1, .. }$0
247             st Record
248             bn Tuple     Tuple($1, ..)$0
249             st Tuple
250             st Invisible
251             md foo
252         "#]],
253     )
254 }
255
256 #[test]
257 fn completes_self_pats() {
258     check_empty(
259         r#"
260 struct Foo(i32);
261 impl Foo {
262     fn foo() {
263         match Foo(0) {
264             a$0
265         }
266     }
267 }
268     "#,
269         expect![[r#"
270             kw mut
271             bn Self Self($1)$0
272             sp Self
273             bn Foo  Foo($1)$0
274             st Foo
275         "#]],
276     )
277 }
278
279 #[test]
280 fn enum_qualified() {
281     // FIXME: Don't show functions, they aren't patterns
282     check(
283         r#"
284 impl Enum {
285     type AssocType = ();
286     const ASSOC_CONST: () = ();
287     fn assoc_fn() {}
288 }
289 fn func() {
290     if let Enum::$0 = unknown {}
291 }
292 "#,
293         expect![[r#"
294             ev TupleV(…)   (u32)
295             ev RecordV     {field: u32}
296             ev UnitV       ()
297             ct ASSOC_CONST const ASSOC_CONST: ()
298             fn assoc_fn()  fn()
299             ta AssocType   type AssocType = ()
300         "#]],
301     );
302 }
303
304 #[test]
305 fn completes_in_record_field_pat() {
306     check_empty(
307         r#"
308 struct Foo { bar: Bar }
309 struct Bar(u32);
310 fn outer(Foo { bar: $0 }: Foo) {}
311 "#,
312         expect![[r#"
313             kw mut
314             bn Foo Foo { bar$1 }$0
315             st Foo
316             bn Bar Bar($1)$0
317             st Bar
318         "#]],
319     )
320 }
321
322 #[test]
323 fn skips_in_record_field_pat_name() {
324     check_empty(
325         r#"
326 struct Foo { bar: Bar }
327 struct Bar(u32);
328 fn outer(Foo { bar$0 }: Foo) {}
329 "#,
330         expect![[r#""#]],
331     )
332 }
333
334 #[test]
335 fn completes_in_fn_param() {
336     check_empty(
337         r#"
338 struct Foo { bar: Bar }
339 struct Bar(u32);
340 fn foo($0) {}
341 "#,
342         expect![[r#"
343             kw mut
344             bn Foo Foo { bar$1 }: Foo$0
345             st Foo
346             bn Bar Bar($1): Bar$0
347             st Bar
348         "#]],
349     )
350 }
351
352 #[test]
353 fn completes_in_closure_param() {
354     check_empty(
355         r#"
356 struct Foo { bar: Bar }
357 struct Bar(u32);
358 fn foo() {
359     |$0| {};
360 }
361 "#,
362         expect![[r#"
363             kw mut
364             bn Foo Foo { bar$1 }$0
365             st Foo
366             bn Bar Bar($1)$0
367             st Bar
368         "#]],
369     )
370 }