cfail tests for derive

This commit is contained in:
Steven Fackler 2019-10-10 15:21:45 -07:00
parent 218d889042
commit e69f1583c8
8 changed files with 101 additions and 4 deletions

View File

@ -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" }

View 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() {}

View 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),
| ^^^^^^^^

View 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() {}

View File

@ -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")]
| ^^^

View File

@ -25,3 +25,8 @@ where
assert_eq!(val, &result);
}
}
#[test]
fn compile_fail() {
trybuild::TestCases::new().compile_fail("src/compile-fail/*.rs");
}

View File

@ -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",
))
}
};

View File

@ -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()
}