multiversx_sc_scenario/scenario/model/value/
value_checkable.rs1use multiversx_chain_vm::host::context::TxFunctionName;
2
3use super::{value_set_big_uint::*, BytesValue, CheckValue, U64Value};
4use num_bigint::BigUint;
5
6pub trait Checkable<V> {
7 fn check(&self, value: V) -> bool;
8}
9
10impl Checkable<&[u8]> for BytesValue {
11 fn check(&self, value: &[u8]) -> bool {
12 self.value.as_slice() == value
13 }
14}
15
16impl Checkable<&str> for BytesValue {
17 fn check(&self, value: &str) -> bool {
18 self.check(value.as_bytes())
19 }
20}
21
22impl Checkable<&Vec<u8>> for BytesValue {
23 fn check(&self, value: &Vec<u8>) -> bool {
24 &self.value == value
25 }
26}
27
28impl Checkable<&TxFunctionName> for BytesValue {
29 fn check(&self, value: &TxFunctionName) -> bool {
30 self.value.as_slice() == value.as_str().as_bytes()
31 }
32}
33
34impl Checkable<&BigUint> for BigUintValue {
35 fn check(&self, value: &BigUint) -> bool {
36 &self.value == value
37 }
38}
39
40impl Checkable<u64> for U64Value {
41 fn check(&self, value: u64) -> bool {
42 self.value == value
43 }
44}
45
46impl<V, T> Checkable<V> for CheckValue<T>
47where
48 T: Checkable<V> + Default,
49{
50 fn check(&self, value: V) -> bool {
51 match self {
52 CheckValue::Star => true,
53 CheckValue::Equal(eq) => eq.check(value),
54 }
55 }
56}
57
58impl Checkable<&[Vec<u8>]> for Vec<CheckValue<BytesValue>> {
59 fn check(&self, values: &[Vec<u8>]) -> bool {
60 if self.len() != values.len() {
61 return false;
62 }
63 for (i, cv) in self.iter().enumerate() {
64 if !cv.check(values[i].as_slice()) {
65 return false;
66 }
67 }
68 true
69 }
70}
71
72#[cfg(test)]
73mod tests {
74 use crate::{
75 scenario::model::{BytesValue, CheckValue, Checkable, U64Value},
76 scenario_format::serde_raw::ValueSubTree,
77 };
78
79 #[test]
80 fn check_bytes() {
81 let bv = BytesValue {
82 value: b"abc".to_vec(),
83 original: ValueSubTree::Str("abc".to_string()),
84 };
85 assert!(bv.check(&b"abc"[..]));
86
87 let cb_eq = CheckValue::Equal(bv);
88 assert!(cb_eq.check(&b"abc"[..]));
89 assert!(!cb_eq.check(&b"abd"[..]));
90
91 let cb_star: CheckValue<BytesValue> = CheckValue::Star;
92 assert!(cb_star.check(&b"anything_really"[..]));
93 }
94
95 #[test]
96 fn check_u64() {
97 let u64v = U64Value {
98 value: 123,
99 original: ValueSubTree::Str("123".to_string()),
100 };
101 assert!(u64v.check(123u64));
102
103 let cb_eq = CheckValue::Equal(u64v);
104 assert!(cb_eq.check(123u64));
105
106 let cb_star: CheckValue<U64Value> = CheckValue::Star;
107 assert!(cb_star.check(1234567890));
108 }
109}