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.

rpsoracle_test.gno

1.46 Kb · 67 lines
 1package rpsoracle
 2
 3import (
 4	"testing"
 5
 6	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/daily/rps/v0"
 7	"gno.land/p/nt/avl/v0"
 8	"gno.land/p/nt/testutils/v0"
 9)
10
11func resetState() {
12	players = avl.Tree{}
13	nonce = 0
14	totalRounds = 0
15	oracleCorrect = 0
16	var zero address
17	topOutwitter = zero
18	topOutwitterWins = 0
19}
20
21func TestBeats(t *testing.T) {
22	if beats(rps.Rock) != rps.Paper {
23		t.Fatal("beats(Rock) should be Paper")
24	}
25	if beats(rps.Paper) != rps.Scissors {
26		t.Fatal("beats(Paper) should be Scissors")
27	}
28	if beats(rps.Scissors) != rps.Rock {
29		t.Fatal("beats(Scissors) should be Rock")
30	}
31}
32
33func TestPredictPicksMostFrequent(t *testing.T) {
34	ps := &playerState{}
35	ps.Counts[rps.Paper] = 5
36	ps.Counts[rps.Rock] = 1
37	if got := predict(ps, 0); got != rps.Paper {
38		t.Fatalf("predict = %v, want Paper", got)
39	}
40}
41
42// Crossing: Play mutates realm state, so the test needs `cur realm`.
43func TestOracleCountersPredictableRock(cur realm, t *testing.T) {
44	resetState()
45	alice := testutils.TestAddress("alice")
46	testing.SetRealm(testing.NewUserRealm(alice))
47
48	for i := 0; i < 5; i++ {
49		Play(cross(cur), "rock")
50	}
51	ps := getOrCreate(alice)
52	if ps.Losses == 0 {
53		t.Fatal("expected the oracle to eventually counter an all-rock player")
54	}
55	if ps.Rounds != 5 {
56		t.Fatalf("Rounds = %d, want 5", ps.Rounds)
57	}
58}
59
60// Non-crossing: Render is a plain read.
61func TestRenderHome(t *testing.T) {
62	resetState()
63	out := Render("")
64	if out == "" {
65		t.Fatal("Render(\"\") should not be empty")
66	}
67}