Skip to content

Commit 68065c6

Browse files
author
Katrina Owen
committed
Implement generator for beer-song
1 parent 40e35ed commit 68065c6

File tree

5 files changed

+129
-50
lines changed

5 files changed

+129
-50
lines changed

exercises/beer-song/.meta/.version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3

exercises/beer-song/beer_song_test.rb

Lines changed: 61 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,77 @@
11
#!/usr/bin/env ruby
2+
# encoding: utf-8
23
gem 'minitest', '>= 5.0.0'
34
require 'minitest/autorun'
45
require_relative 'beer_song'
56

7+
# Common test data version: e699ab6
68
class BeerSongTest < Minitest::Test
7-
def test_the_first_verse
8-
expected = "99 bottles of beer on the wall, 99 bottles of beer.\n" \
9-
"Take one down and pass it around, 98 bottles of beer on the wall.\n"
9+
def test_first_verse
10+
# skip
11+
expected = <<-TEXT
12+
99 bottles of beer on the wall, 99 bottles of beer.
13+
Take one down and pass it around, 98 bottles of beer on the wall.
14+
TEXT
1015
assert_equal expected, BeerSong.new.verse(99)
1116
end
1217

13-
def test_another_verse
18+
def test_middle_verse
1419
skip
15-
expected = "3 bottles of beer on the wall, 3 bottles of beer.\n" \
16-
"Take one down and pass it around, 2 bottles of beer on the wall.\n"
17-
assert_equal expected, BeerSong.new.verse(3)
20+
expected = <<-TEXT
21+
44 bottles of beer on the wall, 44 bottles of beer.
22+
Take one down and pass it around, 43 bottles of beer on the wall.
23+
TEXT
24+
assert_equal expected, BeerSong.new.verse(44)
1825
end
1926

20-
def test_verse_2
27+
def test_third_to_last_verse
2128
skip
22-
expected = "2 bottles of beer on the wall, 2 bottles of beer.\n" \
23-
"Take one down and pass it around, 1 bottle of beer on the wall.\n"
29+
expected = <<-TEXT
30+
2 bottles of beer on the wall, 2 bottles of beer.
31+
Take one down and pass it around, 1 bottle of beer on the wall.
32+
TEXT
2433
assert_equal expected, BeerSong.new.verse(2)
2534
end
2635

27-
def test_verse_1
36+
def test_penultimate_verse
2837
skip
29-
expected = "1 bottle of beer on the wall, 1 bottle of beer.\n" \
30-
"Take it down and pass it around, no more bottles of beer on the wall.\n"
38+
expected = <<-TEXT
39+
1 bottle of beer on the wall, 1 bottle of beer.
40+
Take it down and pass it around, no more bottles of beer on the wall.
41+
TEXT
3142
assert_equal expected, BeerSong.new.verse(1)
3243
end
3344

34-
def test_verse_0
45+
def test_last_verse
3546
skip
36-
expected = "No more bottles of beer on the wall, no more bottles of beer.\n" \
37-
"Go to the store and buy some more, 99 bottles of beer on the wall.\n"
47+
expected = <<-TEXT
48+
No more bottles of beer on the wall, no more bottles of beer.
49+
Go to the store and buy some more, 99 bottles of beer on the wall.
50+
TEXT
3851
assert_equal expected, BeerSong.new.verse(0)
3952
end
4053

41-
def test_a_couple_verses
54+
def test_last_4_verses
4255
skip
43-
expected = "99 bottles of beer on the wall, 99 bottles of beer.\n" \
44-
"Take one down and pass it around, 98 bottles of beer on the wall.\n" \
45-
"\n" \
46-
"98 bottles of beer on the wall, 98 bottles of beer.\n" \
47-
"Take one down and pass it around, 97 bottles of beer on the wall.\n"
48-
assert_equal expected, BeerSong.new.verses(99, 98)
49-
end
56+
expected = <<-TEXT
57+
3 bottles of beer on the wall, 3 bottles of beer.
58+
Take one down and pass it around, 2 bottles of beer on the wall.
5059
51-
def test_a_few_verses
52-
skip
53-
expected = "2 bottles of beer on the wall, 2 bottles of beer.\n" \
54-
"Take one down and pass it around, 1 bottle of beer on the wall.\n" \
55-
"\n" \
56-
"1 bottle of beer on the wall, 1 bottle of beer.\n" \
57-
"Take it down and pass it around, no more bottles of beer on the wall.\n" \
58-
"\n" \
59-
"No more bottles of beer on the wall, no more bottles of beer.\n" \
60-
"Go to the store and buy some more, 99 bottles of beer on the wall.\n"
61-
assert_equal expected, BeerSong.new.verses(2, 0)
60+
2 bottles of beer on the wall, 2 bottles of beer.
61+
Take one down and pass it around, 1 bottle of beer on the wall.
62+
63+
1 bottle of beer on the wall, 1 bottle of beer.
64+
Take it down and pass it around, no more bottles of beer on the wall.
65+
66+
No more bottles of beer on the wall, no more bottles of beer.
67+
Go to the store and buy some more, 99 bottles of beer on the wall.
68+
TEXT
69+
assert_equal expected, BeerSong.new.lyrics(3, 0)
6270
end
6371

