blob: 702f0c53317fafe69816a00ec7fdf911f2c260a9 (
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
50
51
52
53
54
55
56
57
58
59
60
|
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());
}
#[test]
fn test_shallow() {
#[derive(Deserialize)]
struct Foo {
#[serde(rename = "bar")]
_bar: f64,
}
let json = "{\"bar\": []}";
assert!(from_str::<MayBe<Foo>>(json).is_err());
}
|