| 1 |
use ron::error::{Position, Span, SpannedError}; |
| 2 |
|
| 3 |
#[cfg(feature = "internal-span-substring-test")] |
| 4 |
use ron::util::span_substring::check_error_span_inclusive; |
| 5 |
|
| 6 |
#[cfg(feature = "internal-span-substring-test")] |
| 7 |
use ron::util::span_substring::check_error_span_exclusive; |
| 8 |
|
| 9 |
#[derive(Debug, serde::Deserialize, PartialEq)] |
| 10 |
#[serde(deny_unknown_fields)] |
| 11 |
enum TestEnum { |
| 12 |
StructVariant { a: bool, b: char, c: i32 }, |
| 13 |
NewtypeVariant(TestStruct), |
| 14 |
} |
| 15 |
|
| 16 |
#[derive(Debug, serde::Deserialize, PartialEq)] |
| 17 |
#[serde(tag = "type")] |
| 18 |
enum TestEnumInternal { |
| 19 |
StructVariant { a: bool }, |
| 20 |
} |
| 21 |
|
| 22 |
#[derive(Debug, serde::Deserialize, PartialEq)] |
| 23 |
#[serde(tag = "type", content = "content")] |
| 24 |
enum TestEnumAdjacent { |
| 25 |
StructVariant { a: bool }, |
| 26 |
} |
| 27 |
|
| 28 |
#[derive(Debug, serde::Deserialize, PartialEq)] |
| 29 |
#[serde(untagged)] |
| 30 |
enum TestEnumUntagged { |
| 31 |
StructVariant { a: bool }, |
| 32 |
} |
| 33 |
|
| 34 |
#[derive(Debug, serde::Deserialize, PartialEq)] |
| 35 |
#[serde(deny_unknown_fields)] |
| 36 |
struct TestStruct { |
| 37 |
a: bool, |
| 38 |
b: char, |
| 39 |
c: i32, |
| 40 |
} |
| 41 |
|
| 42 |
#[test] |
| 43 |
fn test_unknown_enum_variant() { |
| 44 |
assert_eq!( |
| 45 |
ron::from_str::<TestEnum>("NotAVariant"), |
| 46 |
Err(SpannedError { |
| 47 |
code: ron::Error::NoSuchEnumVariant { |
| 48 |
expected: &["StructVariant", "NewtypeVariant"], |
| 49 |
found: String::from("NotAVariant"), |
| 50 |
outer: Some(String::from("TestEnum")), |
| 51 |
}, |
| 52 |
span: Span { |
| 53 |
start: Position { line: 1, col: 1 }, |
| 54 |
end: Position { line: 1, col: 12 }, |
| 55 |
} |
| 56 |
}) |
| 57 |
); |
| 58 |
|
| 59 |
#[cfg(feature = "internal-span-substring-test")] |
| 60 |
check_error_span_exclusive::<TestEnum>( |
| 61 |
"NotAVariant", |
| 62 |
Err(SpannedError { |
| 63 |
code: ron::Error::NoSuchEnumVariant { |
| 64 |
expected: &["StructVariant", "NewtypeVariant"], |
| 65 |
found: String::from("NotAVariant"), |
| 66 |
outer: Some(String::from("TestEnum")), |
| 67 |
}, |
| 68 |
span: Span { |
| 69 |
start: Position { line: 1, col: 1 }, |
| 70 |
end: Position { line: 1, col: 12 }, |
| 71 |
}, |
| 72 |
}), |
| 73 |
"NotAVariant", |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
#[test] |
| 78 |
fn test_struct_enum_fields() { |
| 79 |
let bogus_struct = "StructVariant(a: true, b: 'b', c: -42, d: \"gotcha\")"; |
| 80 |
let expected_err = Err(SpannedError { |
| 81 |
code: ron::Error::NoSuchStructField { |
| 82 |
expected: &["a", "b", "c"], |
| 83 |
found: String::from("d"), |
| 84 |
outer: Some(String::from("StructVariant")), |
| 85 |
}, |
| 86 |
span: Span { |
| 87 |
start: Position { line: 1, col: 40 }, |
| 88 |
end: Position { line: 1, col: 41 }, |
| 89 |
}, |
| 90 |
}); |
| 91 |
|
| 92 |
assert_eq!(ron::from_str::<TestEnum>(bogus_struct), expected_err); |
| 93 |
|
| 94 |
#[cfg(feature = "internal-span-substring-test")] |
| 95 |
check_error_span_inclusive::<TestEnum>(bogus_struct, expected_err, "d:"); |
| 96 |
|
| 97 |
let bogus_struct = "StructVariant(a: true, c: -42)"; |
| 98 |
let expected_err = Err(SpannedError { |
| 99 |
code: ron::Error::MissingStructField { |
| 100 |
field: "b", |
| 101 |
outer: Some(String::from("StructVariant")), |
| 102 |
}, |
| 103 |
span: Span { |
| 104 |
start: Position { line: 1, col: 30 }, |
| 105 |
end: Position { line: 1, col: 30 }, |
| 106 |
}, |
| 107 |
}); |
| 108 |
|
| 109 |
assert_eq!(ron::from_str::<TestEnum>(bogus_struct), expected_err); |
| 110 |
|
| 111 |
#[cfg(feature = "internal-span-substring-test")] |
| 112 |
check_error_span_inclusive::<TestEnum>(bogus_struct, expected_err, ")"); |
| 113 |
|
| 114 |
let bogus_struct = "StructVariant(a: true, b: 'b', a: false, c: -42)"; |
| 115 |
let expected_err = Err(SpannedError { |
| 116 |
code: ron::Error::DuplicateStructField { |
| 117 |
field: "a", |
| 118 |
outer: Some(String::from("StructVariant")), |
| 119 |
}, |
| 120 |
span: Span { |
| 121 |
start: Position { line: 1, col: 32 }, |
| 122 |
end: Position { line: 1, col: 33 }, |
| 123 |
}, |
| 124 |
}); |
| 125 |
|
| 126 |
assert_eq!(ron::from_str::<TestEnum>(bogus_struct), expected_err); |
| 127 |
|
| 128 |
#[cfg(feature = "internal-span-substring-test")] |
| 129 |
check_error_span_inclusive::<TestEnum>(bogus_struct, expected_err, "a:"); |
| 130 |
} |
| 131 |
|
| 132 |
#[test] |
| 133 |
fn test_newtype_enum_fields() { |
| 134 |
let bogus_struct = "#![enable(unwrap_variant_newtypes)] NewtypeVariant(a: true, b: 'b', c: -42, d: \"gotcha\")"; |
| 135 |
let expected_err = Err(SpannedError { |
| 136 |
code: ron::Error::NoSuchStructField { |
| 137 |
expected: &["a", "b", "c"], |
| 138 |
found: String::from("d"), |
| 139 |
outer: Some(String::from("NewtypeVariant")), |
| 140 |
}, |
| 141 |
span: Span { |
| 142 |
start: Position { line: 1, col: 77 }, |
| 143 |
end: Position { line: 1, col: 78 }, |
| 144 |
}, |
| 145 |
}); |
| 146 |
|
| 147 |
assert_eq!(ron::from_str::<TestEnum>(bogus_struct), expected_err); |
| 148 |
|
| 149 |
#[cfg(feature = "internal-span-substring-test")] |
| 150 |
check_error_span_inclusive::<TestEnum>(bogus_struct, expected_err, "d:"); |
| 151 |
|
| 152 |
let bogus_struct = "#![enable(unwrap_variant_newtypes)] NewtypeVariant(a: true, c: -42)"; |
| 153 |
let expected_err = Err(SpannedError { |
| 154 |
code: ron::Error::MissingStructField { |
| 155 |
field: "b", |
| 156 |
outer: Some(String::from("NewtypeVariant")), |
| 157 |
}, |
| 158 |
span: Span { |
| 159 |
start: Position { line: 1, col: 67 }, |
| 160 |
end: Position { line: 1, col: 67 }, |
| 161 |
}, |
| 162 |
}); |
| 163 |
|
| 164 |
assert_eq!(ron::from_str::<TestEnum>(bogus_struct), expected_err); |
| 165 |
|
| 166 |
#[cfg(feature = "internal-span-substring-test")] |
| 167 |
check_error_span_inclusive::<TestEnum>(bogus_struct, expected_err, ")"); |
| 168 |
|
| 169 |
let bogus_struct = |
| 170 |
"#![enable(unwrap_variant_newtypes)] NewtypeVariant(a: true, b: 'b', a: false, c: -42)"; |
| 171 |
let expected_err = Err(SpannedError { |
| 172 |
code: ron::Error::DuplicateStructField { |
| 173 |
field: "a", |
| 174 |
outer: Some(String::from("NewtypeVariant")), |
| 175 |
}, |
| 176 |
span: Span { |
| 177 |
start: Position { line: 1, col: 69 }, |
| 178 |
end: Position { line: 1, col: 70 }, |
| 179 |
}, |
| 180 |
}); |
| 181 |
|
| 182 |
assert_eq!(ron::from_str::<TestEnum>(bogus_struct), expected_err); |
| 183 |
|
| 184 |
#[cfg(feature = "internal-span-substring-test")] |
| 185 |
check_error_span_inclusive::<TestEnum>(bogus_struct, expected_err, "a:"); |
| 186 |
} |
| 187 |
|
| 188 |
#[test] |
| 189 |
fn test_struct_fields() { |
| 190 |
let bogus_struct = "TestStruct(a: true, b: 'b', c: -42, d: \"gotcha\")"; |
| 191 |
let expected_err = Err(SpannedError { |
| 192 |
code: ron::Error::NoSuchStructField { |
| 193 |
expected: &["a", "b", "c"], |
| 194 |
found: String::from("d"), |
| 195 |
outer: Some(String::from("TestStruct")), |
| 196 |
}, |
| 197 |
span: Span { |
| 198 |
start: Position { line: 1, col: 37 }, |
| 199 |
end: Position { line: 1, col: 38 }, |
| 200 |
}, |
| 201 |
}); |
| 202 |
|
| 203 |
assert_eq!(ron::from_str::<TestStruct>(bogus_struct), expected_err); |
| 204 |
|
| 205 |
#[cfg(feature = "internal-span-substring-test")] |
| 206 |
check_error_span_inclusive::<TestStruct>(bogus_struct, expected_err, "d:"); |
| 207 |
|
| 208 |
assert_eq!( |
| 209 |
ron::from_str::<TestStruct>("TestStruct(a: true, c: -42)"), |
| 210 |
Err(SpannedError { |
| 211 |
code: ron::Error::MissingStructField { |
| 212 |
field: "b", |
| 213 |
outer: Some(String::from("TestStruct")), |
| 214 |
}, |
| 215 |
span: Span { |
| 216 |
start: Position { line: 1, col: 27 }, |
| 217 |
end: Position { line: 1, col: 27 }, |
| 218 |
} |
| 219 |
}) |
| 220 |
); |
| 221 |
|
| 222 |
let bogus_struct = "TestStruct(a: true, b: 'b', a: false, c: -42)"; |
| 223 |
let expected_err = Err(SpannedError { |
| 224 |
code: ron::Error::DuplicateStructField { |
| 225 |
field: "a", |
| 226 |
outer: Some(String::from("TestStruct")), |
| 227 |
}, |
| 228 |
span: Span { |
| 229 |
start: Position { line: 1, col: 29 }, |
| 230 |
end: Position { line: 1, col: 30 }, |
| 231 |
}, |
| 232 |
}); |
| 233 |
|
| 234 |
assert_eq!(ron::from_str::<TestStruct>(bogus_struct), expected_err); |
| 235 |
|
| 236 |
#[cfg(feature = "internal-span-substring-test")] |
| 237 |
check_error_span_inclusive::<TestStruct>(bogus_struct, expected_err, "a:"); |
| 238 |
} |
| 239 |
|
| 240 |
#[test] |
| 241 |
fn test_internally_tagged_enum() { |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
let bogus_struct = "(type: \"StructVariant\")"; |
| 248 |
let expected_err = Err(SpannedError { |
| 249 |
code: ron::Error::MissingStructField { |
| 250 |
field: "a", |
| 251 |
outer: None, |
| 252 |
}, |
| 253 |
span: Span { |
| 254 |
start: Position { line: 1, col: 23 }, |
| 255 |
end: Position { line: 1, col: 24 }, |
| 256 |
}, |
| 257 |
}); |
| 258 |
|
| 259 |
assert_eq!( |
| 260 |
ron::from_str::<TestEnumInternal>(bogus_struct), |
| 261 |
expected_err |
| 262 |
); |
| 263 |
|
| 264 |
#[cfg(feature = "internal-span-substring-test")] |
| 265 |
check_error_span_exclusive::<TestEnumInternal>(bogus_struct, expected_err, ")"); |
| 266 |
} |
| 267 |
|
| 268 |
#[test] |
| 269 |
fn test_adjacently_tagged_enum() { |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
assert_eq!( |
| 274 |
ron::from_str::<TestEnumAdjacent>("(type: StructVariant, content: (d: 4))"), |
| 275 |
Err(SpannedError { |
| 276 |
code: ron::Error::MissingStructField { |
| 277 |
field: "a", |
| 278 |
outer: Some(String::from("TestEnumAdjacent")), |
| 279 |
}, |
| 280 |
span: Span { |
| 281 |
start: Position { line: 1, col: 37 }, |
| 282 |
end: Position { line: 1, col: 37 }, |
| 283 |
} |
| 284 |
}) |
| 285 |
); |
| 286 |
} |
| 287 |
|
| 288 |
#[test] |
| 289 |
fn test_untagged_enum() { |
| 290 |
|
| 291 |
|
| 292 |
let bogus_struct = "(a: true, a: false)"; |
| 293 |
let expected_err = Err(SpannedError { |
| 294 |
code: ron::Error::Message(String::from( |
| 295 |
"data did not match any variant of untagged enum TestEnumUntagged", |
| 296 |
)), |
| 297 |
span: Span { |
| 298 |
start: Position { line: 1, col: 19 }, |
| 299 |
end: Position { line: 1, col: 20 }, |
| 300 |
}, |
| 301 |
}); |
| 302 |
|
| 303 |
assert_eq!( |
| 304 |
ron::from_str::<TestEnumUntagged>(bogus_struct), |
| 305 |
expected_err |
| 306 |
); |
| 307 |
|
| 308 |
#[cfg(feature = "internal-span-substring-test")] |
| 309 |
check_error_span_exclusive::<TestEnumUntagged>(bogus_struct, expected_err, ")"); |
| 310 |
} |