~nromdotcom/gemif

ref: 0d4b60a8720279f402503491905f77a2a27b9d1e gemif/pkg/gamemanager/gamestate_test.go -rw-r--r-- 3.3 KiB
0d4b60a8Norm MacLennan Add a few more tests 1 year, 5 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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))
		})
	}
}

func TestRemoveCondition(t *testing.T) {
	arrayTests := []struct {
		name      string
		state     gamemanager.GameState
		condition string   // input
		expected  []string // expected result
	}{
		{
			"Remove condition not met",
			gamemanager.GameState{
				StoryID:     "a",
				CurrentRoom: "a",
				Conditions:  []string{},
			},
			"did_thing",
			[]string{},
		},
		{
			"Remove condition already met",
			gamemanager.GameState{
				StoryID:     "a",
				CurrentRoom: "a",
				Conditions:  []string{"did_thing"},
			},
			"did_thing",
			[]string{},
		},
		{
			"Remove when met with several other conditions",
			gamemanager.GameState{
				StoryID:     "a",
				CurrentRoom: "a",
				Conditions:  []string{"something", "did_thing", "something_else", "more_stuff", "and_again"},
			},
			"did_thing",
			[]string{"something", "and_again", "something_else", "more_stuff"},
		},
		{
			"Remove when met as last in list",
			gamemanager.GameState{
				StoryID:     "a",
				CurrentRoom: "a",
				Conditions:  []string{"something", "something_else", "more_stuff", "and_again", "did_thing"},
			},
			"did_thing",
			[]string{"something", "something_else", "more_stuff", "and_again"},
		},
	}

	t.Parallel()

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