From c3a6ff060e8ed9c7da4ec371aa8c37db81a50a7b Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 8 Sep 2023 19:59:22 -0500 Subject: [PATCH] test(ser): Verify optional field behavior --- crates/toml/tests/testsuite/serde.rs | 59 ++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/crates/toml/tests/testsuite/serde.rs b/crates/toml/tests/testsuite/serde.rs index 1d2fe4f0..03b40259 100644 --- a/crates/toml/tests/testsuite/serde.rs +++ b/crates/toml/tests/testsuite/serde.rs @@ -1115,3 +1115,62 @@ fn serialize_array_with_none_value() { let err = toml::to_string(&input).unwrap_err(); snapbox::assert_eq("unsupported None value", err.to_string()); } + +#[test] +fn serialize_array_with_optional_struct_field() { + #[derive(Debug, Deserialize, Serialize)] + struct Document { + values: Vec, + } + + #[derive(Debug, Deserialize, Serialize)] + struct OptionalField { + x: u8, + y: Option, + } + + let input = Document { + values: vec![ + OptionalField { x: 0, y: Some(4) }, + OptionalField { x: 2, y: Some(5) }, + OptionalField { x: 3, y: Some(7) }, + ], + }; + let expected = "\ +[[values]] +x = 0 +y = 4 + +[[values]] +x = 2 +y = 5 + +[[values]] +x = 3 +y = 7 +"; + let raw = toml::to_string(&input).unwrap(); + snapbox::assert_eq(expected, raw); + + let input = Document { + values: vec![ + OptionalField { x: 0, y: Some(4) }, + OptionalField { x: 2, y: None }, + OptionalField { x: 3, y: Some(7) }, + ], + }; + let expected = "\ +[[values]] +x = 0 +y = 4 + +[[values]] +x = 2 + +[[values]] +x = 3 +y = 7 +"; + let raw = toml::to_string(&input).unwrap(); + snapbox::assert_eq(expected, raw); +}