diff options
Diffstat (limited to 'serde_dhall/tests')
-rw-r--r-- | serde_dhall/tests/de.rs | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/serde_dhall/tests/de.rs b/serde_dhall/tests/de.rs new file mode 100644 index 0000000..41a44bd --- /dev/null +++ b/serde_dhall/tests/de.rs @@ -0,0 +1,85 @@ +use serde::Deserialize; +use serde_dhall::{from_str, from_str_auto_type, StaticType}; + +#[test] +fn test_de_typed() { + fn parse<T: serde_dhall::de::Deserialize + StaticType>(s: &str) -> T { + from_str_auto_type(s).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![]); + 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); +} + +#[test] +fn test_de_untyped() { + fn parse<T: serde_dhall::de::Deserialize>(s: &str) -> T { + from_str(s).unwrap() + } + + // Test tuples on record of wrong type + assert_eq!( + parse::<(u64, String, isize)>(r#"{ y = "foo", x = 1, z = +42 }"#), + (1, "foo".to_owned(), 42) + ); + + use std::collections::HashMap; + let mut expected_map = HashMap::new(); + expected_map.insert("x".to_string(), 1); + expected_map.insert("y".to_string(), 2); + assert_eq!( + parse::<HashMap<String, usize>>("{ x = 1, y = 2 }"), + expected_map + ); + + use std::collections::BTreeMap; + let mut expected_map = BTreeMap::new(); + expected_map.insert("x".to_string(), 1); + expected_map.insert("y".to_string(), 2); + assert_eq!( + parse::<BTreeMap<String, usize>>("{ x = 1, y = 2 }"), + expected_map + ); +} |