-
-
Notifications
You must be signed in to change notification settings - Fork 654
/
bowling_test.go
66 lines (60 loc) · 2.01 KB
/
bowling_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
package bowling
import "testing"
const previousRollErrorMessage = `FAIL: %s
Unexpected error occurred: %q
while applying the previous rolls for the
test case: %v
The error was returned from Roll(%d) for previousRolls[%d].`
func applyPreviousRolls(g *Game, rolls []int) (index, pins int, err error) {
for index, pins := range rolls {
if err := g.Roll(pins); err != nil {
return index, pins, err
}
}
return 0, 0, nil
}
func TestScore(t *testing.T) {
for _, tc := range scoreTestCases {
g := NewGame()
index, pins, err := applyPreviousRolls(g, tc.previousRolls)
if err != nil {
t.Fatalf(previousRollErrorMessage,
tc.description, err, tc.previousRolls, pins, index)
}
score, err := g.Score()
if tc.valid {
var _ error = err
if err != nil {
t.Fatalf("FAIL: %s : Score() after Previous Rolls: %#v expected %d, got error %s",
tc.description, tc.previousRolls, tc.score, err)
} else if score != tc.score {
t.Fatalf("%s : Score() after Previous Rolls: %#v expected %d, got %d",
tc.description, tc.previousRolls, tc.score, score)
}
} else if err == nil {
t.Fatalf("FAIL: %s : Score() after Previous Rolls: %#v expected an error, got score %d\n\tExplanation: %s",
tc.description, tc.previousRolls, score, tc.explainText)
}
t.Logf("PASS: %s", tc.description)
}
}
func TestRoll(t *testing.T) {
for _, tc := range rollTestCases {
g := NewGame()
index, pins, err := applyPreviousRolls(g, tc.previousRolls)
if err != nil {
t.Fatalf(previousRollErrorMessage,
tc.description, err, tc.previousRolls, pins, index)
}
err = g.Roll(tc.roll)
if tc.valid && err != nil {
var _ error = err
t.Fatalf("FAIL: %s : Roll(%d) after Previous Rolls: %#v got unexpected error \"%s\".",
tc.description, tc.roll, tc.previousRolls, err)
} else if !tc.valid && err == nil {
t.Fatalf("FAIL: %s : Roll(%d) after Previous Rolls: %#v expected an error.\n\tExplanation: %s",
tc.description, tc.roll, tc.previousRolls, tc.explainText)
}
t.Logf("PASS: %s", tc.description)
}
}