aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorEdward Barnard2017-05-03 12:29:21 +0100
committerEdward Barnard2017-05-03 12:29:48 +0100
commit56e2c5d4ca34b2e40d15ac5b2ceb98f34add6b1b (patch)
tree53447eb26505f2c671da06c8e8f4e4fd5e60a138 /tests
parenta9f71a02928fd275221ce5fc12c8dc2cfc55407e (diff)
downloadrust-plist-56e2c5d4ca34b2e40d15ac5b2ceb98f34add6b1b.tar.bz2
Serialise Option<T> struct fields as T if value if Some and ignore field if value is None.
Diffstat (limited to 'tests')
-rw-r--r--tests/serde_tests/mod.rs43
1 files changed, 37 insertions, 6 deletions
diff --git a/tests/serde_tests/mod.rs b/tests/serde_tests/mod.rs
index 5235896..e490a2c 100644
--- a/tests/serde_tests/mod.rs
+++ b/tests/serde_tests/mod.rs
@@ -107,10 +107,10 @@ fn dog() {
let comparison = &[StartDictionary(Some(1)),
StringValue("Dog".to_owned()),
- StartDictionary(Some(1)),
+ StartDictionary(None),
StringValue("inner".to_owned()),
StartArray(Some(1)),
- StartDictionary(Some(3)),
+ StartDictionary(None),
StringValue("a".to_owned()),
StringValue("".to_owned()),
StringValue("b".to_owned()),
@@ -165,14 +165,12 @@ fn cat() {
let comparison = &[StartDictionary(Some(1)),
StringValue("Cat".to_owned()),
- StartDictionary(Some(3)),
+ StartDictionary(None),
StringValue("age".to_owned()),
IntegerValue(12),
StringValue("name".to_owned()),
StringValue("Paws".to_owned()),
StringValue("firmware".to_owned()),
- StartDictionary(Some(1)),
- StringValue("Some".to_owned()),
StartArray(Some(9)),
IntegerValue(0),
IntegerValue(1),
@@ -185,7 +183,6 @@ fn cat() {
IntegerValue(8),
EndArray,
EndDictionary,
- EndDictionary,
EndDictionary];
assert_roundtrip(cat, Some(comparison));
@@ -206,3 +203,37 @@ fn newtype_struct() {
assert_roundtrip(newtype, Some(comparison));
}
+
+#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
+struct TypeWithOptions {
+ a: Option<String>,
+ b: Option<u32>,
+ c: Option<Box<TypeWithOptions>>
+}
+
+#[test]
+fn type_with_options() {
+ let inner = TypeWithOptions {
+ a: None,
+ b: Some(12),
+ c: None
+ };
+
+ let ty = TypeWithOptions {
+ a: Some("hello".to_owned()),
+ b: None,
+ c: Some(Box::new(inner))
+ };
+
+ let comparison = &[StartDictionary(None),
+ StringValue("a".to_owned()),
+ StringValue("hello".to_owned()),
+ StringValue("c".to_owned()),
+ StartDictionary(None),
+ StringValue("b".to_owned()),
+ IntegerValue(12),
+ EndDictionary,
+ EndDictionary];
+
+ assert_roundtrip(ty, Some(comparison));
+}