1
exercism fetch delphi bowling

uBowlingTests.pas

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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
unit uBowlingTests;

interface
uses
  DUnitX.TestFramework,
  uBowling;

type

  [TestFixture]
  BowlingTests = class(TObject)
  private
    class function RollMany(pins: array of integer; game: IBowlingGame): IBowlingGame; static;
  public
    [Test]
//  [Ignore('Comment the "[Ignore]" statement to run the test')]
    procedure Should_be_able_to_score_a_game_with_all_zeros;

    [Test]
    [Ignore]
    procedure Should_be_able_to_score_a_game_with_no_strikes_or_spares;

    [Test]
    [Ignore]
    procedure A_spare_followed_by_zeros_is_worth_ten_points;

    [Test]
    [Ignore]
    procedure Points_scored_in_the_roll_after_a_spare_are_counted_twice;

    [Test]
    [Ignore]
    procedure Consecutive_spares_each_get_a_one_roll_bonus;

    [Test]
    [Ignore]
    procedure A_spare_in_the_last_frame_gets_a_one_roll_bonus_that_is_counted_once;

    [Test]
    [Ignore]
    procedure A_strike_earns_ten_points_in_frame_with_a_single_roll;

    [Test]
    [Ignore]
    procedure Points_scored_in_the_two_rolls_after_a_strike_are_counted_twice_as_a_bonus;

    [Test]
    [Ignore]
    procedure Consecutive_strikes_each_get_the_two_roll_bonus;

    [Test]
    [Ignore]
    procedure A_strike_in_the_last_frame_gets_a_two_roll_bonus_that_is_counted_once;

    [Test]
    [Ignore]
    procedure Rolling_a_spare_with_the_two_roll_bonus_does_not_get_a_bonus_roll;

    [Test]
    [Ignore]
    procedure Strikes_with_the_two_roll_bonus_do_not_get_bonus_rolls;

    [Test]
    [Ignore]
    procedure A_strike_with_the_one_roll_bonus_after_a_spare_in_the_last_frame_does_not_get_a_bonus;

    [Test]
    [Ignore]
    procedure All_strikes_is_a_perfect_game;

    [Test]
    [Ignore]
    procedure Rolls_cannot_score_negative_points;

    [Test]
    [Ignore]
    procedure A_roll_cannot_score_more_than_10_points;

    [Test]
    [Ignore]
    procedure Two_rolls_in_a_frame_cannot_score_more_than_10_points;

    [Test]
    [Ignore]
    procedure Two_bonus_rolls_after_a_strike_in_the_last_frame_cannot_score_more_than_10_points;

    [Test]
    [Ignore]
    procedure An_unstarted_game_cannot_be_scored;

    [Test]
    [Ignore]
    procedure An_incomplete_game_cannot_be_scored;

    [Test]
    [Ignore]
    procedure A_game_with_more_than_ten_frames_cannot_be_scored;

    [Test]
    [Ignore]
    procedure Bonus_rolls_for_a_strike_in_the_last_frame_must_be_rolled_before_score_can_be_calculated;

    [Test]
    [Ignore]
    procedure Both_bonus_rolls_for_a_strike_in_the_last_frame_must_be_rolled_before_score_can_be_calculated;

    [Test]
    [Ignore]
    procedure Bonus_roll_for_a_spare_in_the_last_frame_must_be_rolled_before_score_can_be_calculated;
  end;

implementation
uses System.SysUtils;

procedure BowlingTests.Should_be_able_to_score_a_game_with_all_zeros;
var game: IBowlingGame;
begin
  game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], NewBowlingGame);
  assert.AreEqual(0, game.Score);
end;

procedure BowlingTests.Should_be_able_to_score_a_game_with_no_strikes_or_spares;
var game: IBowlingGame;
begin
  game := RollMany([3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6], NewBowlingGame);
  assert.AreEqual(90, game.Score);
end;

procedure BowlingTests.A_spare_followed_by_zeros_is_worth_ten_points;
var game: IBowlingGame;
begin
  game := RollMany([6, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], NewBowlingGame);
  assert.AreEqual(10, game.Score);
end;

procedure BowlingTests.Points_scored_in_the_roll_after_a_spare_are_counted_twice;
var game: IBowlingGame;
begin
  game := RollMany([6, 4, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], NewBowlingGame);
  assert.AreEqual(16, game.Score);
end;

procedure BowlingTests.Consecutive_spares_each_get_a_one_roll_bonus;
var game: IBowlingGame;
begin
  game := RollMany([5, 5, 3, 7, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], NewBowlingGame);
  assert.AreEqual(31, game.Score);
end;

procedure BowlingTests.A_spare_in_the_last_frame_gets_a_one_roll_bonus_that_is_counted_once;
var game: IBowlingGame;
begin
  game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 7], NewBowlingGame);
  assert.AreEqual(17, game.Score);
