From 5fb5318714ba0594e05f2a133344065b2eaf1bf6 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 8 Sep 2023 20:02:37 -0500 Subject: [PATCH] test(ser): Show existing enum behavior --- crates/toml/tests/testsuite/serde.rs | 46 ++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/crates/toml/tests/testsuite/serde.rs b/crates/toml/tests/testsuite/serde.rs index 03b40259..cec3f95f 100644 --- a/crates/toml/tests/testsuite/serde.rs +++ b/crates/toml/tests/testsuite/serde.rs @@ -1174,3 +1174,49 @@ y = 7 let raw = toml::to_string(&input).unwrap(); snapbox::assert_eq(expected, raw); } + +#[test] +fn serialize_array_with_enum_of_optional_struct_field() { + #[derive(Debug, Deserialize, Serialize)] + struct Document { + values: Vec, + } + + #[derive(Debug, Deserialize, Serialize)] + enum Choice { + Optional(OptionalField), + Empty, + } + + #[derive(Debug, Deserialize, Serialize)] + struct OptionalField { + x: u8, + y: Option, + } + + let input = Document { + values: vec![ + Choice::Optional(OptionalField { x: 0, y: Some(4) }), + Choice::Empty, + Choice::Optional(OptionalField { x: 2, y: Some(5) }), + Choice::Optional(OptionalField { x: 3, y: Some(7) }), + ], + }; + let expected = "values = [{}, \"Empty\", {}, {}] +"; + let raw = toml::to_string(&input).unwrap(); + snapbox::assert_eq(expected, raw); + + let input = Document { + values: vec![ + Choice::Optional(OptionalField { x: 0, y: Some(4) }), + Choice::Empty, + Choice::Optional(OptionalField { x: 2, y: None }), + Choice::Optional(OptionalField { x: 3, y: Some(7) }), + ], + }; + let expected = "values = [{}, \"Empty\", {}, {}] +"; + let raw = toml::to_string(&input).unwrap(); + snapbox::assert_eq(expected, raw); +}