diff options
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()); +} |