Skip to content

Instantly share code, notes, and snippets.

@rab
Forked from jimweirich/beer_song_test.rb
Last active December 28, 2015 19:59
Show Gist options
  • Save rab/7554237 to your computer and use it in GitHub Desktop.
Save rab/7554237 to your computer and use it in GitHub Desktop.
module Beer
class Song
VERSE_TEMPLATE = "%d bottles of beer on the wall, %d bottles of beer.\n" +
"Take one down and pass it around, %d bottle%s of beer on the wall.\n"
PENULTIMATE_VERSE = "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"
FINAL_VERSE = "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"
def verses(starting, ending=0)
starting.downto(ending).map {|number| verse(number) }.join("\n") + "\n"
end
def verse(number)
case number
when 1
PENULTIMATE_VERSE
when 0
FINAL_VERSE
else
VERSE_TEMPLATE%[number, number, number - 1, number == 2 ? '' : 's' ]
end
end
end
end
gem 'minitest'
require 'minitest/autorun'
require './beer_song'
class BeerSongTest < Minitest::Test
V8 =
"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"
V7 =
"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"
V6 =
"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"
V3 =
"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"
V2 =
"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"
V1 =
"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"
V0 =
"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"
def combine_verses(*verses)
verses.map { |v| "#{v}\n" }.join
end
def beer_song
@beer_song = Beer::Song.new
end
def teardown
@beer_song = nil
end
# Tests ...
def test_a_typical_verse
assert_equal V8, beer_song.verse(8)
end
def test_another_typical_verse
assert_equal V3, beer_song.verse(3)
end
def test_verse_1
assert_equal V1, beer_song.verse(1)
end
def test_verse_2
assert_equal V2, beer_song.verse(2)
end
def test_verse_0
assert_equal V0, beer_song.verse(0)
end
def test_several_verses
expected = combine_verses(V8, V7, V6)
assert_equal expected, beer_song.verses(8, 6)
end
def test_all_the_rest_of_the_verses
expected = combine_verses(V3, V2, V1, V0)
assert_equal expected, beer_song.verses(3)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment