add tests

This commit is contained in:
soruh 2022-07-13 16:16:48 +02:00
parent 817082cad6
commit a5ad4de111
2 changed files with 177 additions and 0 deletions

View file

@ -123,6 +123,97 @@ mod tests {
use super::*;
#[test]
fn test_generate_new_with_zst_fields() {
check_assist(
generate_new,
r#"
struct Empty;
struct Foo { empty: Empty $0}
"#,
r#"
struct Empty;
struct Foo { empty: Empty }
impl Foo {
fn $0new() -> Self { Self { empty: Empty } }
}
"#,
);
check_assist(
generate_new,
r#"
struct Empty;
struct Foo { baz: String, empty: Empty $0}
"#,
r#"
struct Empty;
struct Foo { baz: String, empty: Empty }
impl Foo {
fn $0new(baz: String) -> Self { Self { baz, empty: Empty } }
}
"#,
);
check_assist(
generate_new,
r#"
enum Empty { Bar }
struct Foo { empty: Empty $0}
"#,
r#"
enum Empty { Bar }
struct Foo { empty: Empty }
impl Foo {
fn $0new() -> Self { Self { empty: Empty::Bar } }
}
"#,
);
// make sure the assist only works on unit variants
check_assist(
generate_new,
r#"
struct Empty {}
struct Foo { empty: Empty $0}
"#,
r#"
struct Empty {}
struct Foo { empty: Empty }
impl Foo {
fn $0new(empty: Empty) -> Self { Self { empty } }
}
"#,
);
check_assist(
generate_new,
r#"
enum Empty { Bar {} }
struct Foo { empty: Empty $0}
"#,
r#"
enum Empty { Bar {} }
struct Foo { empty: Empty }
impl Foo {
fn $0new(empty: Empty) -> Self { Self { empty } }
}
"#,
);
}
#[test]
fn test_generate_new() {
check_assist(

View file

@ -345,6 +345,92 @@ fn test_fn() {
);
}
#[test]
fn test_fill_struct_zst_fields() {
check_fix(
r#"
struct Empty;
struct TestStruct { one: i32, two: Empty }
fn test_fn() {
let s = TestStruct {$0};
}
"#,
r#"
struct Empty;
struct TestStruct { one: i32, two: Empty }
fn test_fn() {
let s = TestStruct { one: 0, two: Empty };
}
"#,
);
check_fix(
r#"
enum Empty { Foo };
struct TestStruct { one: i32, two: Empty }
fn test_fn() {
let s = TestStruct {$0};
}
"#,
r#"
enum Empty { Foo };
struct TestStruct { one: i32, two: Empty }
fn test_fn() {
let s = TestStruct { one: 0, two: Empty::Foo };
}
"#,
);
// make sure the assist doesn't fill non Unit variants
check_fix(
r#"
struct Empty {};
struct TestStruct { one: i32, two: Empty }
fn test_fn() {
let s = TestStruct {$0};
}
"#,
r#"
struct Empty {};
struct TestStruct { one: i32, two: Empty }
fn test_fn() {
let s = TestStruct { one: 0, two: todo!() };
}
"#,
);
check_fix(
r#"
enum Empty { Foo {} };
struct TestStruct { one: i32, two: Empty }
fn test_fn() {
let s = TestStruct {$0};
}
"#,
r#"
enum Empty { Foo {} };
struct TestStruct { one: i32, two: Empty }
fn test_fn() {
let s = TestStruct { one: 0, two: todo!() };
}
"#,
);
}
#[test]
fn test_fill_struct_fields_self() {
check_fix(