-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathroll_test.go
79 lines (74 loc) · 1.77 KB
/
roll_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
package main
import (
"fmt"
"testing"
)
const (
userID = "1234"
)
var formatTests = []string{
"",
"d6",
"10d10-5",
"123456789d123456789+123456789",
"1d1 2d2 3d3",
"10d10>5",
"10d10<5",
"10d10k5",
"2d6 / 2d6",
"mini 2d6 / 2d6",
}
func TestRollFormat(t *testing.T) {
for _, test := range formatTests {
rolls := Parse(test)
verify := fmt.Sprint("<@", userID, "> rolled ")
sum := 0
for i, result := range rolls {
result.Roll()
// Use the last roll as the final.
total := result.Total
switch result.Operator {
case "+":
sum += result.Total
case "-":
sum -= result.Total
case "*":
sum *= result.Total
case "/":
sum /= result.Total
}
if i == 0 {
verify += fmt.Sprint("*", total, "*")
} else {
verify += fmt.Sprint(" ", result.Operator, " *", total, "*")
}
}
if len(rolls) > 1 {
verify += fmt.Sprint(" = *", sum, "*")
}
resp := formatRoll(userID, false, false, rolls)
if resp["response_type"] != "in_channel" {
t.Error("Incorrect response type", resp["response_type"])
}
attach := resp["attachments"].([]D)[0]
if attach["color"] == "" {
t.Error("Missing color")
}
if attach["text"] != verify {
t.Error("Incorrect response text", attach["text"], "instead of", verify)
}
resp = formatRoll(userID, false, true, rolls)
if resp["response_type"] != "ephemeral" {
t.Error("Incorrect response type", resp["response_type"])
}
}
}
func TestFor(t *testing.T) {
rolls := Parse("d20 for initiative, 2d6 + 5 for attack")
resp := formatRoll(userID, false, false, rolls)
attach := resp["attachments"].([]D)[0]
verify := "<@1234> rolled 0 for initiative + 0 + 0 = 0 for attack = 0"
if attach["fallback"] != verify {
t.Error("Incorrect response text", attach["fallback"], "instead of", verify)
}
}