rpsmatch_test.gno
1.39 Kb · 64 lines
1package rpsmatch
2
3import (
4 "testing"
5
6 "gno.land/p/nt/avl/v0"
7 "gno.land/p/nt/testutils/v0"
8)
9
10func resetState() {
11 players = avl.Tree{}
12 nonce = 0
13 totalRounds = 0
14 var zero address
15 champion = zero
16 championWins = 0
17}
18
19// Crossing: Throw mutates realm state, so the test needs `cur realm`.
20func TestThrowUpdatesStats(cur realm, t *testing.T) {
21 resetState()
22 alice := testutils.TestAddress("alice")
23 testing.SetRealm(testing.NewUserRealm(alice))
24 Throw(cross(cur), "rock")
25
26 ps := getOrCreate(alice)
27 if ps.RoundsPlayed != 1 {
28 t.Fatalf("RoundsPlayed = %d, want 1", ps.RoundsPlayed)
29 }
30 if totalRounds != 1 {
31 t.Fatalf("totalRounds = %d, want 1", totalRounds)
32 }
33}
34
35func TestMatchCompletes(cur realm, t *testing.T) {
36 resetState()
37 alice := testutils.TestAddress("alice")
38 testing.SetRealm(testing.NewUserRealm(alice))
39
40 for i := 0; i < 10; i++ {
41 ps := getOrCreate(alice)
42 if ps.Active == nil && ps.MatchesPlayed > 0 {
43 break
44 }
45 Throw(cross(cur), "rock")
46 }
47
48 ps := getOrCreate(alice)
49 if ps.MatchesPlayed == 0 {
50 t.Fatal("expected a match to complete within 10 rounds")
51 }
52 if ps.MatchWins+ps.MatchLosses != ps.MatchesPlayed {
53 t.Fatalf("wins+losses (%d) != matchesPlayed (%d)", ps.MatchWins+ps.MatchLosses, ps.MatchesPlayed)
54 }
55}
56
57// Non-crossing: Render is a plain read.
58func TestRenderHome(t *testing.T) {
59 resetState()
60 out := Render("")
61 if out == "" {
62 t.Fatal("Render(\"\") should not be empty")
63 }
64}