mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-02-23 04:52:28 +00:00
add trybuild tests for OpenapiType and Resource derive macros
This commit is contained in:
parent
9ed24c9bcb
commit
6680887b84
16 changed files with 149 additions and 3 deletions
|
@ -108,7 +108,7 @@ fn expand_variant(variant : &Variant) -> Result<TokenStream>
|
||||||
{
|
{
|
||||||
if variant.fields != Fields::Unit
|
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;
|
let ident = &variant.ident;
|
||||||
|
@ -173,7 +173,7 @@ fn expand_field(field : &Field) -> Result<TokenStream>
|
||||||
{
|
{
|
||||||
let ident = match &field.ident {
|
let ident = match &field.ident {
|
||||||
Some(ident) => 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;
|
let ty = &field.ty;
|
||||||
|
|
||||||
|
@ -242,7 +242,7 @@ fn expand_struct(ident : Ident, generics : Generics, attrs : Vec<Attribute>, inp
|
||||||
.map(expand_field)
|
.map(expand_field)
|
||||||
.collect_to_result()?
|
.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()
|
Fields::Unit => Vec::new()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -15,4 +15,16 @@ fn trybuild_ui()
|
||||||
t.compile_fail("tests/ui/method_too_few_args.rs");
|
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_too_many_args.rs");
|
||||||
t.compile_fail("tests/ui/method_unsafe.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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
14
tests/ui/openapi_type_enum_with_fields.rs
Normal file
14
tests/ui/openapi_type_enum_with_fields.rs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
#[macro_use] extern crate gotham_restful;
|
||||||
|
|
||||||
|
#[derive(OpenapiType)]
|
||||||
|
enum Food
|
||||||
|
{
|
||||||
|
Pasta,
|
||||||
|
Pizza { pineapple : bool },
|
||||||
|
Rice,
|
||||||
|
Other(String)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main()
|
||||||
|
{
|
||||||
|
}
|
11
tests/ui/openapi_type_enum_with_fields.stderr
Normal file
11
tests/ui/openapi_type_enum_with_fields.stderr
Normal 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)
|
||||||
|
| ^^^^^
|
12
tests/ui/openapi_type_nullable_non_bool.rs
Normal file
12
tests/ui/openapi_type_nullable_non_bool.rs
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
#[macro_use] extern crate gotham_restful;
|
||||||
|
|
||||||
|
#[derive(OpenapiType)]
|
||||||
|
struct Foo
|
||||||
|
{
|
||||||
|
#[openapi(nullable = "yes, please")]
|
||||||
|
bar : String
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main()
|
||||||
|
{
|
||||||
|
}
|
5
tests/ui/openapi_type_nullable_non_bool.stderr
Normal file
5
tests/ui/openapi_type_nullable_non_bool.stderr
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
error: Expected bool
|
||||||
|
--> $DIR/openapi_type_nullable_non_bool.rs:6:23
|
||||||
|
|
|
||||||
|
6 | #[openapi(nullable = "yes, please")]
|
||||||
|
| ^^^^^^^^^^^^^
|
12
tests/ui/openapi_type_rename_non_string.rs
Normal file
12
tests/ui/openapi_type_rename_non_string.rs
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
#[macro_use] extern crate gotham_restful;
|
||||||
|
|
||||||
|
#[derive(OpenapiType)]
|
||||||
|
struct Foo
|
||||||
|
{
|
||||||
|
#[openapi(rename = 42)]
|
||||||
|
bar : String
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main()
|
||||||
|
{
|
||||||
|
}
|
5
tests/ui/openapi_type_rename_non_string.stderr
Normal file
5
tests/ui/openapi_type_rename_non_string.stderr
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
error: Expected string literal
|
||||||
|
--> $DIR/openapi_type_rename_non_string.rs:6:21
|
||||||
|
|
|
||||||
|
6 | #[openapi(rename = 42)]
|
||||||
|
| ^^
|
8
tests/ui/openapi_type_tuple_struct.rs
Normal file
8
tests/ui/openapi_type_tuple_struct.rs
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
#[macro_use] extern crate gotham_restful;
|
||||||
|
|
||||||
|
#[derive(OpenapiType)]
|
||||||
|
struct Foo(String);
|
||||||
|
|
||||||
|
fn main()
|
||||||
|
{
|
||||||
|
}
|
5
tests/ui/openapi_type_tuple_struct.stderr
Normal file
5
tests/ui/openapi_type_tuple_struct.stderr
Normal 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);
|
||||||
|
| ^^^^^^^^
|
12
tests/ui/openapi_type_union.rs
Normal file
12
tests/ui/openapi_type_union.rs
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
#[macro_use] extern crate gotham_restful;
|
||||||
|
|
||||||
|
#[derive(OpenapiType)]
|
||||||
|
union IntOrPointer
|
||||||
|
{
|
||||||
|
int: u64,
|
||||||
|
pointer: *mut String
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main()
|
||||||
|
{
|
||||||
|
}
|
5
tests/ui/openapi_type_union.stderr
Normal file
5
tests/ui/openapi_type_union.stderr
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
error: #[derive(OpenapiType)] only works for structs and enums
|
||||||
|
--> $DIR/openapi_type_union.rs:4:1
|
||||||
|
|
|
||||||
|
4 | union IntOrPointer
|
||||||
|
| ^^^^^
|
12
tests/ui/openapi_type_unknown_key.rs
Normal file
12
tests/ui/openapi_type_unknown_key.rs
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
#[macro_use] extern crate gotham_restful;
|
||||||
|
|
||||||
|
#[derive(OpenapiType)]
|
||||||
|
struct Foo
|
||||||
|
{
|
||||||
|
#[openapi(like = "pizza")]
|
||||||
|
bar : String
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main()
|
||||||
|
{
|
||||||
|
}
|
5
tests/ui/openapi_type_unknown_key.stderr
Normal file
5
tests/ui/openapi_type_unknown_key.stderr
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
error: Unknown key
|
||||||
|
--> $DIR/openapi_type_unknown_key.rs:6:12
|
||||||
|
|
|
||||||
|
6 | #[openapi(like = "pizza")]
|
||||||
|
| ^^^^
|
14
tests/ui/resource_unknown_method.rs
Normal file
14
tests/ui/resource_unknown_method.rs
Normal 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()
|
||||||
|
{
|
||||||
|
}
|
14
tests/ui/resource_unknown_method.stderr
Normal file
14
tests/ui/resource_unknown_method.stderr
Normal 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)
|
Loading…
Add table
Reference in a new issue