-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_spec.rb
250 lines (200 loc) · 6.49 KB
/
game_spec.rb
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
require_relative "game"
RSpec.describe Game do
let(:game) { Game.new }
context "new game" do
it "should have a score of 0" do
expect(game.score).to eq 0
end
it "should have 10 total frames" do
expect(Game::TOTAL_FRAMES).to eq 10
end
end
context "one frame" do
it "should have correct score" do
game.throw! knocked_pins: 5
expect(game.score).to eq 5
game.throw! knocked_pins: 3
expect(game.score).to eq 8
end
end
context "multiple frames" do
it "should have correct score" do
game.throw! knocked_pins: 5
game.throw! knocked_pins: 3
game.throw! knocked_pins: 1
game.throw! knocked_pins: 2
expect(game.score).to eq 11
end
context "with a strike" do
it "should have correct score" do
game.throw! knocked_pins: 10
expect(game.score).to eq 10
game.throw! knocked_pins: 5
game.throw! knocked_pins: 3
expect(game.score).to eq 26
end
end
context "with a spare" do
it "should have correct score" do
game.throw! knocked_pins: 4
game.throw! knocked_pins: 6
expect(game.score).to eq 10
game.throw! knocked_pins: 5
game.throw! knocked_pins: 3
expect(game.score).to eq 23
end
end
end
context "all frames" do
before :each do
(Game::TOTAL_FRAMES-1).times do
game.throw! knocked_pins: 5
game.throw! knocked_pins: 3
end
end
it "should not allow more throws" do
game.throw! knocked_pins: 5
game.throw! knocked_pins: 3
expect(game.score).to eq 80
expect(game.game_over?).to eq true
expect { game.throw! knocked_pins: 3 }.to raise_error(GameError)
end
it "should handle strike in last frame" do
game.throw! knocked_pins: 10
expect(game.score).to eq 82
expect(game.game_over?).to eq false
expect { game.throw! knocked_pins: 3 }.not_to raise_error
expect(game.score).to eq 85
expect(game.game_over?).to eq false
expect { game.throw! knocked_pins: 4 }.not_to raise_error
expect(game.score).to eq 89
expect(game.game_over?).to eq true
end
it "should handle 2 strikes in last frame" do
game.throw! knocked_pins: 10
expect(game.score).to eq 82
expect(game.game_over?).to eq false
expect { game.throw! knocked_pins: 10 }.not_to raise_error
expect(game.score).to eq 92
expect(game.game_over?).to eq false
expect { game.throw! knocked_pins: 9 }.not_to raise_error
expect(game.score).to eq 101
expect(game.game_over?).to eq true
end
it "should handle spare in last frame" do
game.throw! knocked_pins: 4
game.throw! knocked_pins: 6
expect(game.score).to eq 82
expect(game.game_over?).to eq false
expect { game.throw! knocked_pins: 3 }.not_to raise_error
expect(game.score).to eq 85
expect(game.game_over?).to eq true
end
end
context "different test cases" do
specify "2 strikes in a row" do
game.throw! knocked_pins: 5
game.throw! knocked_pins: 3
expect(game.score).to eq 8
game.throw! knocked_pins: 10
expect(game.score).to eq 18
game.throw! knocked_pins: 10
expect(game.score).to eq 38
game.throw! knocked_pins: 3
expect(game.score).to eq 47
game.throw! knocked_pins: 4
expect(game.score).to eq 55
end
specify "with strikes and spares in a row" do
game.throw! knocked_pins: 5
game.throw! knocked_pins: 3
expect(game.score).to eq 8
game.throw! knocked_pins: 10
expect(game.score).to eq 18
game.throw! knocked_pins: 2
expect(game.score).to eq 22
game.throw! knocked_pins: 8
expect(game.score).to eq 38
game.throw! knocked_pins: 1
expect(game.score).to eq 40
game.throw! knocked_pins: 9
expect(game.score).to eq 49
game.throw! knocked_pins: 3
expect(game.score).to eq 55
game.throw! knocked_pins: 4
expect(game.score).to eq 59
end
specify "with strike at last 2 frames" do
(Game::TOTAL_FRAMES-2).times do
game.throw! knocked_pins: 5
game.throw! knocked_pins: 3
end
expect(game.score).to eq 64
game.throw! knocked_pins: 10
expect(game.score).to eq 74
expect(game.game_over?).to eq false
game.throw! knocked_pins: 10
expect(game.score).to eq 94
game.throw! knocked_pins: 5
expect(game.score).to eq 104
game.throw! knocked_pins: 3
expect(game.score).to eq 107
expect(game.game_over?).to eq true
end
specify "with spare at last frame" do
(Game::TOTAL_FRAMES-1).times do
game.throw! knocked_pins: 5
game.throw! knocked_pins: 3
end
expect(game.score).to eq 72
game.throw! knocked_pins: 4
game.throw! knocked_pins: 6
expect(game.score).to eq 82
expect(game.game_over?).to eq false
game.throw! knocked_pins: 7
expect(game.score).to eq 89
expect(game.game_over?).to eq true
end
specify "custome - youtube" do
# simulating: 'https://www.youtube.com/watch?v=aBe71sD8o8c'
game.throw! knocked_pins: 8
game.throw! knocked_pins: 2
expect(game.score).to eq 10
expect(game.game_over?).to eq false
game.throw! knocked_pins: 7
game.throw! knocked_pins: 3
expect(game.score).to eq 27
expect(game.game_over?).to eq false
game.throw! knocked_pins: 3
game.throw! knocked_pins: 4
expect(game.score).to eq 37
expect(game.game_over?).to eq false
game.throw! knocked_pins: 10
expect(game.score).to eq 47
expect(game.game_over?).to eq false
game.throw! knocked_pins: 2
game.throw! knocked_pins: 8
expect(game.score).to eq 67
expect(game.game_over?).to eq false
game.throw! knocked_pins: 10
expect(game.score).to eq 87
expect(game.game_over?).to eq false
game.throw! knocked_pins: 10
expect(game.score).to eq 107
expect(game.game_over?).to eq false
game.throw! knocked_pins: 8
game.throw! knocked_pins: 0
expect(game.score).to eq 131
expect(game.game_over?).to eq false
game.throw! knocked_pins: 10
expect(game.score).to eq 141
expect(game.game_over?).to eq false
game.throw! knocked_pins: 8
game.throw! knocked_pins: 2
expect(game.score).to eq 161
game.throw! knocked_pins: 9
expect(game.score).to eq 170
expect(game.game_over?).to eq true
end
end
end