summaryrefslogtreecommitdiff
path: root/serde_dhall/tests
diff options
context:
space:
mode:
authorNadrieril2020-05-10 22:09:43 +0100
committerNadrieril2020-10-28 22:52:41 +0000
commite070270c3f1f10d46281ed7751ff95e15092e7f4 (patch)
tree46c22c75f209c5112030f3db6609ed64ff4888d3 /serde_dhall/tests
parent5f3ca811f09dcf6f09fb9b60fcd2664d06762f39 (diff)
Implement serialization
Diffstat (limited to 'serde_dhall/tests')
-rw-r--r--serde_dhall/tests/de.rs59
-rw-r--r--serde_dhall/tests/serde.rs145
2 files changed, 146 insertions, 58 deletions
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<T: FromDhall + StaticType>(s: &str) -> T {
- from_str(s).static_type_annotation().parse().unwrap()
- }
-
- assert_eq!(parse::<bool>("True"), true);
-
- assert_eq!(parse::<u64>("1"), 1);
- assert_eq!(parse::<u32>("1"), 1);
- assert_eq!(parse::<usize>("1"), 1);
-
- assert_eq!(parse::<i64>("+1"), 1);
- assert_eq!(parse::<i32>("+1"), 1);
- assert_eq!(parse::<isize>("+1"), 1);
-
- assert_eq!(parse::<f64>("1.0"), 1.0);
- assert_eq!(parse::<f32>("1.0"), 1.0);
-
- assert_eq!(parse::<String>(r#""foo""#), "foo".to_owned());
- assert_eq!(parse::<Vec<u64>>("[] : List Natural"), <Vec<u64>>::new());
- assert_eq!(parse::<Vec<u64>>("[1, 2]"), vec![1, 2]);
- assert_eq!(parse::<Option<u64>>("None Natural"), None);
- assert_eq!(parse::<Option<u64>>("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::<Foo>("{ x = 1, y = -2 }"), Foo { x: 1, y: -2 });
-
- #[derive(Debug, PartialEq, Eq, Deserialize, StaticType)]
- enum Bar {
- X(u64),
- Y(i64),
- }
- assert_eq!(parse::<Bar>("< X: Natural | Y: Integer >.X 1"), Bar::X(1));
-
- #[derive(Debug, PartialEq, Eq, Deserialize, StaticType)]
- enum Baz {
- X,
- Y(i64),
- }
- assert_eq!(parse::<Baz>("< X | Y: Integer >.X"), Baz::X);
-
- assert!(from_str("< X | Y: Integer >.Y")
- .static_type_annotation()
- .parse::<Baz>()
- .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<T>(s: &str, x: T)
+ where
+ T: FromDhall + StaticType + PartialEq + std::fmt::Debug,
+ {
+ assert_eq!(
+ from_str(s)
+ .static_type_annotation()
+ .parse::<T>()
+ .map_err(|e| e.to_string()),
+ Ok(x)
+ );
+ }
+ fn assert_ser<T>(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<T>(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", <Vec<u64>>::new());
+ assert_serde("[] : List Text", <Vec<String>>::new());
+ assert_serde(
+ r#"["foo", "bar"]"#,
+ vec!["foo".to_owned(), "bar".to_owned()],
+ );
+ assert_ser(r#"["foo", "bar"]"#, vec!["foo", "bar"]);
+ assert_serde::<Vec<u64>>("[1, 2]", vec![1, 2]);
+ }
+
+ #[test]
+ fn optional() {
+ assert_serde::<Option<u64>>("None Natural", None);
+ assert_serde::<Option<String>>("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>("{=}", Foo);
+
+ // #[derive(
+ // Debug, Clone, PartialEq, Eq, Deserialize, Serialize, StaticType,
+ // )]
+ // struct Bar(u64);
+ // assert_serde::<Bar>("{ _1 = 1 }", Bar (1));
+
+ #[derive(
+ Debug, Clone, PartialEq, Eq, Deserialize, Serialize, StaticType,
+ )]
+ struct Baz {
+ x: u64,
+ y: i64,
+ }
+ assert_serde::<Baz>("{ 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::<Foo>("< X: Natural | Y: Integer >.X 1", Foo::X(1));
+
+ #[derive(
+ Debug, Clone, PartialEq, Eq, Deserialize, Serialize, StaticType,
+ )]
+ enum Bar {
+ X,
+ Y(i64),
+ }
+ assert_serde::<Bar>("< X | Y: Integer >.X", Bar::X);
+
+ assert!(from_str("< X | Y: Integer >.Y")
+ .static_type_annotation()
+ .parse::<Bar>()
+ .is_err());
+ }
+}