aboutsummaryrefslogtreecommitdiffstats
path: root/tests/serde_/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/serde_/mod.rs')
-rw-r--r--tests/serde_/mod.rs209
1 files changed, 209 insertions, 0 deletions
diff --git a/tests/serde_/mod.rs b/tests/serde_/mod.rs
new file mode 100644
index 0000000..a2cbd41
--- /dev/null
+++ b/tests/serde_/mod.rs
@@ -0,0 +1,209 @@
+use plist::{Deserializer, EventWriter, PlistEvent, Serializer};
+use plist::PlistEvent::*;
+use serde::{Deserialize, Serialize};
+use std::fmt::Debug;
+
+struct VecWriter {
+ events: Vec<PlistEvent>,
+}
+
+impl VecWriter {
+ pub fn new() -> VecWriter {
+ VecWriter { events: Vec::new() }
+ }
+
+ pub fn into_inner(self) -> Vec<PlistEvent> {
+ self.events
+ }
+}
+
+impl EventWriter for VecWriter {
+ fn write(&mut self, event: &PlistEvent) -> Result<(), ()> {
+ self.events.push(event.clone());
+ Ok(())
+ }
+}
+
+fn new_serializer() -> Serializer<VecWriter> {
+ Serializer::new(VecWriter::new())
+}
+
+fn new_deserializer(events: Vec<PlistEvent>) -> Deserializer<Vec<Result<PlistEvent, ()>>, ()> {
+ let result_events = events.into_iter().map(|e| Ok(e)).collect();
+ Deserializer::new(result_events)
+}
+
+fn assert_roundtrip<T>(obj: T, comparison: Option<&[PlistEvent]>)
+ where T: Debug + Deserialize + PartialEq + Serialize
+{
+ let mut se = new_serializer();
+
+ obj.serialize(&mut se).unwrap();
+
+ let events = se.into_inner().into_inner();
+
+ match comparison {
+ Some(comparison) => {
+ assert_eq!(&events[..], comparison);
+ }
+ None => (),
+ }
+
+ let mut de = new_deserializer(events);
+
+ let new_obj = T::deserialize(&mut de).unwrap();
+
+ assert_eq!(new_obj, obj);
+}
+
+#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
+enum Animal {
+ Cow,
+ Dog(DogOuter),
+ Frog(Result<String, bool>, Vec<f64>),
+ Cat {
+ age: usize,
+ name: String,
+ firmware: Option<Vec<u8>>,
+ },
+}
+
+#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
+struct DogOuter {
+ inner: Vec<DogInner>,
+}
+
+#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
+struct DogInner {
+ a: (),
+ b: usize,
+ c: Vec<String>,
+}
+
+#[test]
+fn cow() {
+ let cow = Animal::Cow;
+
+ let comparison = &[StartDictionary(Some(1)),
+ StringValue("Cow".to_owned()),
+ StringValue("".to_owned()),
+ EndDictionary];
+
+ assert_roundtrip(cow, Some(comparison));
+}
+
+
+#[test]
+fn dog() {
+ let dog = Animal::Dog(DogOuter {
+ inner: vec![DogInner {
+ a: (),
+ b: 12,
+ c: vec!["a".to_string(), "b".to_string()],
+ }],
+ });
+
+ let comparison = &[StartDictionary(Some(1)),
+ StringValue("Dog".to_owned()),
+ StartDictionary(Some(1)),
+ StringValue("inner".to_owned()),
+ StartArray(Some(1)),
+ StartDictionary(Some(3)),
+ StringValue("a".to_owned()),
+ StringValue("".to_owned()),
+ StringValue("b".to_owned()),
+ IntegerValue(12),
+ StringValue("c".to_owned()),
+ StartArray(Some(2)),
+ StringValue("a".to_owned()),
+ StringValue("b".to_owned()),
+ EndArray,
+ EndDictionary,
+ EndArray,
+ EndDictionary,
+ EndDictionary];
+
+ assert_roundtrip(dog, Some(comparison));
+}
+
+
+#[test]
+fn frog() {
+ let frog = Animal::Frog(Ok("hello".to_owned()),
+ vec![1.0, 2.0, 3.14159, 0.000000001, 1.27e31]);
+
+ let comparison = &[StartDictionary(Some(1)),
+ StringValue("Frog".to_owned()),
+ StartArray(Some(2)),
+ StartDictionary(Some(1)),
+ StringValue("Ok".to_owned()),
+ StringValue("hello".to_owned()),
+ EndDictionary,
+ StartArray(Some(5)),
+ RealValue(1.0),
+ RealValue(2.0),
+ RealValue(3.14159),
+ RealValue(0.000000001),
+ RealValue(1.27e31),
+ EndArray,
+ EndArray,
+ EndDictionary];
+
+ assert_roundtrip(frog, Some(comparison));
+}
+
+
+#[test]
+fn cat() {
+ let cat = Animal::Cat {
+ age: 12,
+ name: "Paws".to_owned(),
+ firmware: Some(vec![0, 1, 2, 3, 4, 5, 6, 7, 8]),
+ };
+
+ let comparison = &[StartDictionary(Some(1)),
+ StringValue("Cat".to_owned()),
+ StartDictionary(Some(3)),
+ 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),
+ IntegerValue(2),
+ IntegerValue(3),
+ IntegerValue(4),
+ IntegerValue(5),
+ IntegerValue(6),
+ IntegerValue(7),
+ IntegerValue(8),
+ EndArray,
+ EndDictionary,
+ EndDictionary,
+ EndDictionary];
+
+ assert_roundtrip(cat, Some(comparison));
+}
+
+#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
+struct NewtypeStruct(NewtypeInner);
+
+#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
+struct NewtypeInner(u8, u8, u8);
+
+#[test]
+fn newtype_struct() {
+ let newtype = NewtypeStruct(NewtypeInner(34, 32, 13));
+
+ let comparison = &[StartArray(Some(3)),
+ IntegerValue(34),
+ IntegerValue(32),
+ IntegerValue(13),
+ EndArray];
+
+ assert_roundtrip(newtype, Some(comparison));
+}