multiversx_sc_codec/
test_util.rsuse crate::*;
use alloc::vec::Vec;
use core::fmt::Debug;
use unwrap_infallible::UnwrapInfallible;
pub fn top_encode_to_vec_u8_or_panic<T: TopEncode>(obj: &T) -> Vec<u8> {
let mut bytes = Vec::<u8>::new();
obj.top_encode_or_handle_err(&mut bytes, PanicErrorHandler)
.unwrap_infallible();
bytes
}
pub fn dep_encode_to_vec_or_panic<T: NestedEncode>(obj: &T) -> Vec<u8> {
let mut bytes = Vec::<u8>::new();
obj.dep_encode_or_handle_err(&mut bytes, PanicErrorHandler)
.unwrap_infallible();
bytes
}
pub fn check_top_encode<T: TopEncode>(obj: &T) -> Vec<u8> {
let fast_exit_bytes = top_encode_to_vec_u8_or_panic(obj);
let result_bytes = top_encode_to_vec_u8(obj).unwrap();
assert_eq!(fast_exit_bytes, result_bytes);
fast_exit_bytes
}
pub fn check_dep_encode<T: NestedEncode>(obj: &T) -> Vec<u8> {
let fast_exit_bytes = dep_encode_to_vec_or_panic(obj);
let result_bytes = dep_encode_to_vec(obj).unwrap();
assert_eq!(fast_exit_bytes, result_bytes);
fast_exit_bytes
}
pub fn dep_decode_from_byte_slice_or_panic<T: NestedDecode>(input: &[u8]) -> T {
dep_decode_from_byte_slice(input, PanicErrorHandler).unwrap_infallible()
}
pub fn check_top_decode<T: TopDecode + PartialEq + Debug>(bytes: &[u8]) -> T {
let fast_exit_obj = T::top_decode_or_handle_err(bytes, PanicErrorHandler).unwrap_infallible();
let result_obj = T::top_decode_or_handle_err(bytes, DefaultErrorHandler).unwrap();
assert_eq!(fast_exit_obj, result_obj);
fast_exit_obj
}
pub fn check_dep_decode<T: NestedDecode + PartialEq + Debug>(bytes: &[u8]) -> T {
let fast_exit_obj = dep_decode_from_byte_slice(bytes, PanicErrorHandler).unwrap_infallible();
let result_obj = dep_decode_from_byte_slice(bytes, DefaultErrorHandler).unwrap();
assert_eq!(fast_exit_obj, result_obj);
fast_exit_obj
}
#[deprecated]
pub fn ser_deser_ok<V>(element: V, expected_bytes: &[u8])
where
V: TopEncode + TopDecode + PartialEq + Debug,
{
check_top_encode_decode(element, expected_bytes);
}
pub fn check_top_encode_decode<V>(element: V, expected_bytes: &[u8])
where
V: TopEncode + TopDecode + PartialEq + Debug,
{
let serialized_bytes = check_top_encode(&element);
assert_eq!(serialized_bytes.as_slice(), expected_bytes);
let deserialized: V = check_top_decode::<V>(&serialized_bytes[..]);
assert_eq!(deserialized, element);
}
pub fn check_dep_encode_decode<V>(element: V, expected_bytes: &[u8])
where
V: NestedEncode + NestedDecode + PartialEq + Debug,
{
let serialized_bytes = check_dep_encode(&element);
assert_eq!(serialized_bytes.as_slice(), expected_bytes);
let deserialized: V = check_dep_decode::<V>(&serialized_bytes[..]);
assert_eq!(deserialized, element);
}