From 9f9facec8c72fd3b86b77d352a268a7cc51b1e08 Mon Sep 17 00:00:00 2001 From: arcnmx Date: Mon, 26 Aug 2019 12:40:13 -0700 Subject: [PATCH] fmt trait tests --- test_suite/Cargo.toml | 5 +++ test_suite/tests/formatting.rs | 74 ++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 test_suite/tests/formatting.rs diff --git a/test_suite/Cargo.toml b/test_suite/Cargo.toml index 90906a2..c02c72f 100644 --- a/test_suite/Cargo.toml +++ b/test_suite/Cargo.toml @@ -37,3 +37,8 @@ edition = "2018" name = "bitflags-test-no-implicit-prelude-2018" path = "tests/no_implicit_prelude_2018.rs" edition = "2018" + +[[test]] +name = "bitflags-formatting" +path = "tests/formatting.rs" +edition = "2018" diff --git a/test_suite/tests/formatting.rs b/test_suite/tests/formatting.rs new file mode 100644 index 0000000..c23142e --- /dev/null +++ b/test_suite/tests/formatting.rs @@ -0,0 +1,74 @@ +use enumflags2_derive::EnumFlags; + +include!("../common.rs"); + +#[test] +fn debug_format() { + use enumflags2::BitFlags; + + // Assert that our Debug output format meets expectations + + assert_eq!( + format!("{:?}", BitFlags::::all()), + "BitFlags(0b1111, A | B | C | D)" + ); + + assert_eq!( + format!("{:?}", BitFlags::::empty()), + "BitFlags(0b0)" + ); + + assert_eq!( + format!("{:04x?}", BitFlags::::all()), + "BitFlags(0x0f, A | B | C | D)" + ); + + assert_eq!( + format!("{:04X?}", BitFlags::::all()), + "BitFlags(0x0F, A | B | C | D)" + ); + + // Also check alternate struct formatting + + assert_eq!( + format!("{:#010?}", BitFlags::::all()), +"BitFlags { + bits: 0b00001111, + flags: A | B | C | D, +}" + ); + + assert_eq!( + format!("{:#?}", BitFlags::::empty()), +"BitFlags { + bits: 0b0, +}" + ); +} + +#[test] +fn format() { + use enumflags2::BitFlags; + + // Assert BitFlags impls fmt::{Binary, Octal, LowerHex, UpperHex} + + assert_eq!( + format!("{:b}", BitFlags::::all()), + "1111" + ); + + assert_eq!( + format!("{:o}", BitFlags::::all()), + "17" + ); + + assert_eq!( + format!("{:x}", BitFlags::::all()), + "f" + ); + + assert_eq!( + format!("{:#04X}", BitFlags::::all()), + "0x0F" + ); +}