multiversx_sc_codec/
test_util.rs

1use crate::*;
2use alloc::vec::Vec;
3use core::fmt::Debug;
4use unwrap_infallible::UnwrapInfallible;
5
6/// Calls top encode and panics if an encoding error occurs.
7/// Do not use in smart contracts!
8pub 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
15/// Calls nested encode and panics if an encoding error occurs.
16/// Do not use in smart contracts!
17pub 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
24/// Calls both the fast exit and the regular top-encode,
25/// compares that the outputs are equal, then returns the result.
26/// To be used in serialization tests.
27pub 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
34/// Calls both the fast exit and the regular dep-encode,
35/// compares that the outputs are equal, then returns the result.
36/// To be used in serialization tests.
37pub 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
44/// Calls nested decode and panics if an encoding error occurs.
45/// Do not use in smart contracts!
46pub 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
50/// Calls both the fast exit and the regular top-decode,
51/// compares that the outputs are equal, then returns the result.
52/// To be used in serialization tests.
53pub 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
60/// Calls both the fast exit and the regular dep-decode,
61/// compares that the outputs are equal, then returns the result.
62/// To be used in serialization tests.
63pub 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/// backwards compatibility only, will remove in next major release
71#[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    // serialize
84    let serialized_bytes = check_top_encode(&element);
85    assert_eq!(serialized_bytes.as_slice(), expected_bytes);
86
87    // deserialize
88    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    // serialize
97    let serialized_bytes = check_dep_encode(&element);
98    assert_eq!(serialized_bytes.as_slice(), expected_bytes);
99
100    // deserialize
101    let deserialized: V = check_dep_decode::<V>(&serialized_bytes[..]);
102    assert_eq!(deserialized, element);
103}