]> git.proxmox.com Git - rustc.git/blob - src/tools/rust-analyzer/crates/ide-completion/src/tests/record.rs
New upstream version 1.65.0+dfsg1
[rustc.git] / src / tools / rust-analyzer / crates / ide-completion / src / tests / record.rs
1 use expect_test::{expect, Expect};
2
3 use crate::tests::completion_list;
4
5 fn check(ra_fixture: &str, expect: Expect) {
6 let actual = completion_list(ra_fixture);
7 expect.assert_eq(&actual);
8 }
9
10 #[test]
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]
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]
50 fn pattern_enum_variant() {
51 check(
52 r#"
53 enum Enum { Variant { foo: u32, bar: u32 } }
54 fn foo(e: Enum) {
55 match e {
56 Enum::Variant { foo, $0 } => (),
57 }
58 }
59 "#,
60 expect![[r#"
61 fd bar u32
62 kw mut
63 kw ref
64 "#]],
65 );
66 }
67
68 #[test]
69 fn record_literal_field_in_macro() {
70 check(
71 r#"
72 macro_rules! m { ($e:expr) => { $e } }
73 struct Struct { field: u32 }
74 fn foo() {
75 m!(Struct { fie$0 })
76 }
77 "#,
78 expect![[r#"
79 fd field u32
80 "#]],
81 );
82 }
83
84 #[test]
85 fn record_pattern_field_in_macro() {
86 check(
87 r"
88 macro_rules! m { ($e:expr) => { $e } }
89 struct Struct { field: u32 }
90
91 fn foo(f: Struct) {
92 m!(match f {
93 Struct { f$0: 92 } => (),
94 })
95 }
96 ",
97 expect![[r#"
98 fd field u32
99 kw mut
100 kw ref
101 "#]],
102 );
103 }
104
105 #[test]
106 fn in_functional_update() {
107 cov_mark::check!(functional_update);
108
109 check(
110 r#"
111 //- minicore:default
112 struct Foo { foo1: u32, foo2: u32 }
113 impl Default for Foo {
114 fn default() -> Self { loop {} }
115 }
116
117 fn main() {
118 let thing = 1;
119 let foo = Foo { foo1: 0, foo2: 0 };
120 let foo2 = Foo { thing, ..$0 }
121 }
122 "#,
123 expect![[r#"
124 fd ..Default::default()
125 fn main() fn()
126 lc foo Foo
127 lc thing i32
128 md core
129 st Foo
130 st Foo {…} Foo { foo1: u32, foo2: u32 }
131 tt Default
132 bt u32
133 kw crate::
134 kw self::
135 "#]],
136 );
137 check(
138 r#"
139 //- minicore:default
140 struct Foo { foo1: u32, foo2: u32 }
141 impl Default for Foo {
142 fn default() -> Self { loop {} }
143 }
144
145 fn main() {
146 let thing = 1;
147 let foo = Foo { foo1: 0, foo2: 0 };
148 let foo2 = Foo { thing, ..Default::$0 }
149 }
150 "#,
151 expect![[r#"
152 fn default() (as Default) fn() -> Self
153 "#]],
154 );
155 }
156
157 #[test]
158 fn functional_update_no_dot() {
159 cov_mark::check!(functional_update_field);
160 // FIXME: This should filter out all completions that do not have the type `Foo`
161 check(
162 r#"
163 //- minicore:default
164 struct Foo { foo1: u32, foo2: u32 }
165 impl Default for Foo {
166 fn default() -> Self { loop {} }
167 }
168
169 fn main() {
170 let thing = 1;
171 let foo = Foo { foo1: 0, foo2: 0 };
172 let foo2 = Foo { thing, $0 }
173 }
174 "#,
175 expect![[r#"
176 fd ..Default::default()
177 fd foo1 u32
178 fd foo2 u32
179 "#]],
180 );
181 }
182
183 #[test]
184 fn functional_update_one_dot() {
185 cov_mark::check!(functional_update_one_dot);
186 check(
187 r#"
188 //- minicore:default
189 struct Foo { foo1: u32, foo2: u32 }
190 impl Default for Foo {
191 fn default() -> Self { loop {} }
192 }
193
194 fn main() {
195 let thing = 1;
196 let foo = Foo { foo1: 0, foo2: 0 };
197 let foo2 = Foo { thing, .$0 }
198 }
199 "#,
200 expect![[r#"
201 fd ..Default::default()
202 sn ..
203 "#]],
204 );
205 }
206
207 #[test]
208 fn empty_union_literal() {
209 check(
210 r#"
211 union Union { foo: u32, bar: f32 }
212
213 fn foo() {
214 let other = Union {
215 $0
216 };
217 }
218 "#,
219 expect![[r#"
220 fd bar f32
221 fd foo u32
222 "#]],
223 )
224 }
225
226 #[test]
227 fn dont_suggest_additional_union_fields() {
228 check(
229 r#"
230 union Union { foo: u32, bar: f32 }
231
232 fn foo() {
233 let other = Union {
234 foo: 1,
235 $0
236 };
237 }
238 "#,
239 expect![[r#""#]],
240 )
241 }