Skip to content

Commit 8cdd8ac

Browse files
add test for trait associated types in contracts
1 parent 2f86d46 commit 8cdd8ac

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/associated_type/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "test_associated_types"
3+
version.workspace = true
4+
authors = ["Stellar Development Foundation <info@stellar.org>"]
5+
license = "Apache-2.0"
6+
edition = "2021"
7+
publish = false
8+
rust-version.workspace = true
9+
10+
[lib]
11+
crate-type = ["cdylib"]
12+
doctest = false
13+
14+
[dependencies]
15+
soroban-sdk = {path = "../../soroban-sdk"}
16+
17+
[dev-dependencies]
18+
soroban-sdk = {path = "../../soroban-sdk", features = ["testutils"]}

tests/associated_type/src/lib.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#![no_std]
2+
use soroban_sdk::{Env, String, contract, contractimpl};
3+
4+
// The associated type to hold a default impl for a trait pattern is a pattern that's seen in the
5+
// OpenZeppelin contract library.
6+
7+
pub struct DefaultImpl;
8+
9+
impl Trait for DefaultImpl {
10+
type Impl = Self;
11+
fn exec(env: &Env) -> String {
12+
String::from_str(env, "default")
13+
}
14+
}
15+
16+
pub trait Trait {
17+
type Impl: Trait;
18+
19+
fn exec(env: &Env) -> String {
20+
Self::Impl::exec(env)
21+
}
22+
}
23+
24+
#[contract]
25+
pub struct Contract;
26+
27+
#[contractimpl]
28+
impl Trait for Contract {
29+
type Impl = DefaultImpl;
30+
fn exec(env: &Env) -> String {
31+
Self::Impl::exec(env)
32+
}
33+
}
34+
35+
#[cfg(test)]
36+
mod test {
37+
use soroban_sdk::{Env, String};
38+
39+
use crate::{Contract, ContractClient};
40+
41+
#[test]
42+
fn test_exec() {
43+
let e = Env::default();
44+
let contract_id = e.register(Contract, ());
45+
let client = ContractClient::new(&e, &contract_id);
46+
47+
let res = client.exec();
48+
assert_eq!(res, String::from_str(&e, "default"));
49+
}
50+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
{
2+
"generators": {
3+
"address": 1,
4+
"nonce": 0,
5+
"mux_id": 0
6+
},
7+
"auth": [
8+
[],
9+
[]
10+
],
11+
"ledger": {
12+
"protocol_version": 23,
13+
"sequence_number": 0,
14+
"timestamp": 0,
15+
"network_id": "0000000000000000000000000000000000000000000000000000000000000000",
16+
"base_reserve": 0,
17+
"min_persistent_entry_ttl": 4096,
18+
"min_temp_entry_ttl": 16,
19+
"max_entry_ttl": 6312000,
20+
"ledger_entries": [
21+
[
22+
{
23+
"contract_data": {
24+
"contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM",
25+
"key": "ledger_key_contract_instance",
26+
"durability": "persistent"
27+
}
28+
},
29+
[
30+
{
31+
"last_modified_ledger_seq": 0,
32+
"data": {
33+
"contract_data": {
34+
"ext": "v0",
35+
"contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM",
36+
"key": "ledger_key_contract_instance",
37+
"durability": "persistent",
38+
"val": {
39+
"contract_instance": {
40+
"executable": {
41+
"wasm": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
42+
},
43+
"storage": null
44+
}
45+
}
46+
}
47+
},
48+
"ext": "v0"
49+
},
50+
4095
51+
]
52+
],
53+
[
54+
{
55+
"contract_code": {
56+
"hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
57+
}
58+
},
59+
[
60+
{
61+
"last_modified_ledger_seq": 0,
62+
"data": {
63+
"contract_code": {
64+
"ext": "v0",
65+
"hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
66+
"code": ""
67+
}
68+
},
69+
"ext": "v0"
70+
},
71+
4095
72+
]
73+
]
74+
]
75+
},
76+
"events": []
77+
}

0 commit comments

Comments
 (0)