Skip to content

Commit

Permalink
Add untagged deserializtion tests
Browse files Browse the repository at this point in the history
I was unconvinced that `serde_yaml`'s deserializer was handling untagged enums correctly, but it appears that it is.

(cherry picked from commit 84be1a77a75b91639970d91dc4198f6d3755ec69)
  • Loading branch information
Fishrock123 authored and acatton committed May 3, 2024
1 parent 3bc5c63 commit 8d51e80
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions tests/test_de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,3 +715,40 @@ fn test_parse_number() {
let err = " 1 ".parse::<Number>().unwrap_err();
assert_eq!(err.to_string(), "failed to parse YAML number");
}

#[test]
fn test_enum_untagged() {
#[derive(Deserialize, PartialEq, Debug)]
#[serde(untagged)]
pub enum UntaggedEnum {
A { r#match: bool },
AB { r#match: String },
B { #[serde(rename = "if")] r#match: bool },
C(String)
}

// A
{
let expected = UntaggedEnum::A { r#match: true };
let deserialized: UntaggedEnum = serde_yaml::from_str("match: True").unwrap();
assert_eq!(expected, deserialized);
}
// AB
{
let expected = UntaggedEnum::AB { r#match: "T".to_owned() };
let deserialized: UntaggedEnum = serde_yaml::from_str("match: T").unwrap();
assert_eq!(expected, deserialized);
}
// B
{
let expected = UntaggedEnum::B { r#match: true };
let deserialized: UntaggedEnum = serde_yaml::from_str("if: True").unwrap();
assert_eq!(expected, deserialized);
}
// C
{
let expected = UntaggedEnum::C("match".to_owned());
let deserialized: UntaggedEnum = serde_yaml::from_str("match").unwrap();
assert_eq!(expected, deserialized);
}
}

0 comments on commit 8d51e80

Please sign in to comment.