summary refs log blame commit diff stats
path: root/tests/maybe.rs
blob: 68db6671a335c0fca5360dfbbbc3b15b6c78ec57 (plain) (tree)















































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