Use pretty_assertions in tests

This commit is contained in:
Graham Esau 2019-08-06 08:55:25 +01:00
parent 70c8d0f763
commit 933eb1ac40
2 changed files with 29 additions and 30 deletions

View file

@ -10,3 +10,6 @@ edition = "2018"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
schemars_derive = { version = "0.1.0", path = "../schemars_derive" } schemars_derive = { version = "0.1.0", path = "../schemars_derive" }
[dev-dependencies]
pretty_assertions = "0.6.1"

View file

@ -1,26 +1,23 @@
use pretty_assertions::assert_eq;
use schemars::schema::*; use schemars::schema::*;
use schemars::{gen, schema_for}; use schemars::{gen, schema_for};
use serde_json::{from_str, to_string_pretty}; use serde_json::{from_str, to_string_pretty};
use std::fs; use std::fs;
#[cfg(test)] #[test]
mod tests { fn schema_matches_default_settings() -> Result<(), Box<dyn std::error::Error>> {
use super::*;
#[test]
fn schema_matches_default_settings() -> Result<(), Box<dyn std::error::Error>> {
let expected_json = fs::read_to_string("tests/schema.json")?; let expected_json = fs::read_to_string("tests/schema.json")?;
let expected: Schema = from_str(&expected_json)?; let expected: Schema = from_str(&expected_json)?;
let actual = schema_for!(Schema); let actual = schema_for!(Schema);
fs::write("tests/schema.actual.json", to_string_pretty(&actual)?)?; fs::write("tests/schema.actual.json", to_string_pretty(&actual)?)?;
assert_eq!(actual, expected, "\n\nGenerated schema did not match saved schema - generated schema has been written to \"tests/schema.actual.json\"."); assert_eq!(actual, expected, "Generated schema did not match saved schema - generated schema has been written to \"tests/schema.actual.json\".");
Ok(()) Ok(())
} }
#[test] #[test]
fn schema_matches_openapi3() -> Result<(), Box<dyn std::error::Error>> { fn schema_matches_openapi3() -> Result<(), Box<dyn std::error::Error>> {
let expected_json = fs::read_to_string("tests/schema-openapi3.json")?; let expected_json = fs::read_to_string("tests/schema-openapi3.json")?;
let expected: Schema = from_str(&expected_json)?; let expected: Schema = from_str(&expected_json)?;
@ -32,7 +29,6 @@ mod tests {
to_string_pretty(&actual)?, to_string_pretty(&actual)?,
)?; )?;
assert_eq!(actual, expected, "\n\nGenerated schema did not match saved schema - generated schema has been written to \"tests/schema-openapi3.actual.json\"."); assert_eq!(actual, expected, "Generated schema did not match saved schema - generated schema has been written to \"tests/schema-openapi3.actual.json\".");
Ok(()) Ok(())
}
} }