Skip to content Search Apps Documentation Source Content 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.38 Kb · 68 lines
 1package rps
 2
 3import (
 4	"testing"
 5
 6	"gno.land/p/nt/uassert/v0"
 7)
 8
 9func TestParse(t *testing.T) {
10	cases := []struct {
11		in   string
12		want Move
13		ok   bool
14	}{
15		{"rock", Rock, true},
16		{"Rock", Rock, true},
17		{" r ", Rock, true},
18		{"PAPER", Paper, true},
19		{"p", Paper, true},
20		{" scissors ", Scissors, true},
21		{"S", Scissors, true},
22		{"lizard", 0, false},
23		{"", 0, false},
24		{"ro", 0, false},
25	}
26	for _, c := range cases {
27		got, ok := Parse(c.in)
28		uassert.Equal(t, c.ok, ok, c.in)
29		uassert.Equal(t, int(c.want), int(got), c.in)
30	}
31}
32
33func TestString(t *testing.T) {
34	uassert.Equal(t, "rock", Rock.String())
35	uassert.Equal(t, "paper", Paper.String())
36	uassert.Equal(t, "scissors", Scissors.String())
37	uassert.Equal(t, "?", Move(3).String())
38	uassert.Equal(t, "?", Move(-1).String())
39}
40
41func TestBeats(t *testing.T) {
42	cases := []struct {
43		a, b Move
44		want int
45	}{
46		{Rock, Rock, 0},
47		{Paper, Paper, 0},
48		{Scissors, Scissors, 0},
49		{Paper, Rock, 1},
50		{Scissors, Paper, 1},
51		{Rock, Scissors, 1},
52		{Rock, Paper, 2},
53		{Paper, Scissors, 2},
54		{Scissors, Rock, 2},
55	}
56	for _, c := range cases {
57		uassert.Equal(t, c.want, Beats(c.a, c.b), c.a.String()+" vs "+c.b.String())
58	}
59}
60
61// Every move parses back from its own name.
62func TestRoundTrip(t *testing.T) {
63	for _, m := range []Move{Rock, Paper, Scissors} {
64		got, ok := Parse(m.String())
65		uassert.True(t, ok)
66		uassert.Equal(t, int(m), int(got))
67	}
68}