demo_grc20_test.gno
1.64 Kb · 57 lines
1package demo_grc20
2
3import (
4 "strings"
5 "testing"
6
7 "gno.land/p/nt/testutils/v0"
8)
9
10func expectPanicDemo(t *testing.T, label string, fn func()) {
11 t.Helper()
12 if r := revive(fn); r == nil {
13 t.Fatalf("%s: expected panic, got none", label)
14 }
15}
16
17func TestDemoGRC20FaucetAndTransfers(cur realm, t *testing.T) {
18 alice := testutils.TestAddress("alice")
19 bob := testutils.TestAddress("bob")
20
21 if Name() != "Smelt Demo Token" || Symbol() != "SMLT" || Decimals() != 6 {
22 t.Fatalf("unexpected token metadata: %s / %s / %d", Name(), Symbol(), Decimals())
23 }
24
25 testing.SetRealm(testing.NewUserRealm(alice))
26 Faucet(cross(cur))
27 if got := BalanceOf(alice); got != faucetAmount {
28 t.Fatalf("alice expected %d after faucet, got %d", faucetAmount, got)
29 }
30
31 expectPanicDemo(t, "double faucet claim", func() {
32 Faucet(cross(cur))
33 })
34
35 Transfer(cross(cur), bob, 250)
36 if BalanceOf(alice) != 750 || BalanceOf(bob) != 250 {
37 t.Fatalf("unexpected balances after transfer: alice=%d bob=%d", BalanceOf(alice), BalanceOf(bob))
38 }
39
40 Approve(cross(cur), bob, 100)
41 if Allowance(alice, bob) != 100 {
42 t.Fatalf("expected allowance 100, got %d", Allowance(alice, bob))
43 }
44
45 testing.SetRealm(testing.NewUserRealm(bob))
46 TransferFrom(cross(cur), alice, bob, 60)
47 if BalanceOf(alice) != 690 || BalanceOf(bob) != 310 || Allowance(alice, bob) != 40 {
48 t.Fatalf("unexpected post-TransferFrom state: alice=%d bob=%d allow=%d", BalanceOf(alice), BalanceOf(bob), Allowance(alice, bob))
49 }
50
51 rendered := Render("")
52 for _, want := range []string{"Smelt Demo Token", "SMLT", "Faucet", "Transfer"} {
53 if !strings.Contains(rendered, want) {
54 t.Errorf("Render() missing %q:\n%s", want, rendered)
55 }
56 }
57}