1
exercism fetch lua bowling

bowling_spec.lua

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
local BowlingScorer = require('bowling')

describe('bowling', function()
  local scorer

  before_each(function()
    scorer = BowlingScorer()
  end)

  local function roll_n_times(pins, n)
    for i = 1, n do
      if type(pins) == 'table' then
        for _, pins in ipairs(pins) do scorer.roll(pins) end
      else
        scorer.roll(pins)
      end
    end
  end

  it('should score a game with all gutterballs', function()
    roll_n_times(0, 20)
    assert.equal(0, scorer.score())
  end)

  it('should allow a game with all open frames', function()
    roll_n_times({ 6, 3 }, 10)
    assert.equal(90, scorer.score())
  end)

  it('should score a spare followed by zeros as 10', function()
    scorer.roll(6)
    scorer.roll(4)
    roll_n_times(0, 18)
    assert.equal(10, scorer.score())
  end)

  it('should count points scored in the roll after a spare twice', function()
    scorer.roll(6)
    scorer.roll(4)
    scorer.roll(3)
    roll_n_times(0, 17)
    assert.equal(16, scorer.score())
  end)

  it('should allow a one roll bonus for each consecutive spare', function()
    scorer.roll(5)
    scorer.roll(5)
    scorer.roll(3)
    scorer.roll(7)
    scorer.roll(4)
    roll_n_times(0, 15)
    assert.equal(31, scorer.score())
  end)

  it('should score a fill ball for a spare in the last frame', function()
    roll_n_times(0, 18)
    scorer.roll(7)
    scorer.roll(3)
    scorer.roll(7)
    assert.equal(17, scorer.score())
  end)

  it('should score a strike as 10 points when the next frame earns no points', function()
    scorer.roll(10)
    roll_n_times(0, 18)
    assert.equal(10, scorer.score())
  end)

  it('should score the next two rolls after a strike as double', function()
    scorer.roll(10)
    scorer.roll(5)
    scorer.roll(3)
    roll_n_times(0, 16)
    assert.equal(26, scorer.score())
  end)

  it('should correctly score multiple strikes in a row', function()
    scorer.roll(10)
    scorer.roll(10)
    scorer.roll(10)
    scorer.roll(5)
    scorer.roll(3)
    roll_n_times(0, 12)
    assert.equal(81, scorer.score())
  end)

  it('should allow fill balls when the final frame is a strike', function()
    roll_n_times(0, 18)
    scorer.roll(10)
    scorer.roll(7)
    scorer.roll(1)
    assert.equal(18, scorer.score())
  end)

  it('should not allow a bonus roll after rolling a spare after a strike on the last frame', function()
    roll_n_times(0, 18)
    scorer.roll(10)
    scorer.roll(7)
    scorer.roll(3)
    assert.equal(20, scorer.score())
  end)

  it('should not allow bonus rolls for strikes rolled with fill balls from a strike', function()
    roll_n_times(0, 18)
    roll_n_times(10, 3)
    assert.equal(30, scorer.score())
  end)

  it('should not allow bonus rolls for strikes rolled the fill ball from a spare', function()
    roll_n_times(0, 18)
    scorer.roll(7)
    scorer.roll(3)
    scorer.roll(10)
    assert.equal(20, scorer.score())
  end)

  it('should be able to score a perfect game', function()
    roll_n_times(10, 12)
    assert.equal(300, scorer.score())
  end)

  it('should not allow a roll with negative pins', function()
    assert.has_error(function() scorer.roll(-1) end)
  end)

  it('should not allow a roll better than a strike', function()
    assert.has_error(function() scorer.roll(11) end)
  end)

  it('should not allow two rolls in a frame to exceed 10 pins', function()
    scorer.roll(5)
    assert.has_error(function() scorer.roll(6) end)
  end)

  it('should not allow fill balls after a strike in the final frame to score more than 10 points', function()
    roll_n_times(0, 18)
    scorer.roll(10)
    scorer.roll(5)
    assert.has_error(function() scorer.roll(6) end)
  end)

  it('should not allow the score to be taken at the beginning of the game', function()
    assert.has_error(function() scorer.score() end)
  end)

  it('should not allow the score to be taken before 10 frames have been played', function()
    roll_n_times(0, 19)
    assert.has_error(function() scorer.score() end)
  end)

  it('should not allow more rolls after 10 frames have been played', function()
    roll_n_times(0, 20)
    assert.has_error(function() scorer.roll(0) end)
  end)

  it('should not allow the game to be scored before either fill ball for a strike in the final frame', function()
    roll_n_times(10, 10)
    assert.has_error(function() scorer.score() end)
  end)

  it('should not allow the game to be scored before either both fill balls for a strike in the final frame', function()
    roll_n_times(10, 11)
    assert.has_error(function() scorer.score() end)
  end)

  it('should not allow the game to be scored before either the fill ball for a spare in the final frame', function()
    roll_n_times(0, 18)
    scorer.roll(7)
    scorer.roll(3)
    assert.has_error(function() scorer.score() end)
  end)

  it('should allow multiple independent scorers to be created', function()
    local s1 = BowlingScorer()
    local s2 = BowlingScorer()

    for i = 1, 20 do
      s1.roll(3)
    end

    for i = 1, 20 do
      s2.roll(2)
    end

    assert.equal(60, s1.score())
    assert.equal(40, s2.score())
  end)
end)