Module strict_encoding::test_helpers [−][src]
Expand description
Helping macros and functions for creating test coverage for strict-encoded data.
Testing enum encoding
Enums having assigned primitive 8-bit values (i.e. u8
values) should be
tested with test_encoding_enum_u8_exhaustive
, which is a macro
performing the most exhaustive testing.
If the enum primitive values are of non-u8
-type, then
test_encoding_enum_by_values
should be used. It does not performs
exhaustive tests, but covers tests comparing strict encoding with the actual
primitive value.
If the enum has no primitive values, has associated values (tuple- or
structure-based) or any enum variant defines custom
#[strict_encoding(value = ...)]
attribute, testing should be performed
with test_encoding_enum
macro.
Testing structures and unions
If you have an object which encoding you’d like to test, use
test_object_encoding_roundtrip
method.
If you have a byte string test vector representing some serialized object,
use test_vec_decoding_roundtrip
method.
If you have both an object and an independent test vector, representing its
serialization (which should not be obtained by just encoding the object),
use test_encoding_roundtrip
method.
General guidelines
Proper testing should not exercise asset
s and instead propagate errors
returned by test macros and methods to the return of the test case function
with ?
operator:
use strict_encoding::test_helpers::*;
#[derive(Clone, PartialEq, Eq, Debug, StrictEncode, StrictDecode)]
struct Data(pub Vec<u8>);
#[test]
fn test_data_encoding() -> Result<(), DataEncodingTestFailure<Data>> {
let data1 = Data(vec![0x01, 0x02]);
test_encoding_roundtrip(&data1, &[0x02, 0x00, 0x01, 0x02])?;
let data2 = Data(vec![0xff]);
test_encoding_roundtrip(&data2, &[0x02, 0x00, 0xff])?;
Ok(())
}
Enums
Failures happening during strict encoding tests of struct and union encodings.
Failures happening during strict encoding tests of enum encodings.
Functions
Test helper performing double encode-decode roundtrip for an object
and a matching binary encoding test vector. Object type must be
PartialEq + Clone + Debug
.
Test helper performing encode-decode roundtrip for a provided object. Object
type must be PartialEq + Clone + Debug
.
Test helper performing decode-eecode roundtrip for a provided test vector byte string.