| 1 |
use std::num::NonZeroU32; |
| 2 |
|
| 3 |
use ron::error::{Error, Position, Span, SpannedError}; |
| 4 |
use serde::{ |
| 5 |
de::{Deserialize, Error as DeError, Unexpected}, |
| 6 |
Deserializer, |
| 7 |
}; |
| 8 |
|
| 9 |
#[cfg(feature = "internal-span-substring-test")] |
| 10 |
use ron::util::span_substring::check_error_span_inclusive; |
| 11 |
|
| 12 |
#[cfg(feature = "internal-span-substring-test")] |
| 13 |
use ron::util::span_substring::check_error_span_exclusive; |
| 14 |
|
| 15 |
#[derive(Debug, serde::Deserialize, PartialEq)] |
| 16 |
#[serde(deny_unknown_fields)] |
| 17 |
enum Test { |
| 18 |
TupleVariant(i32, String), |
| 19 |
StructVariant { a: bool, b: NonZeroU32, c: i32 }, |
| 20 |
} |
| 21 |
|
| 22 |
#[derive(Debug, PartialEq)] |
| 23 |
struct TypeError; |
| 24 |
|
| 25 |
impl<'de> Deserialize<'de> for TypeError { |
| 26 |
fn deserialize<D: Deserializer<'de>>(_deserializer: D) -> Result<Self, D::Error> { |
| 27 |
Err(D::Error::invalid_type(Unexpected::Unit, &"impossible")) |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
#[test] |
| 32 |
fn test_error_positions() { |
| 33 |
let bogus_struct = " ()"; |
| 34 |
let expected_err = Err(SpannedError { |
| 35 |
code: Error::InvalidValueForType { |
| 36 |
expected: String::from("impossible"), |
| 37 |
found: String::from("a unit value"), |
| 38 |
}, |
| 39 |
span: Span { |
| 40 |
start: Position { line: 1, col: 1 }, |
| 41 |
end: Position { line: 1, col: 3 }, |
| 42 |
}, |
| 43 |
}); |
| 44 |
|
| 45 |
assert_eq!(ron::from_str::<TypeError>(bogus_struct), expected_err,); |
| 46 |
|
| 47 |
#[cfg(feature = "internal-span-substring-test")] |
| 48 |
check_error_span_inclusive::<TypeError>(bogus_struct, expected_err, " ("); |
| 49 |
|
| 50 |
let bogus_struct = "StructVariant(a: true, b: 0, c: -42)"; |
| 51 |
let expected_err = Err(SpannedError { |
| 52 |
code: Error::InvalidValueForType { |
| 53 |
expected: String::from("a nonzero u32"), |
| 54 |
found: String::from("the unsigned integer `0`"), |
| 55 |
}, |
| 56 |
span: Span { |
| 57 |
start: Position { line: 1, col: 27 }, |
| 58 |
end: Position { line: 1, col: 28 }, |
| 59 |
}, |
| 60 |
}); |
| 61 |
|
| 62 |
assert_eq!(ron::from_str::<Test>(bogus_struct), expected_err); |
| 63 |
|
| 64 |
#[cfg(feature = "internal-span-substring-test")] |
| 65 |
check_error_span_inclusive::<Test>(bogus_struct, expected_err, "0,"); |
| 66 |
|
| 67 |
let bogus_struct = "TupleVariant(42)"; |
| 68 |
let expected_err = Err(SpannedError { |
| 69 |
code: Error::ExpectedDifferentLength { |
| 70 |
expected: String::from("tuple variant Test::TupleVariant with 2 elements"), |
| 71 |
found: 1, |
| 72 |
}, |
| 73 |
span: Span { |
| 74 |
start: Position { line: 1, col: 16 }, |
| 75 |
end: Position { line: 1, col: 16 }, |
| 76 |
}, |
| 77 |
}); |
| 78 |
|
| 79 |
assert_eq!(ron::from_str::<Test>(bogus_struct), expected_err); |
| 80 |
|
| 81 |
#[cfg(feature = "internal-span-substring-test")] |
| 82 |
check_error_span_inclusive::<Test>(bogus_struct, expected_err, ")"); |
| 83 |
|
| 84 |
let bogus_struct = "NotAVariant"; |
| 85 |
let expected_err = Err(SpannedError { |
| 86 |
code: Error::NoSuchEnumVariant { |
| 87 |
expected: &["TupleVariant", "StructVariant"], |
| 88 |
found: String::from("NotAVariant"), |
| 89 |
outer: Some(String::from("Test")), |
| 90 |
}, |
| 91 |
span: Span { |
| 92 |
start: Position { line: 1, col: 1 }, |
| 93 |
end: Position { line: 1, col: 12 }, |
| 94 |
}, |
| 95 |
}); |
| 96 |
|
| 97 |
assert_eq!(ron::from_str::<Test>(bogus_struct), expected_err); |
| 98 |
|
| 99 |
#[cfg(feature = "internal-span-substring-test")] |
| 100 |
check_error_span_exclusive::<Test>(bogus_struct, expected_err, "NotAVariant"); |
| 101 |
|
| 102 |
let bogus_struct = "StructVariant(a: true, b: 1, c: -42, d: \"gotcha\")"; |
| 103 |
let expected_err = Err(SpannedError { |
| 104 |
code: Error::NoSuchStructField { |
| 105 |
expected: &["a", "b", "c"], |
| 106 |
found: String::from("d"), |
| 107 |
outer: Some(String::from("StructVariant")), |
| 108 |
}, |
| 109 |
span: Span { |
| 110 |
start: Position { line: 1, col: 38 }, |
| 111 |
end: Position { line: 1, col: 39 }, |
| 112 |
}, |
| 113 |
}); |
| 114 |
|
| 115 |
assert_eq!(ron::from_str::<Test>(bogus_struct), expected_err); |
| 116 |
|
| 117 |
#[cfg(feature = "internal-span-substring-test")] |
| 118 |
check_error_span_inclusive::<Test>(bogus_struct, expected_err, "d:"); |
| 119 |
|
| 120 |
let bogus_struct = "StructVariant(a: true, c: -42)"; |
| 121 |
let expected_err = Err(SpannedError { |
| 122 |
code: Error::MissingStructField { |
| 123 |
field: "b", |
| 124 |
outer: Some(String::from("StructVariant")), |
| 125 |
}, |
| 126 |
span: Span { |
| 127 |
start: Position { line: 1, col: 30 }, |
| 128 |
end: Position { line: 1, col: 30 }, |
| 129 |
}, |
| 130 |
}); |
| 131 |
|
| 132 |
assert_eq!(ron::from_str::<Test>(bogus_struct), expected_err); |
| 133 |
|
| 134 |
#[cfg(feature = "internal-span-substring-test")] |
| 135 |
check_error_span_inclusive::<Test>(bogus_struct, expected_err, ")"); |
| 136 |
|
| 137 |
let bogus_struct = "StructVariant(a: true, b: 1, a: false, c: -42)"; |
| 138 |
let expected_err = Err(SpannedError { |
| 139 |
code: Error::DuplicateStructField { |
| 140 |
field: "a", |
| 141 |
outer: Some(String::from("StructVariant")), |
| 142 |
}, |
| 143 |
span: Span { |
| 144 |
start: Position { line: 1, col: 30 }, |
| 145 |
end: Position { line: 1, col: 31 }, |
| 146 |
}, |
| 147 |
}); |
| 148 |
|
| 149 |
assert_eq!(ron::from_str::<Test>(bogus_struct), expected_err); |
| 150 |
|
| 151 |
#[cfg(feature = "internal-span-substring-test")] |
| 152 |
check_error_span_inclusive::<Test>(bogus_struct, expected_err, "a:"); |
| 153 |
} |