~nromdotcom/gemif

ref: 9b7a8a567d14485941e0fa82d75b91b02d15b5e6 gemif/pkg/gamemanager/gamestate_test.go -rw-r--r-- 1.9 KiB
9b7a8a56Norm MacLennan try switching to protobuf for state tokens 1 year, 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package gamemanager_test

import (
	"gemif/pkg/gamemanager"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestConditionMet(t *testing.T) {
	arrayTests := []struct {
		name      string
		state     gamemanager.GameState
		condition string // input
		expected  bool   // expected result
	}{
		{
			"Not meet when condition missing",
			gamemanager.GameState{
				StoryID:     "a",
				CurrentRoom: "a",
				Conditions:  make([]string, 1),
			},
			"did_thing", false,
		},
		{
			"Meet when condition exists",
			gamemanager.GameState{
				StoryID:     "a",
				CurrentRoom: "a",
				Conditions:  []string{"did_thing"},
			},
			"did_thing", true,
		},
	}

	t.Parallel()

	for _, tc := range arrayTests {
		tc := tc
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()
			actual := tc.state.ConditionMet(tc.condition)
			assert.True(t, assert.ObjectsAreEqualValues(tc.expected, actual))
		})
	}
}

func TestMeetCondition(t *testing.T) {
	arrayTests := []struct {
		name      string
		state     gamemanager.GameState
		condition string   // input
		expected  []string // expected result
	}{
		{
			"Meet condition for first time",
			gamemanager.GameState{
				StoryID:     "a",
				CurrentRoom: "a",
				Conditions:  []string{},
			},
			"did_thing",
			[]string{"did_thing"},
		},
		{
			"Meet condition that already exists",
			gamemanager.GameState{
				StoryID:     "a",
				CurrentRoom: "a",
				Conditions:  []string{"did_thing"},
			},
			"did_thing",
			[]string{"did_thing"},
		},
		{
			"Meet when other conditions already met",
			gamemanager.GameState{
				StoryID:     "a",
				CurrentRoom: "a",
				Conditions:  []string{"something_else"},
			},
			"did_thing",
			[]string{"something_else", "did_thing"},
		},
	}

	t.Parallel()

	for _, tc := range arrayTests {
		tc := tc
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()
			tc.state.MeetCondition(tc.condition)
			assert.True(t, assert.ObjectsAreEqualValues(tc.expected, tc.state.Conditions))
		})
	}
}