Macro strict_encoding::test_encoding_enum_by_values[][src]

macro_rules! test_encoding_enum_by_values {
    ($enum:path as $ty:ty; $( $item:path => $val:expr ),+) => { ... };
    ($se:ident => $enum:path as $ty:ty; $( $item:path => $val:expr ),+) => { ... };
}
Expand description

Macro testing encodings of all enum variants for enums with assigned primitive integer values.

Macro expands into an expression of Result<(), EnumEncodingTestFailure> type.

Macro extends functionality of test_encoding_enum and should be used whenever enum has assigned primitive integer values. If the primitive values are of u8 type, test_encoding_enum_u8_exhaustive should be used instead of this macro.

Covered test cases

  • Each enum variant must have a primitive value
  • Primitive value representing enum variant must be equal to strict encoding of the same variant. If a primitive enum value occupies of several bytes (u16, u32 and other large integer types), strict encoding must match little-endian encoding of the value
  • Roundtrip encoding-decoding of the enum variant must result in the original value
  • Each enum variant must be equal to itself
  • Each enum variant must not be equal to any other enum variant
  • Enum variants must be ordered according to their primitive values

Example


#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug)]
#[repr(u8)]
#[derive(StrictEncode, StrictDecode)]
#[strict_encoding(repr = u8, by_value)]
enum Bits {
    Bit8 = 8,
    Bit16 = 16,
}

test_encoding_enum_by_values!(
    Bits as u8;
    Bits::Bit8 => 8_u8, Bits::Bit16 => 16_u8
).unwrap();