multiversx_sc_codec/
test_util.rs1use crate::*;
2use alloc::vec::Vec;
3use core::fmt::Debug;
4use unwrap_infallible::UnwrapInfallible;
5
6pub fn top_encode_to_vec_u8_or_panic<T: TopEncode>(obj: &T) -> Vec<u8> {
9 let mut bytes = Vec::<u8>::new();
10 obj.top_encode_or_handle_err(&mut bytes, PanicErrorHandler)
11 .unwrap_infallible();
12 bytes
13}
14
15pub fn dep_encode_to_vec_or_panic<T: NestedEncode>(obj: &T) -> Vec<u8> {
18 let mut bytes = Vec::<u8>::new();
19 obj.dep_encode_or_handle_err(&mut bytes, PanicErrorHandler)
20 .unwrap_infallible();
21 bytes
22}
23
24pub fn check_top_encode<T: TopEncode>(obj: &T) -> Vec<u8> {
28 let fast_exit_bytes = top_encode_to_vec_u8_or_panic(obj);
29 let result_bytes = top_encode_to_vec_u8(obj).unwrap();
30 assert_eq!(fast_exit_bytes, result_bytes);
31 fast_exit_bytes
32}
33
34pub fn check_dep_encode<T: NestedEncode>(obj: &T) -> Vec<u8> {
38 let fast_exit_bytes = dep_encode_to_vec_or_panic(obj);
39 let result_bytes = dep_encode_to_vec(obj).unwrap();
40 assert_eq!(fast_exit_bytes, result_bytes);
41 fast_exit_bytes
42}
43
44pub fn dep_decode_from_byte_slice_or_panic<T: NestedDecode>(input: &[u8]) -> T {
47 dep_decode_from_byte_slice(input, PanicErrorHandler).unwrap_infallible()
48}
49
50pub fn check_top_decode<T: TopDecode + PartialEq + Debug>(bytes: &[u8]) -> T {
54 let fast_exit_obj = T::top_decode_or_handle_err(bytes, PanicErrorHandler).unwrap_infallible();
55 let result_obj = T::top_decode_or_handle_err(bytes, DefaultErrorHandler).unwrap();
56 assert_eq!(fast_exit_obj, result_obj);
57 fast_exit_obj
58}
59
60pub fn check_dep_decode<T: NestedDecode + PartialEq + Debug>(bytes: &[u8]) -> T {
64 let fast_exit_obj = dep_decode_from_byte_slice(bytes, PanicErrorHandler).unwrap_infallible();
65 let result_obj = dep_decode_from_byte_slice(bytes, DefaultErrorHandler).unwrap();
66 assert_eq!(fast_exit_obj, result_obj);
67 fast_exit_obj
68}
69
70#[deprecated]
72pub fn ser_deser_ok<V>(element: V, expected_bytes: &[u8])
73where
74 V: TopEncode + TopDecode + PartialEq + Debug,
75{
76 check_top_encode_decode(element, expected_bytes);
77}
78
79pub fn check_top_encode_decode<V>(element: V, expected_bytes: &[u8])
80where
81 V: TopEncode + TopDecode + PartialEq + Debug,
82{
83 let serialized_bytes = check_top_encode(&element);
85 assert_eq!(serialized_bytes.as_slice(), expected_bytes);
86
87 let deserialized: V = check_top_decode::<V>(&serialized_bytes[..]);
89 assert_eq!(deserialized, element);
90}
91
92pub fn check_dep_encode_decode<V>(element: V, expected_bytes: &[u8])
93where
94 V: NestedEncode + NestedDecode + PartialEq + Debug,
95{
96 let serialized_bytes = check_dep_encode(&element);
98 assert_eq!(serialized_bytes.as_slice(), expected_bytes);
99
100 let deserialized: V = check_dep_decode::<V>(&serialized_bytes[..]);
102 assert_eq!(deserialized, element);
103}