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.

rps_test.gno

1.92 Kb · 53 lines
 1package rps
 2
 3import (
 4	"strings"
 5	"testing"
 6
 7	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/daily/rps/v0"
 8	"gno.land/p/nt/testutils/v0"
 9	"gno.land/p/nt/uassert/v0"
10)
11
12func TestDecide(t *testing.T) {
13	// decide: player perspective.
14	uassert.Equal(t, outDraw, decide(rps.Rock, rps.Rock), "rock vs rock")
15	uassert.Equal(t, outWin, decide(rps.Rock, rps.Scissors), "rock beats scissors")
16	uassert.Equal(t, outLose, decide(rps.Rock, rps.Paper), "rock loses to paper")
17	uassert.Equal(t, outWin, decide(rps.Paper, rps.Rock), "paper beats rock")
18	uassert.Equal(t, outWin, decide(rps.Scissors, rps.Paper), "scissors beats paper")
19	uassert.Equal(t, outLose, decide(rps.Scissors, rps.Rock), "scissors loses to rock")
20
21	// houseMove determinism.
22	uassert.Equal(t, int(houseMove(10, 0)), int(houseMove(10, 0)), "same inputs -> same move")
23	if houseMove(10, 0) == houseMove(10, 1) && houseMove(10, 1) == houseMove(10, 2) {
24		t.Error("play count should vary house move across values")
25	}
26}
27
28func TestPlayRecordsTally(cur realm, t *testing.T) {
29	user := testutils.TestAddress("rps-player")
30	testing.SetRealm(testing.NewUserRealm(user))
31
32	before := totalRounds
33	Play(cross(cur),"rock")
34	Play(cross(cur),"paper")
35	Play(cross(cur),"scissors")
36
37	uassert.Equal(t, before+3, totalRounds, "global rounds incremented")
38
39	tl := getTally(user.String())
40	uassert.True(t, tl != nil, "player tally exists")
41	uassert.Equal(t, 3, tl.plays, "player played 3 rounds")
42	uassert.Equal(t, tl.wins+tl.losses+tl.draws, tl.plays, "outcomes sum to plays")
43
44	// Render must show the global tally header and the player.
45	out := Render("")
46	uassert.True(t, strings.Contains(out, "Global tally"), "render has global tally")
47	uassert.True(t, strings.Contains(out, "Recent rounds"), "render has recent rounds")
48
49	// Invalid choice aborts (crossing fn -> cross-realm abort).
50	uassert.AbortsWithMessage(t, cur, "invalid choice: use rock, paper, or scissors", func() {
51		Play(cross(cur),"lizard")
52	})
53}