| 1 |
use std::num::NonZeroU32; |
| 2 |
|
| 3 |
use ron::error::{Error, Position, SpannedError}; |
| 4 |
use serde::{ |
| 5 |
de::{Deserialize, Error as DeError, Unexpected}, |
| 6 |
Deserializer, |
| 7 |
}; |
| 8 |
|
| 9 |
#[derive(Debug, serde::Deserialize, PartialEq)] |
| 10 |
#[serde(deny_unknown_fields)] |
| 11 |
enum Test { |
| 12 |
TupleVariant(i32, String), |
| 13 |
StructVariant { a: bool, b: NonZeroU32, c: i32 }, |
| 14 |
} |
| 15 |
|
| 16 |
#[derive(Debug, PartialEq)] // GRCOV_EXCL_LINE |
| 17 |
struct TypeError; |
| 18 |
|
| 19 |
impl<'de> Deserialize<'de> for TypeError { |
| 20 |
fn deserialize<D: Deserializer<'de>>(_deserializer: D) -> Result<Self, D::Error> { |
| 21 |
Err(D::Error::invalid_type(Unexpected::Unit, &"impossible")) |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
#[test] |
| 26 |
fn test_error_positions() { |
| 27 |
assert_eq!( |
| 28 |
ron::from_str::<TypeError>(" ()"), |
| 29 |
Err(SpannedError { |
| 30 |
code: Error::InvalidValueForType { |
| 31 |
expected: String::from("impossible"), |
| 32 |
found: String::from("a unit value"), |
| 33 |
}, |
| 34 |
position: Position { line: 1, col: 3 }, |
| 35 |
}) |
| 36 |
); |
| 37 |
|
| 38 |
assert_eq!( |
| 39 |
ron::from_str::<Test>("StructVariant(a: true, b: 0, c: -42)"), |
| 40 |
Err(SpannedError { |
| 41 |
code: Error::InvalidValueForType { |
| 42 |
expected: String::from("a nonzero u32"), |
| 43 |
found: String::from("the unsigned integer `0`"), |
| 44 |
}, |
| 45 |
position: Position { line: 1, col: 28 }, |
| 46 |
}) |
| 47 |
); |
| 48 |
|
| 49 |
assert_eq!( |
| 50 |
ron::from_str::<Test>("TupleVariant(42)"), |
| 51 |
Err(SpannedError { |
| 52 |
code: Error::ExpectedDifferentLength { |
| 53 |
expected: String::from("tuple variant Test::TupleVariant with 2 elements"), |
| 54 |
found: 1, |
| 55 |
}, |
| 56 |
position: Position { line: 1, col: 16 }, |
| 57 |
}) |
| 58 |
); |
| 59 |
|
| 60 |
assert_eq!( |
| 61 |
ron::from_str::<Test>("NotAVariant"), |
| 62 |
Err(SpannedError { |
| 63 |
code: Error::NoSuchEnumVariant { |
| 64 |
expected: &["TupleVariant", "StructVariant"], |
| 65 |
found: String::from("NotAVariant"), |
| 66 |
outer: Some(String::from("Test")), |
| 67 |
}, |
| 68 |
position: Position { line: 1, col: 12 }, |
| 69 |
}) |
| 70 |
); |
| 71 |
|
| 72 |
assert_eq!( |
| 73 |
ron::from_str::<Test>("StructVariant(a: true, b: 1, c: -42, d: \"gotcha\")"), |
| 74 |
Err(SpannedError { |
| 75 |
code: Error::NoSuchStructField { |
| 76 |
expected: &["a", "b", "c"], |
| 77 |
found: String::from("d"), |
| 78 |
outer: Some(String::from("StructVariant")), |
| 79 |
}, |
| 80 |
position: Position { line: 1, col: 39 }, |
| 81 |
}) |
| 82 |
); |
| 83 |
|
| 84 |
assert_eq!( |
| 85 |
ron::from_str::<Test>("StructVariant(a: true, c: -42)"), |
| 86 |
Err(SpannedError { |
| 87 |
code: Error::MissingStructField { |
| 88 |
field: "b", |
| 89 |
outer: Some(String::from("StructVariant")), |
| 90 |
}, |
| 91 |
position: Position { line: 1, col: 30 }, |
| 92 |
}) |
| 93 |
); |
| 94 |
|
| 95 |
assert_eq!( |
| 96 |
ron::from_str::<Test>("StructVariant(a: true, b: 1, a: false, c: -42)"), |
| 97 |
Err(SpannedError { |
| 98 |
code: Error::DuplicateStructField { |
| 99 |
field: "a", |
| 100 |
outer: Some(String::from("StructVariant")), |
| 101 |
}, |
| 102 |
position: Position { line: 1, col: 31 }, |
| 103 |
}) |
| 104 |
); |
| 105 |
} |