-
Notifications
You must be signed in to change notification settings - Fork 4
/
property_reader_test.go
197 lines (142 loc) · 4.23 KB
/
property_reader_test.go
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package boardgame
import (
"reflect"
"testing"
)
type propertyReaderTestStruct struct {
A int
B bool
C string
S Stack
P PlayerIndex
//d should be excluded since it is lowercase
d string
}
func (p *propertyReaderTestStruct) ReadSetter() PropertyReadSetter {
return getDefaultReadSetter(p)
}
func TestPropertyReaderImpl(t *testing.T) {
deck := NewDeck()
p := &propertyReaderTestStruct{
C: "bam",
S: deck.NewSizedStack(3),
P: 3,
}
s := p.ReadSetter()
result := s.Props()
expected := map[string]PropertyType{"A": TypeInt, "B": TypeBool, "C": TypeString, "S": TypeStack, "P": TypePlayerIndex}
if !reflect.DeepEqual(result, expected) {
t.Error("PropertyReaderPropsImpl returned wrong result. Got", result, "expected", expected)
}
field, err := s.Prop("C")
if err != nil {
t.Error("Unexpected error fetching generic prop", err)
}
if field.(string) != "bam" {
t.Error("Got back wrong value from Prop. Got", field, "expected 'foo'")
}
field, err = s.Prop("d")
if err == nil {
t.Error("Didn't get expected error when fetching invalid property")
}
if field != nil {
t.Error("Expected to not get back a result for private field, but did", field)
}
if err := s.SetProp("A", 4); err != nil {
t.Error("Setting A to 4 failed: ", err)
}
if p.A != 4 {
t.Error("Using setProp to set to 4 failed.")
}
if err := s.SetProp("A", "string"); err == nil {
t.Error("Trying to set a string into an int slot didn't fail")
}
if p.A != 4 {
t.Error("Failed setting into a field modified the value")
}
intResult, err := s.IntProp("A")
if err != nil {
t.Error("Unexpected error fetching int prop", err)
}
if intResult != 4 {
t.Error("Unexpected result from intprop", intResult)
}
intResult, err = s.IntProp("B")
if err == nil {
t.Error("Fetch on non-int prop with intprop did not fail")
}
boolResult, err := s.BoolProp("B")
if err != nil {
t.Error("Unexpected error fetching bool prop", err)
}
if boolResult != p.B {
t.Error("Unexpected bool result")
}
boolResult, err = s.BoolProp("A")
if err == nil {
t.Error("Didn't get error fetching non-bool prop")
}
stringResult, err := s.StringProp("C")
if err != nil {
t.Error("Unexpected error fetching string", err)
}
if stringResult != p.C {
t.Error("Unexpeted string result")
}
stringResult, err = s.StringProp("A")
if err == nil {
t.Error("Didn't error trying to string prop a non-string")
}
playerIndexResult, err := s.PlayerIndexProp("P")
if err != nil {
t.Error("Unexpected error fetching PlayerINdex", err)
}
if playerIndexResult != p.P {
t.Error("unexpected playerindex result")
}
playerIndexResult, err = s.PlayerIndexProp("A")
if err == nil {
t.Error("Didn't error trying to PlayerIndex prop an int")
}
stackResult, err := s.StackProp("S")
if err != nil {
t.Error("Unexpted error fetching stack", err)
}
if stackResult != p.S {
t.Error("Unexpected sized stack result")
}
stackResult, err = s.StackProp("A")
if err == nil {
t.Error("didn't get error for unreasonable sized stack fetch")
}
}
func TestGenericReader(t *testing.T) {
reader := newGenericReader()
if _, err := reader.Prop("test"); err == nil {
t.Error("A read on an empty reader didn't fail")
}
if err := reader.SetIntProp("intProp", 1); err != nil {
t.Error("Unexpected error setting int: ", err)
}
if intVal, err := reader.IntProp("intProp"); err != nil {
t.Error("Unexpected error reading back vaid int", err)
} else if intVal != 1 {
t.Error("Reading back legit int was not expected val. Got", intVal, "wanted", 1)
}
if err := reader.SetBoolProp("boolProp", true); err != nil {
t.Error("Unexpected error setting bool: ", err)
}
if boolVal, err := reader.BoolProp("boolProp"); err != nil {
t.Error("Unexpected error reading back valid bool", err)
} else if boolVal != true {
t.Error("Reading back legit bool was not expected val. Got", boolVal, "wanted", true)
}
if err := reader.SetIntProp("boolProp", 2); err == nil {
t.Error("Setting an int on a previously set bool prop didn't fail as expected")
}
if val, err := reader.Prop("intProp"); err != nil {
t.Error("Got unexpected error reading back generic prop", err)
} else if val.(int) != 1 {
t.Error("Reading back generic value didn't get right int. WAnted", 1, "got", val)
}
}