multiversx_sc_scenario/scenario/model/transaction/
tx_expect.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use super::TxResponse;
use crate::{
    scenario::model::{BytesValue, CheckLogs, CheckValue, CheckValueList, U64Value},
    scenario_format::{
        interpret_trait::{InterpretableFrom, InterpreterContext, IntoRaw},
        serde_raw::TxExpectRaw,
    },
    scenario_model::Checkable,
};
use multiversx_chain_vm::tx_mock::result_values_to_string;
use multiversx_sc::chain_core::types::ReturnCode;

#[derive(Debug, Clone)]
pub struct TxExpect {
    pub out: CheckValueList,
    pub status: CheckValue<U64Value>,
    pub message: CheckValue<BytesValue>,
    pub logs: CheckLogs,
    pub gas: CheckValue<U64Value>,
    pub refund: CheckValue<U64Value>,
    pub build_from_response: bool,
    pub additional_error_message: String,
}

impl Default for TxExpect {
    fn default() -> Self {
        Self::ok()
    }
}

impl TxExpect {
    pub fn ok() -> Self {
        TxExpect {
            out: CheckValue::Star,
            status: CheckValue::Equal(U64Value::zero()),
            message: CheckValue::Star,
            logs: CheckLogs::Star,
            gas: CheckValue::Star,
            refund: CheckValue::Star,
            build_from_response: true,
            additional_error_message: Default::default(),
        }
    }

    pub fn err<S, E>(status_code_expr: S, err_msg_expr: E) -> Self
    where
        U64Value: From<S>,
        BytesValue: From<E>,
    {
        let status_code = U64Value::from(status_code_expr);
        let err_msg = BytesValue::from(err_msg_expr);

        TxExpect {
            out: CheckValue::Star,
            status: CheckValue::Equal(status_code),
            message: CheckValue::Equal(err_msg),
            logs: CheckLogs::Star,
            gas: CheckValue::Star,
            refund: CheckValue::Star,
            build_from_response: true,
            additional_error_message: Default::default(),
        }
    }

    pub fn user_error<E>(err_msg_expr: E) -> Self
    where
        BytesValue: From<E>,
    {
        Self::err(ReturnCode::UserError, err_msg_expr)
    }

    pub fn no_result(mut self) -> Self {
        self.out = CheckValue::Equal(Vec::new());
        self.build_from_response = false;
        self
    }

    pub fn result(mut self, value: &str) -> Self {
        let mut check_results = match self.out {
            CheckValue::Star => Vec::new(),
            CheckValue::Equal(check_results) => check_results,
        };
        check_results.push(CheckValue::Equal(BytesValue::interpret_from(
            value,
            &InterpreterContext::default(),
        )));
        self.out = CheckValue::Equal(check_results);
        self.build_from_response = false;
        self
    }

    pub fn additional_error_message<A>(mut self, message: A) -> Self
    where
        A: AsRef<str>,
    {
        self.additional_error_message = message.as_ref().to_string();
        self
    }

    fn check_response(&self, tx_response: &TxResponse) {
        assert!(
            self.status.check(tx_response.tx_error.status.as_u64()),
            "{}result code mismatch. Want: {}. Have: {}. Message: {}",
            &self.additional_error_message,
            self.status,
            tx_response.tx_error.status,
            &tx_response.tx_error.message,
        );

        assert!(
            self.out.check(&tx_response.out),
            "{}bad out value. Want: [{}]. Have: [{}]",
            &self.additional_error_message,
            self.out_to_string(),
            result_values_to_string(&tx_response.out),
        );

        assert!(
            self.message.check(tx_response.tx_error.message.as_str()),
            "{}result message mismatch. Want: {}. Have: {}.",
            &self.additional_error_message,
            &self.message,
            &tx_response.tx_error.message,
        );
    }

    pub(crate) fn update_from_response(&mut self, tx_response: &TxResponse) {
        if self.build_from_response {
            self.check_response(tx_response);
            *self = tx_response.to_expect();
        }
    }
}

impl InterpretableFrom<TxExpectRaw> for TxExpect {
    fn interpret_from(from: TxExpectRaw, context: &InterpreterContext) -> Self {
        TxExpect {
            out: CheckValueList::interpret_from(from.out, context),
            status: CheckValue::<U64Value>::interpret_from(from.status, context),
            logs: CheckLogs::interpret_from(from.logs, context),
            message: CheckValue::<BytesValue>::interpret_from(from.message, context),
            gas: CheckValue::<U64Value>::interpret_from(from.gas, context),
            refund: CheckValue::<U64Value>::interpret_from(from.refund, context),
            build_from_response: false,
            additional_error_message: Default::default(),
        }
    }
}

impl IntoRaw<TxExpectRaw> for TxExpect {
    fn into_raw(self) -> TxExpectRaw {
        TxExpectRaw {
            out: self.out.into_raw(),
            status: self.status.into_raw_explicit(),
            message: self.message.into_raw(),
            logs: self.logs.into_raw(),
            gas: self.gas.into_raw(),
            refund: self.refund.into_raw(),
        }
    }
}

impl TxExpect {
    pub fn out_to_string(&self) -> String {
        match &self.out {
            CheckValue::Star => "*".to_string(),
            CheckValue::Equal(list) => {
                itertools::join(list.iter().map(|val| format!("{val}")), ", ")
            },
        }
    }
}