use rawenum::rawenum;
#[rawenum(i32, u8)]
#[derive(Debug, PartialEq)]
enum ExplicitEnum {
Zero = 0,
One = 1,
Ten = 10,
NegativeFive = -5, }
#[test]
fn test_explicit_enum_i32_u8() {
assert_eq!(ExplicitEnum::from_i32(0), Some(ExplicitEnum::Zero));
assert_eq!(ExplicitEnum::from_i32(1), Some(ExplicitEnum::One));
assert_eq!(ExplicitEnum::from_i32(10), Some(ExplicitEnum::Ten));
assert_eq!(ExplicitEnum::from_i32(-5), Some(ExplicitEnum::NegativeFive));
assert_eq!(ExplicitEnum::from_i32(99), None);
assert_eq!(ExplicitEnum::from_u8(0), Some(ExplicitEnum::Zero));
assert_eq!(ExplicitEnum::from_u8(1), Some(ExplicitEnum::One));
assert_eq!(ExplicitEnum::from_u8(10), Some(ExplicitEnum::Ten));
assert_eq!(ExplicitEnum::from_u8(251), Some(ExplicitEnum::NegativeFive)); assert_eq!(ExplicitEnum::from_u8(99), None); }
#[rawenum(i16, u64)]
#[derive(Debug, PartialEq)]
enum ImplicitEnum {
A, B, C, }
#[test]
fn test_implicit_enum_i16_u64() {
assert_eq!(ImplicitEnum::from_i16(0), Some(ImplicitEnum::A));
assert_eq!(ImplicitEnum::from_i16(1), Some(ImplicitEnum::B));
assert_eq!(ImplicitEnum::from_i16(2), Some(ImplicitEnum::C));
assert_eq!(ImplicitEnum::from_i16(3), None); assert_eq!(ImplicitEnum::from_i16(-1), None);
assert_eq!(ImplicitEnum::from_u64(0), Some(ImplicitEnum::A));
assert_eq!(ImplicitEnum::from_u64(1), Some(ImplicitEnum::B));
assert_eq!(ImplicitEnum::from_u64(2), Some(ImplicitEnum::C));
assert_eq!(ImplicitEnum::from_u64(3), None); }
#[rawenum(i8, i64)]
#[derive(Debug, PartialEq)]
enum MixedEnum {
Start = 100, Next, Jump = 200, Another, End, }
#[test]
fn test_mixed_enum_i8_i64() {
assert_eq!(MixedEnum::from_i8(100), Some(MixedEnum::Start));
assert_eq!(MixedEnum::from_i8(101), Some(MixedEnum::Next));
assert_eq!(MixedEnum::from_i8(-56), Some(MixedEnum::Jump)); assert_eq!(MixedEnum::from_i8(-55), Some(MixedEnum::Another)); assert_eq!(MixedEnum::from_i8(-54), Some(MixedEnum::End)); assert_eq!(MixedEnum::from_i8(50), None);
assert_eq!(MixedEnum::from_i64(100), Some(MixedEnum::Start));
assert_eq!(MixedEnum::from_i64(101), Some(MixedEnum::Next));
assert_eq!(MixedEnum::from_i64(200), Some(MixedEnum::Jump));
assert_eq!(MixedEnum::from_i64(201), Some(MixedEnum::Another));
assert_eq!(MixedEnum::from_i64(202), Some(MixedEnum::End));
assert_eq!(MixedEnum::from_i64(99), None); assert_eq!(MixedEnum::from_i64(150), None); assert_eq!(MixedEnum::from_i64(300), None); }
#[rawenum(i32)]
#[derive(Debug, PartialEq)]
enum ZeroEnum {
First = 0,
Second, }
#[test]
fn test_zero_enum_i32() {
assert_eq!(ZeroEnum::from_i32(0), Some(ZeroEnum::First));
assert_eq!(ZeroEnum::from_i32(1), Some(ZeroEnum::Second));
assert_eq!(ZeroEnum::from_i32(2), None);
}
#[rawenum(i8, u8)]
#[derive(Debug, PartialEq)]
enum NegativeEnum {
NegOne = -1, NegTwo = -2, Zero = 0, One = 1, }
#[test]
fn test_negative_enum_i8_u8() {
assert_eq!(NegativeEnum::from_i8(-1), Some(NegativeEnum::NegOne));
assert_eq!(NegativeEnum::from_i8(-2), Some(NegativeEnum::NegTwo));
assert_eq!(NegativeEnum::from_i8(0), Some(NegativeEnum::Zero));
assert_eq!(NegativeEnum::from_i8(1), Some(NegativeEnum::One));
assert_eq!(NegativeEnum::from_i8(99), None);
assert_eq!(NegativeEnum::from_u8(255), Some(NegativeEnum::NegOne));
assert_eq!(NegativeEnum::from_u8(254), Some(NegativeEnum::NegTwo));
assert_eq!(NegativeEnum::from_u8(0), Some(NegativeEnum::Zero));
assert_eq!(NegativeEnum::from_u8(1), Some(NegativeEnum::One));
assert_eq!(NegativeEnum::from_u8(99), None);
}