-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
285 lines (227 loc) · 7.57 KB
/
parser_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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package main
import (
"testing"
)
/* Test that a basic roll (XdY) works as expected. */
func TestParseBasicRoll(t *testing.T) {
result, rolls, error := ParseExpression("2d20")
if error != nil {
t.Fatalf("Parsing 2d20 failed with error: %s", error)
}
if len(rolls) != 1 {
t.Fatalf("Roll 2d20: Wrong number of diceroll expressions in result")
}
if rolls[0].Expression != "2d20" {
t.Fatalf("Roll 2d20: 2d20 not found in the results")
}
if len(rolls[0].Results) != 2 {
t.Fatalf("Roll 2d20: rolled %d instead of 2 dice", len(rolls[0].Results))
}
sum := 0
for _, v := range rolls[0].Results {
if v < 1 || v > 20 {
t.Fatalf("Roll 2d20: rolled a %d which is out of range", v)
}
sum += v
}
if sum != result {
t.Fatalf("Roll 2d20: Result %d was not the sum of the rolls (%d)", result, sum)
}
}
/* Test that arithmetic is parsed with correct order of operations. */
func TestParseArithmeticExpression(t *testing.T) {
result, rolls, error := ParseExpression("1+(4*6)/2")
if error != nil {
t.Fatalf("Parsing 1+(4*6)/2 failed with error: %s", error)
}
if len(rolls) != 0 {
t.Fatalf("Parse 1+(4*6)/2: rolls was not zero")
}
if result != 13 {
t.Fatalf("Parse 1+(4*6)/2: got %d instead of 13", result)
}
}
/* Test that rolling advantage works as expected */
func TestRollAdvantage(t *testing.T) {
for i := 0; i < 10; i++ {
result, rolls, error := ParseExpression("2d20!")
if error != nil {
t.Fatalf("Parsing 2d20! failed with error: %s", error)
}
if len(rolls) != 1 {
t.Fatalf("Roll 2d20!: Wrong number of diceroll expressions in result")
}
if rolls[0].Expression != "2d20!" {
t.Fatalf("Roll 2d20!: 2d20! not found in the results")
}
if len(rolls[0].Results) != 2 {
t.Fatalf("Roll 2d20!: rolled %d instead of 2 dice", len(rolls[0].Results))
}
max := rolls[0].Results[0]
if max < rolls[0].Results[1] {
max = rolls[0].Results[1]
}
if max != result {
t.Fatalf("Advantage chose the lower option")
}
}
}
/* Test that rolling disadvantage works as expected */
func TestRollDisadvantage(t *testing.T) {
for i := 0; i < 10; i++ {
result, rolls, error := ParseExpression("2d20?")
if error != nil {
t.Fatalf("Parsing 2d20? failed with error: %s", error)
}
if len(rolls) != 1 {
t.Fatalf("Roll 2d20?: Wrong number of diceroll expressions in result")
}
if rolls[0].Expression != "2d20?" {
t.Fatalf("Roll 2d20?: 2d20? not found in the results")
}
if len(rolls[0].Results) != 2 {
t.Fatalf("Roll 2d20?: rolled %d instead of 2 dice", len(rolls[0].Results))
}
max := rolls[0].Results[0]
if max > rolls[0].Results[1] {
max = rolls[0].Results[1]
}
if max != result {
t.Fatalf("Disadvantage chose the higher option")
}
}
}
/* Test that combining dice notation with arithmetic works as expected */
func TestParseDiceWithMath(t *testing.T) {
for i := 0; i < 20; i++ {
result, rolls, error := ParseExpression("1d20 + 5")
if error != nil {
t.Fatalf("Parsing 1d20 + 5 failed with error: %s", error)
}
if len(rolls) != 1 {
t.Fatalf("Roll 1d20 + 5: Wrong number of diceroll expressions in result")
}
if rolls[0].Expression != "1d20" {
t.Fatalf("Roll 1d20 + 5: 1d20 not found in the results")
}
if len(rolls[0].Results) != 1 {
t.Fatalf("Roll 1d20 + 5: rolled %d instead of 1 die", len(rolls[0].Results))
}
rollResult := rolls[0].Results[0]
if rollResult < 1 || rollResult > 20 {
t.Fatalf("Roll 1d20 + 5: got invalid roll result %d", rollResult)
}
expected := rollResult + 5
if result != expected {
t.Fatalf("Result was %d instead of %d", result, expected)
}
}
}
/* Test that rolling shorthand works */
func TestParseShorthand(t *testing.T) {
expression := "d20"
result, rolls, error := ParseExpression(expression)
if error != nil {
t.Fatalf("Parsing %s failed with error: %s", expression, error)
}
if len(rolls) != 1 {
t.Fatalf("Roll %s: Wrong number of diceroll expressions in result", expression)
}
if rolls[0].Expression != "d20" {
t.Fatalf("Roll %s: d20 not found in the results", expression)
}
if len(rolls[0].Results) != 1 {
t.Fatalf("Roll %s: rolled %d instead of 1 die for d20", expression, len(rolls[0].Results))
}
rollResult := rolls[0].Results[0]
if rollResult < 1 || rollResult > 20 {
t.Fatalf("Roll %s: Roll result %d out of range", expression, rollResult)
}
if rollResult != result {
t.Fatalf("Roll %s: Result of expression (%d) not what expected (%d)", expression, result, rollResult)
}
}
/* Test that rolling shorthand ("d20") notation works in an expression */
func TestParseShorthand_1(t *testing.T) {
expression := "2d20 + d20"
result, rolls, error := ParseExpression(expression)
if error != nil {
t.Fatalf("Parsing %s failed with error: %s", expression, error)
}
if len(rolls) != 2 {
t.Fatalf("Roll %s: Wrong number of diceroll expressions in result", expression)
}
if rolls[0].Expression != "2d20" {
t.Fatalf("Roll %s: 2d20 not found in the results", expression)
}
if rolls[1].Expression != "d20" {
t.Fatalf("Roll %s: d20 not found in the results", expression)
}
if len(rolls[0].Results) != 2 {
t.Fatalf("Roll %s: rolled %d instead of 2 dice for 2d20", expression, len(rolls[0].Results))
}
if len(rolls[1].Results) != 1 {
t.Fatalf("Roll %s: rolled %d instead of 1 die for d20", expression, len(rolls[1].Results))
}
sum := 0
for _, v := range rolls[0].Results {
if v < 1 || v > 20 {
t.Fatalf("Roll %s: rolled a %d which is out of range for 2d20", expression, v)
}
sum += v
}
rollResult := rolls[1].Results[0]
if rollResult < 1 || rollResult > 20 {
t.Fatalf("Roll %s: got invalid roll result for d20: %d", expression, rollResult)
}
expected := rollResult + sum
if result != expected {
t.Fatalf("Roll %s: Result was %d instead of %d", expression, result, expected)
}
}
func TestParseShorthand_2(t *testing.T) {
expression := "d20 + 2d20"
result, rolls, error := ParseExpression(expression)
if error != nil {
t.Fatalf("Parsing %s failed with error: %s", expression, error)
}
if len(rolls) != 2 {
t.Fatalf("Roll %s: Wrong number of diceroll expressions in result", expression)
}
if rolls[1].Expression != "2d20" {
t.Fatalf("Roll %s: 2d20 not found in the results", expression)
}
if rolls[0].Expression != "d20" {
t.Fatalf("Roll %s: d20 not found in the results", expression)
}
if len(rolls[1].Results) != 2 {
t.Fatalf("Roll %s: rolled %d instead of 2 dice for 2d20", expression, len(rolls[0].Results))
}
if len(rolls[0].Results) != 1 {
t.Fatalf("Roll %s: rolled %d instead of 1 die for d20", expression, len(rolls[1].Results))
}
sum := 0
for _, v := range rolls[1].Results {
if v < 1 || v > 20 {
t.Fatalf("Roll %s: rolled a %d which is out of range for 2d20", expression, v)
}
sum += v
}
rollResult := rolls[0].Results[0]
if rollResult < 1 || rollResult > 20 {
t.Fatalf("Roll %s: got invalid roll result for d20: %d", expression, rollResult)
}
expected := rollResult + sum
if result != expected {
t.Fatalf("Roll %s: Result was %d instead of %d", expression, result, expected)
}
}
/* Test that filling a macro works as expected */
func TestFillMacro(t *testing.T) {
macro := "A + (B / 2)"
inputs := []string{"2d20", "10"}
result := FillMacro(macro, inputs)
if result != "2d20 + (10 / 2)" {
t.Fatalf("FillMacro failed; gave result %s", result)
}
}