Skip to content

Commit 8e23c6c

Browse files
arcnmxmeithecatte
authored andcommitted
fmt trait tests
1 parent 1270704 commit 8e23c6c

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

test_suite/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,8 @@ edition = "2018"
3737
name = "bitflags-test-no-implicit-prelude-2018"
3838
path = "tests/no_implicit_prelude_2018.rs"
3939
edition = "2018"
40+
41+
[[test]]
42+
name = "bitflags-formatting"
43+
path = "tests/formatting.rs"
44+
edition = "2018"

test_suite/tests/formatting.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
use enumflags2_derive::EnumFlags;
2+
3+
include!("../common.rs");
4+
5+
#[test]
6+
fn debug_format() {
7+
use enumflags2::BitFlags;
8+
9+
// Assert that our Debug output format meets expectations
10+
11+
assert_eq!(
12+
format!("{:?}", BitFlags::<Test>::all()),
13+
"BitFlags<Test>(0b1111, A | B | C | D)"
14+
);
15+
16+
assert_eq!(
17+
format!("{:?}", BitFlags::<Test>::empty()),
18+
"BitFlags<Test>(0b0)"
19+
);
20+
21+
assert_eq!(
22+
format!("{:04x?}", BitFlags::<Test>::all()),
23+
"BitFlags<Test>(0x0f, A | B | C | D)"
24+
);
25+
26+
assert_eq!(
27+
format!("{:04X?}", BitFlags::<Test>::all()),
28+
"BitFlags<Test>(0x0F, A | B | C | D)"
29+
);
30+
31+
// Also check alternate struct formatting
32+
33+
assert_eq!(
34+
format!("{:#010?}", BitFlags::<Test>::all()),
35+
"BitFlags<Test> {
36+
bits: 0b00001111,
37+
flags: A | B | C | D,
38+
}"
39+
);
40+
41+
assert_eq!(
42+
format!("{:#?}", BitFlags::<Test>::empty()),
43+
"BitFlags<Test> {
44+
bits: 0b0,
45+
}"
46+
);
47+
}
48+
49+
#[test]
50+
fn format() {
51+
use enumflags2::BitFlags;
52+
53+
// Assert BitFlags<T> impls fmt::{Binary, Octal, LowerHex, UpperHex}
54+
55+
assert_eq!(
56+
format!("{:b}", BitFlags::<Test>::all()),
57+
"1111"
58+
);
59+
60+
assert_eq!(
61+
format!("{:o}", BitFlags::<Test>::all()),
62+
"17"
63+
);
64+
65+
assert_eq!(
66+
format!("{:x}", BitFlags::<Test>::all()),
67+
"f"
68+
);
69+
70+
assert_eq!(
71+
format!("{:#04X}", BitFlags::<Test>::all()),
72+
"0x0F"
73+
);
74+
}

0 commit comments

Comments
 (0)