64-
def test_the_whole_song
72+
def test_all_verses
6573
skip
66-
expected = <<-SONG
74+
expected = <<-TEXT
6775
99 bottles of beer on the wall, 99 bottles of beer.
6876
Take one down and pass it around, 98 bottles of beer on the wall.
6977
@@ -363,18 +371,27 @@ def test_the_whole_song
363371
364372
No more bottles of beer on the wall, no more bottles of beer.
365373
Go to the store and buy some more, 99 bottles of beer on the wall.
366-
SONG
367-
assert_equal expected, BeerSong.new.lyrics
374+
TEXT
375+
assert_equal expected, BeerSong.new.lyrics(99, 0)
368376
end
369-
370-
# Problems in exercism evolve over time,
371-
# as we find better ways to ask questions.
377+
# Problems in exercism evolve over time, as we find better ways to ask
378+
# questions.
372379
# The version number refers to the version of the problem you solved,
373380
# not your solution.
374381
#
375-
# Define a constant named VERSION inside of BookKeeping.
382+
# Define a constant named VERSION inside of the top level BookKeeping
383+
# module, which may be placed near the end of your file.
384+
#
385+
# In your file, it will look like this:
386+
#
387+
# module BookKeeping
388+
# VERSION = 1 # Where the version number matches the one in the test.
389+
# end
390+
#
391+
# If you are curious, read more about constants on RubyDoc:
392+
# http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html
376393
def test_bookkeeping
377394
skip
378-
assert_equal 2, BookKeeping::VERSION
395+
assert_equal 3, BookKeeping::VERSION
379396
end
380397
end

exercises/beer-song/example.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
module BookKeeping
2-
VERSION = 2
2+
VERSION = 3
33
end
44

55
class BeerSong
6-
def lyrics
7-
verses(99, 0)
8-
end
9-
10-
def verses(upper_bound, lower_bound)
6+
def lyrics(upper_bound, lower_bound)
117
upper_bound.downto(lower_bound).map { |i| verse(i) }.join("\n")
128
end
139

exercises/beer-song/example.tt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env ruby
2+
# encoding: utf-8
3+
gem 'minitest', '>= 5.0.0'
4+
require 'minitest/autorun'
5+
require_relative 'beer_song'
6+
7+
# Common test data version: <%= abbreviated_commit_hash %>
8+
class BeerSongTest < Minitest::Test<% test_cases.each do |test_case| %>
9+
def <%= test_case.test_name %>
10+
<%= test_case.skipped %>
11+
expected = <<-TEXT
12+
<%= test_case.expected %>
13+
TEXT
14+
assert_equal expected, <%= test_case.workload %>
15+
end
16+
<% end %>
17+
<%= IO.read(XRUBY_LIB + '/bookkeeping.md') %>
18+
def test_bookkeeping
19+
skip
20+
assert_equal <%= version %>, BookKeeping::VERSION
21+
end
22+
end

lib/beer_song_cases.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require 'exercise_cases'
2+
3+
class BeerSongCase < OpenStruct
4+
def test_name
5+
'test_%s' % description.tr(' ', '_')
6+
end
7+
8+
def workload
9+
"BeerSong.new.%s(%s)" % [section, workload_arguments]
10+
end
11+
12+
def expected
13+
self["expected"].gsub('\n', '"\n" \\')
14+
end
15+
16+
def skipped
17+
index.zero? ? '# skip' : 'skip'
18+
end
19+
20+
private
21+
22+
def workload_arguments
23+
if section == 'lyrics'
24+
"%s, %s" % [self["beginning"], self["end"]]
25+
else
26+
number
27+
end
28+
end
29+
end
30+
31+
BeerSongCases = proc do |data|
32+
i = 0
33+
json = JSON.parse(data)
34+
cases = []
35+
%w(verse lyrics).each do |section|
36+
json[section]['cases'].each do |row|
37+
row = row.merge(row.merge('index' => i, 'section' => section))
38+
cases << BeerSongCase.new(row)
39+
i += 1
40+
end
41+
end
42+
cases
43+
end

0 commit comments

Comments
 (0)