From 3fcdc7266207da7943073928a89e7b6f202678b9 Mon Sep 17 00:00:00 2001 From: Graham Esau Date: Sat, 7 Sep 2019 23:23:01 +0100 Subject: [PATCH] Add unit tests for map JsonSchemas --- schemars/src/json_schema_impls/maps.rs | 68 ++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/schemars/src/json_schema_impls/maps.rs b/schemars/src/json_schema_impls/maps.rs index f1f376a..e823ad1 100644 --- a/schemars/src/json_schema_impls/maps.rs +++ b/schemars/src/json_schema_impls/maps.rs @@ -41,3 +41,71 @@ macro_rules! map_impl { map_impl!( JsonSchema for std::collections::BTreeMap); map_impl!( JsonSchema for std::collections::HashMap); + +#[cfg(test)] +mod tests { + use super::*; + use crate::gen::*; + use crate::tests::{custom_schema_object_for, schema_for}; + use pretty_assertions::assert_eq; + use std::collections::BTreeMap; + + #[test] + fn schema_for_map_any_value() { + for bool_schemas in &[BoolSchemas::Enable, BoolSchemas::AdditionalPropertiesOnly] { + let settings = SchemaSettings { + bool_schemas: *bool_schemas, + ..Default::default() + }; + let schema = custom_schema_object_for::>(settings); + assert_eq!( + schema.instance_type, + Some(SingleOrVec::from(InstanceType::Object)) + ); + assert_eq!( + schema.extensions.get("additionalProperties"), + Some(&json!(true)) + ); + } + } + + #[test] + fn schema_for_map_any_value_no_bool_schema() { + let settings = SchemaSettings { + bool_schemas: BoolSchemas::Disable, + ..Default::default() + }; + let schema = custom_schema_object_for::>(settings); + assert_eq!( + schema.instance_type, + Some(SingleOrVec::from(InstanceType::Object)) + ); + assert_eq!( + schema.extensions.get("additionalProperties"), + Some(&json!(Schema::Object(Default::default()))) + ); + } + + #[test] + fn schema_for_map_int_value() { + for bool_schemas in &[ + BoolSchemas::Enable, + BoolSchemas::Disable, + BoolSchemas::AdditionalPropertiesOnly, + ] { + let settings = SchemaSettings { + bool_schemas: *bool_schemas, + ..Default::default() + }; + let schema = custom_schema_object_for::>(settings); + assert_eq!( + schema.instance_type, + Some(SingleOrVec::from(InstanceType::Object)) + ); + assert_eq!( + schema.extensions.get("additionalProperties"), + Some(&json!(schema_for::())) + ); + } + } +}