summaryrefslogtreecommitdiffstats
path: root/vendor/serde_ignored/tests/test.rs
blob: ce7a2bf21b16f9dc2d2bbb32eb474c3738407eb3 (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#![allow(
    clippy::derive_partial_eq_without_eq,
    clippy::from_iter_instead_of_collect,
    clippy::zero_sized_map_values
)]

use serde::Deserialize;
use serde_derive::Deserialize;
use std::collections::{BTreeMap as Map, BTreeSet as Set};
use std::iter::FromIterator;

fn assert_ignored<'de, T>(json: &'de str, expected: &[&str]) -> T
where
    T: Deserialize<'de>,
{
    let de = &mut serde_json::Deserializer::from_str(json);

    let mut unused = Set::new();

    let value: T = serde_ignored::deserialize(de, |path| {
        unused.insert(path.to_string());
    })
    .unwrap();

    let expected = Set::from_iter(expected.iter().copied().map(str::to_owned));
    assert_eq!(unused, expected);

    value
}

#[derive(Debug, Deserialize)]
struct V {
    #[allow(dead_code)]
    used: (),
}

#[test]
fn test_readme() {
    #[derive(Debug, PartialEq, Deserialize)]
    struct Package {
        name: String,
        dependencies: Map<String, Dependency>,
    }

    #[derive(Debug, PartialEq, Deserialize)]
    struct Dependency {
        version: String,
    }

    let json = r#"{
        "name": "demo",
        "dependencies": {
            "serde": {
                "version": "1.0",
                "typo1": ""
            }
        },
        "typo2": {
            "inner": ""
        },
        "typo3": {}
    }"#;

    let ignored = &["dependencies.serde.typo1", "typo2", "typo3"];
    let p: Package = assert_ignored(json, ignored);

    let expected = Package {
        name: "demo".to_owned(),
        dependencies: {
            let mut map = Map::new();
            map.insert(
                "serde".to_owned(),
                Dependency {
                    version: "1.0".to_owned(),
                },
            );
            map
        },
    };
    assert_eq!(p, expected);
}

#[test]
fn test_int_key() {
    #[derive(Debug, Deserialize)]
    struct Test {
        #[allow(dead_code)]
        a: Map<usize, V>,
    }

    let json = r#"{
        "a": {
            "2": {
                "used": null,
                "unused": null
            }
        }
    }"#;

    let ignored = &["a.2.unused"];
    assert_ignored::<Test>(json, ignored);
}

#[test]
fn test_newtype_key() {
    type Test = Map<Key, V>;

    #[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Deserialize)]
    struct Key(&'static str);

    let json = r#"{
        "k": {
            "used": null,
            "unused": null
        }
    }"#;

    let ignored = &["k.unused"];
    assert_ignored::<Test>(json, ignored);
}

#[test]
fn test_unit_variant_key() {
    type Test = Map<Key, V>;

    #[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Deserialize)]
    enum Key {
        First,
        Second,
    }

    let json = r#"{
        "First": {
            "used": null,
            "unused": null
        }
    }"#;

    let ignored = &["First.unused"];
    assert_ignored::<Test>(json, ignored);
}