Skip to content

Commit c3a6ff0

Browse files
committed
test(ser): Verify optional field behavior
1 parent 39eea9e commit c3a6ff0

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

crates/toml/tests/testsuite/serde.rs

+59
Original file line numberDiff line numberDiff line change
@@ -1115,3 +1115,62 @@ fn serialize_array_with_none_value() {
11151115
let err = toml::to_string(&input).unwrap_err();
11161116
snapbox::assert_eq("unsupported None value", err.to_string());
11171117
}
1118+
1119+
#[test]
1120+
fn serialize_array_with_optional_struct_field() {
1121+
#[derive(Debug, Deserialize, Serialize)]
1122+
struct Document {
1123+
values: Vec<OptionalField>,
1124+
}
1125+
1126+
#[derive(Debug, Deserialize, Serialize)]
1127+
struct OptionalField {
1128+
x: u8,
1129+
y: Option<u8>,
1130+
}
1131+
1132+
let input = Document {
1133+
values: vec![
1134+
OptionalField { x: 0, y: Some(4) },
1135+
OptionalField { x: 2, y: Some(5) },
1136+
OptionalField { x: 3, y: Some(7) },
1137+
],
1138+
};
1139+
let expected = "\
1140+
[[values]]
1141+
x = 0
1142+
y = 4
1143+
1144+
[[values]]
1145+
x = 2
1146+
y = 5
1147+
1148+
[[values]]
1149+
x = 3
1150+
y = 7
1151+
";
1152+
let raw = toml::to_string(&input).unwrap();
1153+
snapbox::assert_eq(expected, raw);
1154+
1155+
let input = Document {
1156+
values: vec![
1157+
OptionalField { x: 0, y: Some(4) },
1158+
OptionalField { x: 2, y: None },
1159+
OptionalField { x: 3, y: Some(7) },
1160+
],
1161+
};
1162+
let expected = "\
1163+
[[values]]
1164+
x = 0
1165+
y = 4
1166+
1167+
[[values]]
1168+
x = 2
1169+
1170+
[[values]]
1171+
x = 3
1172+
y = 7
1173+
";
1174+
let raw = toml::to_string(&input).unwrap();
1175+
snapbox::assert_eq(expected, raw);
1176+
}

0 commit comments

Comments
 (0)