foo20_test.gno

2.95 Kb ยท 113 lines
  1package foo20
  2
  3import (
  4	"testing"
  5
  6	"gno.land/p/nt/testutils"
  7	"gno.land/p/nt/uassert"
  8)
  9
 10func TestReadOnlyPublicMethods(t *testing.T) {
 11	var (
 12		admin = address("g1manfred47kzduec920z88wfr64ylksmdcedlf5")
 13		alice = testutils.TestAddress("alice")
 14		bob   = testutils.TestAddress("bob")
 15	)
 16
 17	type test struct {
 18		name    string
 19		balance int64
 20		fn      func() int64
 21	}
 22
 23	// check balances #1.
 24	{
 25		tests := []test{
 26			{"TotalSupply", 10_000_000_000, func() int64 { return TotalSupply() }},
 27			{"BalanceOf(admin)", 10_000_000_000, func() int64 { return BalanceOf(admin) }},
 28			{"BalanceOf(alice)", 0, func() int64 { return BalanceOf(alice) }},
 29			{"Allowance(admin, alice)", 0, func() int64 { return Allowance(admin, alice) }},
 30			{"BalanceOf(bob)", 0, func() int64 { return BalanceOf(bob) }},
 31		}
 32		for _, tc := range tests {
 33			got := tc.fn()
 34			uassert.Equal(t, got, tc.balance)
 35		}
 36	}
 37
 38	// bob uses the faucet.
 39	testing.SetOriginCaller(bob)
 40	Faucet(cross)
 41
 42	// check balances #2.
 43	{
 44		tests := []test{
 45			{"TotalSupply", 10_010_000_000, func() int64 { return TotalSupply() }},
 46			{"BalanceOf(admin)", 10_000_000_000, func() int64 { return BalanceOf(admin) }},
 47			{"BalanceOf(alice)", 0, func() int64 { return BalanceOf(alice) }},
 48			{"Allowance(admin, alice)", 0, func() int64 { return Allowance(admin, alice) }},
 49			{"BalanceOf(bob)", 10_000_000, func() int64 { return BalanceOf(bob) }},
 50		}
 51		for _, tc := range tests {
 52			got := tc.fn()
 53			uassert.Equal(t, got, tc.balance)
 54		}
 55	}
 56}
 57
 58func TestErrConditions(t *testing.T) {
 59	var (
 60		admin = address("g1manfred47kzduec920z88wfr64ylksmdcedlf5")
 61		empty = address("")
 62	)
 63
 64	type test struct {
 65		name    string
 66		msg     string
 67		isCross bool
 68		fn      func()
 69	}
 70
 71	privateLedger.Mint(address(admin), 10000)
 72	{
 73		tests := []test{
 74			{"Transfer(admin, 1)", "cannot send transfer to self", false, 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 := address(admin)
 79				if err := privateLedger.Transfer(adminAddr, adminAddr, 1); err != nil {
 80					panic(err)
 81				}
 82			}},
 83			{"Approve(empty, 1))", "invalid address", true, func() { Approve(cross, empty, 1) }},
 84		}
 85		for _, tc := range tests {
 86			if tc.isCross {
 87				uassert.AbortsWithMessage(t, tc.msg, tc.fn)
 88			} else {
 89				uassert.PanicsWithMessage(t, tc.msg, tc.fn)
 90			}
 91		}
 92	}
 93}
 94
 95//func TestNewFoo20(t *testing.T) {
 96//	t.Run("invalid input", func(t *testing.T) {
 97//		testCases := []struct {
 98//			msg string
 99//			fn  func()
100//		}{
101//			// Test AbortsWithMessage
102//	uassert.PanicsWithMessage", func() { NewFoo20("foo", "f", 0) }},
103//			{"symbol cannot be empty", func() { NewFoo20("foo", "", 1) }},
104//			{"name cannot be empty", func() { NewFoo20("", "f", 1) }},
105//		}
106//		for _, tc := range testCases {
107//			uassert.AbortsWithMessage(t, tc.msg, tc.fn)
108//		}
109//	})
110//	t.Run("transfer", func(t *testing.T) {
111//		// ... existing code ...
112//	})
113//}