-
Notifications
You must be signed in to change notification settings - Fork 0
/
rolling_test.go
94 lines (80 loc) · 2.95 KB
/
rolling_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
package roll
import (
"math/rand"
"strings"
"testing"
)
// Ensure the parser works
func TestParse(t *testing.T) {
var tests = []struct {
seed int64
in string
out string
}{
// Fate rolls
{seed: 0, in: "2dF+4", out: `Rolled "2dF+4" and got ⊟, ⊟ for a total of 2`},
{seed: 0, in: "dF", out: `Rolled "dF" and got ⊟ for a total of -1`},
// Normal Die (3)
{seed: 0, in: "3d3-2", out: `Rolled "3d3-2" and got 1, 1, 2 for a total of 2`},
{seed: 0, in: "d3+1", out: `Rolled "d3+1" and got 1 for a total of 2`},
// Normal Die (6)
{seed: 0, in: "3d6+4", out: `Rolled "3d6+4" and got 1, 1, 2 for a total of 8`},
{seed: 0, in: "d6-1", out: `Rolled "d6-1" and got 1 for a total of 0`},
// Normal Die (6) Successes
{seed: 0, in: "3d6+4>4f=6", out: `Rolled "3d6+4>4f=6" and got 1, 1, 2 for a total of 2`},
{seed: 0, in: "d6-1<3", out: `Rolled "d6-1<3" and got 1 for a total of 1`},
// Grouped rolls
{seed: 0, in: "{3d6+4}", out: `Rolled "{3d6+4}" and got 5, 5, 6 for a total of 16`},
{seed: 0, in: "{3d6, 2d8}", out: `Rolled "{3d6, 2d8}" and got 4, 7 for a total of 11`},
{seed: 0, in: "{3d6 + 2d8}", out: `Rolled "{3d6 + 2d8}" and got 1, 1, 2, 3, 4 for a total of 11`},
// Grouped Successes
{seed: 0, in: "{3d6 + 2d8}>3", out: `Rolled "{3d6 + 2d8}>3" and got 1, 1, 2, 3, 4 for a total of 1`},
{seed: 0, in: "{3d6 + 2d8}>2f=1", out: `Rolled "{3d6 + 2d8}>2f=1" and got 1, 1, 2, 3, 4 for a total of 0`},
// Errors
{seed: 0, in: "3dX-2", out: `unrecognised die type "dX"`},
{seed: 0, in: "CRAP", out: `found unexpected token "C"`},
}
for i, tt := range tests {
rand.Seed(tt.seed)
out, err := Parse(strings.NewReader(tt.in))
if err != nil {
if err.Error() != tt.out {
t.Errorf("%d. unexpected parse error: exp=%v got=%v", i, tt.out, err)
}
} else if tt.out != out {
t.Errorf("%d. result mismatch: exp=%v got=%v", i, tt.out, out)
}
}
}
// Ensure the parser works
func TestParseString(t *testing.T) {
var tests = []struct {
seed int64
in string
out string
}{
// Fate rolls
{seed: 0, in: "2dF+4", out: `Rolled "2dF+4" and got ⊟, ⊟ for a total of 2`},
{seed: 0, in: "dF", out: `Rolled "dF" and got ⊟ for a total of -1`},
// Normal Die (3)
{seed: 0, in: "3d3-2", out: `Rolled "3d3-2" and got 1, 1, 2 for a total of 2`},
{seed: 0, in: "d3+1", out: `Rolled "d3+1" and got 1 for a total of 2`},
// Normal Die (6)
{seed: 0, in: "3d6+4", out: `Rolled "3d6+4" and got 1, 1, 2 for a total of 8`},
{seed: 0, in: "d6-1", out: `Rolled "d6-1" and got 1 for a total of 0`},
// Errors
{seed: 0, in: "3dX-2", out: `unrecognised die type "dX"`},
{seed: 0, in: "CRAP", out: `found unexpected token "C"`},
}
for i, tt := range tests {
rand.Seed(tt.seed)
out, err := ParseString(tt.in)
if err != nil {
if err.Error() != tt.out {
t.Errorf("%d. unexpected parse error: exp=%v got=%v", i, tt.out, err)
}
} else if tt.out != out {
t.Errorf("%d. result mismatch: exp=%v got=%v", i, tt.out, out)
}
}
}