-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
242 lines (227 loc) · 4.43 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
package roll
import (
"reflect"
"strings"
"testing"
)
// Ensure the parser can parse strings into Statement ASTs.
func TestParser_Parse(t *testing.T) {
var tests = []struct {
s string
roll Roll
err string
}{
// Simple roll
{
s: `3d6`,
roll: &DiceRoll{
Multiplier: 3,
Die: NormalDie(6),
Modifier: 0,
},
},
// Fate roll statement
{
s: `4dF`,
roll: &DiceRoll{
Multiplier: 4,
Die: FateDie(0),
Modifier: 0,
},
},
// Simple roll with modifier
{
s: `3d6+4`,
roll: &DiceRoll{
Multiplier: 3,
Die: NormalDie(6),
Modifier: 4,
},
},
// Fate roll with modifier
{
s: `3dF+4`,
roll: &DiceRoll{
Multiplier: 3,
Die: FateDie(0),
Modifier: 4,
},
},
// Simple roll with multiple modifiers
{
s: `3d6+4-1+6-3`,
roll: &DiceRoll{
Multiplier: 3,
Die: NormalDie(6),
Modifier: 6,
},
},
// Simple roll with no multiplier
{
s: `d6`,
roll: &DiceRoll{
Multiplier: 1,
Die: NormalDie(6),
Modifier: 0,
},
},
// Simple roll with limit
{
s: `4d6kh3`,
roll: &DiceRoll{
Multiplier: 4,
Die: NormalDie(6),
Limit: &LimitOp{
Type: KeepHighest,
Amount: 3,
},
},
},
// Multi-roll, compounded on 5s, keep top 3, sort descending, +3
{
s: `6d6!!5kh3sd+3`,
roll: &DiceRoll{
Multiplier: 6,
Die: NormalDie(6),
Modifier: 3,
Sort: Descending,
Limit: &LimitOp{
Type: KeepHighest,
Amount: 3,
},
Exploding: &ExplodingOp{
Type: Compounded,
ComparisonOp: &ComparisonOp{
Type: Equals,
Value: 5,
},
},
},
},
// Multi-roll, reroll 2s, reroll once on 4s, successes > 3, failures on 1s
{
s: `6d6r2ro4>3f=1`,
roll: &DiceRoll{
Multiplier: 6,
Die: NormalDie(6),
Rerolls: []RerollOp{
RerollOp{
ComparisonOp: &ComparisonOp{
Type: Equals,
Value: 2,
},
},
RerollOp{
ComparisonOp: &ComparisonOp{
Type: Equals,
Value: 4,
},
Once: true,
},
},
Success: &ComparisonOp{
Type: GreaterThan,
Value: 3,
},
Failure: &ComparisonOp{
Type: Equals,
Value: 1,
},
},
},
// Grouped multi-roll, drop lowest, successes on 1s, fails > 5
{
s: `{3d6+4,2d8}dl=1f>5`,
roll: &GroupedRoll{
Rolls: []Roll{
&DiceRoll{
Multiplier: 3,
Die: NormalDie(6),
Modifier: 4,
},
&DiceRoll{
Multiplier: 2,
Die: NormalDie(8),
},
},
Limit: &LimitOp{
Amount: 1,
Type: DropLowest,
},
Success: &ComparisonOp{
Type: Equals,
Value: 1,
},
Failure: &ComparisonOp{
Type: GreaterThan,
Value: 5,
},
Combined: false,
},
},
// Grouped combined nested multi-roll, keep high 3, succ <4, fail >3
{
s: `{3d6+2d8-{4d4-1}dl}kh3<4f>3`,
roll: &GroupedRoll{
Rolls: []Roll{
&DiceRoll{
Multiplier: 3,
Die: NormalDie(6),
},
&DiceRoll{
Multiplier: 2,
Die: NormalDie(8),
},
&GroupedRoll{
Rolls: []Roll{
&DiceRoll{
Multiplier: 4,
Die: NormalDie(4),
Modifier: -1,
},
},
Limit: &LimitOp{
Amount: 1,
Type: DropLowest,
},
Combined: true,
Negative: true,
},
},
Limit: &LimitOp{
Amount: 3,
Type: KeepHighest,
},
Success: &ComparisonOp{
Type: LessThan,
Value: 4,
},
Failure: &ComparisonOp{
Type: GreaterThan,
Value: 3,
},
Combined: true,
},
},
// Errors
{s: `foo`, err: `found unexpected token "f"`},
{s: `dX`, err: `unrecognised die type "dX"`},
{s: `d4--`, err: `found unexpected token "-"`},
{s: `3d4d5`, err: `found unexpected token "d5"`},
}
for i, tt := range tests {
roll, err := NewParser(strings.NewReader(tt.s)).Parse()
if !reflect.DeepEqual(tt.err, errstring(err)) {
t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
} else if tt.err == "" && !reflect.DeepEqual(tt.roll, roll) {
t.Errorf("%d. %q\n\nroll mismatch:\n\nexp=%#v (%s)\n\ngot=%#v (%s)\n\n", i, tt.s, tt.roll, tt.roll.String(), roll, roll.String())
}
}
}
// errstring returns the string representation of an error.
func errstring(err error) string {
if err != nil {
return err.Error()
}
return ""
}