Lines
100 %
Functions
Branches
use ron::error::{Position, Span, SpannedError};
#[cfg(feature = "internal-span-substring-test")]
use ron::util::span_substring::check_error_span_inclusive;
use ron::util::span_substring::check_error_span_exclusive;
#[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: ron::Error::NoSuchEnumVariant {
expected: &["StructVariant", "NewtypeVariant"],
found: String::from("NotAVariant"),
outer: Some(String::from("TestEnum")),
},
span: Span {
start: Position { line: 1, col: 1 },
end: Position { line: 1, col: 12 },
})
);
check_error_span_exclusive::<TestEnum>(
"NotAVariant",
}),
fn test_struct_enum_fields() {
let bogus_struct = "StructVariant(a: true, b: 'b', c: -42, d: \"gotcha\")";
let expected_err = Err(SpannedError {
code: ron::Error::NoSuchStructField {
expected: &["a", "b", "c"],
found: String::from("d"),
outer: Some(String::from("StructVariant")),
start: Position { line: 1, col: 40 },
end: Position { line: 1, col: 41 },
});
assert_eq!(ron::from_str::<TestEnum>(bogus_struct), expected_err);
check_error_span_inclusive::<TestEnum>(bogus_struct, expected_err, "d:");
let bogus_struct = "StructVariant(a: true, c: -42)";
code: ron::Error::MissingStructField {
field: "b",
start: Position { line: 1, col: 30 },
end: Position { line: 1, col: 30 },
check_error_span_inclusive::<TestEnum>(bogus_struct, expected_err, ")");
let bogus_struct = "StructVariant(a: true, b: 'b', a: false, c: -42)";
code: ron::Error::DuplicateStructField {
field: "a",
start: Position { line: 1, col: 32 },
end: Position { line: 1, col: 33 },
check_error_span_inclusive::<TestEnum>(bogus_struct, expected_err, "a:");
fn test_newtype_enum_fields() {
let bogus_struct = "#![enable(unwrap_variant_newtypes)] NewtypeVariant(a: true, b: 'b', c: -42, d: \"gotcha\")";
outer: Some(String::from("NewtypeVariant")),
start: Position { line: 1, col: 77 },
end: Position { line: 1, col: 78 },
let bogus_struct = "#![enable(unwrap_variant_newtypes)] NewtypeVariant(a: true, c: -42)";
start: Position { line: 1, col: 67 },
end: Position { line: 1, col: 67 },
let bogus_struct =
"#![enable(unwrap_variant_newtypes)] NewtypeVariant(a: true, b: 'b', a: false, c: -42)";
start: Position { line: 1, col: 69 },
end: Position { line: 1, col: 70 },
fn test_struct_fields() {
let bogus_struct = "TestStruct(a: true, b: 'b', c: -42, d: \"gotcha\")";
outer: Some(String::from("TestStruct")),
start: Position { line: 1, col: 37 },
end: Position { line: 1, col: 38 },
assert_eq!(ron::from_str::<TestStruct>(bogus_struct), expected_err);
check_error_span_inclusive::<TestStruct>(bogus_struct, expected_err, "d:");
ron::from_str::<TestStruct>("TestStruct(a: true, c: -42)"),
start: Position { line: 1, col: 27 },
end: Position { line: 1, col: 27 },
let bogus_struct = "TestStruct(a: true, b: 'b', a: false, c: -42)";
start: Position { line: 1, col: 29 },
check_error_span_inclusive::<TestStruct>(bogus_struct, expected_err, "a:");
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
let bogus_struct = "(type: \"StructVariant\")";
outer: None,
start: Position { line: 1, col: 23 },
end: Position { line: 1, col: 24 },
ron::from_str::<TestEnumInternal>(bogus_struct),
expected_err
check_error_span_exclusive::<TestEnumInternal>(bogus_struct, expected_err, ")");
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")),
end: Position { line: 1, col: 37 },
fn test_untagged_enum() {
// Note: Errors inside untagged enums are not bubbled up
let bogus_struct = "(a: true, a: false)";
code: ron::Error::Message(String::from(
"data did not match any variant of untagged enum TestEnumUntagged",
)),
start: Position { line: 1, col: 19 },
end: Position { line: 1, col: 20 },
ron::from_str::<TestEnumUntagged>(bogus_struct),
check_error_span_exclusive::<TestEnumUntagged>(bogus_struct, expected_err, ")");