Skip to content

Commit e44f5df

Browse files
authored
Cleanup schemars tests (#862)
2 parents 03a071a + ca69f67 commit e44f5df

File tree

5 files changed

+63
-137
lines changed

5 files changed

+63
-137
lines changed

serde_with/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ required-features = ["alloc"]
239239

240240
[[test]]
241241
name = "schemars_0_8"
242-
path = "tests/schemars_0_8.rs"
242+
path = "tests/schemars_0_8/main.rs"
243243
required-features = ["schemars_0_8"]
244244

245245
[[test]]

serde_with/tests/schemars_0_8.rs renamed to serde_with/tests/schemars_0_8/main.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ macro_rules! declare_snapshot_test {
5656
schema.push('\n');
5757

5858
let filename = concat!("./", module_path!(), "::", stringify!($test), ".json")
59-
.replace("::", "/");
59+
.replace("::", "/")
60+
.replace("schemars_0_8/", "");
6061

6162
let expected = expect_file![filename];
6263
expected.assert_eq(&schema);
@@ -95,7 +96,7 @@ fn schemars_basic() {
9596
let mut schema = serde_json::to_string_pretty(&schema).expect("schema could not be serialized");
9697
schema.push('\n');
9798

98-
let expected = expect_file!["./schemars_0_8/schemars_basic.json"];
99+
let expected = expect_file!["./schemars_basic.json"];
99100
expected.assert_eq(&schema);
100101
}
101102

@@ -174,7 +175,7 @@ fn schemars_deserialize_only_bug_735() {
174175
let mut schema = serde_json::to_string_pretty(&schema).expect("schema could not be serialized");
175176
schema.push('\n');
176177

177-
let expected = expect_file!["./schemars_0_8/schemars_deserialize_only_bug_735.json"];
178+
let expected = expect_file!["./schemars_deserialize_only_bug_735.json"];
178179
expected.assert_eq(&schema);
179180
}
180181

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#![allow(dead_code, missing_docs)]
2+
3+
use serde::Serialize;
4+
5+
#[track_caller]
6+
pub fn check_matches_schema<T>(value: &serde_json::Value)
7+
where
8+
T: schemars_0_8::JsonSchema,
9+
{
10+
use jsonschema::Validator;
11+
use std::fmt::Write;
12+
13+
let schema_object = serde_json::to_value(schemars_0_8::schema_for!(T))
14+
.expect("schema for T could not be serialized to json");
15+
let schema = match Validator::new(&schema_object) {
16+
Ok(schema) => schema,
17+
Err(e) => panic!("schema for T was not a valid JSON schema: {e}"),
18+
};
19+
20+
if let Err(err) = schema.validate(value) {
21+
let mut message = String::new();
22+
23+
let _ = writeln!(
24+
&mut message,
25+
"Object was not valid according to its own schema:"
26+
);
27+
28+
let _ = writeln!(&mut message, " -> {err}");
29+
let _ = writeln!(&mut message);
30+
let _ = writeln!(&mut message, "Object Value:");
31+
let _ = writeln!(
32+
&mut message,
33+
"{}",
34+
serde_json::to_string_pretty(&value).unwrap_or_else(|e| format!("> error: {e}"))
35+
);
36+
let _ = writeln!(&mut message);
37+
let _ = writeln!(&mut message, "JSON Schema:");
38+
let _ = writeln!(
39+
&mut message,
40+
"{}",
41+
serde_json::to_string_pretty(&schema_object)
42+
.unwrap_or_else(|e| format!("> error: {e}"))
43+
);
44+
45+
panic!("{}", message);
46+
};
47+
}
48+
49+
#[track_caller]
50+
pub fn check_valid_json_schema<T>(value: &T)
51+
where
52+
T: schemars_0_8::JsonSchema + Serialize,
53+
{
54+
let value = serde_json::to_value(value).expect("could not serialize T to json");
55+
56+
check_matches_schema::<T>(&value);
57+
}

serde_with/tests/schemars_0_9/utils.rs

Lines changed: 1 addition & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,6 @@
11
#![allow(dead_code, missing_docs)]
22

3-
use core::fmt::Debug;
4-
use expect_test::Expect;
5-
use pretty_assertions::assert_eq;
6-
use serde::{de::DeserializeOwned, Serialize};
7-
8-
#[track_caller]
9-
pub fn is_equal<T>(value: T, expected: Expect)
10-
where
11-
T: Debug + DeserializeOwned + PartialEq + Serialize,
12-
{
13-
let serialized = serde_json::to_string_pretty(&value).unwrap();
14-
expected.assert_eq(&serialized);
15-
assert_eq!(
16-
value,
17-
serde_json::from_str::<T>(&serialized).unwrap(),
18-
"Deserialization differs from expected value."
19-
);
20-
}
21-
22-
/// Like [`is_equal`] but not pretty-print
23-
#[track_caller]
24-
pub fn is_equal_compact<T>(value: T, expected: Expect)
25-
where
26-
T: Debug + DeserializeOwned + PartialEq + Serialize,
27-
{
28-
let serialized = serde_json::to_string(&value).unwrap();
29-
expected.assert_eq(&serialized);
30-
assert_eq!(
31-
value,
32-
serde_json::from_str::<T>(&serialized).unwrap(),
33-
"Deserialization differs from expected value."
34-
);
35-
}
36-
37-
#[track_caller]
38-
pub fn check_deserialization<T>(value: T, deserialize_from: &str)
39-
where
40-
T: Debug + DeserializeOwned + PartialEq,
41-
{
42-
assert_eq!(
43-
value,
44-
serde_json::from_str::<T>(deserialize_from).unwrap(),
45-
"Deserialization differs from expected value."
46-
);
47-
}
48-
49-
#[track_caller]
50-
pub fn check_serialization<T>(value: T, serialize_to: Expect)
51-
where
52-
T: Debug + Serialize,
53-
{
54-
serialize_to.assert_eq(&serde_json::to_string_pretty(&value).unwrap());
55-
}
56-
57-
#[track_caller]
58-
pub fn check_error_serialization<T>(value: T, error_msg: Expect)
59-
where
60-
T: Debug + Serialize,
61-
{
62-
error_msg.assert_eq(
63-
&serde_json::to_string_pretty(&value)
64-
.unwrap_err()
65-
.to_string(),
66-
);
67-
}
68-
69-
#[track_caller]
70-
pub fn check_error_deserialization<T>(deserialize_from: &str, error_msg: Expect)
71-
where
72-
T: Debug + DeserializeOwned,
73-
{
74-
error_msg.assert_eq(
75-
&serde_json::from_str::<T>(deserialize_from)
76-
.unwrap_err()
77-
.to_string(),
78-
);
79-
}
3+
use serde::Serialize;
804

815
#[track_caller]
826
pub fn check_matches_schema<T>(value: &serde_json::Value)

serde_with/tests/utils.rs

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -77,59 +77,3 @@ where
7777
.to_string(),
7878
);
7979
}
80-
81-
#[track_caller]
82-
pub fn check_matches_schema<T>(value: &serde_json::Value)
83-
where
84-
T: schemars_0_8::JsonSchema,
85-
{
86-
use jsonschema::Validator;
87-
use std::fmt::Write;
88-
89-
if cfg!(feature = "schemars_0_8") {
90-
let schema_object = serde_json::to_value(schemars_0_8::schema_for!(T))
91-
.expect("schema for T could not be serialized to json");
92-
let schema = match Validator::new(&schema_object) {
93-
Ok(schema) => schema,
94-
Err(e) => panic!("schema for T was not a valid JSON schema: {e}"),
95-
};
96-
97-
if let Err(err) = schema.validate(value) {
98-
let mut message = String::new();
99-
100-
let _ = writeln!(
101-
&mut message,
102-
"Object was not valid according to its own schema:"
103-
);
104-
105-
let _ = writeln!(&mut message, " -> {err}");
106-
let _ = writeln!(&mut message);
107-
let _ = writeln!(&mut message, "Object Value:");
108-
let _ = writeln!(
109-
&mut message,
110-
"{}",
111-
serde_json::to_string_pretty(&value).unwrap_or_else(|e| format!("> error: {e}"))
112-
);
113-
let _ = writeln!(&mut message);
114-
let _ = writeln!(&mut message, "JSON Schema:");
115-
let _ = writeln!(
116-
&mut message,
117-
"{}",
118-
serde_json::to_string_pretty(&schema_object)
119-
.unwrap_or_else(|e| format!("> error: {e}"))
120-
);
121-
122-
panic!("{}", message);
123-
};
124-
}
125-
}
126-
127-
#[track_caller]
128-
pub fn check_valid_json_schema<T>(value: &T)
129-
where
130-
T: schemars_0_8::JsonSchema + Serialize,
131-
{
132-
let value = serde_json::to_value(value).expect("could not serialize T to json");
133-
134-
check_matches_schema::<T>(&value);
135-
}

0 commit comments

Comments
 (0)