From 00dab6142ed30a5de93ab0c549916b10fa60036a Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 1 Jan 2023 15:32:32 +0300 Subject: test(tvix/serde): add initial set of deserialisation tests Change-Id: I0a9779edf0296c25d37fb5f75f8fc5852fe00121 Reviewed-on: https://cl.tvl.fyi/c/depot/+/7715 Tested-by: BuildkiteCI Autosubmit: tazjin Reviewed-by: flokli --- tvix/serde/Cargo.toml | 2 +- tvix/serde/src/de_tests.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++++ tvix/serde/src/lib.rs | 3 ++ 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 tvix/serde/src/de_tests.rs (limited to 'tvix/serde') diff --git a/tvix/serde/Cargo.toml b/tvix/serde/Cargo.toml index 37a0597e0451..8ffc11a4eb6e 100644 --- a/tvix/serde/Cargo.toml +++ b/tvix/serde/Cargo.toml @@ -5,4 +5,4 @@ edition = "2021" [dependencies] tvix-eval = { path = "../eval" } -serde = "1.0" +serde = { version = "1.0", features = ["derive"] } diff --git a/tvix/serde/src/de_tests.rs b/tvix/serde/src/de_tests.rs new file mode 100644 index 000000000000..1b9bc04d2362 --- /dev/null +++ b/tvix/serde/src/de_tests.rs @@ -0,0 +1,82 @@ +use serde::Deserialize; +use std::collections::HashMap; + +use crate::de::from_str; + +#[test] +fn deserialize_none() { + let result: Option = from_str("null").expect("should deserialize"); + assert_eq!(None, result); +} + +#[test] +fn deserialize_some() { + let result: Option = from_str("40 + 2").expect("should deserialize"); + assert_eq!(Some(42), result); +} + +#[test] +fn deserialize_string() { + let result: String = from_str( + r#" + let greeter = name: "Hello ${name}!"; + in greeter "Slartibartfast" + "#, + ) + .expect("should deserialize"); + + assert_eq!(result, "Hello Slartibartfast!"); +} + +#[test] +fn deserialize_empty_list() { + let result: Vec = from_str("[ ]").expect("should deserialize"); + assert!(result.is_empty()) +} + +#[test] +fn deserialize_integer_list() { + let result: Vec = + from_str("builtins.map (n: n + 2) [ 21 40 67 ]").expect("should deserialize"); + assert_eq!(result, vec![23, 42, 69]); +} + +#[test] +fn deserialize_empty_map() { + let result: HashMap = from_str("{ }").expect("should deserialize"); + assert!(result.is_empty()); +} + +#[test] +fn deserialize_integer_map() { + let result: HashMap = from_str("{ age = 40 + 2; }").expect("should deserialize"); + assert_eq!(result.len(), 1); + assert_eq!(*result.get("age").unwrap(), 42); +} + +#[test] +fn deserialize_struct() { + #[derive(Debug, Deserialize, PartialEq)] + struct Person { + name: String, + age: usize, + } + + let result: Person = from_str( + r#" + { + name = "Slartibartfast"; + age = 42; + } + "#, + ) + .expect("should deserialize"); + + assert_eq!( + result, + Person { + name: "Slartibartfast".into(), + age: 42, + } + ); +} diff --git a/tvix/serde/src/lib.rs b/tvix/serde/src/lib.rs index b3052be596f7..41ad23b18587 100644 --- a/tvix/serde/src/lib.rs +++ b/tvix/serde/src/lib.rs @@ -6,3 +6,6 @@ mod de; mod error; pub use de::from_str; + +#[cfg(test)] +mod de_tests; -- cgit 1.4.1