diff --git a/schemars/.gitignore b/schemars/.gitignore index 67d434b..ccbedc4 100644 --- a/schemars/.gitignore +++ b/schemars/.gitignore @@ -1,4 +1,5 @@ /target **/*.rs.bk Cargo.lock -/tests/*.actual.* \ No newline at end of file +/tests/actual/*.json +/tests/expected/README.md \ No newline at end of file diff --git a/schemars/tests/actual/README.md b/schemars/tests/actual/README.md new file mode 100644 index 0000000..9fbbbab --- /dev/null +++ b/schemars/tests/actual/README.md @@ -0,0 +1,3 @@ +# Actual Generated Schemas + +If a test fails because a generated schema did not match the expected JSON, then the actual schema will be written to a JSON file in this directory. \ No newline at end of file diff --git a/schemars/tests/expected/naming-custom.json b/schemars/tests/expected/naming-custom.json new file mode 100644 index 0000000..0ee1e29 --- /dev/null +++ b/schemars/tests/expected/naming-custom.json @@ -0,0 +1,30 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "a-new-name-Array_Of_String-Integer-Integer", + "type": "object", + "properties": { + "inner": { + "$ref": "#/definitions/MySimpleStruct" + }, + "t": { + "type": "integer" + }, + "u": { + "type": "null" + }, + "v": { + "type": "boolean" + }, + "w": { + "type": "array", + "items": { + "type": "string" + } + } + }, + "definitions": { + "another-new-name": { + "type": "object" + } + } +} \ No newline at end of file diff --git a/schemars/tests/expected/naming-default.json b/schemars/tests/expected/naming-default.json new file mode 100644 index 0000000..05c5763 --- /dev/null +++ b/schemars/tests/expected/naming-default.json @@ -0,0 +1,30 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "MyStruct_For_Integer_And_Null_And_Boolean_And_Array_Of_String", + "type": "object", + "properties": { + "inner": { + "$ref": "#/definitions/MySimpleStruct" + }, + "t": { + "type": "integer" + }, + "u": { + "type": "null" + }, + "v": { + "type": "boolean" + }, + "w": { + "type": "array", + "items": { + "type": "string" + } + } + }, + "definitions": { + "MySimpleStruct": { + "type": "object" + } + } +} \ No newline at end of file diff --git a/schemars/tests/schema-openapi3.json b/schemars/tests/expected/schema-openapi3.json similarity index 100% rename from schemars/tests/schema-openapi3.json rename to schemars/tests/expected/schema-openapi3.json diff --git a/schemars/tests/schema.json b/schemars/tests/expected/schema.json similarity index 100% rename from schemars/tests/schema.json rename to schemars/tests/expected/schema.json diff --git a/schemars/tests/metaschema.rs b/schemars/tests/metaschema.rs deleted file mode 100644 index 697b47f..0000000 --- a/schemars/tests/metaschema.rs +++ /dev/null @@ -1,35 +0,0 @@ -use pretty_assertions::assert_eq; -use schemars::schema::*; -use schemars::{gen, schema_for}; -use serde_json::{from_str, to_string_pretty}; -use std::fs; -use std::error::Error; - -#[test] -fn schema_matches_default_settings() -> Result<(), Box> { - let expected_json = fs::read_to_string("tests/schema.json")?; - let expected: Schema = from_str(&expected_json)?; - - let actual = schema_for!(Schema)?; - fs::write("tests/schema.actual.json", to_string_pretty(&actual)?)?; - - assert_eq!(actual, expected, "Generated schema did not match saved schema - generated schema has been written to \"tests/schema.actual.json\"."); - Ok(()) -} - -#[test] -fn schema_matches_openapi3() -> Result<(), Box> { - let expected_json = fs::read_to_string("tests/schema-openapi3.json")?; - let expected: Schema = from_str(&expected_json)?; - - let actual = gen::SchemaSettings::openapi3() - .into_generator() - .into_root_schema_for::()?; - fs::write( - "tests/schema-openapi3.actual.json", - to_string_pretty(&actual)?, - )?; - - assert_eq!(actual, expected, "Generated schema did not match saved schema - generated schema has been written to \"tests/schema-openapi3.actual.json\"."); - Ok(()) -} diff --git a/schemars/tests/naming.rs b/schemars/tests/naming.rs index 91a52cb..f32e358 100644 --- a/schemars/tests/naming.rs +++ b/schemars/tests/naming.rs @@ -1,87 +1,41 @@ -use pretty_assertions::assert_eq; -use schemars::{schema_for, MakeSchema}; -use serde::{Deserialize, Serialize}; -use std::error::Error; +use schemars::MakeSchema; -const EXPECTED: &str = r#" -{ - "$schema": "http://json-schema.org/draft-07/schema#", - "title": "MyStruct_For_Integer_And_Null_And_Boolean_And_Array_Of_String", - "type": "object", - "properties": { - "float": { - "type": "number" - }, - "t": { - "type": "integer" - }, - "u": { - "type": "null" - }, - "v": { - "type": "boolean" - }, - "w": { - "type": "array", - "items": { - "type": "string" - } - } - } -} -"#; +mod util; +use util::*; -#[derive(Serialize, Deserialize, Debug, PartialEq, MakeSchema)] -struct MyStruct { - t: T, - u: U, - float: f32, - v: V, - w: W, +#[derive(MakeSchema)] +pub struct MyStruct { + pub t: T, + pub u: U, + pub v: V, + pub w: W, + pub inner: MySimpleStruct, } -#[derive(Serialize, Deserialize, Debug, PartialEq, MakeSchema)] -#[serde(rename = "MyStruct")] -struct MyRenamedStruct { - t: T, - u: U, - float: f32, - v: V, - w: W, -} - -#[derive(Serialize, Deserialize, Debug, PartialEq, MakeSchema)] -#[serde(remote = "MyStruct")] -struct MyRemoteStruct { - t: T, - u: U, - float: f32, - v: V, - w: W, -} +#[derive(MakeSchema)] +pub struct MySimpleStruct {} #[test] -fn default_name_multiple_type_params() -> Result<(), Box> { - let actual = schema_for!(MyStruct>)?; - let expected = serde_json::from_str(EXPECTED)?; - assert_eq!(actual, expected); - Ok(()) +fn default_name_multiple_type_params() -> TestResult { + test_default_generated_schema::>>("naming-default") } +#[derive(MakeSchema)] +#[serde(rename = "a-new-name---")] +pub struct MyRenamedStruct { + pub t: T, + pub u: U, + pub v: V, + pub w: W, + pub inner: MySimpleRenamedStruct, +} + +#[derive(MakeSchema)] +#[serde(rename = "another-new-name")] +pub struct MySimpleRenamedStruct {} + #[test] #[ignore] // not yet implemented -fn overriden_with_rename_name_multiple_type_params() -> Result<(), Box> { - let actual = schema_for!(MyRenamedStruct>)?; - let expected = serde_json::from_str(EXPECTED)?; - assert_eq!(actual, expected); - Ok(()) -} - -#[test] -#[ignore] // not yet implemented -fn overriden_with_remote_name_multiple_type_params() -> Result<(), Box> { - let actual = schema_for!(MyRemoteStruct>)?; - let expected = serde_json::from_str(EXPECTED)?; - assert_eq!(actual, expected); - Ok(()) +fn overriden_with_rename_name_multiple_type_params() -> TestResult { + test_default_generated_schema::>>("naming-custom") } diff --git a/schemars/tests/schema_for_schema.rs b/schemars/tests/schema_for_schema.rs new file mode 100644 index 0000000..3a7e015 --- /dev/null +++ b/schemars/tests/schema_for_schema.rs @@ -0,0 +1,15 @@ +use schemars::gen::SchemaSettings; +use schemars::schema::Schema; + +mod util; +use util::*; + +#[test] +fn schema_matches_default_settings() -> TestResult { + test_default_generated_schema::("schema") +} + +#[test] +fn schema_matches_openapi3() -> TestResult { + test_generated_schema::("schema-openapi3", SchemaSettings::openapi3()) +} diff --git a/schemars/tests/util.rs b/schemars/tests/util.rs new file mode 100644 index 0000000..57f3046 --- /dev/null +++ b/schemars/tests/util.rs @@ -0,0 +1,37 @@ +use pretty_assertions::assert_eq; +use schemars::{gen::SchemaSettings, schema_for, MakeSchema}; +use std::error::Error; +use std::fs; +use std::panic; + +pub type TestResult = Result<(), Box>; + +pub fn test_default_generated_schema(file: &str) -> TestResult { + let expected_json = fs::read_to_string(format!("tests/expected/{}.json", file))?; + let expected = serde_json::from_str(&expected_json)?; + + let actual = schema_for!(T)?; + + if actual != expected { + let actual_json = serde_json::to_string_pretty(&actual)?; + fs::write(format!("tests/actual/{}.json", file), actual_json)?; + } + + assert_eq!(actual, expected); + Ok(()) +} + +pub fn test_generated_schema(file: &str, settings: SchemaSettings) -> TestResult { + let expected_json = fs::read_to_string(format!("tests/expected/{}.json", file))?; + let expected = serde_json::from_str(&expected_json)?; + + let actual = settings.into_generator().into_root_schema_for::()?; + + if actual != expected { + let actual_json = serde_json::to_string_pretty(&actual)?; + fs::write(format!("tests/actual/{}.json", file), actual_json)?; + } + + assert_eq!(actual, expected); + Ok(()) +}