use super::parse_schema;
fn described(text: &str, json: bool) -> Result<String, String> {
parse_schema(text, json).map(|schema| format!("{schema:?}"))
}
const HARD_FAILURES: [(&str, bool, &str); 5] = [
(
"default: &a [*a]",
false,
"yaml: anchor 'a' value contains itself",
),
(
"x-a: &a {y: *a}",
false,
"yaml: anchor 'a' value contains itself",
),
(
"default: {<<: 3}",
false,
"yaml: map merge requires map or sequence of maps as the value",
),
("", true, "unexpected end of JSON input"),
(
"type: string",
true,
"invalid character 'y' looking for beginning of value",
),
];
#[test]
fn hard_yaml_and_json_errors_match_go() {
for (text, json, expected) in HARD_FAILURES {
assert_eq!(described(text, json), Err(expected.to_owned()), "{text}");
}
}
const DECODED: [(&str, &str); 6] = [
(
"default: {n: 2, <<: {n: 1, m: 3}}",
r#"default: Some(Object {"m": Number(3), "n": Number(2)})"#,
),
(
"default: {<<: [{a: 1}, {a: 9, b: 2}]}",
r#"default: Some(Object {"a": Number(1), "b": Number(2)})"#,
),
("x-base: &b {type: string}\n<<: *b", r#"types: ["string"]"#),
(
"properties:\nanyOf: null\nenum: null\ndependencies: null",
"properties: None",
),
("title: !!binary SGVsbG8=", r#"title: "Hello""#),
(
"default: !!binary SGVsbG8=",
r#"default: Some(String("Hello"))"#,
),
];
#[test]
fn merges_nulls_and_binary_decode_as_in_go() {
for (text, expected) in DECODED {
let described = described(text, false).expect("decodes");
assert!(described.contains(expected), "{text}: {described}");
}
}