Skip to content

Instantly share code, notes, and snippets.

@jackphelps
Forked from jimweirich/Rakefile
Last active August 29, 2015 14:22
Show Gist options
  • Save jackphelps/4d08939030f21fb899b5 to your computer and use it in GitHub Desktop.
Save jackphelps/4d08939030f21fb899b5 to your computer and use it in GitHub Desktop.
require 'rspec/given'
require 'beer_song'
module Beer
V99 =
"99 bottles of beer on the wall, 99 bottles of beer.\n" +
"Take one down and pass it around, 98 bottles of beer on the wall.\n"
V98 =
"98 bottles of beer on the wall, 98 bottles of beer.\n" +
"Take one down and pass it around, 97 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"
describe Number do
Given(:bn) { Number.new(n) }
Invariant { bn.pred == Number.new((n+99) % 100) }
context "with 99" do
Given(:n) { 99 }
Then { bn.to_s == '99' }
Then { bn.verse == V99 }
end
context "with 98" do
Given(:n) { 98 }
Then { bn.to_s == '98' }
Then { bn.verse == V98 }
end
context "with 2" do
Given(:n) { 2 }
Then { bn.to_s == '2' }
Then { bn.verse == V2 }
end
context "with 1" do
Given(:n) { 1 }
Then { bn.to_s == '1' }
Then { bn.verse == V1 }
end
context "with 0" do
Given(:n) { 0 }
Then { bn.to_s == 'no more' }
Then { bn.verse == V0 }
end
end
describe Song do
Given(:bottle) { Song.new }
def count(string, pattern)
string.scan(Regexp.new(Regexp.quote(pattern))).size
end
context "one verse" do
When(:result) { bottle.verse(2) }
Then { result == V2 }
end
context "entire song" do
When(:result) { bottle.verses(99) }
Then { count(result, V99 + "\n") == 1 }
Then { count(result, V98 + "\n") == 1 }
Then { count(result, V2 + "\n") == 1 }
Then { count(result, V1 + "\n") == 1 }
Then { count(result, V0 + "\n") == 1 }
end
end
end
require 'minitest/autorun'
require 'minitest/pride'
require './beer_song'
class BeerSongTest < Minitest::Test
def beer_song
@beer_song = Beer::Song.new
end
def teardown
@beer_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, beer_song.verse(8)
end
def test_another_typical_verse
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, beer_song.verse(3)
end
def test_verse_1
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, beer_song.verse(1)
end
def test_verse_2
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, beer_song.verse(2)
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, 99 bottles of beer on the wall.\n"
assert_equal expected, beer_song.verse(0)
end
def test_several_verses
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, beer_song.verses(8, 6)
end
def test_all_the_rest_of_the_verses
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" +
"\n" +
"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" +
"\n" +
"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" +
"\n" +
"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" +
"\n"
assert_equal expected, beer_song.verses(3)
end
end
#!/usr/bin/env ruby
require 'rake/clean'
require 'rake/testtask'
task :default => [:spec, :test]
task :spec do
sh "rspec ."
end
task :test do
ruby "beer_song_test.rb"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment