Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorporate minimum value into 99 Bottles verse #2

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
badf11b
Add skipped test_custom_max_min_bottles_song integration test
trishrempel Jan 14, 2023
2b74132
Add min parameter with default value of 0 to BottleNumber.for
trishrempel Jan 14, 2023
cb27156
Add min to BottleNumber constructor with a default of 0
trishrempel Jan 14, 2023
b84d4e7
Pass the minimum value in BottleNumber.for to the BottleNumber constr…
trishrempel Jan 14, 2023
35b1794
Pass the min parameter to BottleNumber.for in BottleNumber classes
trishrempel Jan 14, 2023
441f7db
Add the min parameter to BottleVerse.lyrics
trishrempel Jan 14, 2023
47847ff
Remove the default value for min in BottleNumber.for
trishrempel Jan 14, 2023
17bf1ae
Add min parameter to VerseFake.lyrics
trishrempel Jan 14, 2023
0108311
Add skipped BottleVerse unit test test_verse_1_with_explicit_min_value
trishrempel Jan 14, 2023
a708254
Add naive implementation of BottleNumberMin and unit test
trishrempel Jan 14, 2023
e99af66
Update BottleNumberMin to proxy quantity and container from BottleNumber
trishrempel Jan 14, 2023
b6996ba
Update BottleNumber.for to use BottleNumberMin if number matches min
trishrempel Jan 14, 2023
7e21245
Pass min parameter to verse_template.lyrics and unskip integration test
trishrempel Jan 14, 2023
54cb31d
Remove unnecessary duplicate methods from BottleNumber0
trishrempel Jan 15, 2023
2d9134b
Introduce bottle_number: parameter with nil default to BottleNumberMi…
trishrempel Jan 16, 2023
a5f5fdb
Update BottleNumber.for to prepare to pass bottle_number to BottleNum…
trishrempel Jan 16, 2023
aa11543
Update BottleNumberMinTest tests to pass bottle_number: to BottleNumb…
trishrempel Jan 16, 2023
609c608
Update BottleNumberMin#new to use bottle_number if it is not nil
trishrempel Jan 16, 2023
0911e00
Update BottleNumber.for to pass bottle_number to BottleNumberMin#new
trishrempel Jan 16, 2023
439cbcf
Remove default value for bottle_number: in BottleNumberMin#new
trishrempel Jan 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 44 additions & 17 deletions lib/bottles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def verses(upper, lower)
end

def verse(number)
verse_template.lyrics(number, max: max)
verse_template.lyrics(number, max: max, min: min)
end
end

Expand All @@ -30,8 +30,8 @@ def min
0
end

def lyrics(number, max: self.max)
new(BottleNumber.for(number, max: max)).lyrics
def lyrics(number, max: self.max, min: self.min)
new(BottleNumber.for(number, max: max, min: min)).lyrics
end
end

Expand All @@ -51,8 +51,8 @@ def lyrics


class BottleNumber
def self.for(number, max:)
case number
def self.for(number, max:, min:)
bottle_number = case number
when 0
BottleNumber0
when 1
Expand All @@ -61,13 +61,25 @@ def self.for(number, max:)
BottleNumber6
else
BottleNumber
end.new(number, max: max)
end.new(number, max: max, min: min)

if (number == min)
BottleNumberMin.new(
number,
max: max,
min: min,
bottle_number: bottle_number
)
else
bottle_number
end
end

attr_reader :number, :max
def initialize(number, max:)
attr_reader :number, :max, :min
def initialize(number, max:, min:)
@number = number
@max = max
@min = min
end

def to_s
Expand All @@ -91,22 +103,14 @@ def pronoun
end

def successor
BottleNumber.for(number - 1, max: max)
BottleNumber.for(number - 1, max: max, min: min)
end
end

class BottleNumber0 < BottleNumber
def quantity
"no more"
end

def action
"Go to the store and buy some more"
end

def successor
BottleNumber.for(max, max: max)
end
end

class BottleNumber1 < BottleNumber
Expand All @@ -128,3 +132,26 @@ def container
"six-pack"
end
end

class BottleNumberMin < BottleNumber
def initialize(number, max:, min:, bottle_number:)
super(number, max: max, min: min)
@bottle_number = bottle_number
end

def quantity
@bottle_number.quantity
end

def container
@bottle_number.container
end

def action
"Go to the store and buy some more"
end

def successor
BottleNumber.for(max, max: max, min: min)
end
end
80 changes: 76 additions & 4 deletions test/bottles_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def min
5
end

def lyrics(number, max: nil)
def lyrics(number, max: nil, min: nil)
"This is verse #{number}.\n"
end
end
Expand Down Expand Up @@ -97,6 +97,15 @@ def test_verse_1
assert_equal expected, BottleVerse.lyrics(1)
end

def test_verse_1_with_explicit_min_value
expected =
"1 bottle of beer on the wall, " +
"1 bottle of beer.\n" +
"Go to the store and buy some more, " +
"99 bottles of beer on the wall.\n"
assert_equal expected, BottleVerse.lyrics(1, min: 1)
end

def test_verse_0
expected =
"No more bottles of beer on the wall, " +
Expand All @@ -116,6 +125,39 @@ def test_verse_0_with_explicit_max_value
end
end

class BottleNumberMinTest < Minitest::Test
def test_verse_1
expected =
"1 bottle of beer on the wall, " +
"1 bottle of beer.\n" +
"Go to the store and buy some more, " +
"99 bottles of beer on the wall.\n"
assert_equal expected, BottleVerse.new(
BottleNumberMin.new(
1,
max: 99,
min: 1,
bottle_number: BottleNumber.for(1, max: 99, min: 1)
)
).lyrics
end

def test_verse_0
expected =
"No more bottles of beer on the wall, " +
"no more bottles of beer.\n" +
"Go to the store and buy some more, " +
"1 six-pack of beer on the wall.\n"
assert_equal expected, BottleVerse.new(
BottleNumberMin.new(
0,
max: 6,
min: 0,
bottle_number: BottleNumber.for(0, max: 6, min: 0)
)
).lyrics
end
end

class CountdownSongTest < Minitest::Test
def test_verse
Expand Down Expand Up @@ -181,7 +223,7 @@ def test_song
end

class Bottles99IntegrationTest < Minitest::Test
def test_partial_7_bottles_song
def test_custom_max_bottles_song
expected = <<-SONG
7 bottles of beer on the wall, 7 bottles of beer.
Take one down and pass it around, 1 six-pack of beer on the wall.
Expand Down Expand Up @@ -212,8 +254,38 @@ def test_partial_7_bottles_song
expected,
CountdownSong.new(
verse_template: BottleVerse,
max: 7,
min: 0
max: 7
).song
)
end

def test_custom_max_min_bottles_song
expected = <<-SONG
1 six-pack of beer on the wall, 1 six-pack of beer.
Take one down and pass it around, 5 bottles of beer on the wall.

5 bottles of beer on the wall, 5 bottles of beer.
Take one down and pass it around, 4 bottles of beer on the wall.

4 bottles of beer on the wall, 4 bottles of beer.
Take one down and pass it around, 3 bottles of beer on the wall.

3 bottles of beer on the wall, 3 bottles of beer.
Take one down and pass it around, 2 bottles of beer on the wall.

2 bottles of beer on the wall, 2 bottles of beer.
Take one down and pass it around, 1 bottle of beer on the wall.

1 bottle of beer on the wall, 1 bottle of beer.
Go to the store and buy some more, 1 six-pack of beer on the wall.
SONG

assert_equal(
expected,
CountdownSong.new(
verse_template: BottleVerse,
max: 6,
min: 1
).song
)
end
Expand Down