From e070270c3f1f10d46281ed7751ff95e15092e7f4 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 10 May 2020 22:09:43 +0100 Subject: Implement serialization --- serde_dhall/tests/de.rs | 59 +----------------- serde_dhall/tests/serde.rs | 145 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+), 58 deletions(-) create mode 100644 serde_dhall/tests/serde.rs (limited to 'serde_dhall/tests') diff --git a/serde_dhall/tests/de.rs b/serde_dhall/tests/de.rs index 3abaad2..0b43de2 100644 --- a/serde_dhall/tests/de.rs +++ b/serde_dhall/tests/de.rs @@ -1,62 +1,5 @@ use serde::Deserialize; -use serde_dhall::{from_str, FromDhall, NumKind, SimpleValue, StaticType}; - -#[test] -fn test_de_typed() { - fn parse(s: &str) -> T { - from_str(s).static_type_annotation().parse().unwrap() - } - - assert_eq!(parse::("True"), true); - - assert_eq!(parse::("1"), 1); - assert_eq!(parse::("1"), 1); - assert_eq!(parse::("1"), 1); - - assert_eq!(parse::("+1"), 1); - assert_eq!(parse::("+1"), 1); - assert_eq!(parse::("+1"), 1); - - assert_eq!(parse::("1.0"), 1.0); - assert_eq!(parse::("1.0"), 1.0); - - assert_eq!(parse::(r#""foo""#), "foo".to_owned()); - assert_eq!(parse::>("[] : List Natural"), >::new()); - assert_eq!(parse::>("[1, 2]"), vec![1, 2]); - assert_eq!(parse::>("None Natural"), None); - assert_eq!(parse::>("Some 1"), Some(1)); - - assert_eq!( - parse::<(u64, String)>(r#"{ _1 = 1, _2 = "foo" }"#), - (1, "foo".to_owned()) - ); - - #[derive(Debug, PartialEq, Eq, Deserialize, StaticType)] - struct Foo { - x: u64, - y: i64, - } - assert_eq!(parse::("{ x = 1, y = -2 }"), Foo { x: 1, y: -2 }); - - #[derive(Debug, PartialEq, Eq, Deserialize, StaticType)] - enum Bar { - X(u64), - Y(i64), - } - assert_eq!(parse::("< X: Natural | Y: Integer >.X 1"), Bar::X(1)); - - #[derive(Debug, PartialEq, Eq, Deserialize, StaticType)] - enum Baz { - X, - Y(i64), - } - assert_eq!(parse::("< X | Y: Integer >.X"), Baz::X); - - assert!(from_str("< X | Y: Integer >.Y") - .static_type_annotation() - .parse::() - .is_err()); -} +use serde_dhall::{from_str, FromDhall, NumKind, SimpleValue}; #[test] fn test_de_untyped() { diff --git a/serde_dhall/tests/serde.rs b/serde_dhall/tests/serde.rs new file mode 100644 index 0000000..39f2f79 --- /dev/null +++ b/serde_dhall/tests/serde.rs @@ -0,0 +1,145 @@ +mod serde { + use serde::{Deserialize, Serialize}; + use serde_dhall::{from_str, serialize, FromDhall, StaticType, ToDhall}; + + fn assert_de(s: &str, x: T) + where + T: FromDhall + StaticType + PartialEq + std::fmt::Debug, + { + assert_eq!( + from_str(s) + .static_type_annotation() + .parse::() + .map_err(|e| e.to_string()), + Ok(x) + ); + } + fn assert_ser(s: &str, x: T) + where + T: ToDhall + StaticType + PartialEq + std::fmt::Debug, + { + assert_eq!( + serialize(&x) + .static_type_annotation() + .to_string() + .map_err(|e| e.to_string()), + Ok(s.to_string()) + ); + } + fn assert_serde(s: &str, x: T) + where + T: ToDhall + + FromDhall + + StaticType + + PartialEq + + std::fmt::Debug + + Clone, + { + assert_de(s, x.clone()); + assert_ser(s, x); + } + + #[test] + fn numbers() { + assert_serde("True", true); + + assert_serde("1", 1u64); + assert_serde("1", 1u32); + assert_serde("1", 1usize); + + assert_serde("+1", 1i64); + assert_serde("+1", 1i32); + assert_serde("+1", 1isize); + + assert_serde("1.0", 1.0f64); + assert_serde("1.0", 1.0f32); + } + + #[test] + fn text() { + assert_serde(r#""foo""#, "foo".to_owned()); + assert_ser(r#""foo""#, "foo"); + } + + #[test] + fn list() { + assert_serde("[] : List Natural", >::new()); + assert_serde("[] : List Text", >::new()); + assert_serde( + r#"["foo", "bar"]"#, + vec!["foo".to_owned(), "bar".to_owned()], + ); + assert_ser(r#"["foo", "bar"]"#, vec!["foo", "bar"]); + assert_serde::>("[1, 2]", vec![1, 2]); + } + + #[test] + fn optional() { + assert_serde::>("None Natural", None); + assert_serde::>("None Text", None); + assert_serde("Some 1", Some(1u64)); + } + + #[test] + fn tuple() { + assert_serde::<()>(r#"{=}"#, ()); + assert_serde::<(u64, String)>( + r#"{ _1 = 1, _2 = "foo" }"#, + (1, "foo".to_owned()), + ); + assert_serde::<(u64, u64, u64, u64)>( + r#"{ _1 = 1, _2 = 2, _3 = 3, _4 = 4 }"#, + (1, 2, 3, 4), + ); + } + + #[test] + fn structs() { + // #[derive( + // Debug, Clone, PartialEq, Eq, Deserialize, Serialize, StaticType, + // )] + // struct Foo; + // assert_serde::("{=}", Foo); + + // #[derive( + // Debug, Clone, PartialEq, Eq, Deserialize, Serialize, StaticType, + // )] + // struct Bar(u64); + // assert_serde::("{ _1 = 1 }", Bar (1)); + + #[derive( + Debug, Clone, PartialEq, Eq, Deserialize, Serialize, StaticType, + )] + struct Baz { + x: u64, + y: i64, + } + assert_serde::("{ x = 1, y = -2 }", Baz { x: 1, y: -2 }); + } + + #[test] + fn enums() { + #[derive( + Debug, Clone, PartialEq, Eq, Deserialize, Serialize, StaticType, + )] + enum Foo { + X(u64), + Y(i64), + } + assert_serde::("< X: Natural | Y: Integer >.X 1", Foo::X(1)); + + #[derive( + Debug, Clone, PartialEq, Eq, Deserialize, Serialize, StaticType, + )] + enum Bar { + X, + Y(i64), + } + assert_serde::("< X | Y: Integer >.X", Bar::X); + + assert!(from_str("< X | Y: Integer >.Y") + .static_type_annotation() + .parse::() + .is_err()); + } +} -- cgit v1.2.3 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 From 93ed3cf67c49bf7016b8b1780d873cfdffcb84c5 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 28 Oct 2020 23:21:19 +0000 Subject: Implement SimpleValue serialization --- serde_dhall/tests/simple_value.rs | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) (limited to 'serde_dhall/tests') diff --git a/serde_dhall/tests/simple_value.rs b/serde_dhall/tests/simple_value.rs index d2792d4..3bd9d64 100644 --- a/serde_dhall/tests/simple_value.rs +++ b/serde_dhall/tests/simple_value.rs @@ -31,27 +31,28 @@ mod simple_value { 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_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()) - // ); + 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(), - // }, - // ); + #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] + struct Foo { + foo: SimpleValue, + } + assert_serde( + "{ foo = True }", + Foo { + foo: bool_true.clone(), + }, + ); } } -- cgit v1.2.3 From 1a53f10c6ed1ef65621b39c40e05da79161047cc Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 28 Oct 2020 23:26:22 +0000 Subject: Move some tests --- serde_dhall/tests/de.rs | 61 ---------------------------------------------- serde_dhall/tests/serde.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 61 deletions(-) delete mode 100644 serde_dhall/tests/de.rs (limited to 'serde_dhall/tests') diff --git a/serde_dhall/tests/de.rs b/serde_dhall/tests/de.rs deleted file mode 100644 index f49bee4..0000000 --- a/serde_dhall/tests/de.rs +++ /dev/null @@ -1,61 +0,0 @@ -use serde::Deserialize; -use serde_dhall::{from_str, FromDhall}; - -#[test] -fn test_de_untyped() { - use std::collections::BTreeMap; - use std::collections::HashMap; - - fn parse(s: &str) -> T { - from_str(s).parse().unwrap() - } - - // Test tuples on record of wrong type - assert_eq!( - parse::<(u64, String, i64)>(r#"{ y = "foo", x = 1, z = +42 }"#), - (1, "foo".to_owned(), 42) - ); - - let mut expected_map = HashMap::new(); - expected_map.insert("x".to_string(), 1); - expected_map.insert("y".to_string(), 2); - assert_eq!( - parse::>("{ x = 1, y = 2 }"), - expected_map - ); - assert_eq!( - parse::>("toMap { x = 1, y = 2 }"), - expected_map - ); - - let mut expected_map = HashMap::new(); - expected_map.insert("if".to_string(), 1); - expected_map.insert("FOO_BAR".to_string(), 2); - expected_map.insert("baz-kux".to_string(), 3); - assert_eq!( - parse::>("{ `if` = 1, FOO_BAR = 2, baz-kux = 3 }"), - expected_map - ); - - let mut expected_map = BTreeMap::new(); - expected_map.insert("x".to_string(), 1); - expected_map.insert("y".to_string(), 2); - assert_eq!( - parse::>("{ x = 1, y = 2 }"), - expected_map - ); - - #[derive(Debug, PartialEq, Eq, Deserialize)] - struct Foo { - x: u64, - y: Option, - } - // Omit optional field - assert_eq!(parse::("{ x = 1 }"), Foo { x: 1, y: None }); - - // https://github.com/Nadrieril/dhall-rust/issues/155 - assert!(from_str("List/length [True, 42]").parse::().is_err()); -} - -// 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 ce25380..03bcc58 100644 --- a/serde_dhall/tests/serde.rs +++ b/serde_dhall/tests/serde.rs @@ -146,4 +146,63 @@ mod serde { .parse::() .is_err()); } + + #[test] + fn test_de_untyped() { + use std::collections::BTreeMap; + use std::collections::HashMap; + + fn parse(s: &str) -> T { + from_str(s).parse().unwrap() + } + + // Test tuples on record of wrong type. Fields are just taken in alphabetic order. + assert_eq!( + parse::<(u64, String, i64)>(r#"{ y = "foo", x = 1, z = +42 }"#), + (1, "foo".to_owned(), 42) + ); + + let mut expected_map = HashMap::new(); + expected_map.insert("x".to_string(), 1); + expected_map.insert("y".to_string(), 2); + assert_eq!( + parse::>("{ x = 1, y = 2 }"), + expected_map + ); + assert_eq!( + parse::>("toMap { x = 1, y = 2 }"), + expected_map + ); + + let mut expected_map = HashMap::new(); + expected_map.insert("if".to_string(), 1); + expected_map.insert("FOO_BAR".to_string(), 2); + expected_map.insert("baz-kux".to_string(), 3); + assert_eq!( + parse::>("{ `if` = 1, FOO_BAR = 2, baz-kux = 3 }"), + expected_map + ); + + let mut expected_map = BTreeMap::new(); + expected_map.insert("x".to_string(), 1); + expected_map.insert("y".to_string(), 2); + assert_eq!( + parse::>("{ x = 1, y = 2 }"), + expected_map + ); + + #[derive(Debug, PartialEq, Eq, Deserialize)] + struct Foo { + x: u64, + y: Option, + } + // Omit optional field + assert_eq!(parse::("{ x = 1 }"), Foo { x: 1, y: None }); + + // https://github.com/Nadrieril/dhall-rust/issues/155 + assert!(from_str("List/length [True, 42]").parse::().is_err()); + } + + // TODO: test various builder configurations + // In particular test cloning and reusing builder } -- cgit v1.2.3 From af16e9699799f8cfbd228e2832e1d5df3653116b Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 28 Oct 2020 23:31:57 +0000 Subject: Fix clippy and formatting --- serde_dhall/tests/serde.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'serde_dhall/tests') diff --git a/serde_dhall/tests/serde.rs b/serde_dhall/tests/serde.rs index 03bcc58..4c184e7 100644 --- a/serde_dhall/tests/serde.rs +++ b/serde_dhall/tests/serde.rs @@ -179,7 +179,9 @@ mod serde { expected_map.insert("FOO_BAR".to_string(), 2); expected_map.insert("baz-kux".to_string(), 3); assert_eq!( - parse::>("{ `if` = 1, FOO_BAR = 2, baz-kux = 3 }"), + parse::>( + "{ `if` = 1, FOO_BAR = 2, baz-kux = 3 }" + ), expected_map ); -- cgit v1.2.3