Skip to content

Commit e6fbd3a

Browse files
Test Wasm execution for alloc test contract (#1452)
### What Add Wasm-based testing for the allocation contract alongside the native tests. ### Why Testing both native and Wasm implementations ensures the contract behaves consistently regardless of execution environment. Makes sure that the alloc functionality works correctly in the actual Wasm VM, which the contract is built to the wasm target for.
1 parent d534103 commit e6fbd3a

File tree

3 files changed

+44
-92
lines changed

3 files changed

+44
-92
lines changed

tests/alloc/src/lib.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,4 @@ impl Contract {
2121
}
2222
}
2323

24-
#[cfg(test)]
25-
mod test {
26-
use soroban_sdk::{vec, Env};
27-
28-
use crate::{Contract, ContractClient};
29-
30-
#[test]
31-
fn test_add() {
32-
let e = Env::default();
33-
let contract_id = e.register(Contract, ());
34-
let client = ContractClient::new(&e, &contract_id);
35-
36-
let list = client.num_list(&5);
37-
assert_eq!(list, vec![&e, 0, 1, 2, 3, 4])
38-
}
39-
}
24+
mod test;

tests/alloc/src/test.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#![cfg(test)]
2+
use soroban_sdk::{testutils::EnvTestConfig, vec, Env};
3+
4+
use crate::{Contract, ContractClient};
5+
6+
mod imported {
7+
soroban_sdk::contractimport!(
8+
file = "../../target/wasm32-unknown-unknown/release/test_alloc.wasm"
9+
);
10+
}
11+
12+
macro_rules! tests {
13+
($context:ident, $contract:expr) => {
14+
mod $context {
15+
use super::*;
16+
17+
#[test]
18+
fn test() {
19+
let e = Env::new_with_config(EnvTestConfig {
20+
// Disable test snapshots because the tests in this repo will run across
21+
// multiple hosts, and this test uses a wasm file that won't build consistently
22+
// across different hosts.
23+
capture_snapshot_at_drop: false,
24+
});
25+
let contract_id = e.register($contract, ());
26+
let client = ContractClient::new(&e, &contract_id);
27+
28+
let list = client.num_list(&50);
29+
assert_eq!(
30+
list,
31+
vec![
32+
&e, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
33+
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
34+
39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
35+
]
36+
)
37+
}
38+
}
39+
};
40+
}
41+
42+
tests!(native, Contract);
43+
tests!(wasm, imported::WASM);

tests/alloc/test_snapshots/test/test_add.1.json

Lines changed: 0 additions & 76 deletions
This file was deleted.

0 commit comments

Comments
 (0)