1package foo20
2
3import (
4 "std"
5 "testing"
6
7 "gno.land/p/demo/testutils"
8 "gno.land/p/demo/uassert"
9 pusers "gno.land/p/demo/users"
10 "gno.land/r/demo/users"
11)
12
13func TestReadOnlyPublicMethods(t *testing.T) {
14 var (
15 admin = pusers.AddressOrName("g1manfred47kzduec920z88wfr64ylksmdcedlf5")
16 alice = pusers.AddressOrName(testutils.TestAddress("alice"))
17 bob = pusers.AddressOrName(testutils.TestAddress("bob"))
18 )
19
20 type test struct {
21 name string
22 balance uint64
23 fn func() uint64
24 }
25
26 // check balances #1.
27 {
28 tests := []test{
29 {"TotalSupply", 10_000_000_000, func() uint64 { return TotalSupply() }},
30 {"BalanceOf(admin)", 10_000_000_000, func() uint64 { return BalanceOf(admin) }},
31 {"BalanceOf(alice)", 0, func() uint64 { return BalanceOf(alice) }},
32 {"Allowance(admin, alice)", 0, func() uint64 { return Allowance(admin, alice) }},
33 {"BalanceOf(bob)", 0, func() uint64 { return BalanceOf(bob) }},
34 }
35 for _, tc := range tests {
36 got := tc.fn()
37 uassert.Equal(t, got, tc.balance)
38 }
39 }
40
41 // bob uses the faucet.
42 std.TestSetOrigCaller(users.Resolve(bob))
43 Faucet()
44
45 // check balances #2.
46 {
47 tests := []test{
48 {"TotalSupply", 10_010_000_000, func() uint64 { return TotalSupply() }},
49 {"BalanceOf(admin)", 10_000_000_000, func() uint64 { return BalanceOf(admin) }},
50 {"BalanceOf(alice)", 0, func() uint64 { return BalanceOf(alice) }},
51 {"Allowance(admin, alice)", 0, func() uint64 { return Allowance(admin, alice) }},
52 {"BalanceOf(bob)", 10_000_000, func() uint64 { return BalanceOf(bob) }},
53 }
54 for _, tc := range tests {
55 got := tc.fn()
56 uassert.Equal(t, got, tc.balance)
57 }
58 }
59}
60
61func TestErrConditions(t *testing.T) {
62 var (
63 admin = pusers.AddressOrName("g1manfred47kzduec920z88wfr64ylksmdcedlf5")
64 alice = pusers.AddressOrName(testutils.TestAddress("alice"))
65 empty = pusers.AddressOrName("")
66 )
67
68 type test struct {
69 name string
70 msg string
71 fn func()
72 }
73
74 privateLedger.Mint(std.Address(admin), 10000)
75 {
76 tests := []test{
77 {"Transfer(admin, 1)", "cannot send transfer to self", func() {
78 // XXX: should replace with: Transfer(admin, 1)
79 // but there is currently a limitation in manipulating the frame stack and simulate
80 // calling this package from an outside point of view.
81 adminAddr := std.Address(admin)
82 if err := privateLedger.Transfer(adminAddr, adminAddr, 1); err != nil {
83 panic(err)
84 }
85 }},
86 {"Approve(empty, 1))", "invalid address", func() { Approve(empty, 1) }},
87 }
88 for _, tc := range tests {
89 t.Run(tc.name, func(t *testing.T) {
90 uassert.PanicsWithMessage(t, tc.msg, tc.fn)
91 })
92 }
93 }
94}
foo20_test.gno
2.55 Kb ยท 94 lines