Lines
100 %
Functions
36.25 %
Branches
use ron::error::{Error, Position, SpannedError};
#[derive(Debug, serde::Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
enum TestEnum {
StructVariant { a: bool, b: char, c: i32 },
NewtypeVariant(TestStruct),
}
#[serde(tag = "type")]
enum TestEnumInternal {
StructVariant { a: bool },
#[serde(tag = "type", content = "content")]
enum TestEnumAdjacent {
#[serde(untagged)]
enum TestEnumUntagged {
struct TestStruct {
a: bool,
b: char,
c: i32,
#[test]
fn test_unknown_enum_variant() {
assert_eq!(
ron::from_str::<TestEnum>("NotAVariant"),
Err(SpannedError {
code: Error::NoSuchEnumVariant {
expected: &["StructVariant", "NewtypeVariant"],
found: String::from("NotAVariant"),
outer: Some(String::from("TestEnum")),
},
position: Position { line: 1, col: 12 },
})
);
fn test_struct_enum_fields() {
ron::from_str::<TestEnum>("StructVariant(a: true, b: 'b', c: -42, d: \"gotcha\")"),
code: Error::NoSuchStructField {
expected: &["a", "b", "c"],
found: String::from("d"),
outer: Some(String::from("StructVariant")),
position: Position { line: 1, col: 41 },
ron::from_str::<TestEnum>("StructVariant(a: true, c: -42)"),
code: Error::MissingStructField {
field: "b",
position: Position { line: 1, col: 30 },
ron::from_str::<TestEnum>("StructVariant(a: true, b: 'b', a: false, c: -42)"),
code: Error::DuplicateStructField {
field: "a",
position: Position { line: 1, col: 33 },
fn test_newtype_enum_fields() {
ron::from_str::<TestEnum>("#![enable(unwrap_variant_newtypes)] NewtypeVariant(a: true, b: 'b', c: -42, d: \"gotcha\")"),
outer: Some(String::from("NewtypeVariant")),
position: Position { line: 1, col: 78 },
ron::from_str::<TestEnum>(
"#![enable(unwrap_variant_newtypes)] NewtypeVariant(a: true, c: -42)"
),
position: Position { line: 1, col: 67 },
"#![enable(unwrap_variant_newtypes)] NewtypeVariant(a: true, b: 'b', a: false, c: -42)"
position: Position { line: 1, col: 70 },
fn test_struct_fields() {
ron::from_str::<TestStruct>("TestStruct(a: true, b: 'b', c: -42, d: \"gotcha\")"),
outer: Some(String::from("TestStruct")),
position: Position { line: 1, col: 38 },
ron::from_str::<TestStruct>("TestStruct(a: true, c: -42)"),
position: Position { line: 1, col: 27 },
ron::from_str::<TestStruct>("TestStruct(a: true, b: 'b', a: false, c: -42)"),
fn test_internally_tagged_enum() {
// Note: Not extracting the variant type is not great,
// but at least not wrong either
// Since the error occurs in serde-generated user code,
// after successfully deserialising, we cannot annotate
ron::from_str::<TestEnumInternal>("(type: \"StructVariant\")"),
outer: None,
position: Position { line: 1, col: 24 },
fn test_adjacently_tagged_enum() {
// Note: TestEnumAdjacent makes sense here since we are now treating
// the enum as a struct
ron::from_str::<TestEnumAdjacent>("(type: StructVariant, content: (d: 4))"),
outer: Some(String::from("TestEnumAdjacent")),
position: Position { line: 1, col: 37 },
fn test_untagged_enum() {
// Note: Errors inside untagged enums are not bubbled up
ron::from_str::<TestEnumUntagged>("(a: true, a: false)"),
code: Error::Message(String::from(
"data did not match any variant of untagged enum TestEnumUntagged"
)),
position: Position { line: 1, col: 20 },