From 134727652dc3a65bc5cf6a4d3cfb1c24fd023d5e Mon Sep 17 00:00:00 2001 From: SoniEx2 Date: Thu, 16 Sep 2021 10:51:39 -0300 Subject: [Project] Soni's Serde Utilities Some utilities for use with serde. --- tests/maybe.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/maybe.rs (limited to 'tests') diff --git a/tests/maybe.rs b/tests/maybe.rs new file mode 100644 index 0000000..68db667 --- /dev/null +++ b/tests/maybe.rs @@ -0,0 +1,48 @@ +use serde::Deserialize; + +use serde_json::from_str; + +use serde_util::MayBe; + +#[test] +fn test_is_t() { + let json = "256"; + assert!(from_str::>(json).unwrap().is_some()); +} + +#[test] +fn test_is_not_t() { + let json = "{}"; + assert!(from_str::>(json).unwrap().is_none()); +} + +#[test] +fn test_is_missing() { + #[derive(Deserialize)] + struct Foo { + bar: MayBe, + } + let json = "{}"; + assert!(from_str::(json).unwrap().bar.is_none()); +} + +#[test] +fn test_t_in_struct() { + #[derive(Deserialize)] + struct Foo { + bar: MayBe, + } + let json = "{\"bar\": 123}"; + assert!(from_str::(json).unwrap().bar.is_some()); +} + + +#[test] +fn test_not_t_in_struct() { + #[derive(Deserialize)] + struct Foo { + bar: MayBe, + } + let json = "{\"bar\": []}"; + assert!(from_str::(json).unwrap().bar.is_none()); +} -- cgit 1.4.1