Skip to content

Commit

Permalink
feat: Ensure that tests are validating the boolean value returned by …
Browse files Browse the repository at this point in the history
…the rule-processing-methods
  • Loading branch information
macisamuele committed Jun 25, 2020
1 parent 460baf2 commit 049691a
Show file tree
Hide file tree
Showing 11 changed files with 279 additions and 293 deletions.
6 changes: 3 additions & 3 deletions src/helpers/replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ pub(crate) fn type_with(
}
Entry::Occupied(mut entry) => {
if let Some(json_primitive_types) = primitive_types.to_schema_value() {
let previous_value = entry.insert(json_primitive_types);
primitive_types != PrimitiveTypesBitMap::from_schema_value(Some(&previous_value))
let previous_value = entry.insert(json_primitive_types.clone());
previous_value != json_primitive_types
} else {
let _ = entry.remove();
true
Expand Down Expand Up @@ -112,7 +112,7 @@ mod tests {
#[test_case(json!({}), bit_map!(PrimitiveType::Boolean), true => json!({"type": "boolean"}))]
#[test_case(json!({"type": "null"}), bit_map!(PrimitiveType::Null), false => json!({"type": "null"}))]
#[test_case(json!({"type": "object"}), bit_map!(PrimitiveType::Null, PrimitiveType::Object), true => json!({"type": ["null", "object"]}))]
#[test_case(json!({"type": ["string", "object"]}), bit_map!(PrimitiveType::Object, PrimitiveType::String), false => json!({"type": ["object", "string"]}))]
#[test_case(json!({"type": ["string", "object"]}), bit_map!(PrimitiveType::Object, PrimitiveType::String), true => json!({"type": ["object", "string"]}))]
#[test_case(json!({"type": "number"}), bit_map!(PrimitiveType::Integer), true => json!({"type": "integer"}))]
// All primitive types case
#[test_case(json!({}), bit_map!(PrimitiveType::Array, PrimitiveType::Boolean, PrimitiveType::Integer, PrimitiveType::Null, PrimitiveType::Number, PrimitiveType::Object, PrimitiveType::String), false => json!({}))]
Expand Down
14 changes: 6 additions & 8 deletions src/keywords/additional_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ mod tests {
use serde_json::{json, Value};
use test_case::test_case;

#[test_case(json!({}) => json!({}))]
#[test_case(json!({"additionalProperties": true}) => json!({}))]
#[test_case(json!({"additionalProperties": {}}) => json!({}))]
#[test_case(json!({"additionalProperties": false}) => json!({"additionalProperties": false}))]
fn test_remove_empty_additional_properties(mut schema: Value) -> Value {
crate::init_logger();
let _ = remove_empty_additional_properties(&mut schema);
schema
#[test_case(&json!({}) => json!({}))]
#[test_case(&json!({"additionalProperties": true}) => json!({}))]
#[test_case(&json!({"additionalProperties": {}}) => json!({}))]
#[test_case(&json!({"additionalProperties": false}) => json!({"additionalProperties": false}))]
fn test_remove_empty_additional_properties(schema: &Value) -> Value {
crate::base_test_keyword_processor(&remove_empty_additional_properties, schema)
}
}
28 changes: 13 additions & 15 deletions src/keywords/const_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub(crate) fn simple_const_cleanup(schema: &mut Value) -> bool {
return false;
}

let const_primitive_type = dbg![PrimitiveType::from_serde_value(dbg![const_value])];
let const_primitive_type = PrimitiveType::from_serde_value(const_value);
if schema_primitive_types.contains(const_primitive_type) {
replace::type_with(
schema_object,
Expand Down Expand Up @@ -53,19 +53,17 @@ mod tests {
use serde_json::{json, Value};
use test_case::test_case;

#[test_case(json!({}) => json!({}))]
#[test_case(json!({"const": []}) => json!({"const": [], "type": "array"}))]
#[test_case(json!({"const": 1}) => json!({"const": 1, "type": "number"}))]
#[test_case(json!({"const": true, "type": "boolean"}) => json!({"const": true, "type": "boolean"}))]
#[test_case(json!({"const": "string", "type": "boolean"}) => json!(false))]
#[test_case(json!({"const": "some-text", "type": ["boolean", "string"]}) => json!({"const": "some-text", "type": "string"}))]
#[test_case(json!({"const": 1, "type": "integer"}) => json!({"const": 1, "type": "integer"}))]
#[test_case(json!({"const": 1, "type": "number"}) => json!({"const": 1, "type": "number"}))]
#[test_case(json!({"const": 1, "type": ["array", "integer"]}) => json!({"const": 1, "type": "integer"}))]
#[test_case(json!({"const": 1, "type": ["array", "number"]}) => json!({"const": 1, "type": "number"}))]
fn test_simple_const_cleanup(mut schema: Value) -> Value {
crate::init_logger();
let _ = simple_const_cleanup(&mut schema);
schema
#[test_case(&json!({}) => json!({}))]
#[test_case(&json!({"const": []}) => json!({"const": [], "type": "array"}))]
#[test_case(&json!({"const": 1}) => json!({"const": 1, "type": "number"}))]
#[test_case(&json!({"const": true, "type": "boolean"}) => json!({"const": true, "type": "boolean"}))]
#[test_case(&json!({"const": "string", "type": "boolean"}) => json!(false))]
#[test_case(&json!({"const": "some-text", "type": ["boolean", "string"]}) => json!({"const": "some-text", "type": "string"}))]
#[test_case(&json!({"const": 1, "type": "integer"}) => json!({"const": 1, "type": "integer"}))]
#[test_case(&json!({"const": 1, "type": "number"}) => json!({"const": 1, "type": "number"}))]
#[test_case(&json!({"const": 1, "type": ["array", "integer"]}) => json!({"const": 1, "type": "integer"}))]
#[test_case(&json!({"const": 1, "type": ["array", "number"]}) => json!({"const": 1, "type": "number"}))]
fn test_simple_const_cleanup(schema: &Value) -> Value {
crate::base_test_keyword_processor(&simple_const_cleanup, schema)
}
}
16 changes: 7 additions & 9 deletions src/keywords/enum_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,12 @@ mod tests {
use serde_json::{json, Value};
use test_case::test_case;

#[test_case(json!({}) => json!({}))]
#[test_case(json!({"enum": []}) => json!({"enum": []}))]
#[test_case(json!({"enum": [1], "type": "string"}) => json!(false))]
#[test_case(json!({"enum": ["0", "1", 2], "type": "string"}) => json!({"enum": ["0", "1"], "type": "string"}))]
#[test_case(json!({"enum": [3, 4, 5], "type": "string"}) => json!(false))]
fn test_remove_extraneous_keys_keyword_type_does_remove_keys(mut schema: Value) -> Value {
crate::init_logger();
let _ = simple_enum_cleanup(&mut schema);
schema
#[test_case(&json!({}) => json!({}))]
#[test_case(&json!({"enum": []}) => json!({"enum": []}))]
#[test_case(&json!({"enum": [1], "type": "string"}) => json!(false))]
#[test_case(&json!({"enum": ["0", "1", 2], "type": "string"}) => json!({"enum": ["0", "1"], "type": "string"}))]
#[test_case(&json!({"enum": [3, 4, 5], "type": "string"}) => json!(false))]
fn test_remove_extraneous_keys_keyword_type_does_remove_keys(schema: &Value) -> Value {
crate::base_test_keyword_processor(&simple_enum_cleanup, schema)
}
}
19 changes: 9 additions & 10 deletions src/keywords/if_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,14 @@ mod tests {
use serde_json::{json, Value};
use test_case::test_case;

#[test_case(json!({}) => json!({}))]
#[test_case(json!({"if": false, "then": {"minLength": 0}, "else": {"maxLength": 0}}) => json!({"allOf": [{"maxLength": 0}]}))]
#[test_case(json!({"if": true, "then": {"minLength": 0}, "else": {"maxLength": 0}}) => json!({"allOf": [{"minLength": 0}]}))]
#[test_case(json!({"if": {"type": "string"}, "then": {"minLength": 0}, "else": {"maxLength": 0}}) => json!({"if": {"type": "string"}, "then": {"minLength": 0}, "else": {"maxLength": 0}}))]
#[test_case(json!({"if": false}) => json!({}))]
#[test_case(json!({"if": true}) => json!({}))]
#[test_case(json!({"if": {"type": "string"}}) => json!({}))]
fn test_simplify_if(mut value: Value) -> Value {
let _ = simplify_if(&mut value);
value
#[test_case(&json!({}) => json!({}))]
#[test_case(&json!({"if": false, "then": {"minLength": 0}, "else": {"maxLength": 0}}) => json!({"allOf": [{"maxLength": 0}]}))]
#[test_case(&json!({"if": true, "then": {"minLength": 0}, "else": {"maxLength": 0}}) => json!({"allOf": [{"minLength": 0}]}))]
#[test_case(&json!({"if": {"type": "string"}, "then": {"minLength": 0}, "else": {"maxLength": 0}}) => json!({"if": {"type": "string"}, "then": {"minLength": 0}, "else": {"maxLength": 0}}))]
#[test_case(&json!({"if": false}) => json!({}))]
#[test_case(&json!({"if": true}) => json!({}))]
#[test_case(&json!({"if": {"type": "string"}}) => json!({}))]
fn test_simplify_if(schema: &Value) -> Value {
crate::base_test_keyword_processor(&simplify_if, schema)
}
}
19 changes: 9 additions & 10 deletions src/keywords/macro_/ignore_keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,14 @@ mod tests {
use serde_json::{json, Value};
use test_case::test_case;

#[test_case(json!({}) => json!({}))]
#[test_case(json!({"additionalItems": true, "items": true}) => json!({"additionalItems": true, "items": true}))]
#[test_case(json!({"additionalItems": true}) => json!({}))]
#[test_case(json!({"else": true, "if": true}) => json!({"else": true, "if": true}))]
#[test_case(json!({"else": true}) => json!({}))]
#[test_case(json!({"then": true, "if": true}) => json!({"then": true, "if": true}))]
#[test_case(json!({"then": true}) => json!({}))]
fn test_remove_keywords_in_must_ignore_groups(mut value: Value) -> Value {
let _ = remove_keywords_in_must_ignore_groups(&mut value);
value
#[test_case(&json!({}) => json!({}))]
#[test_case(&json!({"additionalItems": true, "items": true}) => json!({"additionalItems": true, "items": true}))]
#[test_case(&json!({"additionalItems": true}) => json!({}))]
#[test_case(&json!({"else": true, "if": true}) => json!({"else": true, "if": true}))]
#[test_case(&json!({"else": true}) => json!({}))]
#[test_case(&json!({"then": true, "if": true}) => json!({"then": true, "if": true}))]
#[test_case(&json!({"then": true}) => json!({}))]
fn test_remove_keywords_in_must_ignore_groups(value: &Value) -> Value {
crate::base_test_keyword_processor(&remove_keywords_in_must_ignore_groups, value)
}
}
Loading

0 comments on commit 049691a

Please sign in to comment.