use indoc::indoc; use serde_derive::Deserialize; use std::fmt::Debug;
fn test_error<T>(yaml: &str, expected: &str) where
T: serde::de::DeserializeOwned + Debug,
{ let result = serde_yaml::from_str::<T>(yaml);
assert_eq!(expected, format!("{}", result.unwrap_err()));
}
#[test] fn test_incorrect_type() { let yaml = indoc! {"
---
str "}; let expected = "invalid type: string \"str\", expected i16 at line 2 column 1";
test_error::<i16>(yaml, expected);
}
#[test] fn test_incorrect_nested_type() { #[derive(Deserialize, Debug)] struct A { #[allow(dead_code)]
b: Vec<B>,
} #[derive(Deserialize, Debug)] enum B {
C(C),
} #[derive(Deserialize, Debug)] struct C { #[allow(dead_code)]
d: bool,
} let yaml = indoc! {"
---
b:
- C:
d: fase "}; let expected = "b[0].C.d: invalid type: string \"fase\", expected a boolean at line 4 column 10";
test_error::<A>(yaml, expected);
}
#[test] fn test_empty() { let expected = "EOF while parsing a value";
test_error::<String>("", expected);
}
#[test] fn test_missing_field() { #[derive(Deserialize, Debug)] struct Basic { #[allow(dead_code)]
v: bool, #[allow(dead_code)]
w: bool,
} let yaml = indoc! {"
---
v: true "}; let expected = "missing field `w` at line 2 column 2";
test_error::<Basic>(yaml, expected);
}
#[test] fn test_unknown_anchor() { let yaml = indoc! {"
---
*some "}; let expected = "while parsing node, found unknown anchor at line 2 column 1";
test_error::<String>(yaml, expected);
}
#[test] fn test_ignored_unknown_anchor() { #[derive(Deserialize, Debug)] struct Wrapper { #[allow(dead_code)]
c: (),
} let yaml = indoc! {"
---
b: [*a]
c: ~ "}; let expected = "while parsing node, found unknown anchor at line 2 column 5";
test_error::<Wrapper>(yaml, expected);
}
#[test] fn test_two_documents() { let yaml = indoc! {"
--- 0
--- 1 "}; let expected = "deserializing from YAML containing more than one document is not supported";
test_error::<usize>(yaml, expected);
}
let yaml = "{x:".repeat(1_000) + &"}".repeat(1_000); let expected = "recursion limit exceeded at line 1 column 766";
test_error::<i32>(&yaml, expected);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.