summary refs log tree commit diff stats
path: root/tests/maybe.rs
blob: 143719507a7a80f13a205d480874aff10c991b04 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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());
}

#[test]
fn test_is_not_t() {
    let json = "{}";
    assert!(from_str::<MayBe<f64>>(json).unwrap().is_not());
}

#[test]
fn test_is_missing() {
    #[derive(Deserialize)]
    struct Foo {
        #[serde(rename = "bar")]
        _bar: MayBe<f64>,
    }
    let json = "{}";
    assert!(from_str::<Foo>(json).is_err());
}

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


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