-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathtest.rs
More file actions
203 lines (179 loc) · 5.97 KB
/
Copy pathtest.rs
File metadata and controls
203 lines (179 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#![cfg(test)]
extern crate std;
use crucible::prelude::*;
use crucible::{assert_emitted, assert_reverts};
use soroban_sdk::{symbol_short, Address};
use crate::{Token, TokenClient};
// ---------------------------------------------------------------------------
// Test fixture
// ---------------------------------------------------------------------------
#[fixture]
struct Ctx {
pub env: MockEnv,
pub id: Address,
}
impl Ctx {
pub fn setup() -> Self {
let env = MockEnv::builder()
.with_contract::<Token>()
.with_account("admin", Stroops::xlm(100))
.with_account("alice", Stroops::xlm(100))
.with_account("bob", Stroops::xlm(100))
.build();
let id = env.contract_id::<Token>();
let admin = env.account("admin");
// Initialize with mock auth so admin.require_auth() in sub-calls passes
env.mock_all_auths();
TokenClient::new(env.inner(), &id).initialize(&admin);
Ctx { env, id }
}
fn client(&self) -> TokenClient<'_> {
TokenClient::new(self.env.inner(), &self.id)
}
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[test]
fn test_balance_starts_at_zero() {
let f = Ctx::setup();
let alice = f.env.account("alice");
assert_eq!(f.client().balance(&alice), 0);
}
#[test]
fn test_admin_can_mint() {
let f = Ctx::setup();
f.env.mock_all_auths();
let alice = f.env.account("alice");
f.client().mint(&alice, &1_000_i128);
assert_eq!(f.client().balance(&alice), 1_000);
}
#[test]
fn test_mint_without_auth_reverts() {
// Fresh env — no mock_all_auths, so admin.require_auth() panics
let env = MockEnv::builder()
.with_contract::<Token>()
.with_account("admin", Stroops::xlm(100))
.with_account("alice", Stroops::xlm(100))
.build();
let id = env.contract_id::<Token>();
let admin = env.account("alice"); // wrong admin address
// Do not mock auths; this should trigger auth failure
// env.mock_all_auths(); // removed to test real auth behavior
TokenClient::new(env.inner(), &id).initialize(&admin);
assert_reverts!(
TokenClient::new(env.inner(), &id).mint(&env.account("alice"), &0_i128),
"admin requires auth"
);
}
#[test]
fn test_transfer_moves_balance() {
let f = Ctx::setup();
f.env.mock_all_auths();
let alice = f.env.account("alice");
let bob = f.env.account("bob");
f.client().mint(&alice, &1_000_i128);
f.client().transfer(&alice, &bob, &400_i128);
assert_eq!(f.client().balance(&alice), 600);
assert_eq!(f.client().balance(&bob), 400);
}
#[test]
fn test_transfer_insufficient_balance_reverts() {
let f = Ctx::setup();
f.env.mock_all_auths();
let alice = f.env.account("alice");
let bob = f.env.account("bob");
f.client().mint(&alice, &100_i128);
assert_reverts!(
f.client().transfer(&alice, &bob, &500_i128),
"insufficient balance"
);
}
#[test]
fn test_burn_reduces_balance() {
let f = Ctx::setup();
f.env.mock_all_auths();
let alice = f.env.account("alice");
f.client().mint(&alice, &1_000_i128);
f.client().burn(&alice, &300_i128);
assert_eq!(f.client().balance(&alice), 700);
}
#[test]
fn test_burn_more_than_balance_reverts() {
let f = Ctx::setup();
f.env.mock_all_auths();
let alice = f.env.account("alice");
f.client().mint(&alice, &100_i128);
assert_reverts!(f.client().burn(&alice, &500_i128), "insufficient balance");
}
#[test]
fn test_approve_and_transfer_from() {
let f = Ctx::setup();
f.env.mock_all_auths();
let alice = f.env.account("alice");
let bob = f.env.account("bob");
f.client().mint(&alice, &1_000_i128);
f.client().approve(&alice, &bob, &500_i128);
assert_eq!(f.client().allowance(&alice, &bob), 500);
f.client().transfer_from(&bob, &alice, &bob, &300_i128);
assert_eq!(f.client().balance(&alice), 700);
assert_eq!(f.client().balance(&bob), 300);
assert_eq!(f.client().allowance(&alice, &bob), 200); // 500 - 300
}
#[test]
fn test_transfer_from_exceeds_allowance_reverts() {
let f = Ctx::setup();
f.env.mock_all_auths();
let alice = f.env.account("alice");
let bob = f.env.account("bob");
f.client().mint(&alice, &1_000_i128);
f.client().approve(&alice, &bob, &100_i128);
assert_reverts!(
f.client().transfer_from(&bob, &alice, &bob, &500_i128),
"insufficient allowance"
);
}
#[test]
fn test_mint_emits_event() {
let f = Ctx::setup();
f.env.mock_all_auths();
let alice = f.env.account("alice");
f.client().mint(&alice, &1_000_i128);
assert_emitted!(
f.env,
f.id,
(symbol_short!("mint"), alice.address()),
1_000_i128
);
}
#[test]
fn test_balance_query_emits_no_event() {
// balance() is a read-only query — calling it must not add new events.
// Capture the count before and after to check no new events were emitted
// (setup may emit XLM-mint events, which we don't want to interfere).
let f = Ctx::setup();
let alice = f.env.account("alice");
{
use soroban_sdk::testutils::Events as _;
let before = f.env.inner().events().all().events().len();
f.client().balance(&alice);
let after = f.env.inner().events().all().events().len();
assert_eq!(before, after, "balance() should not emit events");
}
}
#[test]
fn test_xlm_token_is_independent() {
// Demonstrates MockToken (SAC) alongside our custom contract token.
// Both are independent; balances don't interfere.
let f = Ctx::setup();
f.env.mock_all_auths();
let alice = f.env.account("alice");
let xlm = MockToken::xlm(&f.env);
xlm.mint(&alice, 5_000_000); // 0.5 XLM
f.client().mint(&alice, &250_i128);
assert_eq!(
xlm.balance(&alice),
Stroops::xlm(100).as_stroops() + 5_000_000
);
assert_eq!(f.client().balance(&alice), 250);
}