1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-02-22 20:52:27 +00:00

add trybuild tests for OpenapiType and Resource derive macros

This commit is contained in:
Dominic 2020-05-09 15:29:29 +02:00
parent 9ed24c9bcb
commit 6680887b84
Signed by: msrd0
GPG key ID: DCC8C247452E98F9
16 changed files with 149 additions and 3 deletions

View file

@ -108,7 +108,7 @@ fn expand_variant(variant : &Variant) -> Result<TokenStream>
{
if variant.fields != Fields::Unit
{
return Err(Error::new(variant.span(), "Enum Variants with Fields not supported"));
return Err(Error::new(variant.span(), "#[derive(OpenapiType)] does not support enum variants with fields"));
}
let ident = &variant.ident;
@ -173,7 +173,7 @@ fn expand_field(field : &Field) -> Result<TokenStream>
{
let ident = match &field.ident {
Some(ident) => ident,
None => return Err(Error::new(field.span(), "Fields without ident are not supported"))
None => return Err(Error::new(field.span(), "#[derive(OpenapiType)] does not support fields without an ident"))
};
let ty = &field.ty;
@ -242,7 +242,7 @@ fn expand_struct(ident : Ident, generics : Generics, attrs : Vec<Attribute>, inp
.map(expand_field)
.collect_to_result()?
},
Fields::Unnamed(fields) => return Err(Error::new(fields.span(), "Unnamed fields are not supported")),
Fields::Unnamed(fields) => return Err(Error::new(fields.span(), "#[derive(OpenapiType)] does not support unnamed fields")),
Fields::Unit => Vec::new()
};

View file

@ -15,4 +15,16 @@ fn trybuild_ui()
t.compile_fail("tests/ui/method_too_few_args.rs");
t.compile_fail("tests/ui/method_too_many_args.rs");
t.compile_fail("tests/ui/method_unsafe.rs");
t.compile_fail("tests/ui/resource_unknown_method.rs");
// require the openapi feature
if cfg!(feature = "openapi")
{
t.compile_fail("tests/ui/openapi_type_enum_with_fields.rs");
t.compile_fail("tests/ui/openapi_type_nullable_non_bool.rs");
t.compile_fail("tests/ui/openapi_type_rename_non_string.rs");
t.compile_fail("tests/ui/openapi_type_tuple_struct.rs");
t.compile_fail("tests/ui/openapi_type_union.rs");
t.compile_fail("tests/ui/openapi_type_unknown_key.rs");
}
}

View file

@ -0,0 +1,14 @@
#[macro_use] extern crate gotham_restful;
#[derive(OpenapiType)]
enum Food
{
Pasta,
Pizza { pineapple : bool },
Rice,
Other(String)
}
fn main()
{
}

View file

@ -0,0 +1,11 @@
error: #[derive(OpenapiType)] does not support enum variants with fields
--> $DIR/openapi_type_enum_with_fields.rs:7:2
|
7 | Pizza { pineapple : bool },
| ^^^^^
error: #[derive(OpenapiType)] does not support enum variants with fields
--> $DIR/openapi_type_enum_with_fields.rs:9:2
|
9 | Other(String)
| ^^^^^

View file

@ -0,0 +1,12 @@
#[macro_use] extern crate gotham_restful;
#[derive(OpenapiType)]
struct Foo
{
#[openapi(nullable = "yes, please")]
bar : String
}
fn main()
{
}

View file

@ -0,0 +1,5 @@
error: Expected bool
--> $DIR/openapi_type_nullable_non_bool.rs:6:23
|
6 | #[openapi(nullable = "yes, please")]
| ^^^^^^^^^^^^^

View file

@ -0,0 +1,12 @@
#[macro_use] extern crate gotham_restful;
#[derive(OpenapiType)]
struct Foo
{
#[openapi(rename = 42)]
bar : String
}
fn main()
{
}

View file

@ -0,0 +1,5 @@
error: Expected string literal
--> $DIR/openapi_type_rename_non_string.rs:6:21
|
6 | #[openapi(rename = 42)]
| ^^

View file

@ -0,0 +1,8 @@
#[macro_use] extern crate gotham_restful;
#[derive(OpenapiType)]
struct Foo(String);
fn main()
{
}

View file

@ -0,0 +1,5 @@
error: #[derive(OpenapiType)] does not support unnamed fields
--> $DIR/openapi_type_tuple_struct.rs:4:11
|
4 | struct Foo(String);
| ^^^^^^^^

View file

@ -0,0 +1,12 @@
#[macro_use] extern crate gotham_restful;
#[derive(OpenapiType)]
union IntOrPointer
{
int: u64,
pointer: *mut String
}
fn main()
{
}

View file

@ -0,0 +1,5 @@
error: #[derive(OpenapiType)] only works for structs and enums
--> $DIR/openapi_type_union.rs:4:1
|
4 | union IntOrPointer
| ^^^^^

View file

@ -0,0 +1,12 @@
#[macro_use] extern crate gotham_restful;
#[derive(OpenapiType)]
struct Foo
{
#[openapi(like = "pizza")]
bar : String
}
fn main()
{
}

View file

@ -0,0 +1,5 @@
error: Unknown key
--> $DIR/openapi_type_unknown_key.rs:6:12
|
6 | #[openapi(like = "pizza")]
| ^^^^

View file

@ -0,0 +1,14 @@
#[macro_use] extern crate gotham_restful;
#[derive(Resource)]
#[resource(read_any)]
struct FooResource;
#[read_all(FooResource)]
fn read_all()
{
}
fn main()
{
}

View file

@ -0,0 +1,14 @@
error: Unknown method: `read_any'
--> $DIR/resource_unknown_method.rs:4:12
|
4 | #[resource(read_any)]
| ^^^^^^^^
error[E0277]: the trait bound `FooResource: gotham_restful::Resource` is not satisfied
--> $DIR/resource_unknown_method.rs:7:1
|
7 | #[read_all(FooResource)]
| ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `gotham_restful::Resource` is not implemented for `FooResource`
|
= help: see issue #48214
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)