Skip to content

Instantly share code, notes, and snippets.

@revdan
Last active August 29, 2015 14:14
Show Gist options
  • Save revdan/08f4a4a6ab5bbc844093 to your computer and use it in GitHub Desktop.
Save revdan/08f4a4a6ab5bbc844093 to your computer and use it in GitHub Desktop.
exercism.io BeerSong
class BeerSong
attr_reader :number
def verse(number)
@number = number
take_it_away_guys!
end
def verses(first, last)
first.downto(last).map { |x| verse x }.join("\n") << "\n"
end
def sing
verses(99, 0)
end
private
def take_it_away_guys!
"#{number_of_bottles.capitalize} of beer on the wall, " \
"#{number_of_bottles} of beer.\n" \
"#{take_whatever_down or go_to_the_store}, " \
"#{one_fewer_bottles} of beer on the wall.\n"
end
def number_of_bottles(override=nil)
case override or number
when 0 then "no more bottles"
when 1 then "1 bottle"
else "#{override or number} bottles"
end
end
def take_whatever_down
"Take #{number == 1 ? "it" : "one"} down and pass it around" unless number.zero?
end
def go_to_the_store
"Go to the store and buy some more" # but don't drive
end
def one_fewer_bottles
number_of_bottles (number - 1) % 100 # thanks @thomvil :)
end
end
require 'minitest/autorun'
require_relative 'beer_song'
class BeerSongTest < MiniTest::Unit::TestCase
def song
@song = ::BeerSong.new
end
def teardown
@song = nil
end
def test_a_typical_verse
expected = "8 bottles of beer on the wall, 8 bottles of beer.\n" \
"Take one down and pass it around, 7 bottles of beer on the wall.\n"
assert_equal expected, song.verse(8)
end
def test_another_typical_verse
skip
expected = "3 bottles of beer on the wall, 3 bottles of beer.\n" \
"Take one down and pass it around, 2 bottles of beer on the wall.\n"
assert_equal expected, song.verse(3)
end
def test_verse_1
skip
expected = "1 bottle of beer on the wall, 1 bottle of beer.\n" \
"Take it down and pass it around, no more bottles of beer on the wall.\n"
assert_equal expected, song.verse(1)
end
def test_verse_2
skip
expected = "2 bottles of beer on the wall, 2 bottles of beer.\n" \
"Take one down and pass it around, 1 bottle of beer on the wall.\n"
assert_equal expected, song.verse(2)
end
def test_verse_0
skip
expected =
"No more bottles of beer on the wall, no more bottles of beer.\n" \
"Go to the store and buy some more, 99 bottles of beer on the wall.\n"
assert_equal expected, song.verse(0)
end
def test_several_verses
skip
expected = "8 bottles of beer on the wall, 8 bottles of beer.\n" \
"Take one down and pass it around, 7 bottles of beer on the wall.\n\n" \
"7 bottles of beer on the wall, 7 bottles of beer.\n" \
"Take one down and pass it around, 6 bottles of beer on the wall.\n\n" \
"6 bottles of beer on the wall, 6 bottles of beer.\n" \
"Take one down and pass it around, 5 bottles of beer on the wall.\n\n"
assert_equal expected, song.verses(8, 6)
end
def test_the_whole_song
skip
assert_equal song.verses(99, 0), song.sing
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment