use pretty_assertions::assert_eq; use rusty_ulid::Ulid; use schemars::{schema::RootSchema, schema_for, JsonSchema}; use std::error::Error; use std::fs; #[test] fn ulid() -> TestResult { test_default_generated_schema::("ulid") } #[test] fn basics() { assert!(!Ulid::is_referenceable()); assert_eq!(Ulid::schema_name(), "Ulid"); } type TestResult = Result<(), Box>; #[allow(dead_code)] // https://github.com/rust-lang/rust/issues/46379 pub fn test_default_generated_schema(file: &str) -> TestResult { let actual = schema_for!(T); test_schema(&actual, file) } fn test_schema(actual: &RootSchema, file: &str) -> TestResult { let expected_json = match fs::read_to_string(format!("tests/expected/{file}.json")) { Ok(j) => j, Err(e) => { write_actual_to_file(actual, file)?; return Err(Box::from(e)); } }; let expected = &serde_json::from_str(&expected_json)?; if actual != expected { write_actual_to_file(actual, file)?; } assert_eq!(expected, actual); Ok(()) } fn write_actual_to_file(schema: &RootSchema, file: &str) -> TestResult { let actual_json = serde_json::to_string_pretty(&schema)?; fs::write(format!("tests/actual/{file}.json"), actual_json)?; Ok(()) }