Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

standings_test.gno

4.65 Kb · 145 lines
  1package standings
  2
  3import (
  4	"strconv"
  5	"strings"
  6	"testing"
  7
  8	"gno.land/p/nt/avl/v0"
  9	"gno.land/p/nt/ownable/v0"
 10	"gno.land/p/nt/testutils/v0"
 11	"gno.land/p/nt/uassert/v0"
 12)
 13
 14const (
 15	arenaV0 = "gno.land/r/nym-gfanton001/gnomepit/arena/v0"
 16	arenaV1 = "gno.land/r/nym-gfanton001/gnomepit/arena/v1"
 17)
 18
 19var (
 20	alice = testutils.TestAddress("alice")
 21	bob   = testutils.TestAddress("bob")
 22	carol = testutils.TestAddress("carol")
 23	dave  = testutils.TestAddress("dave")
 24)
 25
 26// reset empties the standings and the result log; tests share package globals. It leaves
 27// the writer list alone.
 28func reset() {
 29	players = avl.Tree{}
 30	ladder = nil
 31	clans = nil
 32	results = avl.Tree{}
 33}
 34
 35// ownedBy makes a the owner: under gno test, init's deployer address is empty.
 36func ownedBy(a address) {
 37	owner = ownable.NewWithAddress(a)
 38}
 39
 40// resetWriters leaves the arena the only writer, as init does.
 41func resetWriters() {
 42	writerPaths = nil
 43	writers = avl.Tree{}
 44	addWriter(arenaV0)
 45}
 46
 47// TestInitSeedsTheArena reads the writers init set, so it runs before any test changes them.
 48func TestInitSeedsTheArena(t *testing.T) {
 49	uassert.Equal(t, strings.Join([]string{arenaV0}, ","), strings.Join(Writers(), ","))
 50	uassert.True(t, IsWriter(arenaV0))
 51	uassert.False(t, IsWriter(arenaV1))
 52}
 53
 54func TestRecordRefusesNonWriters(cur realm, t *testing.T) {
 55	resetWriters()
 56	ownedBy(alice)
 57
 58	testing.SetRealm(testing.NewUserRealm(alice))
 59	uassert.AbortsWithMessage(t, cur, "not a writer", func() {
 60		Record(cross(cur), alice, bob, "Pickers", "Ironhide", "a")
 61	})
 62
 63	testing.SetRealm(testing.NewCodeRealm(arenaV1))
 64	uassert.AbortsWithMessage(t, cur, "not a writer", func() {
 65		Record(cross(cur), alice, bob, "Pickers", "Ironhide", "a")
 66	})
 67
 68	testing.SetRealm(testing.NewUserRealm(alice))
 69	RemoveWriter(cross(cur), arenaV0)
 70	testing.SetRealm(testing.NewCodeRealm(arenaV0))
 71	uassert.AbortsWithMessage(t, cur, "not a writer", func() {
 72		Record(cross(cur), alice, bob, "Pickers", "Ironhide", "a")
 73	})
 74	resetWriters()
 75}
 76
 77func TestRecordRefusesBadInput(cur realm, t *testing.T) {
 78	resetWriters()
 79	testing.SetRealm(testing.NewCodeRealm(arenaV0))
 80	long := strings.Repeat("x", 33)
 81	cases := []struct {
 82		name                                 string
 83		challenger, acceptor                 address
 84		challengerClan, acceptorClan, result string
 85		want                                 string
 86	}{
 87		{"same player", alice, alice, "Pickers", "Ironhide", "a", "same player"},
 88		{"unknown result", alice, bob, "Pickers", "Ironhide", "x", "unknown result"},
 89		{"empty clan", alice, bob, "", "Ironhide", "a", "bad clan name"},
 90		{"long clan", alice, bob, "Pickers", long, "a", "bad clan name"},
 91	}
 92	for _, c := range cases {
 93		uassert.AbortsWithMessage(t, cur, c.want, func() {
 94			Record(cross(cur), c.challenger, c.acceptor, c.challengerClan, c.acceptorClan, c.result)
 95		}, c.name)
 96	}
 97}
 98
 99func TestOwnerCalls(cur realm, t *testing.T) {
100	resetWriters()
101	ownedBy(alice)
102
103	testing.SetRealm(testing.NewUserRealm(bob))
104	uassert.AbortsWithMessage(t, cur, "ownable: caller is not owner", func() { AddWriter(cross(cur), arenaV1) })
105	uassert.AbortsWithMessage(t, cur, "ownable: caller is not owner", func() { RemoveWriter(cross(cur), arenaV0) })
106	uassert.AbortsWithMessage(t, cur, "ownable: caller is not owner", func() { TransferOwnership(cross(cur), bob) })
107	uassert.Equal(t, alice, Owner())
108
109	testing.SetRealm(testing.NewUserRealm(alice))
110	TransferOwnership(cross(cur), bob)
111	uassert.Equal(t, bob, Owner())
112}
113
114func TestAddWriterRefuses(cur realm, t *testing.T) {
115	resetWriters()
116	ownedBy(alice)
117	testing.SetRealm(testing.NewUserRealm(alice))
118
119	tooLong := "gno.land/r/" + strings.Repeat("a", 243) + "/v1"
120	for _, p := range []string{"gno.land/p/x/v0", "gno.land/r/x", "gno.land/r/x/v", "gno.land/r/x/vA", "gno.land/r/x#y/v1", "gno.land/r/X/v1", tooLong} {
121		uassert.AbortsWithMessage(t, cur, "bad writer path", func() { AddWriter(cross(cur), p) }, p)
122	}
123	uassert.AbortsWithMessage(t, cur, "already a writer", func() { AddWriter(cross(cur), arenaV0) })
124	uassert.AbortsWithMessage(t, cur, "not a writer", func() { RemoveWriter(cross(cur), arenaV1) })
125
126	// The arena holds writer number 1, so 254 more paths fill all 255.
127	for i := 1; i <= 254; i++ {
128		AddWriter(cross(cur), "gno.land/r/x/v"+strconv.Itoa(i))
129	}
130	uassert.AbortsWithMessage(t, cur, "too many writers", func() { AddWriter(cross(cur), "gno.land/r/x/v255") })
131	resetWriters()
132}
133
134func TestReaddedWriterKeepsItsNumber(cur realm, t *testing.T) {
135	resetWriters()
136	ownedBy(alice)
137	testing.SetRealm(testing.NewUserRealm(alice))
138
139	AddWriter(cross(cur), arenaV1)
140	RemoveWriter(cross(cur), arenaV0)
141	uassert.Equal(t, arenaV1, strings.Join(Writers(), ","))
142	AddWriter(cross(cur), arenaV0)
143	uassert.Equal(t, arenaV0+","+arenaV1, strings.Join(Writers(), ","))
144	resetWriters()
145}