-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdice_test.go
99 lines (89 loc) · 2.55 KB
/
dice_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
package main
import (
"regexp"
"strings"
"testing"
)
func TestFlipCoin(t *testing.T) {
validOutput := regexp.MustCompile(`(?:Heads|Tails).`)
for i := 1; i < 10; i++ {
result := flipCoin("coin")
if !(validOutput.MatchString(result)) {
t.Errorf(`FAIL: Expected heads or tails in output, but result was \"%s\"`, result)
}
}
validOutput = regexp.MustCompile(`(?:(?:Heads|Tails), ){2}(?:Heads|Tails)\.`)
for i := 1; i < 10; i++ {
result := flipCoin("coin 3")
if !(validOutput.MatchString(result)) {
t.Errorf(`FAIL: Expected list of heads or tails in output, but result was \"%s\"`, result)
}
}
validOutput = regexp.MustCompile(`10 coins: [HT]{10}\.`)
for i := 1; i < 10; i++ {
result := flipCoin("coin 10")
if !(validOutput.MatchString(result)) {
t.Errorf(`FAIL: Expected list of H or T in output, but result was \"%s\"`, result)
}
}
errorMsg := "malformed coin toss (max count is 50)"
result := flipCoin("coin 99")
if result != errorMsg {
t.Errorf(`FAIL: Expected count exceeded error, got "%s"`, result)
}
errorMsg = "malformed coin toss (min count is 1)"
result = flipCoin("coin 0")
if result != errorMsg {
t.Errorf(`FAIL: Expected count subceeded error, got "%s"`, result)
}
}
func TestRollDice(t *testing.T) {
plannedFailure := "Try something like '!roll d4', '!roll 3d8', '!roll 2d6+2'"
plannedError := "malformed roll"
errorTables := []struct {
input string
expected string
}{
{"", plannedFailure},
{" ", plannedFailure},
{"4", plannedFailure},
{"2d6+_", plannedFailure},
{"2daf3", plannedFailure},
{"2d6+e", plannedFailure},
{"2e6?_", plannedFailure},
{"-3d8", plannedFailure},
{"3d1", plannedError},
{"2d112", plannedError},
{"4d0", plannedError},
{"100d12", plannedError},
{"4d8+1200", plannedError},
{"92233720368547758072131231", plannedFailure},
{"4d92233720368547758073213123", plannedError},
}
validateOutput := regexp.MustCompile(`\d+ \d+-sided dic?e(?::|\s(?:\([+-]\d\))?:) \d+`)
successTables := []struct {
input string
}{
{"d6"},
{"4d6"},
{"2d20+3"},
{"2d6-2"},
{"50d8"},
}
for _, table := range errorTables {
result := rollDice(table.input)
if !(strings.Contains(result, table.expected)) {
t.Errorf("FAIL: Input %s: Expected %s -- Got %s", table.input, table.expected, result)
} else {
t.Logf("OK: %s", table.input)
}
}
for _, table := range successTables {
result := rollDice(table.input)
if !(validateOutput.MatchString(result)) {
t.Errorf("FAIL: Input %s: Got %s", table.input, result)
} else {
t.Logf("OK: %s -> %s", table.input, result)
}
}
}