cfail tests for derive
This commit is contained in:
parent
218d889042
commit
e69f1583c8
@ -4,6 +4,8 @@ version = "0.1.0"
|
||||
authors = ["Steven Fackler <sfackler@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
[dev-dependencies]
|
||||
trybuild = "1.0"
|
||||
|
||||
postgres-types = { path = "../postgres-types", features = ["derive"] }
|
||||
postgres = { path = "../postgres" }
|
||||
|
25
postgres-derive-test/src/compile-fail/invalid-types.rs
Normal file
25
postgres-derive-test/src/compile-fail/invalid-types.rs
Normal file
@ -0,0 +1,25 @@
|
||||
use postgres_types::{FromSql, ToSql};
|
||||
|
||||
#[derive(ToSql)]
|
||||
struct ToSqlUnit;
|
||||
|
||||
#[derive(FromSql)]
|
||||
struct FromSqlUnit;
|
||||
|
||||
#[derive(ToSql)]
|
||||
struct ToSqlTuple(i32, i32);
|
||||
|
||||
#[derive(FromSql)]
|
||||
struct FromSqlTuple(i32, i32);
|
||||
|
||||
#[derive(ToSql)]
|
||||
enum ToSqlEnum {
|
||||
Foo(i32),
|
||||
}
|
||||
|
||||
#[derive(FromSql)]
|
||||
enum FromSqlEnum {
|
||||
Foo(i32),
|
||||
}
|
||||
|
||||
fn main() {}
|
35
postgres-derive-test/src/compile-fail/invalid-types.stderr
Normal file
35
postgres-derive-test/src/compile-fail/invalid-types.stderr
Normal file
@ -0,0 +1,35 @@
|
||||
error: #[derive(ToSql)] may only be applied to structs, single field tuple structs, and enums
|
||||
--> $DIR/invalid-types.rs:4:1
|
||||
|
|
||||
4 | struct ToSqlUnit;
|
||||
| ^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: #[derive(FromSql)] may only be applied to structs, single field tuple structs, and enums
|
||||
--> $DIR/invalid-types.rs:7:1
|
||||
|
|
||||
7 | struct FromSqlUnit;
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: #[derive(ToSql)] may only be applied to structs, single field tuple structs, and enums
|
||||
--> $DIR/invalid-types.rs:10:1
|
||||
|
|
||||
10 | struct ToSqlTuple(i32, i32);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: #[derive(FromSql)] may only be applied to structs, single field tuple structs, and enums
|
||||
--> $DIR/invalid-types.rs:13:1
|
||||
|
|
||||
13 | struct FromSqlTuple(i32, i32);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: non-C-like enums are not supported
|
||||
--> $DIR/invalid-types.rs:17:5
|
||||
|
|
||||
17 | Foo(i32),
|
||||
| ^^^^^^^^
|
||||
|
||||
error: non-C-like enums are not supported
|
||||
--> $DIR/invalid-types.rs:22:5
|
||||
|
|
||||
22 | Foo(i32),
|
||||
| ^^^^^^^^
|
15
postgres-derive-test/src/compile-fail/unknown-override.rs
Normal file
15
postgres-derive-test/src/compile-fail/unknown-override.rs
Normal file
@ -0,0 +1,15 @@
|
||||
use postgres_types::{FromSql, ToSql};
|
||||
|
||||
#[derive(FromSql)]
|
||||
#[postgres(foo = "bar")]
|
||||
struct Foo {
|
||||
a: i32,
|
||||
}
|
||||
|
||||
#[derive(ToSql)]
|
||||
#[postgres(foo = "bar")]
|
||||
struct Bar {
|
||||
a: i32,
|
||||
}
|
||||
|
||||
fn main() {}
|
@ -0,0 +1,11 @@
|
||||
error: unknown override
|
||||
--> $DIR/unknown-override.rs:4:12
|
||||
|
|
||||
4 | #[postgres(foo = "bar")]
|
||||
| ^^^
|
||||
|
||||
error: unknown override
|
||||
--> $DIR/unknown-override.rs:10:12
|
||||
|
|
||||
10 | #[postgres(foo = "bar")]
|
||||
| ^^^
|
@ -25,3 +25,8 @@ where
|
||||
assert_eq!(val, &result);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compile_fail() {
|
||||
trybuild::TestCases::new().compile_fail("src/compile-fail/*.rs");
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ pub fn expand_derive_fromsql(input: DeriveInput) -> Result<TokenStream, Error> {
|
||||
_ => {
|
||||
return Err(Error::new_spanned(
|
||||
input,
|
||||
"#[derive(ToSql)] may only be applied to structs, single field tuple structs, and enums",
|
||||
"#[derive(FromSql)] may only be applied to structs, single field tuple structs, and enums",
|
||||
))
|
||||
}
|
||||
};
|
||||
|
@ -18,11 +18,15 @@ mod tosql;
|
||||
#[proc_macro_derive(ToSql, attributes(postgres))]
|
||||
pub fn derive_tosql(input: TokenStream) -> TokenStream {
|
||||
let input = syn::parse(input).unwrap();
|
||||
tosql::expand_derive_tosql(input).unwrap().into()
|
||||
tosql::expand_derive_tosql(input)
|
||||
.unwrap_or_else(|e| e.to_compile_error())
|
||||
.into()
|
||||
}
|
||||
|
||||
#[proc_macro_derive(FromSql, attributes(postgres))]
|
||||
pub fn derive_fromsql(input: TokenStream) -> TokenStream {
|
||||
let input = syn::parse(input).unwrap();
|
||||
fromsql::expand_derive_fromsql(input).unwrap().into()
|
||||
fromsql::expand_derive_fromsql(input)
|
||||
.unwrap_or_else(|e| e.to_compile_error())
|
||||
.into()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user