end;

procedure BowlingTests.A_strike_earns_ten_points_in_frame_with_a_single_roll;
var game: IBowlingGame;
begin
  game := RollMany([10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], NewBowlingGame);
  assert.AreEqual(10, game.Score);
end;

procedure BowlingTests.Points_scored_in_the_two_rolls_after_a_strike_are_counted_twice_as_a_bonus;
var game: IBowlingGame;
begin
  game := RollMany([10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], NewBowlingGame);
  assert.AreEqual(26, game.Score);
end;

procedure BowlingTests.Consecutive_strikes_each_get_the_two_roll_bonus;
var game: IBowlingGame;
begin
  game := RollMany([10, 10, 10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], NewBowlingGame);
  assert.AreEqual(81, game.Score);
end;

procedure BowlingTests.A_strike_in_the_last_frame_gets_a_two_roll_bonus_that_is_counted_once;
var game: IBowlingGame;
begin
  game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 1], NewBowlingGame);
  assert.AreEqual(18, game.Score);
end;

procedure BowlingTests.Rolling_a_spare_with_the_two_roll_bonus_does_not_get_a_bonus_roll;
var game: IBowlingGame;
begin
  game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 3], NewBowlingGame);
  assert.AreEqual(20, game.Score);
end;

procedure BowlingTests.Strikes_with_the_two_roll_bonus_do_not_get_bonus_rolls;
var game: IBowlingGame;
begin
  game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10], NewBowlingGame);
  assert.AreEqual(30, game.Score);
end;

procedure BowlingTests.A_strike_with_the_one_roll_bonus_after_a_spare_in_the_last_frame_does_not_get_a_bonus;
var game: IBowlingGame;
begin
  game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 10], NewBowlingGame);
  assert.AreEqual(20, game.Score);
end;

procedure BowlingTests.All_strikes_is_a_perfect_game;
var game: IBowlingGame;
begin
  game := RollMany([10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10], NewBowlingGame);
  assert.AreEqual(300, game.Score);
end;

procedure BowlingTests.Rolls_cannot_score_negative_points;
var game: IBowlingGame;
begin
  game := RollMany([-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], NewBowlingGame);
  Assert.AreEqual(-1, game.Score);
end;

procedure BowlingTests.A_roll_cannot_score_more_than_10_points;
var game: IBowlingGame;
begin
  game := RollMany([11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], NewBowlingGame);
  Assert.AreEqual(-1, game.Score);
end;

procedure BowlingTests.Two_rolls_in_a_frame_cannot_score_more_than_10_points;
var game: IBowlingGame;
begin
  game := RollMany([5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], NewBowlingGame);
  Assert.AreEqual(-1, game.Score);
end;

procedure BowlingTests.Two_bonus_rolls_after_a_strike_in_the_last_frame_cannot_score_more_than_10_points;
var game: IBowlingGame;
begin
  game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 5, 6], NewBowlingGame);
  Assert.AreEqual(-1, game.Score);
end;

procedure BowlingTests.An_unstarted_game_cannot_be_scored;
var game: IBowlingGame;
begin
  game := RollMany([],NewBowlingGame);
  Assert.AreEqual(-1, game.Score);
end;

procedure BowlingTests.An_incomplete_game_cannot_be_scored;
var game: IBowlingGame;
begin
  game := RollMany([0, 0], NewBowlingGame);
  Assert.AreEqual(-1, game.Score);
end;

procedure BowlingTests.A_game_with_more_than_ten_frames_cannot_be_scored;
var game: IBowlingGame;
begin
  game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], NewBowlingGame);
  Assert.AreEqual(-1, game.Score);
end;

procedure BowlingTests.Bonus_rolls_for_a_strike_in_the_last_frame_must_be_rolled_before_score_can_be_calculated;
var game: IBowlingGame;
begin
  game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10], NewBowlingGame);
  Assert.AreEqual(-1, game.Score);
end;

procedure BowlingTests.Both_bonus_rolls_for_a_strike_in_the_last_frame_must_be_rolled_before_score_can_be_calculated;
var game: IBowlingGame;
begin
  game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10], NewBowlingGame);
  Assert.AreEqual(-1, game.Score);
end;

procedure BowlingTests.Bonus_roll_for_a_spare_in_the_last_frame_must_be_rolled_before_score_can_be_calculated;
var game: IBowlingGame;
begin
  game := RollMany([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3], NewBowlingGame);
  Assert.AreEqual(-1, game.Score);
end;

class function BowlingTests.RollMany(pins: array of integer; game: IBowlingGame): IBowlingGame;
var count: integer;
begin
  for count in pins do
    game.Roll(count);
  result := game;
end;

initialization
  TDUnitX.RegisterTestFixture(BowlingTests);
end.