Skip to content

Commit

Permalink
Port rename tests to new integration test style
Browse files Browse the repository at this point in the history
  • Loading branch information
GREsau committed Sep 15, 2024
1 parent e621bdb commit cd97305
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 249 deletions.
14 changes: 0 additions & 14 deletions schemars/tests/expected/schema-name-const-generics.json

This file was deleted.

47 changes: 0 additions & 47 deletions schemars/tests/expected/schema-name-custom.json

This file was deleted.

47 changes: 0 additions & 47 deletions schemars/tests/expected/schema-name-default.json

This file was deleted.

63 changes: 0 additions & 63 deletions schemars/tests/expected/schema-name-mixed-generics.json

This file was deleted.

1 change: 1 addition & 0 deletions schemars/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod inline_subschemas;
mod macros;
mod remote_derive;
mod same_name;
mod schema_name;
#[cfg(feature = "semver1")]
mod semver;
#[cfg(feature = "smallvec1")]
Expand Down
81 changes: 81 additions & 0 deletions schemars/tests/integration/schema_name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use crate::prelude::*;

#[derive(JsonSchema, Deserialize, Serialize, Default)]
struct SimpleStruct {
foo: i32,
}

#[derive(JsonSchema, Deserialize, Serialize, Default)]
#[schemars(rename = "new-name")]
struct RenamedSimpleStruct {
foo: i32,
}

#[test]
fn simple() {
test!(SimpleStruct)
.custom(|schema, _| assert_eq!(schema.get("title"), Some(&"SimpleStruct".into())));

test!(RenamedSimpleStruct)
.custom(|schema, _| assert_eq!(schema.get("title"), Some(&"new-name".into())));
}

#[derive(JsonSchema, Deserialize, Serialize, Default)]
struct TypeParams<T, U, V, W> {
t: T,
u: U,
v: V,
w: W,
}

#[derive(JsonSchema, Deserialize, Serialize, Default)]
#[schemars(rename = "new-name-{W}-{T}-{T}")]
struct RenamedTypeParams<T, U, V, W> {
t: T,
u: U,
v: V,
w: W,
}

#[test]
fn type_params() {
test!(TypeParams<u8, String, bool, ()>).custom(|schema, _| {
assert_eq!(
schema.get("title"),
Some(&"TypeParams_for_uint8_and_string_and_boolean_and_null".into())
)
});

test!(RenamedTypeParams<u8, String, bool, ()>).custom(|schema, _| {
assert_eq!(
schema.get("title"),
Some(&"new-name-null-uint8-uint8".into())
)
});
}

#[derive(JsonSchema, Deserialize, Serialize, Default)]
struct ConstGeneric<const INT: usize, const CHAR: char> {
#[schemars(range(max = INT))]
foo: i32,
}

#[derive(JsonSchema, Deserialize, Serialize, Default)]
#[schemars(rename = "new-name-{INT}")]
struct RenamedConstGeneric<const INT: usize, const CHAR: char> {
#[schemars(range(max = INT))]
foo: i32,
}

#[test]
fn const_generics() {
test!(ConstGeneric<123, 'X'>).custom(|schema, _| {
assert_eq!(
schema.get("title"),
Some(&"ConstGeneric_for_123_and_X".into())
)
});

test!(RenamedConstGeneric<123, 'X'>)
.custom(|schema, _| assert_eq!(schema.get("title"), Some(&"new-name-123".into())));
}
77 changes: 0 additions & 77 deletions schemars/tests/schema_name.rs

This file was deleted.

5 changes: 4 additions & 1 deletion schemars_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ fn derive_json_schema(mut input: syn::DeriveInput, repr: bool) -> syn::Result<To
(
quote! {
schemars::_private::alloc::borrow::Cow::Owned(
schemars::_private::alloc::format!(#schema_name_fmt #(,#type_params=#type_params::schema_name())* #(,#const_params=#const_params)*)
schemars::_private::alloc::format!(
#schema_name_fmt
#(,#type_params=#type_params::schema_name())*
#(,#const_params=schemars::_private::alloc::string::ToString::to_string(&#const_params))*)
)
},
quote! {
Expand Down

0 comments on commit cd97305

Please sign in to comment.