• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use expect_test::{expect, Expect};
2 
3 use crate::tests::completion_list;
4 
check(ra_fixture: &str, expect: Expect)5 fn check(ra_fixture: &str, expect: Expect) {
6     let actual = completion_list(ra_fixture);
7     expect.assert_eq(&actual);
8 }
9 
10 #[test]
without_default_impl()11 fn without_default_impl() {
12     check(
13         r#"
14 struct Struct { foo: u32, bar: usize }
15 
16 fn foo() {
17     let other = Struct {
18         foo: 5,
19         $0
20     };
21 }
22 "#,
23         expect![[r#"
24             fd bar usize
25         "#]],
26     );
27 }
28 
29 #[test]
record_pattern_field()30 fn record_pattern_field() {
31     check(
32         r#"
33 struct Struct { foo: u32, bar: u32 }
34 
35 fn foo(s: Struct) {
36     match s {
37         Struct { foo, $0: 92 } => (),
38     }
39 }
40 "#,
41         expect![[r#"
42             fd bar u32
43             kw mut
44             kw ref
45         "#]],
46     );
47 }
48 
49 #[test]
record_pattern_field_enum()50 fn record_pattern_field_enum() {
51     check(
52         r#"
53 //- minicore:result
54 enum Baz { Foo, Bar }
55 
56 fn foo(baz: Baz) {
57     match baz {
58         Baz::Foo => (),
59         $0
60     }
61 }
62 "#,
63         expect![[r#"
64             en Baz
65             en Result
66             md core
67             ev Err
68             ev Ok
69             bn Baz::Bar Baz::Bar$0
70             bn Baz::Foo Baz::Foo$0
71             bn Err(…)   Err($1)$0
72             bn Ok(…)    Ok($1)$0
73             kw mut
74             kw ref
75         "#]],
76     );
77 
78     check(
79         r#"
80 //- minicore:result
81 enum Baz { Foo, Bar }
82 
83 fn foo(baz: Baz) {
84     use Baz::*;
85     match baz {
86         Foo => (),
87         $0
88     }
89 }
90  "#,
91         expect![[r#"
92          en Baz
93          en Result
94          md core
95          ev Bar
96          ev Err
97          ev Foo
98          ev Ok
99          bn Bar    Bar$0
100          bn Err(…) Err($1)$0
101          bn Foo    Foo$0
102          bn Ok(…)  Ok($1)$0
103          kw mut
104          kw ref
105          "#]],
106     );
107 }
108 
109 #[test]
pattern_enum_variant()110 fn pattern_enum_variant() {
111     check(
112         r#"
113 enum Enum { Variant { foo: u32, bar: u32 } }
114 fn foo(e: Enum) {
115     match e {
116         Enum::Variant { foo, $0 } => (),
117     }
118 }
119 "#,
120         expect![[r#"
121             fd bar u32
122             kw mut
123             kw ref
124         "#]],
125     );
126 }
127 
128 #[test]
record_literal_field_in_macro()129 fn record_literal_field_in_macro() {
130     check(
131         r#"
132 macro_rules! m { ($e:expr) => { $e } }
133 struct Struct { field: u32 }
134 fn foo() {
135     m!(Struct { fie$0 })
136 }
137 "#,
138         expect![[r#"
139             fd field u32
140         "#]],
141     );
142 }
143 
144 #[test]
record_pattern_field_in_macro()145 fn record_pattern_field_in_macro() {
146     check(
147         r"
148 macro_rules! m { ($e:expr) => { $e } }
149 struct Struct { field: u32 }
150 
151 fn foo(f: Struct) {
152     m!(match f {
153         Struct { f$0: 92 } => (),
154     })
155 }
156 ",
157         expect![[r#"
158             fd field u32
159             kw mut
160             kw ref
161         "#]],
162     );
163 }
164 
165 #[test]
in_functional_update()166 fn in_functional_update() {
167     cov_mark::check!(functional_update);
168 
169     check(
170         r#"
171 //- minicore:default
172 struct Foo { foo1: u32, foo2: u32 }
173 impl Default for Foo {
174     fn default() -> Self { loop {} }
175 }
176 
177 fn main() {
178     let thing = 1;
179     let foo = Foo { foo1: 0, foo2: 0 };
180     let foo2 = Foo { thing, ..$0 }
181 }
182 "#,
183         expect![[r#"
184             fd ..Default::default()
185             fn main()               fn()
186             lc foo                  Foo
187             lc thing                i32
188             md core
189             st Foo
190             st Foo {…}              Foo { foo1: u32, foo2: u32 }
191             tt Default
192             bt u32
193             kw crate::
194             kw self::
195         "#]],
196     );
197     check(
198         r#"
199 //- minicore:default
200 struct Foo { foo1: u32, foo2: u32 }
201 impl Default for Foo {
202     fn default() -> Self { loop {} }
203 }
204 
205 fn main() {
206     let thing = 1;
207     let foo = Foo { foo1: 0, foo2: 0 };
208     let foo2 = Foo { thing, ..Default::$0 }
209 }
210 "#,
211         expect![[r#"
212             fn default() (as Default) fn() -> Self
213         "#]],
214     );
215 }
216 
217 #[test]
functional_update_no_dot()218 fn functional_update_no_dot() {
219     cov_mark::check!(functional_update_field);
220     // FIXME: This should filter out all completions that do not have the type `Foo`
221     check(
222         r#"
223 //- minicore:default
224 struct Foo { foo1: u32, foo2: u32 }
225 impl Default for Foo {
226     fn default() -> Self { loop {} }
227 }
228 
229 fn main() {
230     let thing = 1;
231     let foo = Foo { foo1: 0, foo2: 0 };
232     let foo2 = Foo { thing, $0 }
233 }
234 "#,
235         expect![[r#"
236             fd ..Default::default()
237             fd foo1                 u32
238             fd foo2                 u32
239         "#]],
240     );
241 }
242 
243 #[test]
functional_update_one_dot()244 fn functional_update_one_dot() {
245     cov_mark::check!(functional_update_one_dot);
246     check(
247         r#"
248 //- minicore:default
249 struct Foo { foo1: u32, foo2: u32 }
250 impl Default for Foo {
251     fn default() -> Self { loop {} }
252 }
253 
254 fn main() {
255     let thing = 1;
256     let foo = Foo { foo1: 0, foo2: 0 };
257     let foo2 = Foo { thing, .$0 }
258 }
259 "#,
260         expect![[r#"
261             fd ..Default::default()
262             sn ..
263         "#]],
264     );
265 }
266 
267 #[test]
empty_union_literal()268 fn empty_union_literal() {
269     check(
270         r#"
271 union Union { foo: u32, bar: f32 }
272 
273 fn foo() {
274     let other = Union {
275         $0
276     };
277 }
278         "#,
279         expect![[r#"
280             fd bar f32
281             fd foo u32
282         "#]],
283     )
284 }
285 
286 #[test]
dont_suggest_additional_union_fields()287 fn dont_suggest_additional_union_fields() {
288     check(
289         r#"
290 union Union { foo: u32, bar: f32 }
291 
292 fn foo() {
293     let other = Union {
294         foo: 1,
295         $0
296     };
297 }
298         "#,
299         expect![[r#""#]],
300     )
301 }
302