~nromdotcom/gemif

ref: 0d4b60a8720279f402503491905f77a2a27b9d1e gemif/pkg/gamemanager/engineconfig_test.go -rw-r--r-- 2.4 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
package gamemanager_test

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

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

func TestGetSerDeFormat(t *testing.T) {
	arrayTests := []struct {
		name      string
		condition string                          // input
		expected  gamemanager.SerializationFormat // expected result
	}{
		{
			"JSON",
			"json", gamemanager.JSON,
		},
		{
			"Protobuf",
			"proto", gamemanager.Protobuf,
		},
		{
			"Neither",
			"something_else", 0,
		},
	}

	t.Parallel()

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

func TestNewEngineFormatPass(t *testing.T) {
	arrayTests := []struct {
		name     string
		render   bool
		format   string                   // input
		expected gamemanager.EngineConfig // expected result
	}{
		{
			"JSON/true", true, "json",
			gamemanager.EngineConfig{
				RenderDescriptions: true,
				StateTokenFormat:   gamemanager.JSON,
			},
		},
		{
			"Proto/true", true, "proto",
			gamemanager.EngineConfig{
				RenderDescriptions: true,
				StateTokenFormat:   gamemanager.Protobuf,
			},
		},
		{
			"JSON/false", false, "json",
			gamemanager.EngineConfig{
				RenderDescriptions: false,
				StateTokenFormat:   gamemanager.JSON,
			},
		},
		{
			"Proto/false", false, "proto",
			gamemanager.EngineConfig{
				RenderDescriptions: false,
				StateTokenFormat:   gamemanager.Protobuf,
			},
		},
	}

	t.Parallel()

	for _, tc := range arrayTests {
		tc := tc
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()
			actual, _ := gamemanager.NewEngineConfig(tc.render, tc.format)
			assert.True(t, assert.ObjectsAreEqualValues(tc.expected, actual))
		})
	}
}

func TestNewEngineFormatFail(t *testing.T) {
	arrayTests := []struct {
		name     string
		render   bool
		format   string // input
		expected error  // expected result
	}{
		{
			"Neither/true", true, "neither",
			gamemanager.ErrInvalidSerializationFormat,
		},
		{
			"Neither/false", false, "neither",
			gamemanager.ErrInvalidSerializationFormat,
		},
	}

	t.Parallel()

	for _, tc := range arrayTests {
		tc := tc
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()
			_, err := gamemanager.NewEngineConfig(tc.render, tc.format)

			assert.True(t, assert.EqualError(t, err, fmt.Sprintf("%s: %s", tc.expected, tc.format)))
		})
	}
}