Skip to content

Commit 2e365ad

Browse files
committed
refactor: macros
1 parent 6dced7c commit 2e365ad

File tree

2 files changed

+63
-153
lines changed

2 files changed

+63
-153
lines changed

crates/e2e/src/assertions.rs

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,72 +12,108 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
//! Assertion helpers for E2E tests with node backend.
15+
//! Assertion helpers for ink! E2E tests.
1616
//!
17-
//! These macros provide convenient assertions similar to the sandbox test framework.
17+
//! These macros provide convenient assertions similar to FRAME's testing macros,
18+
//! adapted for contract call results.
1819
1920
/// Assert that a contract call succeeded without reverting.
2021
///
21-
/// This works with `CallResult` types returned from contract calls via the node backend.
22+
/// This macro follows FRAME's `assert_ok!` convention for consistency across
23+
/// the Polkadot ecosystem. It verifies that a contract call completed successfully
24+
/// and did not revert.
25+
///
26+
/// # Variants
27+
///
28+
/// - `assert_ok!(result)` - Assert the call didn't revert
29+
/// - `assert_ok!(result, expected)` - Assert the call didn't revert AND the return
30+
/// value equals `expected`
2231
///
2332
/// # Examples
2433
///
2534
/// ```ignore
26-
/// let result = client.call(&alice, &contract_call.transfer(bob_address, amount))
35+
/// // Just assert success
36+
/// let result = client.call(&alice, &contract_call.transfer(bob, amount))
2737
/// .submit()
2838
/// .await?;
29-
/// assert_ok!(&result);
39+
/// assert_ok!(result);
40+
///
41+
/// // Assert success and check return value
42+
/// let result = client.call(&alice, &contract_call.balance_of(bob))
43+
/// .dry_run()
44+
/// .await?;
45+
/// assert_ok!(result, expected_balance);
3046
/// ```
3147
#[macro_export]
3248
macro_rules! assert_ok {
33-
($result:expr) => {{
49+
($result:expr $(,)?) => {{
50+
let result = $result;
51+
if result.dry_run.did_revert() {
52+
panic!(
53+
"Expected call to succeed but it reverted.\nError: {:?}",
54+
result.extract_error()
55+
);
56+
}
57+
result
58+
}};
59+
($result:expr, $expected:expr $(,)?) => {{
3460
let result = $result;
3561
if result.dry_run.did_revert() {
3662
panic!(
3763
"Expected call to succeed but it reverted.\nError: {:?}",
3864
result.extract_error()
3965
);
4066
}
67+
assert_eq!(
68+
result.return_value(),
69+
$expected,
70+
"Return value mismatch"
71+
);
72+
result
4173
}};
4274
}
4375

44-
/// Assert that a contract call reverted with a specific error message.
76+
/// Assert that a contract call reverted with a specific error.
4577
///
46-
/// This works with `CallResult` types returned from contract calls via the node backend.
78+
/// This macro follows FRAME's `assert_noop!` convention, which stands for
79+
/// "assert no operation" - meaning the call should fail without changing state.
80+
/// Since reverted contract calls don't mutate state, this verifies the call
81+
/// reverted with the expected error message.
82+
///
83+
/// # Variants
84+
///
85+
/// - `assert_noop!(result, expected_error)` - Assert the call reverted with an error
86+
/// containing `expected_error`
4787
///
4888
/// # Examples
4989
///
5090
/// ```ignore
51-
/// let result = client.call(&alice, &contract_call.transfer(bob_address, huge_amount))
91+
/// let result = client.call(&alice, &contract_call.transfer(bob, huge_amount))
5292
/// .submit()
5393
/// .await?;
54-
/// assert_noop!(&result, "BalanceLow");
94+
/// assert_noop!(result, "BalanceLow");
5595
/// ```
5696
#[macro_export]
5797
macro_rules! assert_noop {
58-
($result:expr, $expected_err:expr) => {{
98+
($result:expr, $expected_error:expr $(,)?) => {{
5999
let result = $result;
60100
if !result.dry_run.did_revert() {
61101
panic!(
62-
"Expected call to revert with '{}' but it succeeded",
63-
$expected_err
102+
"Expected call to revert with '{}' but it succeeded.\nReturn value: {:?}",
103+
$expected_error,
104+
result.return_data()
64105
);
65106
}
66107

67108
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 {
109+
if actual_error != Some($expected_error.to_string()) {
76110
panic!(
77-
"Expected error containing '{}' but got no error",
78-
$expected_err
111+
"Expected error '{}' but got {:?}",
112+
$expected_error,
113+
actual_error
79114
);
80115
}
116+
result
81117
}};
82118
}
83119

crates/sandbox/src/lib.rs

Lines changed: 4 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -71,141 +71,15 @@ pub use client::{
7171
preset,
7272
};
7373
pub use error::E2EError;
74+
pub use ink_e2e::IntoAccountId;
75+
pub use ink_e2e_macro::test;
76+
77+
// Re-export assertion macros from ink_e2e for unified API across backends
7478
pub use ink_e2e::{
75-
IntoAccountId,
7679
assert_last_event,
7780
assert_noop,
7881
assert_ok,
7982
};
80-
pub use ink_e2e_macro::test;
81-
82-
/// Asserts that a contract call succeeded without reverting.
83-
///
84-
/// This macro follows FRAME's `assert_ok!` convention for consistency across
85-
/// the Polkadot ecosystem. It verifies that a contract call completed successfully
86-
/// and did not revert. If the call reverted, the macro panics with a detailed
87-
/// error message extracted from the call trace.
88-
///
89-
/// # Behavior
90-
///
91-
/// - Takes a `CallResult` as input
92-
/// - Checks if `dry_run.did_revert()` is `false`
93-
/// - Panics with error details if the call reverted
94-
/// - Returns the `CallResult` for further inspection if successful
95-
///
96-
/// # Examples
97-
///
98-
/// ```ignore
99-
/// let result = client.call(&alice, &transfer).submit().await?;
100-
/// assert_ok!(result);
101-
/// ```
102-
#[macro_export]
103-
macro_rules! assert_ok {
104-
($result:expr) => {{
105-
let result = $result;
106-
if result.dry_run.did_revert() {
107-
panic!(
108-
"Expected call to succeed but it reverted.\nError: {:?}",
109-
result.extract_error()
110-
);
111-
}
112-
result
113-
}};
114-
($result:expr, $($msg:tt)+) => {{
115-
let result = $result;
116-
if result.dry_run.did_revert() {
117-
panic!(
118-
"{}\nExpected call to succeed but it reverted.\nError: {:?}",
119-
format_args!($($msg)+),
120-
result.extract_error()
121-
);
122-
}
123-
result
124-
}};
125-
}
126-
127-
/// Asserts that a contract call reverted with a specific error.
128-
///
129-
/// This macro follows FRAME's `assert_noop!` convention, which stands for
130-
/// "assert no operation" - meaning the call should fail without changing state.
131-
/// It verifies that a contract call reverted and that the revert reason matches
132-
/// the expected error string.
133-
///
134-
/// # Behavior
135-
///
136-
/// - Takes a `CallResult` and an expected error string as input
137-
/// - Checks if `dry_run.did_revert()` is `true`
138-
/// - Panics if the call succeeded (did not revert)
139-
/// - Extracts the error from the call trace using `extract_error()`
140-
/// - Panics if the actual error doesn't match the expected error
141-
/// - Returns the `CallResult` if both checks pass
142-
///
143-
/// # Examples
144-
///
145-
/// ```ignore
146-
/// let result = client.call(&alice, &insufficient_transfer).submit().await?;
147-
/// assert_noop!(result, "BalanceLow");
148-
/// ```
149-
#[macro_export]
150-
macro_rules! assert_noop {
151-
($result:expr, $expected_error:expr) => {{
152-
let result = $result;
153-
if !result.dry_run.did_revert() {
154-
panic!(
155-
"Expected call to revert with '{}' but it succeeded",
156-
$expected_error
157-
);
158-
}
159-
160-
let actual_error = result.extract_error();
161-
if actual_error != Some($expected_error.to_string()) {
162-
panic!(
163-
"Expected error '{}' but got {:?}",
164-
$expected_error,
165-
actual_error
166-
);
167-
}
168-
169-
result
170-
}};
171-
($result:expr, $expected_error:expr, $($msg:tt)+) => {{
172-
let result = $result;
173-
if !result.dry_run.did_revert() {
174-
panic!(
175-
"{}\nExpected call to revert with '{}' but it succeeded",
176-
format_args!($($msg)+),
177-
$expected_error
178-
);
179-
}
180-
181-
let actual_error = result.extract_error();
182-
if actual_error != Some($expected_error.to_string()) {
183-
panic!(
184-
"{}\nExpected error '{}' but got {:?}",
185-
format_args!($($msg)+),
186-
$expected_error,
187-
actual_error
188-
);
189-
}
190-
191-
result
192-
}};
193-
}
194-
195-
/// Asserts that the latest contract event matches an expected event.
196-
///
197-
/// This macro verifies that the last emitted contract event from the sandbox
198-
/// matches the provided expected event.
199-
///
200-
/// # Parameters
201-
/// - `client` - Mutable reference to the sandbox client
202-
/// - `event` - The expected event
203-
#[macro_export]
204-
macro_rules! assert_last_event {
205-
($client:expr, $event:expr $(,)?) => {
206-
$crate::client::assert_last_contract_event_inner($client, $event)
207-
};
208-
}
20983

21084
/// A snapshot of the storage.
21185
#[derive(Clone, Debug)]

0 commit comments

Comments
 (0)