1
exercism fetch go bowling

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
67
68
69
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) (int, int, error) {
	for index, pins := range rolls {
		var err error
		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)
	}
}

cases_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
package bowling

// Source: exercism/problem-specifications
// Commit: 26e345e [Bowling] Fix case descriptions (#832)
// Problem Specifications Version: 1.0.1

var scoreTestCases = []struct {
	description   string
	previousRolls []int  // bowling rolls to do before the Score() test
	valid         bool   // true => no error, false => error expected
	score         int    // when .valid == true, the expected score value
	explainText   string // when .valid == false, error explanation text
}{
	{
		"should be able to score a game with all zeros",
		[]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		true,
		0,
		"",
	},
	{
		"should be able to score a game with no strikes or spares",
		[]int{3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6},
		true,
		90,
		"",
	},
	{
		"a spare followed by zeros is worth ten points",
		[]int{6, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		true,
		10,
		"",
	},
	{
		"points scored in the roll after a spare are counted twice",
		[]int{6, 4, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		true,
		16,
		"",
	},
	{
		"consecutive spares each get a one roll bonus",
		[]int{5, 5, 3, 7, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		true,
		31,
		"",
	},
	{
		"a spare in the last frame gets a one roll bonus that is counted once",
		[]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 7},
		true,
		17,
		"",
	},
	{
		"a strike earns ten points in a frame with a single roll",
		[]int{10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		true,
		10,
		"",
	},
	{
		"points scored in the two rolls after a strike are counted twice as a bonus",
		[]int{10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		true,
		26,
		"",
	},
	{
		"consecutive strikes each get the two roll bonus",
		[]int{10, 10, 10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		true,
		81,
		"",
	},
	{
		"a strike in the last frame gets a two roll bonus that is counted once",
		[]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 1},
		true,
		18,
		"",
	},
	{
		"rolling a spare with the two roll bonus does not get a bonus roll",
		[]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 3},
		true,
		20,
		"",
	},
	{
		"strikes with the two roll bonus do not get bonus rolls",
		[]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10},
		true,
		30,
		"",
	},
	{
		"a strike with the one roll bonus after a spare in the last frame does not get a bonus",
		[]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 10},
		true,
		20,
		"",
	},
	{
		"all strikes is a perfect game",
		[]int{10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
		true,
		300,
		"",
	},

	{
		"two bonus rolls after a strike in the last frame can score more than 10 points if one is a strike",
		[]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 6},
		true,
		26,
		"",
	},

	{
		"an unstarted game cannot be scored",
		[]int{},
		false,
		0,
		"Score cannot be taken until the end of the game",
	},
	{
		"an incomplete game cannot be scored",
		[]int{0, 0},
		false,
		0,
		"Score cannot be taken until the end of the game",
	},

	{
		"bonus rolls for a strike in the last frame must be rolled before score can be calculated",
		[]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10},
		false,
		0,
		"Score cannot be taken until the end of the game",
	},
	{
		"both bonus rolls for a strike in the last frame must be rolled before score can be calculated",
		[]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10},
		false,
		0,
		"Score cannot be taken until the end of the game",
	},
	{
		"bonus roll for a spare in the last frame must be rolled before score can be calculated",
		[]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3},
		false,
		0,
		"Score cannot be taken until the end of the game",
	},
}

var rollTestCases = []struct {
	description   string
	previousRolls []int  // bowling rolls to do before the Roll(roll) test
	valid         bool   // true => no error, false => error expected
	roll          int    // pin count for the test roll
	explainText   string // when .valid == false, error explanation text
}{

	{
		"rolls cannot score negative points",
		[]int{},
		false,
		-1,
		"Negative roll is invalid",
	},
	{
		"a roll cannot score more than 10 points",
		[]int{},
		false,
		11,
		"Pin count exceeds pins on the lane",
	},
	{
		"two rolls in a frame cannot score more than 10 points",
		[]int{5},
		false,
		6,
		"Pin count exceeds pins on the lane",
	},
	{
		"bonus roll after a strike in the last frame cannot score more than 10 points",
		[]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10},
		false,
		11,
		"Pin count exceeds pins on the lane",
	},
	{
		"two bonus rolls after a strike in the last frame cannot score more than 10 points",
		[]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 5},
		false,
		6,
		"Pin count exceeds pins on the lane",
	},

	{
		"the second bonus rolls after a strike in the last frame cannot be a strike if the first one is not a strike",
		[]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 6},
		false,
		10,
		"Pin count exceeds pins on the lane",
	},
	{
		"second bonus roll after a strike in the last frame cannot score more than 10 points",
		[]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10},
		false,
		11,
		"Pin count exceeds pins on the lane",
	},

	{
		"cannot roll if game already has ten frames",
		[]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		false,
		0,
		"Cannot roll after game is over",
	},
}