Skip to content

Commit

Permalink
Have a test per type (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
illicitonion authored Apr 15, 2023
1 parent f1ee727 commit 190a939
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 33 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock.msrv

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions num_enum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ num_enum_derive = { version = "=0.6.0", path = "../num_enum_derive", default-fea

[dev-dependencies]
anyhow = "1.0.14"
paste = "1"
rustversion = "1.0.4"
trybuild = "1.0.49"
walkdir = "2"
68 changes: 35 additions & 33 deletions num_enum/tests/from_primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,44 @@ mod num_enum {}
mod std {}

macro_rules! has_from_primitive_number {
( $type:ty ) => {{
#[derive(Debug, Eq, PartialEq, FromPrimitive)]
#[repr($type)]
enum Enum {
Zero = 0,
#[num_enum(default)]
NonZero = 1,
( $type:ty ) => {
paste::paste! {
#[test]
fn [<has_from_primitive_number_ $type>]() {
#[derive(Debug, Eq, PartialEq, FromPrimitive)]
#[repr($type)]
enum Enum {
Zero = 0,
#[num_enum(default)]
NonZero = 1,
}

let zero = Enum::from_primitive(0 as $type);
assert_eq!(zero, Enum::Zero);

let one = Enum::from_primitive(1 as $type);
assert_eq!(one, Enum::NonZero);

let two = Enum::from_primitive(2 as $type);
assert_eq!(two, Enum::NonZero);
}
}

let zero = Enum::from_primitive(0 as $type);
assert_eq!(zero, Enum::Zero);

let one = Enum::from_primitive(1 as $type);
assert_eq!(one, Enum::NonZero);

let two = Enum::from_primitive(2 as $type);
assert_eq!(two, Enum::NonZero);
}};
};
}

#[test]
fn has_from_primitive_number() {
has_from_primitive_number!(u8);
has_from_primitive_number!(u16);
has_from_primitive_number!(u32);
has_from_primitive_number!(u64);
has_from_primitive_number!(usize);
has_from_primitive_number!(i8);
has_from_primitive_number!(i16);
has_from_primitive_number!(i32);
has_from_primitive_number!(i64);
has_from_primitive_number!(isize);
// repr with 128-bit type is unstable
// has_from_primitive_number!(u128);
// has_from_primitive_number!(i128);
}
has_from_primitive_number!(u8);
has_from_primitive_number!(u16);
has_from_primitive_number!(u32);
has_from_primitive_number!(u64);
has_from_primitive_number!(usize);
has_from_primitive_number!(i8);
has_from_primitive_number!(i16);
has_from_primitive_number!(i32);
has_from_primitive_number!(i64);
has_from_primitive_number!(isize);
// repr with 128-bit type is unstable
// has_from_primitive_number!(u128);
// has_from_primitive_number!(i128);

#[test]
fn has_from_primitive_number_standard_default_attribute() {
Expand Down

0 comments on commit 190a939

Please sign in to comment.