coins_test.gno

3.37 Kb ยท 124 lines
  1package coins
  2
  3import (
  4	"std"
  5	"strings"
  6	"testing"
  7
  8	"gno.land/p/demo/testutils"
  9	"gno.land/p/demo/ufmt"
 10	"gno.land/p/leon/ctg"
 11)
 12
 13func TestBalanceChecker(t *testing.T) {
 14	denom1 := "testtoken1"
 15	denom2 := "testtoken2"
 16	addr1 := testutils.TestAddress("user1")
 17	addr2 := testutils.TestAddress("user2")
 18
 19	coinsRealm := std.NewCodeRealm("gno.land/r/gnoland/coins")
 20	testing.SetRealm(coinsRealm)
 21
 22	testing.IssueCoins(addr1, std.NewCoins(std.NewCoin(denom1, 1000000)))
 23	testing.IssueCoins(addr2, std.NewCoins(std.NewCoin(denom1, 501)))
 24
 25	testing.IssueCoins(addr2, std.NewCoins(std.NewCoin(denom2, 12345)))
 26
 27	gnoAddr, _ := ctg.ConvertCosmosToGno("cosmos1s2v4tdskccx2p3yyvzem4mw5nn5fprwcku77hr")
 28	osmoAddr := "osmo1fl48vsnmsdzcv85q5d2q4z5ajdha8yu3aq6l09"
 29	gnoAddr1, _ := ctg.ConvertAnyToGno(osmoAddr)
 30
 31	testing.IssueCoins(gnoAddr1, std.NewCoins(std.NewCoin(denom2, 12345)))
 32
 33	tests := []struct {
 34		name      string
 35		path      string
 36		contains  string
 37		wantPanic bool
 38	}{
 39		{
 40			name:     "homepage",
 41			path:     "",
 42			contains: "# Gno.land Coins Explorer",
 43		},
 44		// TODO: not supported yet
 45		// {
 46		// 	name:     "total supply",
 47		// 	path:     denom,
 48		// 	expected: "Balance: 1500000testtoken",
 49		// },
 50		{
 51			name:     "addr1's coin balance",
 52			path:     ufmt.Sprintf("balances?address=%s&coin=%s", addr1.String(), denom1),
 53			contains: ufmt.Sprintf("`%s` has `%d%s`", addr1.String(), 1000000, denom1),
 54		},
 55		{
 56			name:     "addr2's full balances",
 57			path:     ufmt.Sprintf("balances?address=%s", addr2.String()),
 58			contains: ufmt.Sprintf("This page shows full coin balances of `%s` at block", addr2.String()),
 59		},
 60		{
 61			name: "addr2's full balances",
 62			path: ufmt.Sprintf("balances?address=%s", addr2.String()),
 63			contains: `| testtoken1 | 501 |
 64| testtoken2 | 12345 |`,
 65		},
 66		{
 67			name:     "addr2's coin balance",
 68			path:     ufmt.Sprintf("balances?address=%s&coin=%s", addr2.String(), denom1),
 69			contains: ufmt.Sprintf("`%s` has `%d%s`", addr2.String(), 501, denom1),
 70		},
 71		{
 72			name:     "cosmos addr conversion",
 73			path:     "convert/cosmos1s2v4tdskccx2p3yyvzem4mw5nn5fprwcku77hr",
 74			contains: ufmt.Sprintf("`cosmos1s2v4tdskccx2p3yyvzem4mw5nn5fprwcku77hr` on Cosmos matches `%s`", gnoAddr),
 75		},
 76		{
 77			name:     "balances bech32 auto convert",
 78			path:     ufmt.Sprintf("balances?address=%s&coin=%s", osmoAddr, denom1),
 79			contains: "Automatically converted `osmo1fl48vsnmsdzcv85q5d2q4z5ajdha8yu3aq6l09`",
 80		},
 81		{
 82			name:     "single coin balance bech32 auto convert",
 83			path:     ufmt.Sprintf("balances?address=%s", osmoAddr),
 84			contains: "Automatically converted `osmo1fl48vsnmsdzcv85q5d2q4z5ajdha8yu3aq6l09`",
 85		},
 86		{
 87			name:      "no addr",
 88			path:      "balances?address=",
 89			contains:  "Please input a valid address",
 90			wantPanic: false,
 91		},
 92		{
 93			name:      "no addr",
 94			path:      "balances?address=&coin=",
 95			contains:  "Please input a valid address and coin denomination.",
 96			wantPanic: false,
 97		},
 98		{
 99			name:      "invalid path",
100			path:      "invalid",
101			contains:  "404",
102			wantPanic: false,
103		},
104	}
105
106	for _, tt := range tests {
107		t.Run(tt.name, func(t *testing.T) {
108			if tt.wantPanic {
109				defer func() {
110					if r := recover(); r == nil {
111						t.Errorf("expected panic for %s", tt.name)
112					}
113				}()
114			}
115
116			result := Render(tt.path)
117			if !tt.wantPanic {
118				if !strings.Contains(result, tt.contains) {
119					t.Errorf("expected %s to contain %s", result, tt.contains)
120				}
121			}
122		})
123	}
124}