Skip to content

Commit 3c305c7

Browse files
committed
test(serde): Show NewType(None) behavior
1 parent 7bd5eaf commit 3c305c7

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

crates/toml/tests/serde/general.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,48 @@ fn serialize_struct_with_none_struct() {
15211521
}
15221522
}
15231523

1524+
#[test]
1525+
fn serialize_struct_with_newtype_with_none() {
1526+
#[derive(Debug, Default, PartialEq, Serialize, Deserialize)]
1527+
struct CanBeEmpty {
1528+
#[serde(default)]
1529+
a: NewType,
1530+
#[serde(default)]
1531+
b: NewType,
1532+
}
1533+
1534+
#[derive(Debug, Default, PartialEq, Serialize, Deserialize)]
1535+
struct NewType(Option<String>);
1536+
1537+
let input = "[bar]
1538+
1539+
[baz]
1540+
1541+
[bazv]
1542+
a = \"foo\"
1543+
1544+
[foo]";
1545+
let value: BTreeMap<String, CanBeEmpty> = crate::from_str(input).unwrap();
1546+
1547+
let mut expected: BTreeMap<String, CanBeEmpty> = BTreeMap::new();
1548+
expected.insert("bar".to_owned(), CanBeEmpty::default());
1549+
expected.insert("baz".to_owned(), CanBeEmpty::default());
1550+
expected.insert(
1551+
"bazv".to_owned(),
1552+
CanBeEmpty {
1553+
a: NewType(Some("foo".to_owned())),
1554+
b: NewType(None),
1555+
},
1556+
);
1557+
expected.insert("foo".to_owned(), CanBeEmpty::default());
1558+
1559+
assert_eq!(value, expected);
1560+
assert_data_eq!(
1561+
crate::to_string(&value).unwrap_err().to_string(),
1562+
str!["unsupported None value"].raw()
1563+
);
1564+
}
1565+
15241566
#[test]
15251567
fn span_for_sequence_as_map() {
15261568
#[allow(dead_code)]

crates/toml_edit/tests/serde/general.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,48 @@ fn serialize_struct_with_none_struct() {
14641464
}
14651465
}
14661466

1467+
#[test]
1468+
fn serialize_struct_with_newtype_with_none() {
1469+
#[derive(Debug, Default, PartialEq, Serialize, Deserialize)]
1470+
struct CanBeEmpty {
1471+
#[serde(default)]
1472+
a: NewType,
1473+
#[serde(default)]
1474+
b: NewType,
1475+
}
1476+
1477+
#[derive(Debug, Default, PartialEq, Serialize, Deserialize)]
1478+
struct NewType(Option<String>);
1479+
1480+
let input = "[bar]
1481+
1482+
[baz]
1483+
1484+
[bazv]
1485+
a = \"foo\"
1486+
1487+
[foo]";
1488+
let value: BTreeMap<String, CanBeEmpty> = crate::from_str(input).unwrap();
1489+
1490+
let mut expected: BTreeMap<String, CanBeEmpty> = BTreeMap::new();
1491+
expected.insert("bar".to_owned(), CanBeEmpty::default());
1492+
expected.insert("baz".to_owned(), CanBeEmpty::default());
1493+
expected.insert(
1494+
"bazv".to_owned(),
1495+
CanBeEmpty {
1496+
a: NewType(Some("foo".to_owned())),
1497+
b: NewType(None),
1498+
},
1499+
);
1500+
expected.insert("foo".to_owned(), CanBeEmpty::default());
1501+
1502+
assert_eq!(value, expected);
1503+
assert_data_eq!(
1504+
crate::to_string(&value).unwrap_err().to_string(),
1505+
str!["unsupported None value"].raw()
1506+
);
1507+
}
1508+
14671509
#[test]
14681510
fn span_for_sequence_as_map() {
14691511
#[allow(dead_code)]

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy