foo20_test.gno

2.98 Kb ยท 114 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 int64
 21		fn      func() int64
 22	}
 23
 24	// check balances #1.
 25	{
 26		tests := []test{
 27			{"TotalSupply", 10_000_000_000, func() int64 { return TotalSupply() }},
 28			{"BalanceOf(admin)", 10_000_000_000, func() int64 { return BalanceOf(admin) }},
 29			{"BalanceOf(alice)", 0, func() int64 { return BalanceOf(alice) }},
 30			{"Allowance(admin, alice)", 0, func() int64 { return Allowance(admin, alice) }},
 31			{"BalanceOf(bob)", 0, func() int64 { 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	testing.SetOriginCaller(bob)
 41	Faucet(cross)
 42
 43	// check balances #2.
 44	{
 45		tests := []test{
 46			{"TotalSupply", 10_010_000_000, func() int64 { return TotalSupply() }},
 47			{"BalanceOf(admin)", 10_000_000_000, func() int64 { return BalanceOf(admin) }},
 48			{"BalanceOf(alice)", 0, func() int64 { return BalanceOf(alice) }},
 49			{"Allowance(admin, alice)", 0, func() int64 { return Allowance(admin, alice) }},
 50			{"BalanceOf(bob)", 10_000_000, func() int64 { 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		isCross bool
 69		fn      func()
 70	}
 71
 72	privateLedger.Mint(std.Address(admin), 10000)
 73	{
 74		tests := []test{
 75			{"Transfer(admin, 1)", "cannot send transfer to self", false, func() {
 76				// XXX: should replace with: Transfer(admin, 1)
 77				// but there is currently a limitation in manipulating the frame stack and simulate
 78				// calling this package from an outside point of view.
 79				adminAddr := std.Address(admin)
 80				if err := privateLedger.Transfer(adminAddr, adminAddr, 1); err != nil {
 81					panic(err)
 82				}
 83			}},
 84			{"Approve(empty, 1))", "invalid address", true, func() { Approve(cross, empty, 1) }},
 85		}
 86		for _, tc := range tests {
 87			if tc.isCross {
 88				uassert.AbortsWithMessage(t, tc.msg, tc.fn)
 89			} else {
 90				uassert.PanicsWithMessage(t, tc.msg, tc.fn)
 91			}
 92		}
 93	}
 94}
 95
 96//func TestNewFoo20(t *testing.T) {
 97//	t.Run("invalid input", func(t *testing.T) {
 98//		testCases := []struct {
 99//			msg string
100//			fn  func()
101//		}{
102//			// Test AbortsWithMessage
103//	uassert.PanicsWithMessage", func() { NewFoo20("foo", "f", 0) }},
104//			{"symbol cannot be empty", func() { NewFoo20("foo", "", 1) }},
105//			{"name cannot be empty", func() { NewFoo20("", "f", 1) }},
106//		}
107//		for _, tc := range testCases {
108//			uassert.AbortsWithMessage(t, tc.msg, tc.fn)
109//		}
110//	})
111//	t.Run("transfer", func(t *testing.T) {
112//		// ... existing code ...
113//	})
114//}