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
use crate::{data::Value, Datum};
use super::TxTester;
impl TxTester {
/// ## Filtering methods for testing outputs
///
/// Not apply filter to outputs
pub fn all_outputs(&mut self) -> &mut Self {
self.outputs_evaluating = self.tx_body.outputs.clone();
self
}
/// ## Filtering methods for testing outputs
///
/// Filter outputs by address
pub fn outputs_at(&mut self, address: &str) -> &mut Self {
self.outputs_evaluating = self
.tx_body
.outputs
.iter()
.filter(|output| output.address == address)
.cloned()
.collect();
self
}
/// ## Filtering methods for testing outputs
///
/// Filter outputs by unit
pub fn outputs_with(&mut self, unit: &str) -> &mut Self {
self.outputs_evaluating = self
.tx_body
.outputs
.iter()
.filter(|output| {
let output_value = Value::from_asset_vec(&output.amount);
let quantity = output_value.get(unit);
quantity > 0
})
.cloned()
.collect();
self
}
/// ## Filtering methods for testing outputs
///
/// Filter outputs by policy ID
pub fn outputs_with_policy(&mut self, policy_id: &str) -> &mut Self {
self.outputs_evaluating = self
.tx_body
.outputs
.iter()
.filter(|output| {
let output_value = Value::from_asset_vec(&output.amount);
let assets = output_value.get_policy_assets(policy_id);
assets.len() > 0
})
.cloned()
.collect();
self
}
/// ## Filtering methods for testing outputs
///
/// Filter outputs by address and policy ID
pub fn outputs_at_with_policy(&mut self, address: &str, policy_id: &str) -> &mut Self {
self.outputs_evaluating = self
.tx_body
.outputs
.iter()
.filter(|output| {
let output_value = Value::from_asset_vec(&output.amount);
let assets = output_value.get_policy_assets(policy_id);
output.address == address && assets.len() > 0
})
.cloned()
.collect();
self
}
/// ## Filtering methods for testing outputs
///
/// Filter outputs by address and unit
pub fn outputs_at_with(&mut self, address: &str, unit: &str) -> &mut Self {
self.outputs_evaluating = self
.tx_body
.outputs
.iter()
.filter(|output| {
let output_value = Value::from_asset_vec(&output.amount);
let quantity = output_value.get(unit);
output.address == address && quantity > 0
})
.cloned()
.collect();
self
}
/// ## Testing methods for outputs
///
/// *Reminder - It must be called after filtering methods for outputs*
///
/// Check if outputs contain the expected value.
pub fn outputs_value(&mut self, expected_value: Value) -> &mut Self {
let mut value = Value::new();
self.outputs_evaluating.iter().for_each(|output| {
value.add_assets(&output.amount);
});
let is_value_correct = value.eq(&expected_value);
if !is_value_correct {
self.add_trace(
"outputs_value",
&format!(
"tx outputs {:?} have value {:?}, expected {:?}",
self.outputs_evaluating, value, expected_value
),
);
}
self
}
/// ## Testing methods for outputs
///
/// *Reminder - It must be called after filtering methods for outputs*
///
/// Check if outputs contain a specific inline datum.
pub fn outputs_inline_datum_exist(&mut self, datum_cbor: &str) -> &mut Self {
let outputs_with_inline_datum: Vec<_> = self
.outputs_evaluating
.iter()
.filter(|output| {
if let Some(Datum::Inline(datum)) = &output.datum {
*datum == *datum_cbor
} else {
false
}
})
.cloned()
.collect();
if outputs_with_inline_datum.is_empty() {
self.add_trace(
"outputs_inline_datum_exist",
&format!("No outputs with inline datum matching: {}", datum_cbor),
);
}
self
}
}