File: 152_bitflags.rs

package info (click to toggle)
rust-ron 0.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,096 kB
  • sloc: makefile: 2
file content (61 lines) | stat: -rw-r--r-- 1,769 bytes parent folder | download | duplicates (20)
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
use bitflags::bitflags;
use option_set::option_set;

// GRCOV_EXCL_START
bitflags! {
    #[derive(
        Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash,
        serde::Serialize, serde::Deserialize,
    )]
    struct TestGood: u8 {
        const ONE = 1;
        const TWO = 1 << 1;
        const THREE  = 1 << 2;
    }
}
// GRCOV_EXCL_STOP

// GRCOV_EXCL_START
option_set! {
    struct TestBad: UpperCamel + u8 {
        const ONE = 1;
        const TWO = 1 << 1;
        const THREE  = 1 << 2;
    }
}
// GRCOV_EXCL_STOP

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

    let flag_good = TestGood::ONE | TestGood::TWO;

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

    assert_eq!(json_ser_good, "\"ONE | TWO\"");
    assert_eq!(ron_ser_good, "(\"ONE | TWO\")");

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

    assert_eq!(json_de_good, flag_good);
    assert_eq!(ron_de_good, flag_good);

    // option_set
    let flag_bad = TestBad::ONE | TestBad::TWO;

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

    assert_eq!(json_ser_bad, "[\"One\",\"Two\"]");
    assert_eq!(ron_ser_bad, "[\"One\",\"Two\"]");

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

    assert_eq!(json_de_bad, flag_bad);
    assert_eq!(ron_de_bad, flag_bad);
}