coins_test.gno

2.46 Kb ยท 99 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
29	tests := []struct {
30		name      string
31		path      string
32		contains  string
33		wantPanic bool
34	}{
35		{
36			name:     "homepage",
37			path:     "",
38			contains: "# Gno.land Coins Explorer",
39		},
40		// TODO: not supported yet
41		// {
42		// 	name:     "total supply",
43		// 	path:     denom,
44		// 	expected: "Balance: 1500000testtoken",
45		// },
46
47		{
48			name:     "addr1's coin balance",
49			path:     ufmt.Sprintf("balances/%s?coin=%s", addr1.String(), denom1),
50			contains: ufmt.Sprintf("`%s` has `%d%s`", addr1.String(), 1000000, denom1),
51		},
52		{
53			name:     "addr2's full balances",
54			path:     ufmt.Sprintf("balances/%s", addr2.String()),
55			contains: ufmt.Sprintf("This page shows full coin balances of `%s` at block", addr2.String()),
56		},
57		{
58			name: "addr2's full balances",
59			path: ufmt.Sprintf("balances/%s", addr2.String()),
60			contains: `| testtoken1 | 501 |
61| testtoken2 | 12345 |`,
62		},
63		{
64			name:     "addr2's coin balance",
65			path:     ufmt.Sprintf("balances/%s?coin=%s", addr2.String(), denom1),
66			contains: ufmt.Sprintf("`%s` has `%d%s`", addr2.String(), 501, denom1),
67		},
68		{
69			name:     "cosmos addr conversion",
70			path:     "convert/cosmos1s2v4tdskccx2p3yyvzem4mw5nn5fprwcku77hr",
71			contains: ufmt.Sprintf("`cosmos1s2v4tdskccx2p3yyvzem4mw5nn5fprwcku77hr` on Cosmos matches `%s`", gnoAddr),
72		},
73		{
74			name:      "invalid path",
75			path:      "invalid",
76			contains:  "404",
77			wantPanic: false,
78		},
79	}
80
81	for _, tt := range tests {
82		t.Run(tt.name, func(t *testing.T) {
83			if tt.wantPanic {
84				defer func() {
85					if r := recover(); r == nil {
86						t.Errorf("expected panic for %s", tt.name)
87					}
88				}()
89			}
90
91			result := Render(tt.path)
92			if !tt.wantPanic {
93				if !strings.Contains(result, tt.contains) {
94					t.Errorf("expected %s to contain %s", result, tt.contains)
95				}
96			}
97		})
98	}
99}