summaryrefslogtreecommitdiffstats
path: root/vendor/serde_repr/tests/test.rs
blob: 8c44827dd0675da243dc6885dd64ffbffc3bbd5f (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
#![allow(
    // Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
    clippy::nonstandard_macro_braces,
    clippy::wildcard_imports,
)]

use serde_repr::{Deserialize_repr, Serialize_repr};

mod small_prime {
    use super::*;

    #[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)]
    #[repr(u8)]
    enum SmallPrime {
        Two = 2,
        Three = 3,
        Five = 5,
        Seven = 7,
    }

    #[test]
    fn test_serialize() {
        let j = serde_json::to_string(&SmallPrime::Seven).unwrap();
        assert_eq!(j, "7");
    }

    #[test]
    fn test_deserialize() {
        let p: SmallPrime = serde_json::from_str("2").unwrap();
        assert_eq!(p, SmallPrime::Two);
    }
}

mod other {
    use super::*;

    #[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)]
    #[repr(u8)]
    enum TestOther {
        A,
        B,
        #[serde(other, rename = "useless")]
        Other,
    }

    #[test]
    fn test_deserialize() {
        let p: TestOther = serde_json::from_str("0").unwrap();
        assert_eq!(p, TestOther::A);
        let p: TestOther = serde_json::from_str("1").unwrap();
        assert_eq!(p, TestOther::B);
        let p: TestOther = serde_json::from_str("5").unwrap();
        assert_eq!(p, TestOther::Other);
    }
}

mod implicit_discriminant {
    use super::*;

    #[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)]
    #[repr(u8)]
    enum ImplicitDiscriminant {
        Zero,
        One,
        Two,
        Three,
    }

    #[test]
    fn test_serialize() {
        let j = serde_json::to_string(&ImplicitDiscriminant::Three).unwrap();
        assert_eq!(j, "3");
    }

    #[test]
    fn test_deserialize() {
        let p: ImplicitDiscriminant = serde_json::from_str("2").unwrap();
        assert_eq!(p, ImplicitDiscriminant::Two);
    }
}