diff options
author | Nadrieril | 2020-10-28 23:40:28 +0000 |
---|---|---|
committer | GitHub | 2020-10-28 23:40:28 +0000 |
commit | f8234684b1129dac84d09adfb24bdc0f98448b9b (patch) | |
tree | 1440cfb514fa856718074f2b1ebce801459b5c0c /serde_dhall/tests/simple_value.rs | |
parent | 70727acbda68e104f60ae1dbbe95adbcec08a628 (diff) | |
parent | af16e9699799f8cfbd228e2832e1d5df3653116b (diff) |
Merge pull request #189 from Nadrieril/serialize
Diffstat (limited to 'serde_dhall/tests/simple_value.rs')
-rw-r--r-- | serde_dhall/tests/simple_value.rs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/serde_dhall/tests/simple_value.rs b/serde_dhall/tests/simple_value.rs new file mode 100644 index 0000000..3bd9d64 --- /dev/null +++ b/serde_dhall/tests/simple_value.rs @@ -0,0 +1,58 @@ +mod simple_value { + use serde::{Deserialize, Serialize}; + use serde_dhall::{ + from_str, serialize, FromDhall, NumKind, SimpleValue, ToDhall, + }; + + fn assert_de<T>(s: &str, x: T) + where + T: FromDhall + PartialEq + std::fmt::Debug, + { + assert_eq!(from_str(s).parse::<T>().map_err(|e| e.to_string()), Ok(x)); + } + fn assert_ser<T>(s: &str, x: T) + where + T: ToDhall + PartialEq + std::fmt::Debug, + { + assert_eq!( + serialize(&x).to_string().map_err(|e| e.to_string()), + Ok(s.to_string()) + ); + } + fn assert_serde<T>(s: &str, x: T) + where + T: ToDhall + FromDhall + PartialEq + std::fmt::Debug + Clone, + { + assert_de(s, x.clone()); + assert_ser(s, x); + } + + #[test] + fn test_serde() { + let bool_true = SimpleValue::Num(NumKind::Bool(true)); + // https://github.com/Nadrieril/dhall-rust/issues/184 + assert_serde("[True]", vec![bool_true.clone()]); + + assert_de("< Foo >.Foo", SimpleValue::Union("Foo".into(), None)); + assert_de( + "< Foo: Bool >.Foo True", + SimpleValue::Union("Foo".into(), Some(Box::new(bool_true.clone()))), + ); + + assert_eq!( + serialize(&SimpleValue::Union("Foo".into(), None)).to_string().map_err(|e| e.to_string()), + Err("cannot serialize value without a type annotation: Union(\"Foo\", None)".to_string()) + ); + + #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] + struct Foo { + foo: SimpleValue, + } + assert_serde( + "{ foo = True }", + Foo { + foo: bool_true.clone(), + }, + ); + } +} |