From 3b728aff86a086f71f013b77a715c33748d9f6a8 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 28 Oct 2020 21:45:42 +0000 Subject: Make type annotation optional to allow serializing SimpleValue --- serde_dhall/tests/de.rs | 34 +---------------------- serde_dhall/tests/serde.rs | 8 ++++-- serde_dhall/tests/simple_value.rs | 57 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 35 deletions(-) create mode 100644 serde_dhall/tests/simple_value.rs (limited to 'serde_dhall/tests') diff --git a/serde_dhall/tests/de.rs b/serde_dhall/tests/de.rs index 0b43de2..f49bee4 100644 --- a/serde_dhall/tests/de.rs +++ b/serde_dhall/tests/de.rs @@ -1,5 +1,5 @@ use serde::Deserialize; -use serde_dhall::{from_str, FromDhall, NumKind, SimpleValue}; +use serde_dhall::{from_str, FromDhall}; #[test] fn test_de_untyped() { @@ -57,37 +57,5 @@ fn test_de_untyped() { assert!(from_str("List/length [True, 42]").parse::().is_err()); } -#[test] -fn test_de_simplevalue() { - let bool_true = SimpleValue::Num(NumKind::Bool(true)); - // https://github.com/Nadrieril/dhall-rust/issues/184 - assert_eq!( - from_str("[ True ]").parse::>().unwrap(), - vec![bool_true.clone()] - ); - - assert_eq!( - from_str("< Foo >.Foo").parse::().unwrap(), - SimpleValue::Union("Foo".into(), None) - ); - assert_eq!( - from_str("< Foo: Bool >.Foo True") - .parse::() - .unwrap(), - SimpleValue::Union("Foo".into(), Some(Box::new(bool_true.clone()))) - ); - - #[derive(Debug, PartialEq, Eq, Deserialize)] - struct Foo { - foo: SimpleValue, - } - assert_eq!( - from_str("{ foo = True }").parse::().unwrap(), - Foo { - foo: bool_true.clone() - }, - ); -} - // TODO: test various builder configurations // In particular test cloning and reusing builder diff --git a/serde_dhall/tests/serde.rs b/serde_dhall/tests/serde.rs index 39f2f79..ce25380 100644 --- a/serde_dhall/tests/serde.rs +++ b/serde_dhall/tests/serde.rs @@ -75,9 +75,13 @@ mod serde { #[test] fn optional() { - assert_serde::>("None Natural", None); - assert_serde::>("None Text", None); + assert_serde("None Natural", None::); + assert_serde("None Text", None::); assert_serde("Some 1", Some(1u64)); + assert_eq!( + serialize(&None::).to_string().map_err(|e| e.to_string()), + Err("cannot serialize value without a type annotation: Optional(None)".to_string()) + ); } #[test] diff --git a/serde_dhall/tests/simple_value.rs b/serde_dhall/tests/simple_value.rs new file mode 100644 index 0000000..d2792d4 --- /dev/null +++ b/serde_dhall/tests/simple_value.rs @@ -0,0 +1,57 @@ +mod simple_value { + use serde::{Deserialize, Serialize}; + use serde_dhall::{ + from_str, serialize, FromDhall, NumKind, SimpleValue, ToDhall, + }; + + fn assert_de(s: &str, x: T) + where + T: FromDhall + PartialEq + std::fmt::Debug, + { + assert_eq!(from_str(s).parse::().map_err(|e| e.to_string()), Ok(x)); + } + fn assert_ser(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(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::Optional(None)).to_string().map_err(|e| e.to_string()), + // Err("cannot serialize value without a type annotation: Optional(None)".to_string()) + // ); + + // #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] + // struct Foo { + // foo: SimpleValue, + // } + // assert_serde( + // "{ foo = True }", + // Foo { + // foo: bool_true.clone(), + // }, + // ); + } +} -- cgit v1.2.3