-
-
Save krashev/0f3ec992bac2bbf397c9 to your computer and use it in GitHub Desktop.
B team
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Bottles | |
def song | |
verses(99, 0) | |
end | |
def verses(upper_bound, lower_bound) | |
upper_bound.downto(lower_bound).map { |i| verse(i) }.join("\n") | |
end | |
def verse(verse_number) | |
current_bn = create_bottles(verse_number) | |
next_bn = create_bottles(current_bn.next) | |
"#{current_bn.quantity.capitalize} #{current_bn.container} of beer on the wall, " + | |
"#{current_bn.quantity} #{current_bn.container} of beer.\n" + | |
"#{current_bn.action}, " + | |
"#{next_bn.quantity} #{next_bn.container} of beer on the wall.\n" | |
end | |
private | |
def create_bottles(number) | |
if number == 0 | |
BottleNumber0.new(number) | |
elsif number == 1 | |
BottleNumber1.new(number) | |
elsif number == 6 | |
BottleNumber6.new(number) | |
else | |
BottleNumber.new(number) | |
end | |
end | |
end | |
class BottleNumber | |
attr_reader :number | |
def initialize(number) | |
@number = number | |
end | |
def container | |
'bottles' | |
end | |
def pronoun | |
'one' | |
end | |
def quantity | |
number.to_s | |
end | |
def action | |
"Take #{pronoun} down and pass it around" | |
end | |
def next | |
number - 1 | |
end | |
end | |
class BottleNumber0 < BottleNumber | |
def quantity | |
'no more' | |
end | |
def action | |
"Go to the store and buy some more" | |
end | |
def next | |
99 | |
end | |
end | |
class BottleNumber1 < BottleNumber | |
def pronoun | |
'it' | |
end | |
def container | |
'bottle' | |
end | |
end | |
class BottleNumber6 < BottleNumber | |
def quantity | |
"1" | |
end | |
def container | |
'six-pack' | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem 'minitest', '~> 5.3' | |
require 'minitest/autorun' | |
require 'minitest/pride' | |
require_relative '../lib/bottles' | |
class BottlesTest < Minitest::Test | |
attr_reader :bottles | |
def setup | |
@bottles = ::Bottles.new | |
end | |
def test_the_first_verse | |
expected = <<-VERSE | |
99 bottles of beer on the wall, 99 bottles of beer. | |
Take one down and pass it around, 98 bottles of beer on the wall. | |
VERSE | |
assert_equal expected, bottles.verse(99) | |
end | |
def test_another_verse | |
expected = <<-VERSE | |
89 bottles of beer on the wall, 89 bottles of beer. | |
Take one down and pass it around, 88 bottles of beer on the wall. | |
VERSE | |
assert_equal expected, bottles.verse(89) | |
end | |
def test_verse_2 | |
expected = <<-VERSE | |
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. | |
VERSE | |
assert_equal expected, bottles.verse(2) | |
end | |
def test_verse_1 | |
expected = <<-VERSE | |
1 bottle of beer on the wall, 1 bottle of beer. | |
Take it down and pass it around, no more bottles of beer on the wall. | |
VERSE | |
assert_equal expected, bottles.verse(1) | |
end | |
def test_verse_0 | |
expected = <<-VERSE | |
No more bottles of beer on the wall, no more bottles of beer. | |
Go to the store and buy some more, 99 bottles of beer on the wall. | |
VERSE | |
assert_equal expected, bottles.verse(0) | |
end | |
def test_a_couple_verses | |
expected = <<-VERSES | |
99 bottles of beer on the wall, 99 bottles of beer. | |
Take one down and pass it around, 98 bottles of beer on the wall. | |
98 bottles of beer on the wall, 98 bottles of beer. | |
Take one down and pass it around, 97 bottles of beer on the wall. | |
VERSES | |
assert_equal expected, bottles.verses(99, 98) | |
end | |
def test_a_few_verses | |
expected = <<-VERSES | |
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. | |
Take it down and pass it around, no more bottles of beer on the wall. | |
No more bottles of beer on the wall, no more bottles of beer. | |
Go to the store and buy some more, 99 bottles of beer on the wall. | |
VERSES | |
assert_equal expected, bottles.verses(2, 0) | |
end | |
def test_the_whole_song | |
assert_equal bottles.verses(99, 0), bottles.song | |
end | |
def test_7 | |
expected = <<-VERSE | |
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. | |
VERSE | |
assert_equal expected, bottles.verse(7) | |
end | |
def test_6 | |
expected = <<-VERSE | |
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. | |
VERSE | |
assert_equal expected, bottles.verse(6) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment