Skip to content

Commit

Permalink
reorganize crate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Aug 12, 2021
1 parent 06a232c commit 70c4797
Show file tree
Hide file tree
Showing 14 changed files with 835 additions and 94 deletions.
62 changes: 62 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1638,4 +1638,66 @@ mod tests {
fn test_empty_bitflags() {
bitflags! {}
}

#[test]
fn test_u128_bitflags() {
bitflags! {
struct Flags128: u128 {
const A = 0x0000_0000_0000_0000_0000_0000_0000_0001;
const B = 0x0000_0000_0000_1000_0000_0000_0000_0000;
const C = 0x8000_0000_0000_0000_0000_0000_0000_0000;
const ABC = Self::A.bits | Self::B.bits | Self::C.bits;
}
}

assert_eq!(Flags128::ABC, Flags128::A | Flags128::B | Flags128::C);
assert_eq!(Flags128::A.bits, 0x0000_0000_0000_0000_0000_0000_0000_0001);
assert_eq!(Flags128::B.bits, 0x0000_0000_0000_1000_0000_0000_0000_0000);
assert_eq!(Flags128::C.bits, 0x8000_0000_0000_0000_0000_0000_0000_0000);
assert_eq!(
Flags128::ABC.bits,
0x8000_0000_0000_1000_0000_0000_0000_0001
);
assert_eq!(format!("{:?}", Flags128::A), "A");
assert_eq!(format!("{:?}", Flags128::B), "B");
assert_eq!(format!("{:?}", Flags128::C), "C");
assert_eq!(format!("{:?}", Flags128::ABC), "A | B | C | ABC");
}

#[test]
fn test_serde_bitflags_serialize() {
let flags = SerdeFlags::A | SerdeFlags::B;

let serialized = serde_json::to_string(&flags).unwrap();

assert_eq!(serialized, r#"{"bits":3}"#);
}

#[test]
fn test_serde_bitflags_deserialize() {
let deserialized: SerdeFlags = serde_json::from_str(r#"{"bits":12}"#).unwrap();

let expected = SerdeFlags::C | SerdeFlags::D;

assert_eq!(deserialized.bits, expected.bits);
}

#[test]
fn test_serde_bitflags_roundtrip() {
let flags = SerdeFlags::A | SerdeFlags::B;

let deserialized: SerdeFlags = serde_json::from_str(&serde_json::to_string(&flags).unwrap()).unwrap();

assert_eq!(deserialized.bits, flags.bits);
}

bitflags! {
#[derive(serde::Serialize, serde::Deserialize)]
struct SerdeFlags: u32 {
const A = 1;
const B = 2;
const C = 4;
const D = 8;
}
}
}
2 changes: 1 addition & 1 deletion tests/external_no_std.rs → tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ bitflags! {
}

#[test]
fn smoke() {
fn basic() {
assert_eq!(Flags::ABC, Flags::A | Flags::B | Flags::C);
}
12 changes: 12 additions & 0 deletions tests/compile-fail/non_integer_base/missing_bitops.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use bitflags::bitflags;

#[derive(Clone, Copy)]
struct MyInt(u8);

bitflags! {
struct Flags128: MyInt {
const A = MyInt(0b0000_0001);
const B = MyInt(0b0000_0010);
const C = MyInt(0b0000_0100);
}
}
Loading

0 comments on commit 70c4797

Please sign in to comment.