Skip to content

Commit

Permalink
test: added maps_custom test case to schemas.
Browse files Browse the repository at this point in the history
  • Loading branch information
JosiahBull committed Dec 6, 2024
1 parent 85c55d0 commit 3046658
Show file tree
Hide file tree
Showing 2 changed files with 319 additions and 5 deletions.
26 changes: 21 additions & 5 deletions typify/tests/schemas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,33 @@ fn test_schemas() {
env_logger::init();
// Make sure output is up to date.
for entry in glob("tests/schemas/*.json").expect("Failed to read glob pattern") {
validate_schema(entry.unwrap()).unwrap();
let entry = entry.unwrap();
let out_path = entry.clone().with_extension("rs");
validate_schema(entry, out_path, &mut TypeSpaceSettings::default()).unwrap();
}

// Make sure it all compiles.
trybuild::TestCases::new().pass("tests/schemas/*.rs");
}

fn validate_schema(path: std::path::PathBuf) -> Result<(), Box<dyn Error>> {
let mut out_path = path.clone();
out_path.set_extension("rs");
/// Ensure that setting the global config to use a custom map type works.
#[test]
fn test_custom_map() {
validate_schema(
"tests/schemas/maps.json".into(),
"tests/schemas/maps_custom.rs".into(),
TypeSpaceSettings::default().with_map_type("std::collections::BTreeMap".to_string()),
)
.unwrap();

trybuild::TestCases::new().pass("tests/schemas/maps_custom.rs");
}

fn validate_schema(
path: std::path::PathBuf,
out_path: std::path::PathBuf,
typespace: &mut TypeSpaceSettings,
) -> Result<(), Box<dyn Error>> {
let file = File::open(path)?;
let reader = BufReader::new(file);

Expand All @@ -40,7 +56,7 @@ fn validate_schema(path: std::path::PathBuf) -> Result<(), Box<dyn Error>> {
let schema = serde_json::from_value(schema_raw).unwrap();

let mut type_space = TypeSpace::new(
TypeSpaceSettings::default()
typespace
.with_replacement(
"HandGeneratedType",
"String",
Expand Down
298 changes: 298 additions & 0 deletions typify/tests/schemas/maps_custom.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,298 @@
#[doc = r" Error types."]
pub mod error {
#[doc = r" Error from a TryFrom or FromStr implementation."]
pub struct ConversionError(::std::borrow::Cow<'static, str>);
impl ::std::error::Error for ConversionError {}
impl ::std::fmt::Display for ConversionError {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> Result<(), ::std::fmt::Error> {
::std::fmt::Display::fmt(&self.0, f)
}
}
impl ::std::fmt::Debug for ConversionError {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> Result<(), ::std::fmt::Error> {
::std::fmt::Debug::fmt(&self.0, f)
}
}
impl From<&'static str> for ConversionError {
fn from(value: &'static str) -> Self {
Self(value.into())
}
}
impl From<String> for ConversionError {
fn from(value: String) -> Self {
Self(value.into())
}
}
}
#[doc = "DeadSimple"]
#[doc = r""]
#[doc = r" <details><summary>JSON schema</summary>"]
#[doc = r""]
#[doc = r" ```json"]
#[doc = "{"]
#[doc = " \"title\": \"DeadSimple\","]
#[doc = " \"type\": \"object\","]
#[doc = " \"$comment\": \"usual case of a map whose name must come from its title\""]
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
pub struct DeadSimple(pub ::serde_json::Map<::std::string::String, ::serde_json::Value>);
impl ::std::ops::Deref for DeadSimple {
type Target = ::serde_json::Map<::std::string::String, ::serde_json::Value>;
fn deref(&self) -> &::serde_json::Map<::std::string::String, ::serde_json::Value> {
&self.0
}
}
impl From<DeadSimple> for ::serde_json::Map<::std::string::String, ::serde_json::Value> {
fn from(value: DeadSimple) -> Self {
value.0
}
}
impl From<&DeadSimple> for DeadSimple {
fn from(value: &DeadSimple) -> Self {
value.clone()
}
}
impl From<::serde_json::Map<::std::string::String, ::serde_json::Value>> for DeadSimple {
fn from(value: ::serde_json::Map<::std::string::String, ::serde_json::Value>) -> Self {
Self(value)
}
}
#[doc = "Eh"]
#[doc = r""]
#[doc = r" <details><summary>JSON schema</summary>"]
#[doc = r""]
#[doc = r" ```json"]
#[doc = "{"]
#[doc = " \"type\": \"string\","]
#[doc = " \"format\": \"^a*$\""]
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(
:: serde :: Deserialize,
:: serde :: Serialize,
Clone,
Debug,
Eq,
Hash,
Ord,
PartialEq,
PartialOrd,
)]
pub struct Eh(pub ::std::string::String);
impl ::std::ops::Deref for Eh {
type Target = ::std::string::String;
fn deref(&self) -> &::std::string::String {
&self.0
}
}
impl From<Eh> for ::std::string::String {
fn from(value: Eh) -> Self {
value.0
}
}
impl From<&Eh> for Eh {
fn from(value: &Eh) -> Self {
value.clone()
}
}
impl From<::std::string::String> for Eh {
fn from(value: ::std::string::String) -> Self {
Self(value)
}
}
impl ::std::str::FromStr for Eh {
type Err = ::std::convert::Infallible;
fn from_str(value: &str) -> ::std::result::Result<Self, Self::Err> {
Ok(Self(value.to_string()))
}
}
impl ::std::fmt::Display for Eh {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
self.0.fmt(f)
}
}
#[doc = "MapWithDateKeys"]
#[doc = r""]
#[doc = r" <details><summary>JSON schema</summary>"]
#[doc = r""]
#[doc = r" ```json"]
#[doc = "{"]
#[doc = " \"type\": \"object\","]
#[doc = " \"additionalProperties\": {"]
#[doc = " \"$ref\": \"#/definitions/Value\""]
#[doc = " },"]
#[doc = " \"propertyNames\": {"]
#[doc = " \"format\": \"date\""]
#[doc = " },"]
#[doc = " \"$comment\": \"test that a type isn't needed for propertyNames\""]
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
pub struct MapWithDateKeys(pub std::collections::BTreeMap<chrono::naive::NaiveDate, Value>);
impl ::std::ops::Deref for MapWithDateKeys {
type Target = std::collections::BTreeMap<chrono::naive::NaiveDate, Value>;
fn deref(&self) -> &std::collections::BTreeMap<chrono::naive::NaiveDate, Value> {
&self.0
}
}
impl From<MapWithDateKeys> for std::collections::BTreeMap<chrono::naive::NaiveDate, Value> {
fn from(value: MapWithDateKeys) -> Self {
value.0
}
}
impl From<&MapWithDateKeys> for MapWithDateKeys {
fn from(value: &MapWithDateKeys) -> Self {
value.clone()
}
}
impl From<std::collections::BTreeMap<chrono::naive::NaiveDate, Value>> for MapWithDateKeys {
fn from(value: std::collections::BTreeMap<chrono::naive::NaiveDate, Value>) -> Self {
Self(value)
}
}
#[doc = "MapWithDateTimeKeys"]
#[doc = r""]
#[doc = r" <details><summary>JSON schema</summary>"]
#[doc = r""]
#[doc = r" ```json"]
#[doc = "{"]
#[doc = " \"type\": \"object\","]
#[doc = " \"additionalProperties\": {"]
#[doc = " \"$ref\": \"#/definitions/Value\""]
#[doc = " },"]
#[doc = " \"propertyNames\": {"]
#[doc = " \"format\": \"date-time\""]
#[doc = " },"]
#[doc = " \"$comment\": \"test that a type isn't needed for propertyNames\""]
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
pub struct MapWithDateTimeKeys(
pub std::collections::BTreeMap<chrono::DateTime<chrono::offset::Utc>, Value>,
);
impl ::std::ops::Deref for MapWithDateTimeKeys {
type Target = std::collections::BTreeMap<chrono::DateTime<chrono::offset::Utc>, Value>;
fn deref(&self) -> &std::collections::BTreeMap<chrono::DateTime<chrono::offset::Utc>, Value> {
&self.0
}
}
impl From<MapWithDateTimeKeys>
for std::collections::BTreeMap<chrono::DateTime<chrono::offset::Utc>, Value>
{
fn from(value: MapWithDateTimeKeys) -> Self {
value.0
}
}
impl From<&MapWithDateTimeKeys> for MapWithDateTimeKeys {
fn from(value: &MapWithDateTimeKeys) -> Self {
value.clone()
}
}
impl From<std::collections::BTreeMap<chrono::DateTime<chrono::offset::Utc>, Value>>
for MapWithDateTimeKeys
{
fn from(
value: std::collections::BTreeMap<chrono::DateTime<chrono::offset::Utc>, Value>,
) -> Self {
Self(value)
}
}
#[doc = "MapWithKeys"]
#[doc = r""]
#[doc = r" <details><summary>JSON schema</summary>"]
#[doc = r""]
#[doc = r" ```json"]
#[doc = "{"]
#[doc = " \"type\": \"object\","]
#[doc = " \"additionalProperties\": {"]
#[doc = " \"$ref\": \"#/definitions/Value\""]
#[doc = " },"]
#[doc = " \"propertyNames\": {"]
#[doc = " \"$ref\": \"#/definitions/Eh\""]
#[doc = " }"]
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)]
pub struct MapWithKeys(pub std::collections::BTreeMap<Eh, Value>);
impl ::std::ops::Deref for MapWithKeys {
type Target = std::collections::BTreeMap<Eh, Value>;
fn deref(&self) -> &std::collections::BTreeMap<Eh, Value> {
&self.0
}
}
impl From<MapWithKeys> for std::collections::BTreeMap<Eh, Value> {
fn from(value: MapWithKeys) -> Self {
value.0
}
}
impl From<&MapWithKeys> for MapWithKeys {
fn from(value: &MapWithKeys) -> Self {
value.clone()
}
}
impl From<std::collections::BTreeMap<Eh, Value>> for MapWithKeys {
fn from(value: std::collections::BTreeMap<Eh, Value>) -> Self {
Self(value)
}
}
#[doc = "Value"]
#[doc = r""]
#[doc = r" <details><summary>JSON schema</summary>"]
#[doc = r""]
#[doc = r" ```json"]
#[doc = "{"]
#[doc = " \"type\": \"string\""]
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(
:: serde :: Deserialize,
:: serde :: Serialize,
Clone,
Debug,
Eq,
Hash,
Ord,
PartialEq,
PartialOrd,
)]
pub struct Value(pub ::std::string::String);
impl ::std::ops::Deref for Value {
type Target = ::std::string::String;
fn deref(&self) -> &::std::string::String {
&self.0
}
}
impl From<Value> for ::std::string::String {
fn from(value: Value) -> Self {
value.0
}
}
impl From<&Value> for Value {
fn from(value: &Value) -> Self {
value.clone()
}
}
impl From<::std::string::String> for Value {
fn from(value: ::std::string::String) -> Self {
Self(value)
}
}
impl ::std::str::FromStr for Value {
type Err = ::std::convert::Infallible;
fn from_str(value: &str) -> ::std::result::Result<Self, Self::Err> {
Ok(Self(value.to_string()))
}
}
impl ::std::fmt::Display for Value {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
self.0.fmt(f)
}
}
fn main() {}

0 comments on commit 3046658

Please sign in to comment.