|
1 | 1 | #[test_only] |
2 | | -module 0xb35962eed27b9a272d82673f2b7a99e7257b7b1a9af02c1a09143dacbaf498bd::escrow_tests { |
3 | | - use 0x1::signer; |
4 | | - use 0x1::vector; |
5 | | - use 0xb35962eed27b9a272d82673f2b7a99e7257b7b1a9af02c1a09143dacbaf498bd::escrow; |
| 2 | +module Escrow::escrow_tests { |
| 3 | + use Escrow::escrow; |
6 | 4 |
|
7 | 5 | const BUYER: address = @0x1; |
8 | 6 | const SELLER: address = @0x2; |
9 | 7 |
|
10 | | - // Мусорка, чтобы не было implicit drop для Wallet/Deal |
11 | | - struct Trash has key { |
12 | | - buyers: vector<escrow::Wallet>, |
13 | | - sellers: vector<escrow::Wallet>, |
14 | | - deals: vector<escrow::Deal>, |
15 | | - } |
16 | | - |
17 | | - public entry fun init_trash(s: &signer) { |
18 | | - move_to(s, Trash { |
19 | | - buyers: vector::empty<escrow::Wallet>(), |
20 | | - sellers: vector::empty<escrow::Wallet>(), |
21 | | - deals: vector::empty<escrow::Deal>(), |
22 | | - }); |
23 | | - } |
24 | | - |
25 | | - fun sink_all(s: &signer, bw: escrow::Wallet, sw: escrow::Wallet, d: escrow::Deal) acquires Trash { |
26 | | - let t = borrow_global_mut<Trash>(signer::address_of(s)); |
27 | | - vector::push_back(&mut t.buyers, bw); |
28 | | - vector::push_back(&mut t.sellers, sw); |
29 | | - vector::push_back(&mut t.deals, d); |
30 | | - } |
31 | | - |
32 | | - // === happy path: deposit -> fund -> release === |
33 | | - #[test(admin = @0xAA)] |
34 | | - public entry fun test_happy(admin: &signer) acquires Trash { |
35 | | - init_trash(admin); |
36 | | - |
37 | | - let bw = escrow::new_wallet(); |
38 | | - let sw = escrow::new_wallet(); |
| 8 | + #[test] |
| 9 | + public entry fun test_happy_flow() { |
| 10 | + let mut bw = escrow::new_wallet(); |
| 11 | + let mut sw = escrow::new_wallet(); |
39 | 12 |
|
40 | 13 | escrow::deposit(&mut bw, 150); |
41 | | - let d = escrow::create(BUYER, SELLER, 100); |
| 14 | + let mut d = escrow::create(BUYER, SELLER, 100); |
42 | 15 |
|
43 | 16 | escrow::fund(BUYER, &mut bw, &mut d); |
44 | 17 | escrow::release(SELLER, &mut sw, &mut d); |
45 | 18 |
|
46 | 19 | assert!(escrow::balance(&bw) == 50, 0); |
47 | 20 | assert!(escrow::balance(&sw) == 100, 1); |
48 | 21 |
|
49 | | - sink_all(admin, bw, sw, d); |
50 | | - } |
51 | | - |
52 | | - // Нельзя фондить недостаточную сумму (E_INSUFFICIENT = 4) |
53 | | - #[test(admin = @0xAB)] |
54 | | - #[expected_failure(abort_code = 4, location = 0xb35962eed27b9a272d82673f2b7a99e7257b7b1a9af02c1a09143dacbaf498bd::escrow)] |
55 | | - public entry fun test_insufficient(admin: &signer) acquires Trash { |
56 | | - init_trash(admin); |
57 | | - |
58 | | - let bw = escrow::new_wallet(); |
59 | | - let sw = escrow::new_wallet(); |
60 | | - |
61 | | - escrow::deposit(&mut bw, 50); // меньше, чем нужно |
62 | | - let d = escrow::create(BUYER, SELLER, 100); |
63 | | - |
64 | | - // упадёт по E_INSUFFICIENT (4) |
65 | | - escrow::fund(BUYER, &mut bw, &mut d); |
66 | | - |
67 | | - sink_all(admin, bw, sw, d); |
| 22 | + // Поглощаем ресурсы (утилиты есть в модуле) |
| 23 | + escrow::destroy_deal_for_test(d); |
| 24 | + escrow::destroy_wallet_for_test(bw); |
| 25 | + escrow::destroy_wallet_for_test(sw); |
68 | 26 | } |
69 | 27 |
|
70 | | - // Нельзя фондить не-байеру (E_NOT_BUYER = 5) |
71 | | - #[test(admin = @0xAC)] |
72 | | - #[expected_failure(abort_code = 5, location = 0xb35962eed27b9a272d82673f2b7a99e7257b7b1a9af02c1a09143dacbaf498bd::escrow)] |
73 | | - public entry fun test_not_buyer(admin: &signer) acquires Trash { |
74 | | - init_trash(admin); |
75 | | - |
76 | | - let bw = escrow::new_wallet(); |
77 | | - let sw = escrow::new_wallet(); |
78 | | - |
79 | | - escrow::deposit(&mut bw, 150); |
80 | | - let d = escrow::create(BUYER, SELLER, 100); |
81 | | - |
82 | | - // адрес не совпадает с BUYER |
| 28 | + // Негатив: не тот покупатель на fund -> E_NOT_BUYER = 5 |
| 29 | + #[test] |
| 30 | + #[expected_failure(abort_code = 5, location = Escrow::escrow)] |
| 31 | + public entry fun test_fund_wrong_buyer_fails() { |
| 32 | + let mut bw = escrow::new_wallet(); |
| 33 | + let mut d = escrow::create(BUYER, SELLER, 10); |
| 34 | + escrow::deposit(&mut bw, 10); |
| 35 | + // wrong buyer: |
83 | 36 | escrow::fund(@0xDEAD, &mut bw, &mut d); |
| 37 | + escrow::destroy_deal_for_test(d); |
| 38 | + escrow::destroy_wallet_for_test(bw); |
| 39 | + } |
84 | 40 |
|
85 | | - sink_all(admin, bw, sw, d); |
| 41 | + // Негатив: release до fund -> E_NOT_FUNDED = 2 |
| 42 | + #[test] |
| 43 | + #[expected_failure(abort_code = 2, location = Escrow::escrow)] |
| 44 | + public entry fun test_release_not_funded_fails() { |
| 45 | + let mut sw = escrow::new_wallet(); |
| 46 | + let mut d = escrow::create(BUYER, SELLER, 10); |
| 47 | + escrow::release(SELLER, &mut sw, &mut d); |
| 48 | + escrow::destroy_deal_for_test(d); |
| 49 | + escrow::destroy_wallet_for_test(sw); |
86 | 50 | } |
87 | 51 | } |
0 commit comments