Commit a60b7e8
Simplify contract event testing (#1638)
### What
Adds a testutils struct `ContractEvents` to be returned by
`env.events().all()` that now returns the XDR variants of the emitted
contract events. To simplify testing, Events defined by `contractevents`
can be converted to the XDR variant with `to_contract_event`.
Additional partial eq impl were added to maintain backwards
compatibility with users who test with existing methods. This will
impact tests where users directly check the length of the events array,
via `all().len()`, who will now need to write `all().events.len()`.
Lastly, this also allows the debug information when testing events to
display actual values, making it easier to determine what went wrong
during test failures:
Before:
```
left: Vec(Ok((Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM), Vec(Ok(Symbol(transfer)), Ok(Address(obj#35)), Ok(Address(obj#37))), Map(obj#43))))
right: Vec(Ok((Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM), Vec(Ok(Symbol(transfer)), Ok(Address(obj#11)), Ok(Address(obj#29))), Map(obj#53))))
```
After:
```
left: [ContractEvent { ext: V0, contract_id: Some(ContractId(Hash(0000000000000000000000000000000000000000000000000000000000000001))), type_: Contract, body: V0(ContractEventV0 { topics: VecM([Symbol(ScSymbol(StringM(transfer))), Address(Contract(ContractId(Hash(0000000000000000000000000000000000000000000000000000000000000002)))), Address(Account(AccountId(PublicKeyTypeEd25519(Uint256(0000000000000000000000000000000000000000000000000000000000000003)))))]), data: Map(Some(ScMap(VecM([ScMapEntry { key: Symbol(ScSymbol(StringM(amount))), val: I128(Int128Parts { hi: 0, lo: 1 }) }, ScMapEntry { key: Symbol(ScSymbol(StringM(to_muxed_id))), val: U64(1) }])))) }) }]
right: [ContractEvent { ext: V0, contract_id: Some(ContractId(Hash(0000000000000000000000000000000000000000000000000000000000000001))), type_: Contract, body: V0(ContractEventV0 { topics: VecM([Symbol(ScSymbol(StringM(transfer))), Address(Contract(ContractId(Hash(0000000000000000000000000000000000000000000000000000000000000002)))), Address(Account(AccountId(PublicKeyTypeEd25519(Uint256(0000000000000000000000000000000000000000000000000000000000000003)))))]), data: Map(Some(ScMap(VecM([ScMapEntry { key: Symbol(ScSymbol(StringM(amount))), val: I128(Int128Parts { hi: 0, lo: 0 }) }, ScMapEntry { key: Symbol(ScSymbol(StringM(to_muxed_id))), val: U64(1) }])))) }) }]
```
An example test:
```rust
#[test]
fn test_event() {
let env = Env::default();
let contract_id = env.register(Contract, ());
let client = ContractClient::new(&env, &contract_id);
let from = Address::generate(&env);
let to = MuxedAddress::generate(&env);
let amount = 1i128;
client.transfer(&from, &to, &amount);
assert_eq!(
env.events().all(),
std::vec![Transfer {
from: from.clone(),
to: to.address(),
amount,
to_muxed_id: to.id(),
}
.to_contract_event(&env, &contract_id)],
);
}
```
### Why
Solves #1566. Makes it easier to validate events during tests.
### Known limitations
None
---------
Co-authored-by: Leigh <351529+leighmcculloch@users.noreply.github.com>1 parent d89ee3b commit a60b7e8
File tree
9 files changed
+979
-333
lines changed- soroban-sdk
- src
- tests
- test_snapshots/tests/contract_event
- tests-expanded
- tests
- events_ref/src
- events/src
9 files changed
+979
-333
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
61 | 64 | | |
62 | 65 | | |
63 | 66 | | |
64 | 67 | | |
65 | 68 | | |
66 | 69 | | |
67 | 70 | | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
68 | 86 | | |
69 | 87 | | |
70 | 88 | | |
| |||
116 | 134 | | |
117 | 135 | | |
118 | 136 | | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | 137 | | |
123 | 138 | | |
124 | 139 | | |
125 | | - | |
| 140 | + | |
126 | 141 | | |
127 | | - | |
128 | | - | |
| 142 | + | |
| 143 | + | |
129 | 144 | | |
130 | 145 | | |
131 | 146 | | |
132 | 147 | | |
133 | 148 | | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
144 | 153 | | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
150 | 157 | | |
151 | | - | |
152 | | - | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
153 | 161 | | |
154 | 162 | | |
0 commit comments