1
use bitflags::bitflags;
2
use option_set::option_set;
3

            
4
// GRCOV_EXCL_START
5
bitflags! {
6
    #[derive(
7
        Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash,
8
        serde::Serialize, serde::Deserialize,
9
    )]
10
    struct TestGood: u8 {
11
        const ONE = 1;
12
        const TWO = 1 << 1;
13
        const THREE  = 1 << 2;
14
    }
15
}
16
// GRCOV_EXCL_STOP
17

            
18
// GRCOV_EXCL_START
19
option_set! {
20
    struct TestBad: UpperCamel + u8 {
21
        const ONE = 1;
22
        const TWO = 1 << 1;
23
        const THREE  = 1 << 2;
24
    }
25
}
26
// GRCOV_EXCL_STOP
27

            
28
#[test]
29
4
fn test_bitflags() {
30
4
    // Test case provided by jaynus in
31
4
    // https://github.com/ron-rs/ron/issues/152#issue-421298302
32
4

            
33
4
    let flag_good = TestGood::ONE | TestGood::TWO;
34
4

            
35
4
    let json_ser_good = serde_json::ser::to_string(&flag_good).unwrap();
36
4
    let ron_ser_good = ron::ser::to_string(&flag_good).unwrap();
37
4

            
38
4
    assert_eq!(json_ser_good, "\"ONE | TWO\"");
39
4
    assert_eq!(ron_ser_good, "(\"ONE | TWO\")");
40

            
41
4
    let json_de_good: TestGood = serde_json::de::from_str(json_ser_good.as_str()).unwrap();
42
4
    let ron_de_good: TestGood = ron::de::from_str(ron_ser_good.as_str()).unwrap();
43
4

            
44
4
    assert_eq!(json_de_good, flag_good);
45
4
    assert_eq!(ron_de_good, flag_good);
46

            
47
    // option_set
48
4
    let flag_bad = TestBad::ONE | TestBad::TWO;
49
4

            
50
4
    let json_ser_bad = serde_json::ser::to_string(&flag_bad).unwrap();
51
4
    let ron_ser_bad = ron::ser::to_string(&flag_bad).unwrap();
52
4

            
53
4
    assert_eq!(json_ser_bad, "[\"One\",\"Two\"]");
54
4
    assert_eq!(ron_ser_bad, "[\"One\",\"Two\"]");
55

            
56
4
    let json_de_bad: TestBad = serde_json::de::from_str(json_ser_bad.as_str()).unwrap();
57
4
    let ron_de_bad: TestBad = ron::de::from_str(ron_ser_bad.as_str()).unwrap();
58
4

            
59
4
    assert_eq!(json_de_bad, flag_bad);
60
4
    assert_eq!(ron_de_bad, flag_bad);
61
4
}