foo20_test.gno

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