Skip to content

Commit 37865d1

Browse files
committed
refactor schemars related tests
1 parent ccf51f5 commit 37865d1

1 file changed

Lines changed: 22 additions & 73 deletions

File tree

src/lib.rs

Lines changed: 22 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2651,51 +2651,9 @@ mod impl_schemars {
26512651

26522652
#[test]
26532653
fn schema_generation_does_not_panic_for_common_floats() {
2654-
{
2655-
let schema = schemars::gen::SchemaGenerator::default()
2656-
.into_root_schema_for::<OrderedFloat<f32>>();
2657-
assert_eq!(
2658-
schema.schema.instance_type,
2659-
Some(schemars::schema::SingleOrVec::Single(std::boxed::Box::new(
2660-
schemars::schema::InstanceType::Number
2661-
)))
2662-
);
2663-
assert_eq!(
2664-
schema.schema.metadata.unwrap().title.unwrap(),
2665-
std::string::String::from("float")
2666-
);
2667-
}
2668-
{
2669-
let schema = schemars::gen::SchemaGenerator::default()
2670-
.into_root_schema_for::<OrderedFloat<f64>>();
2671-
assert_eq!(
2672-
schema.schema.instance_type,
2673-
Some(schemars::schema::SingleOrVec::Single(std::boxed::Box::new(
2674-
schemars::schema::InstanceType::Number
2675-
)))
2676-
);
2677-
assert_eq!(
2678-
schema.schema.metadata.unwrap().title.unwrap(),
2679-
std::string::String::from("double")
2680-
);
2681-
}
2682-
{
2683-
let schema =
2684-
schemars::gen::SchemaGenerator::default().into_root_schema_for::<NotNan<f32>>();
2685-
assert_eq!(
2686-
schema.schema.instance_type,
2687-
Some(schemars::schema::SingleOrVec::Single(std::boxed::Box::new(
2688-
schemars::schema::InstanceType::Number
2689-
)))
2690-
);
2691-
assert_eq!(
2692-
schema.schema.metadata.unwrap().title.unwrap(),
2693-
std::string::String::from("float")
2694-
);
2695-
}
2696-
{
2697-
let schema =
2698-
schemars::gen::SchemaGenerator::default().into_root_schema_for::<NotNan<f64>>();
2654+
fn test_schema_properties<T: schemars::JsonSchema>(title: &str) {
2655+
let schema = schemars::r#gen::SchemaGenerator::default().into_root_schema_for::<T>();
2656+
26992657
assert_eq!(
27002658
schema.schema.instance_type,
27012659
Some(schemars::schema::SingleOrVec::Single(std::boxed::Box::new(
@@ -2704,40 +2662,31 @@ mod impl_schemars {
27042662
);
27052663
assert_eq!(
27062664
schema.schema.metadata.unwrap().title.unwrap(),
2707-
std::string::String::from("double")
2665+
std::string::String::from(title)
27082666
);
27092667
}
2668+
2669+
test_schema_properties::<OrderedFloat<f32>>("float");
2670+
test_schema_properties::<OrderedFloat<f64>>("double");
2671+
test_schema_properties::<NotNan<f32>>("float");
2672+
test_schema_properties::<NotNan<f64>>("double");
27102673
}
2674+
27112675
#[test]
27122676
fn ordered_float_schema_match_primitive_schema() {
2713-
{
2714-
let of_schema = schemars::gen::SchemaGenerator::default()
2715-
.into_root_schema_for::<OrderedFloat<f32>>();
2716-
let prim_schema =
2717-
schemars::gen::SchemaGenerator::default().into_root_schema_for::<f32>();
2718-
assert_eq!(of_schema, prim_schema);
2719-
}
2720-
{
2721-
let of_schema = schemars::gen::SchemaGenerator::default()
2722-
.into_root_schema_for::<OrderedFloat<f64>>();
2723-
let prim_schema =
2724-
schemars::gen::SchemaGenerator::default().into_root_schema_for::<f64>();
2725-
assert_eq!(of_schema, prim_schema);
2726-
}
2727-
{
2728-
let of_schema =
2729-
schemars::gen::SchemaGenerator::default().into_root_schema_for::<NotNan<f32>>();
2730-
let prim_schema =
2731-
schemars::gen::SchemaGenerator::default().into_root_schema_for::<f32>();
2732-
assert_eq!(of_schema, prim_schema);
2733-
}
2734-
{
2735-
let of_schema =
2736-
schemars::gen::SchemaGenerator::default().into_root_schema_for::<NotNan<f64>>();
2737-
let prim_schema =
2738-
schemars::gen::SchemaGenerator::default().into_root_schema_for::<f64>();
2739-
assert_eq!(of_schema, prim_schema);
2677+
fn test_schema_eq<Wrapped: schemars::JsonSchema, Inner: schemars::JsonSchema>() {
2678+
let wrapped_schema =
2679+
schemars::r#gen::SchemaGenerator::default().into_root_schema_for::<Wrapped>();
2680+
let primitive_schema =
2681+
schemars::r#gen::SchemaGenerator::default().into_root_schema_for::<Inner>();
2682+
2683+
assert_eq!(wrapped_schema, primitive_schema);
27402684
}
2685+
2686+
test_schema_eq::<OrderedFloat<f32>, f32>();
2687+
test_schema_eq::<OrderedFloat<f64>, f64>();
2688+
test_schema_eq::<NotNan<f32>, f32>();
2689+
test_schema_eq::<NotNan<f64>, f64>();
27412690
}
27422691
}
27432692

0 commit comments

Comments
 (0)