Skip to content

Implement generator for beer-song #553

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

Merged
merged 1 commit into from
Mar 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions exercises/beer-song/.meta/.version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3
98 changes: 62 additions & 36 deletions exercises/beer-song/beer_song_test.rb
Original file line number Diff line number Diff line change
@@ -1,69 +1,86 @@
#!/usr/bin/env ruby
# encoding: utf-8
gem 'minitest', '>= 5.0.0'
require 'minitest/autorun'
require_relative 'beer_song'

# Common test data version: 9f3d48a
class BeerSongTest < Minitest::Test
def test_the_first_verse
expected = "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"
def test_first_generic_verse
# skip
expected = <<-TEXT
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the <<-TEXT means that we can have the ending delimiter indented.

At the moment the ending delimiter is on the 1 column, so this is not needed. See comment regarding the generator for note.

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.
TEXT
assert_equal expected, BeerSong.new.verse(99)
end

def test_another_verse
def test_last_generic_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"
expected = <<-TEXT
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.
TEXT
assert_equal expected, BeerSong.new.verse(3)
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"
expected = <<-TEXT
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.
TEXT
assert_equal expected, BeerSong.new.verse(2)
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"
expected = <<-TEXT
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.
TEXT
assert_equal expected, BeerSong.new.verse(1)
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"
expected = <<-TEXT
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.
TEXT
assert_equal expected, BeerSong.new.verse(0)
end

def test_a_couple_verses
def test_first_two_verses
skip
expected = "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" \
"\n" \
"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"
expected = <<-TEXT
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.
TEXT
assert_equal expected, BeerSong.new.verses(99, 98)
end

def test_a_few_verses
def test_last_three_verses
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" \
"\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"
expected = <<-TEXT
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.
TEXT
assert_equal expected, BeerSong.new.verses(2, 0)
end

def test_the_whole_song
def test_all_verses
skip
expected = <<-SONG
expected = <<-TEXT
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.

Expand Down Expand Up @@ -363,18 +380,27 @@ def test_the_whole_song

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.
SONG
assert_equal expected, BeerSong.new.lyrics
TEXT
assert_equal expected, BeerSong.new.verses(99, 0)
end

# Problems in exercism evolve over time,
# as we find better ways to ask questions.
# Problems in exercism evolve over time, as we find better ways to ask
# questions.
# The version number refers to the version of the problem you solved,
# not your solution.
#
# Define a constant named VERSION inside of BookKeeping.
# Define a constant named VERSION inside of the top level BookKeeping
# module, which may be placed near the end of your file.
#
# In your file, it will look like this:
#
# module BookKeeping
# VERSION = 1 # Where the version number matches the one in the test.
# end
#
# If you are curious, read more about constants on RubyDoc:
# http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html
def test_bookkeeping
skip
assert_equal 2, BookKeeping::VERSION
assert_equal 3, BookKeeping::VERSION
end
end
6 changes: 1 addition & 5 deletions exercises/beer-song/example.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
module BookKeeping
VERSION = 2
VERSION = 3
end

class BeerSong
def lyrics
verses(99, 0)
end

def verses(upper_bound, lower_bound)
upper_bound.downto(lower_bound).map { |i| verse(i) }.join("\n")
end
Expand Down
22 changes: 22 additions & 0 deletions exercises/beer-song/example.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env ruby
# encoding: utf-8
gem 'minitest', '>= 5.0.0'
require 'minitest/autorun'
require_relative 'beer_song'

# Common test data version: <%= abbreviated_commit_hash %>
class BeerSongTest < Minitest::Test<% test_cases.each do |test_case| %>
def <%= test_case.test_name %>
<%= test_case.skipped %>
expected = <<-TEXT
<%= test_case.expected %>
TEXT
assert_equal expected, <%= test_case.workload %>
end
<% end %>
<%= IO.read(XRUBY_LIB + '/bookkeeping.md') %>
def test_bookkeeping
skip
assert_equal <%= version %>, BookKeeping::VERSION
end
end
43 changes: 43 additions & 0 deletions lib/beer_song_cases.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'exercise_cases'

class BeerSongCase < OpenStruct
def test_name
'test_%s' % description.tr(' ', '_')
end

def workload
"BeerSong.new.%s(%s)" % [property, workload_arguments]
end

def expected
self["expected"].gsub('\n', '"\n" \\')
end

def skipped
index.zero? ? '# skip' : 'skip'
end

private

def workload_arguments
if property == 'verse'
number
else
"%s, %s" % [self["beginning"], self["end"]]
end
end
end

BeerSongCases = proc do |data|
i = 0
cases = []
JSON.parse(data)["cases"].each do |section|
section["cases"].each do |tests|
tests["cases"].each do |test|
cases << BeerSongCase.new(test.merge('index' => i))
i += 1
end
end
end
cases
end