Skip to content

Commit 25ad3d5

Browse files
author
root28root
committed
mytoken/tests: sink Coin to avoid drop; +pos/+neg cases
1 parent cc7fbd9 commit 25ad3d5

1 file changed

Lines changed: 42 additions & 14 deletions

File tree

mytoken/tests/my_token_tests.move

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,53 @@
1-
module MyToken::coin_tests {
2-
use MyToken::coin;
1+
#[test_only]
2+
module 0xb35962eed27b9a272d82673f2b7a99e7257b7b1a9af02c1a09143dacbaf498bd::my_token_tests {
3+
use std::signer;
4+
use std::option;
5+
use 0xb35962eed27b9a272d82673f2b7a99e7257b7b1a9af02c1a09143dacbaf498bd::coin;
36

4-
#[test]
5-
public fun test_mint_and_balance() {
7+
/// Тестовая «мусорка», чтобы не дропать Coin локально.
8+
struct Trash has key { inner: option::Option<coin::Coin> }
9+
10+
public entry fun init_trash(s: &signer) {
11+
move_to(s, Trash { inner: option::none<coin::Coin>() });
12+
}
13+
14+
fun sink(s: &signer, c: coin::Coin) acquires Trash {
15+
let t = borrow_global_mut<Trash>(signer::address_of(s));
16+
// Если уже что-то лежит — перезапишем, тестам это ок.
17+
t.inner = option::some<coin::Coin>(c);
18+
}
19+
20+
// === Позитивы ===
21+
22+
#[test(admin = @0xA)]
23+
public entry fun test_mint_balance_ok(admin: &signer) acquires Trash {
24+
init_trash(admin);
625
let c = coin::mint(100);
726
assert!(coin::balance(&c) == 100, 0);
27+
sink(admin, c); // поглотили, локальных Coin без drop не осталось
828
}
929

10-
#[test]
11-
public fun test_transfer() {
12-
let c1 = coin::mint(100);
13-
let c2 = coin::transfer(&mut c1, 40);
30+
#[test(admin = @0xB)]
31+
public entry fun test_transfer_split_ok(admin: &signer) acquires Trash {
32+
init_trash(admin);
33+
let c = coin::mint(100);
34+
let (c1, c2) = coin::split(c, 60); // 60 и 40
1435
assert!(coin::balance(&c1) == 60, 1);
1536
assert!(coin::balance(&c2) == 40, 2);
37+
sink(admin, c1);
38+
sink(admin, c2);
1639
}
1740

18-
#[test]
19-
public fun test_burn() {
20-
let c = coin::mint(50);
21-
let burned = coin::burn(c);
22-
assert!(burned == 50, 3);
41+
// === Негатив ===
42+
43+
#[test(admin = @0xC)]
44+
#[expected_failure] // ожидаем падение при сплите > баланса
45+
public entry fun test_split_fail_overflow(admin: &signer) acquires Trash {
46+
init_trash(admin);
47+
let c = coin::mint(10);
48+
let (_a, _b) = coin::split(c, 11);
49+
// Если почему-то не упало — всё равно поглотим, чтобы не дропнулось
50+
sink(admin, _a);
51+
sink(admin, _b);
2352
}
2453
}
25-

0 commit comments

Comments
 (0)