|
| 1 | +// Copyright (C) Use Ink (UK) Ltd. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +//! Assertion helpers for E2E tests with node backend. |
| 16 | +//! |
| 17 | +//! These macros provide convenient assertions similar to the sandbox test framework. |
| 18 | +
|
| 19 | +/// Assert that a contract call succeeded without reverting. |
| 20 | +/// |
| 21 | +/// This works with `CallResult` types returned from contract calls via the node backend. |
| 22 | +/// |
| 23 | +/// # Examples |
| 24 | +/// |
| 25 | +/// ```ignore |
| 26 | +/// let result = client.call(&alice, &contract_call.transfer(bob_address, amount)) |
| 27 | +/// .submit() |
| 28 | +/// .await?; |
| 29 | +/// assert_ok!(&result); |
| 30 | +/// ``` |
| 31 | +#[macro_export] |
| 32 | +macro_rules! assert_ok { |
| 33 | + ($result:expr) => {{ |
| 34 | + let result = $result; |
| 35 | + if result.dry_run.did_revert() { |
| 36 | + panic!( |
| 37 | + "Expected call to succeed but it reverted.\nError: {:?}", |
| 38 | + result.extract_error() |
| 39 | + ); |
| 40 | + } |
| 41 | + }}; |
| 42 | +} |
| 43 | + |
| 44 | +/// Assert that a contract call reverted with a specific error message. |
| 45 | +/// |
| 46 | +/// This works with `CallResult` types returned from contract calls via the node backend. |
| 47 | +/// |
| 48 | +/// # Examples |
| 49 | +/// |
| 50 | +/// ```ignore |
| 51 | +/// let result = client.call(&alice, &contract_call.transfer(bob_address, huge_amount)) |
| 52 | +/// .submit() |
| 53 | +/// .await?; |
| 54 | +/// assert_noop!(&result, "BalanceLow"); |
| 55 | +/// ``` |
| 56 | +#[macro_export] |
| 57 | +macro_rules! assert_noop { |
| 58 | + ($result:expr, $expected_err:expr) => {{ |
| 59 | + let result = $result; |
| 60 | + if !result.dry_run.did_revert() { |
| 61 | + panic!( |
| 62 | + "Expected call to revert with '{}' but it succeeded", |
| 63 | + $expected_err |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + let actual_error = result.extract_error(); |
| 68 | + if let Some(error) = actual_error { |
| 69 | + if !error.contains($expected_err) { |
| 70 | + panic!( |
| 71 | + "Expected error containing '{}' but got: {}", |
| 72 | + $expected_err, error |
| 73 | + ); |
| 74 | + } |
| 75 | + } else { |
| 76 | + panic!( |
| 77 | + "Expected error containing '{}' but got no error", |
| 78 | + $expected_err |
| 79 | + ); |
| 80 | + } |
| 81 | + }}; |
| 82 | +} |
| 83 | + |
| 84 | +/// Assert that the last event from a contract call matches the expected event. |
| 85 | +/// |
| 86 | +/// This macro extracts events from the contract result and compares the last |
| 87 | +/// emitted event with the expected event structure by comparing encoded bytes. |
| 88 | +/// |
| 89 | +/// # Examples |
| 90 | +/// |
| 91 | +/// ```ignore |
| 92 | +/// let result = client.call(&alice, &contract_call.transfer(bob_address, amount)) |
| 93 | +/// .submit() |
| 94 | +/// .await?; |
| 95 | +/// |
| 96 | +/// assert_last_event!( |
| 97 | +/// &result, |
| 98 | +/// Transfer { |
| 99 | +/// from: contract.addr, |
| 100 | +/// to: bob_address, |
| 101 | +/// value: amount |
| 102 | +/// } |
| 103 | +/// ); |
| 104 | +/// ``` |
| 105 | +#[macro_export] |
| 106 | +macro_rules! assert_last_event { |
| 107 | + ($result:expr, $expected_event:expr) => {{ |
| 108 | + // Extract the last contract event from the result |
| 109 | + let events = ($result) |
| 110 | + .contract_emitted_events() |
| 111 | + .expect("Failed to get contract events"); |
| 112 | + |
| 113 | + if events.is_empty() { |
| 114 | + panic!("No events emitted, expected: {:?}", $expected_event); |
| 115 | + } |
| 116 | + |
| 117 | + let last_event = events.last().expect("No last event"); |
| 118 | + |
| 119 | + // Compare the encoded event data |
| 120 | + let expected_bytes = ::ink::scale::Encode::encode(&$expected_event); |
| 121 | + let actual_bytes = &last_event.event.data; |
| 122 | + |
| 123 | + if &expected_bytes != actual_bytes { |
| 124 | + panic!( |
| 125 | + "Event mismatch!\nExpected (encoded): {:?}\nActual (encoded): {:?}", |
| 126 | + expected_bytes, actual_bytes |
| 127 | + ); |
| 128 | + } |
| 129 | + }}; |
| 130 | +} |
0 commit comments