Skip to content

Commit

Permalink
Port "deprecated" tests to new integration test style
Browse files Browse the repository at this point in the history
  • Loading branch information
GREsau committed Sep 8, 2024
1 parent 3823b8a commit 1fd1029
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 41 deletions.
39 changes: 0 additions & 39 deletions schemars/tests/deprecated.rs

This file was deleted.

69 changes: 69 additions & 0 deletions schemars/tests/integration/deprecated.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#![allow(deprecated)]

use crate::prelude::*;

#[derive(JsonSchema, Default, Serialize, Deserialize)]
#[deprecated]
struct DeprecatedStruct {
foo: i32,
#[deprecated]
bar: bool,
}

#[allow(deprecated)]
#[test]
fn deprecated_struct() {
test!(DeprecatedStruct)
.assert_snapshot()
.assert_allows_ser_roundtrip_default()
.custom(|schema, _| {
assert_eq!(
schema.as_value().pointer("/deprecated"),
Some(&Value::Bool(true)),
);
assert_eq!(
schema.as_value().pointer("/properties/bar/deprecated"),
Some(&Value::Bool(true)),
);
});
}

#[derive(JsonSchema, Default, Serialize, Deserialize)]
#[deprecated]
enum DeprecatedEnum {
#[default]
Unit,
#[deprecated]
DeprecatedUnitVariant,
#[deprecated]
DeprecatedStructVariant {
foo: i32,
#[deprecated]
deprecated_field: bool,
},
}

#[test]
fn deprecated_enum() {
test!(DeprecatedEnum)
.assert_snapshot()
.assert_allows_ser_roundtrip_default()
.custom(|schema, _| {
assert_eq!(
schema.as_value().pointer("/deprecated"),
Some(&Value::Bool(true)),
);
assert_eq!(
schema.as_value().pointer("/oneOf/1/deprecated"),
Some(&Value::Bool(true)),
);
assert_eq!(
schema.as_value().pointer("/oneOf/2/deprecated"),
Some(&Value::Bool(true)),
);
assert_eq!(
schema.as_value().pointer("/oneOf/2/properties/DeprecatedStructVariant/properties/deprecated_field/deprecated"),
Some(&Value::Bool(true)),
);
});
}
3 changes: 3 additions & 0 deletions schemars/tests/integration/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::disallowed_names)]

#[cfg(feature = "arrayvec07")]
mod arrayvec;
mod bound;
Expand All @@ -9,6 +11,7 @@ mod contract;
mod crate_alias;
#[cfg(any(feature = "rust_decimal1", feature = "bigdecimal04"))]
mod decimal;
mod deprecated;

mod prelude {
pub use crate::test;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "DeprecatedEnum",
"oneOf": [
{
"type": "string",
"enum": [
"Unit"
]
},
{
"type": "string",
"const": "DeprecatedUnitVariant",
"deprecated": true
},
{
"type": "object",
"properties": {
"DeprecatedStructVariant": {
"type": "object",
"properties": {
"foo": {
"type": "integer",
"format": "int32"
},
"deprecated_field": {
"type": "boolean",
"deprecated": true
}
},
"required": [
"foo",
"deprecated_field"
]
}
},
"required": [
"DeprecatedStructVariant"
],
"additionalProperties": false,
"deprecated": true
}
],
"deprecated": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
"type": "integer",
"format": "int32"
},
"deprecated_field": {
"bar": {
"type": "boolean",
"deprecated": true
}
},
"required": [
"foo",
"deprecated_field"
"bar"
],
"deprecated": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "DeprecatedStruct",
"type": "object",
"properties": {
"foo": {
"type": "integer",
"format": "int32"
},
"bar": {
"type": "boolean",
"deprecated": true
}
},
"required": [
"foo",
"bar"
],
"deprecated": true
}
5 changes: 5 additions & 0 deletions schemars/tests/integration/test_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ impl<T: JsonSchema> TestHelper<T> {
self
}

pub fn custom(&self, assertion: impl Fn(&Schema, Contract)) {
assertion(self.de_schema(), Contract::Deserialize);
assertion(self.ser_schema(), Contract::Serialize);
}

fn schema_for<T2: JsonSchema>(&self, contract: Contract) -> Schema {
self.settings
.clone()
Expand Down

0 comments on commit 1fd1029

Please sign in to comment.