diff options
author | SoniEx2 <endermoneymod@gmail.com> | 2021-09-16 10:51:39 -0300 |
---|---|---|
committer | SoniEx2 <endermoneymod@gmail.com> | 2021-09-16 10:51:39 -0300 |
commit | 134727652dc3a65bc5cf6a4d3cfb1c24fd023d5e (patch) | |
tree | af84264ada4c309cbf78a48e9030748a7ecac110 /tests |
[Project] Soni's Serde Utilities
Some utilities for use with serde.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/maybe.rs | 48 |
1 files changed, 48 insertions, 0 deletions
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::<MayBe<f64>>(json).unwrap().is_some()); +} + +#[test] +fn test_is_not_t() { + let json = "{}"; + assert!(from_str::<MayBe<f64>>(json).unwrap().is_none()); +} + +#[test] +fn test_is_missing() { + #[derive(Deserialize)] + struct Foo { + bar: MayBe<f64>, + } + let json = "{}"; + assert!(from_str::<Foo>(json).unwrap().bar.is_none()); +} + +#[test] +fn test_t_in_struct() { + #[derive(Deserialize)] + struct Foo { + bar: MayBe<f64>, + } + let json = "{\"bar\": 123}"; + assert!(from_str::<Foo>(json).unwrap().bar.is_some()); +} + + +#[test] +fn test_not_t_in_struct() { + #[derive(Deserialize)] + struct Foo { + bar: MayBe<f64>, + } + let json = "{\"bar\": []}"; + assert!(from_str::<Foo>(json).unwrap().bar.is_none()); +} |