Skip to content

pig-latin: generate tests using canonical data #563

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 3 commits into from
Apr 21, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/pig-latin/.meta/.version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
4 changes: 4 additions & 0 deletions exercises/pig-latin/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ def parse_initial_consonant_sound_and_remainder
word.scan(/\A([^aeiou]?qu|[^aeiou]+)(.*)/).first
end
end

module BookKeeping
VERSION = 1
end
18 changes: 18 additions & 0 deletions exercises/pig-latin/example.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env ruby
gem 'minitest', '>= 5.0.0'
require 'minitest/autorun'
require_relative 'pig_latin'

# Common test data version: <%= abbreviated_commit_hash %>
class PigLatinTest < Minitest::Test<% test_cases.each do |test_case| %>
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a newline here to split the class definition from the template code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Blech. ERB. Adding a newline here causes formatting problems in the generated test file.

Copy link
Member

Choose a reason for hiding this comment

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

I believe you can use a backslash to avoid that? There is also something in ERB itself that lets you ignore the new line.

Minor thing though though.

The generated file being exemplar is more important.

def <%= test_case.name %>
<%= test_case.skipped %>
<%= test_case.workload %>
end
<% end %>
<%= IO.read(XRUBY_LIB + '/bookkeeping.md') %>
def test_bookkeeping
skip
assert_equal <%= version %>, BookKeeping::VERSION
end
end
99 changes: 73 additions & 26 deletions exercises/pig-latin/pig_latin_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,78 +3,125 @@
require 'minitest/autorun'
require_relative 'pig_latin'

# Common test data version: d4e9a22
class PigLatinTest < Minitest::Test
def test_word_beginning_with_a
assert_equal 'appleay', PigLatin.translate('apple')
# skip
assert_equal "appleay", PigLatin.translate("apple")
end

def test_other_word_beginning_e
def test_word_beginning_with_e
skip
assert_equal 'earay', PigLatin.translate('ear')
assert_equal "earay", PigLatin.translate("ear")
end

def test_word_beginning_with_i
skip
assert_equal "iglooay", PigLatin.translate("igloo")
end

def test_word_beginning_with_o
skip
assert_equal "objectay", PigLatin.translate("object")
end

def test_word_beginning_with_u
skip
assert_equal "underay", PigLatin.translate("under")
end

def test_word_beginning_with_a_vowel_and_followed_by_a_qu
skip
assert_equal "equalay", PigLatin.translate("equal")
end

def test_word_beginning_with_p
skip
assert_equal 'igpay', PigLatin.translate('pig')
assert_equal "igpay", PigLatin.translate("pig")
end

def test_word_beginning_with_k
skip
assert_equal 'oalakay', PigLatin.translate('koala')
assert_equal "oalakay", PigLatin.translate("koala")
end

def test_word_beginning_with_ch
def test_word_beginning_with_y
skip
assert_equal 'airchay', PigLatin.translate('chair')
assert_equal "ellowyay", PigLatin.translate("yellow")
end

def test_word_beginning_with_qu
def test_word_beginning_with_x
skip
assert_equal 'eenquay', PigLatin.translate('queen')
assert_equal "enonxay", PigLatin.translate("xenon")
end

def test_word_with_consonant_preceding_qu
def test_word_beginning_with_q_without_a_following_u
skip
assert_equal 'aresquay', PigLatin.translate('square')
assert_equal "atqay", PigLatin.translate("qat")
end

def test_word_beginning_with_th
def test_word_beginning_with_ch
skip
assert_equal 'erapythay', PigLatin.translate('therapy')
assert_equal "airchay", PigLatin.translate("chair")
end

def test_word_beginning_with_thr
def test_word_beginning_with_qu
skip
assert_equal 'ushthray', PigLatin.translate('thrush')
assert_equal "eenquay", PigLatin.translate("queen")
end

def test_word_beginning_with_sch
def test_word_beginning_with_qu_and_a_preceding_consonant
skip
assert_equal 'oolschay', PigLatin.translate('school')
assert_equal "aresquay", PigLatin.translate("square")
end

def test_translates_phrase
def test_word_beginning_with_th
skip
assert_equal 'ickquay astfay unray', PigLatin.translate('quick fast run')
assert_equal "erapythay", PigLatin.translate("therapy")
end

def test_word_beginning_with_ye
def test_word_beginning_with_thr
skip
assert_equal 'ellowyay', PigLatin.translate('yellow')
assert_equal "ushthray", PigLatin.translate("thrush")
end

def test_word_beginning_with_yt
def test_word_beginning_with_sch
skip
assert_equal 'yttriaay', PigLatin.translate('yttria')
assert_equal "oolschay", PigLatin.translate("school")
end

def test_word_beginning_with_xe
def test_word_beginning_with_yt
skip
assert_equal 'enonxay', PigLatin.translate('xenon')
assert_equal "yttriaay", PigLatin.translate("yttria")
end

def test_word_beginning_with_xr
skip
assert_equal 'xrayay', PigLatin.translate('xray')
assert_equal "xrayay", PigLatin.translate("xray")
end

def test_a_whole_phrase
skip
assert_equal "ickquay astfay unray", PigLatin.translate("quick fast run")
end
# 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 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 1, BookKeeping::VERSION
end
end
22 changes: 22 additions & 0 deletions lib/pig_latin_cases.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'exercise_cases'

class PigLatinCase < OpenStruct
def name
'test_%s' % description.tr('- ', '__')
end

def workload
%Q(assert_equal #{expected.inspect}, PigLatin.translate(#{input.inspect}))
end

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

PigLatinCases = proc do |data|
JSON.parse(data)['cases'].
flat_map {|section| section['cases'] }.
each_with_index.
reduce([]) {|cases, (test, i)| cases << PigLatinCase.new(test.merge('index' => i)) }
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a bit of a mega code pipeline.
I wonder is it possible to add some intention revealing variable names into the mix.

end