Skip to content

Commit a5ad4de

Browse files
committed
add tests
1 parent 817082c commit a5ad4de

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed

crates/ide-assists/src/handlers/generate_new.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,97 @@ mod tests {
123123

124124
use super::*;
125125

126+
#[test]
127+
fn test_generate_new_with_zst_fields() {
128+
check_assist(
129+
generate_new,
130+
r#"
131+
struct Empty;
132+
133+
struct Foo { empty: Empty $0}
134+
"#,
135+
r#"
136+
struct Empty;
137+
138+
struct Foo { empty: Empty }
139+
140+
impl Foo {
141+
fn $0new() -> Self { Self { empty: Empty } }
142+
}
143+
"#,
144+
);
145+
check_assist(
146+
generate_new,
147+
r#"
148+
struct Empty;
149+
150+
struct Foo { baz: String, empty: Empty $0}
151+
"#,
152+
r#"
153+
struct Empty;
154+
155+
struct Foo { baz: String, empty: Empty }
156+
157+
impl Foo {
158+
fn $0new(baz: String) -> Self { Self { baz, empty: Empty } }
159+
}
160+
"#,
161+
);
162+
check_assist(
163+
generate_new,
164+
r#"
165+
enum Empty { Bar }
166+
167+
struct Foo { empty: Empty $0}
168+
"#,
169+
r#"
170+
enum Empty { Bar }
171+
172+
struct Foo { empty: Empty }
173+
174+
impl Foo {
175+
fn $0new() -> Self { Self { empty: Empty::Bar } }
176+
}
177+
"#,
178+
);
179+
180+
// make sure the assist only works on unit variants
181+
check_assist(
182+
generate_new,
183+
r#"
184+
struct Empty {}
185+
186+
struct Foo { empty: Empty $0}
187+
"#,
188+
r#"
189+
struct Empty {}
190+
191+
struct Foo { empty: Empty }
192+
193+
impl Foo {
194+
fn $0new(empty: Empty) -> Self { Self { empty } }
195+
}
196+
"#,
197+
);
198+
check_assist(
199+
generate_new,
200+
r#"
201+
enum Empty { Bar {} }
202+
203+
struct Foo { empty: Empty $0}
204+
"#,
205+
r#"
206+
enum Empty { Bar {} }
207+
208+
struct Foo { empty: Empty }
209+
210+
impl Foo {
211+
fn $0new(empty: Empty) -> Self { Self { empty } }
212+
}
213+
"#,
214+
);
215+
}
216+
126217
#[test]
127218
fn test_generate_new() {
128219
check_assist(

crates/ide-diagnostics/src/handlers/missing_fields.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,92 @@ fn test_fn() {
345345
);
346346
}
347347

348+
#[test]
349+
fn test_fill_struct_zst_fields() {
350+
check_fix(
351+
r#"
352+
struct Empty;
353+
354+
struct TestStruct { one: i32, two: Empty }
355+
356+
fn test_fn() {
357+
let s = TestStruct {$0};
358+
}
359+
"#,
360+
r#"
361+
struct Empty;
362+
363+
struct TestStruct { one: i32, two: Empty }
364+
365+
fn test_fn() {
366+
let s = TestStruct { one: 0, two: Empty };
367+
}
368+
"#,
369+
);
370+
check_fix(
371+
r#"
372+
enum Empty { Foo };
373+
374+
struct TestStruct { one: i32, two: Empty }
375+
376+
fn test_fn() {
377+
let s = TestStruct {$0};
378+
}
379+
"#,
380+
r#"
381+
enum Empty { Foo };
382+
383+
struct TestStruct { one: i32, two: Empty }
384+
385+
fn test_fn() {
386+
let s = TestStruct { one: 0, two: Empty::Foo };
387+
}
388+
"#,
389+
);
390+
391+
// make sure the assist doesn't fill non Unit variants
392+
check_fix(
393+
r#"
394+
struct Empty {};
395+
396+
struct TestStruct { one: i32, two: Empty }
397+
398+
fn test_fn() {
399+
let s = TestStruct {$0};
400+
}
401+
"#,
402+
r#"
403+
struct Empty {};
404+
405+
struct TestStruct { one: i32, two: Empty }
406+
407+
fn test_fn() {
408+
let s = TestStruct { one: 0, two: todo!() };
409+
}
410+
"#,
411+
);
412+
check_fix(
413+
r#"
414+
enum Empty { Foo {} };
415+
416+
struct TestStruct { one: i32, two: Empty }
417+
418+
fn test_fn() {
419+
let s = TestStruct {$0};
420+
}
421+
"#,
422+
r#"
423+
enum Empty { Foo {} };
424+
425+
struct TestStruct { one: i32, two: Empty }
426+
427+
fn test_fn() {
428+
let s = TestStruct { one: 0, two: todo!() };
429+
}
430+
"#,
431+
);
432+
}
433+
348434
#[test]
349435
fn test_fill_struct_fields_self() {
350436
check_fix(

0 commit comments

Comments
 (0)