foo20_test.gno

2.24 Kb ยท 86 lines
 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("g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq")
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("g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq")
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	std.TestSetOrigCaller(users.Resolve(admin))
75	{
76		tests := []test{
77			{"Transfer(admin, 1)", "cannot send transfer to self", func() { Transfer(admin, 1) }},
78			{"Approve(empty, 1))", "invalid address", func() { Approve(empty, 1) }},
79		}
80		for _, tc := range tests {
81			t.Run(tc.name, func(t *testing.T) {
82				uassert.PanicsWithMessage(t, tc.msg, tc.fn)
83			})
84		}
85	}
86}