-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame_spec.rb
164 lines (133 loc) · 4.72 KB
/
frame_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
require_relative "frame"
RSpec.describe Frame do
context "NOT a last frame" do
let(:frame) { Frame.new }
context "general" do
it "should have score 0 at start" do
expect(frame.score).to eq 0
end
it "should update score after a throw" do
frame.throw! knocked_pins: 5
expect(frame.score).to eq 5
end
it "should not be able to knok more pins then standing" do
expect { frame.throw! knocked_pins: (Frame::TOTAL_PINS + 1) }.
to raise_error(FrameError)
end
it "should not be over at start or after a throw with less knocks" do
expect(frame.over?).to eq false
frame.throw! knocked_pins: 5
expect(frame.over?).to eq false
end
end
context "knocks only a part of the pins" do
before :each do
frame.throw! knocked_pins: 3
frame.throw! knocked_pins: 4
end
it "should be over" do
expect(frame.over?).to eq true
end
it "should return correct score" do
expect(frame.score).to eq 7
end
it "should not accept extra score" do
expect(frame.update_extra_score(added_points: 5)).to eq false
expect(frame.score).to eq 7
end
it "should not be able to throw again" do
expect { frame.throw!(knocked_pins: 5) }.to raise_error(FrameError)
end
end
context "throws a spare" do
before :each do
frame.throw! knocked_pins: 3
frame.throw! knocked_pins: (Frame::TOTAL_PINS - 3)
end
it "should be over" do
expect(frame.over?).to eq true
end
it "should return correct score" do
expect(frame.score).to eq Frame::TOTAL_PINS
end
it "should accept one extra score" do
expect(frame.update_extra_score(added_points: 5)).to eq true
expect(frame.score).to eq (Frame::TOTAL_PINS + 5)
expect(frame.update_extra_score(added_points: 6)).to eq false
expect(frame.score).to eq (Frame::TOTAL_PINS + 5)
end
it "should not be able to throw again" do
expect { frame.throw!(knocked_pins: 5) }.to raise_error(FrameError)
end
end
context "throws a strike" do
before :each do
frame.throw! knocked_pins: Frame::TOTAL_PINS
end
it "should be over" do
expect(frame.over?).to eq true
end
it "should return correct score" do
expect(frame.score).to eq Frame::TOTAL_PINS
end
it "should accept two extra scores" do
expect(frame.update_extra_score(added_points: 5)).to eq true
expect(frame.score).to eq (Frame::TOTAL_PINS + 5)
expect(frame.update_extra_score(added_points: 6)).to eq true
expect(frame.score).to eq (Frame::TOTAL_PINS + 5 + 6)
expect(frame.update_extra_score(added_points: 7)).to eq false
expect(frame.score).to eq (Frame::TOTAL_PINS + 5 + 6)
end
it "should not be able to throw again" do
expect { frame.throw!(knocked_pins: 5) }.to raise_error(FrameError)
end
end
end
context "IS a last frame" do
let(:frame) { Frame.new last: true}
it "should have score 0 at start" do
expect(frame.score).to eq 0
end
it "should allow 2 normal throws" do
frame.throw! knocked_pins: 3
frame.throw! knocked_pins: 2
expect(frame.score).to eq 5
expect(frame.over?).to eq true
expect { frame.throw! knocked_pins: 1 }.to raise_error(FrameError)
end
it "should allow 3 throws if first is a strike" do
frame.throw! knocked_pins: 10
expect(frame.score).to eq 10
expect(frame.over?).to eq false
frame.throw! knocked_pins: 2
expect(frame.score).to eq 12
expect(frame.over?).to eq false
frame.throw! knocked_pins: 3
expect(frame.score).to eq 15
expect(frame.over?).to eq true
expect { frame.throw! knocked_pins: 1 }.to raise_error(FrameError)
end
it "should allow 3 throws if first 2 are a strike" do
frame.throw! knocked_pins: 10
expect(frame.score).to eq 10
expect(frame.over?).to eq false
frame.throw! knocked_pins: 10
expect(frame.score).to eq 20
expect(frame.over?).to eq false
frame.throw! knocked_pins: 6
expect(frame.score).to eq 26
expect(frame.over?).to eq true
expect { frame.throw! knocked_pins: 1 }.to raise_error(FrameError)
end
it "should allow 3 throws if first 2 are a spare" do
frame.throw! knocked_pins: 2
frame.throw! knocked_pins: 8
expect(frame.score).to eq 10
expect(frame.over?).to eq false
frame.throw! knocked_pins: 2
expect(frame.score).to eq 12
expect(frame.over?).to eq true
expect { frame.throw! knocked_pins: 1 }.to raise_error(FrameError)
end
end
end