Skip to content

Commit 64e45f1

Browse files
committed
Add tests for non-externally tagged enums
1 parent 49460ac commit 64e45f1

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

tests/393_serde_errors.rs

+69
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,24 @@ enum TestEnum {
77
NewtypeVariant(TestStruct),
88
}
99

10+
#[derive(Debug, serde::Deserialize, PartialEq)]
11+
#[serde(tag = "type")]
12+
enum TestEnumInternal {
13+
StructVariant { a: bool },
14+
}
15+
16+
#[derive(Debug, serde::Deserialize, PartialEq)]
17+
#[serde(tag = "type", content = "content")]
18+
enum TestEnumAdjacent {
19+
StructVariant { a: bool },
20+
}
21+
22+
#[derive(Debug, serde::Deserialize, PartialEq)]
23+
#[serde(untagged)]
24+
enum TestEnumUntagged {
25+
StructVariant { a: bool },
26+
}
27+
1028
#[derive(Debug, serde::Deserialize, PartialEq)]
1129
#[serde(deny_unknown_fields)]
1230
struct TestStruct {
@@ -144,3 +162,54 @@ fn test_struct_fields() {
144162
})
145163
);
146164
}
165+
166+
#[test]
167+
fn test_internally_tagged_enum() {
168+
// Note: Not extracting the variant type is not great,
169+
// but at least not wrong either
170+
// Since the error occurs in serde-generated user code,
171+
// after successfully deserialising, we cannot annotate
172+
173+
assert_eq!(
174+
ron::from_str::<TestEnumInternal>("(type: \"StructVariant\")"),
175+
Err(SpannedError {
176+
code: Error::MissingStructField {
177+
field: "a",
178+
outer: None,
179+
},
180+
position: Position { line: 1, col: 24 },
181+
})
182+
);
183+
}
184+
185+
#[test]
186+
fn test_adjacently_tagged_enum() {
187+
// Note: TestEnumAdjacent makes sense here since we are now treating
188+
// the enum as a struct
189+
190+
assert_eq!(
191+
ron::from_str::<TestEnumAdjacent>("(type: \"StructVariant\", content: (d: 4))"),
192+
Err(SpannedError {
193+
code: Error::MissingStructField {
194+
field: "a",
195+
outer: Some(String::from("TestEnumAdjacent")),
196+
},
197+
position: Position { line: 1, col: 39 },
198+
})
199+
);
200+
}
201+
202+
#[test]
203+
fn test_untagged_enum() {
204+
// Note: Errors inside untagged enums are not bubbled up
205+
206+
assert_eq!(
207+
ron::from_str::<TestEnumUntagged>("(a: true, a: false)"),
208+
Err(SpannedError {
209+
code: Error::Message(String::from(
210+
"data did not match any variant of untagged enum TestEnumUntagged"
211+
)),
212+
position: Position { line: 1, col: 20 },
213+
})
214+
);
215+
}

0 commit comments

Comments
 (0)