multiversx_sc_scenario/scenario/model/value/
value_checkable.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use multiversx_chain_vm::tx_mock::TxFunctionName;

use super::{value_set_big_uint::*, BytesValue, CheckValue, U64Value};
use num_bigint::BigUint;

pub trait Checkable<V> {
    fn check(&self, value: V) -> bool;
}

impl Checkable<&[u8]> for BytesValue {
    fn check(&self, value: &[u8]) -> bool {
        self.value.as_slice() == value
    }
}

impl Checkable<&str> for BytesValue {
    fn check(&self, value: &str) -> bool {
        self.check(value.as_bytes())
    }
}

impl Checkable<&Vec<u8>> for BytesValue {
    fn check(&self, value: &Vec<u8>) -> bool {
        &self.value == value
    }
}

impl Checkable<&TxFunctionName> for BytesValue {
    fn check(&self, value: &TxFunctionName) -> bool {
        self.value.as_slice() == value.as_str().as_bytes()
    }
}

impl Checkable<&BigUint> for BigUintValue {
    fn check(&self, value: &BigUint) -> bool {
        &self.value == value
    }
}

impl Checkable<u64> for U64Value {
    fn check(&self, value: u64) -> bool {
        self.value == value
    }
}

impl<V, T> Checkable<V> for CheckValue<T>
where
    T: Checkable<V> + Default,
{
    fn check(&self, value: V) -> bool {
        match self {
            CheckValue::Star => true,
            CheckValue::Equal(eq) => eq.check(value),
        }
    }
}

impl Checkable<&[Vec<u8>]> for Vec<CheckValue<BytesValue>> {
    fn check(&self, values: &[Vec<u8>]) -> bool {
        if self.len() != values.len() {
            return false;
        }
        for (i, cv) in self.iter().enumerate() {
            if !cv.check(values[i].as_slice()) {
                return false;
            }
        }
        true
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        scenario::model::{BytesValue, CheckValue, Checkable, U64Value},
        scenario_format::serde_raw::ValueSubTree,
    };

    #[test]
    fn check_bytes() {
        let bv = BytesValue {
            value: b"abc".to_vec(),
            original: ValueSubTree::Str("abc".to_string()),
        };
        assert!(bv.check(&b"abc"[..]));

        let cb_eq = CheckValue::Equal(bv);
        assert!(cb_eq.check(&b"abc"[..]));
        assert!(!cb_eq.check(&b"abd"[..]));

        let cb_star: CheckValue<BytesValue> = CheckValue::Star;
        assert!(cb_star.check(&b"anything_really"[..]));
    }

    #[test]
    fn check_u64() {
        let u64v = U64Value {
            value: 123,
            original: ValueSubTree::Str("123".to_string()),
        };
        assert!(u64v.check(123u64));

        let cb_eq = CheckValue::Equal(u64v);
        assert!(cb_eq.check(123u64));

        let cb_star: CheckValue<U64Value> = CheckValue::Star;
        assert!(cb_star.check(1234567890));
    }
}