From 34df08fe35361318a23c548dc12076bc1f1f08d3 Mon Sep 17 00:00:00 2001 From: Geoff Hubbard Date: Sun, 6 Nov 2016 19:29:49 +0000 Subject: [PATCH] Pangram: Add failure messages to tests Also: Refactored generator. Renamed generator methods to match common interface. Updated tests to match the xcommon canonical-data.json Renamed method `pangram?` to match Ruby style. --- exercises/pangram/.version | 2 +- exercises/pangram/example.rb | 4 +-- exercises/pangram/example.tt | 12 +++---- exercises/pangram/pangram_test.rb | 55 +++++++++++++++++-------------- lib/pangram_cases.rb | 23 ++++++++++--- 5 files changed, 57 insertions(+), 39 deletions(-) diff --git a/exercises/pangram/.version b/exercises/pangram/.version index d8263ee986..e440e5c842 100644 --- a/exercises/pangram/.version +++ b/exercises/pangram/.version @@ -1 +1 @@ -2 \ No newline at end of file +3 \ No newline at end of file diff --git a/exercises/pangram/example.rb b/exercises/pangram/example.rb index c8ce0560ae..154efb89c6 100644 --- a/exercises/pangram/example.rb +++ b/exercises/pangram/example.rb @@ -1,9 +1,9 @@ module BookKeeping - VERSION = 2 + VERSION = 3 end class Pangram - def self.is_pangram?(str) + def self.pangram?(str) downcased_str = str.downcase ('a'..'z').all? { |letter| downcased_str.include?(letter) } end diff --git a/exercises/pangram/example.tt b/exercises/pangram/example.tt index cd4b8cd4df..8bd214e729 100644 --- a/exercises/pangram/example.tt +++ b/exercises/pangram/example.tt @@ -4,16 +4,14 @@ gem 'minitest', '>= 5.0.0' require 'minitest/autorun' require_relative 'pangram' -# Test data version: -# <%= sha1 %> +# Test data version: # <%= sha1 %> class PangramTest < Minitest::Test<% test_cases.each do |test_case| %> - def <%= test_case.name %><% if test_case.skipped? %> - skip<% end %> - str = '<%= test_case.input %>'<% if test_case.expected %> - assert<% else %> - refute<% end %> <%= test_case.do %> + def <%= test_case.name %> + <%= test_case.skipped? %> + <%= test_case.workload %> end <% end %> + <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> def test_bookkeeping skip diff --git a/exercises/pangram/pangram_test.rb b/exercises/pangram/pangram_test.rb index 396d0eb20a..b26f29330c 100755 --- a/exercises/pangram/pangram_test.rb +++ b/exercises/pangram/pangram_test.rb @@ -4,61 +4,66 @@ require 'minitest/autorun' require_relative 'pangram' -# Test data version: -# deb225e Implement canonical dataset for scrabble-score problem (#255) - +# Test data version: # 2adfe21 class PangramTest < Minitest::Test def test_sentence_empty - str = '' - refute Pangram.is_pangram?(str) + # skip + phrase = '' + refute Pangram.pangram?(phrase), "#{phrase.inspect} is NOT a pangram" end def test_pangram_with_only_lower_case skip - str = 'the quick brown fox jumps over the lazy dog' - assert Pangram.is_pangram?(str) + phrase = 'the quick brown fox jumps over the lazy dog' + assert Pangram.pangram?(phrase), "#{phrase.inspect} IS a pangram" end def test_missing_character_x skip - str = 'a quick movement of the enemy will jeopardize five gunboats' - refute Pangram.is_pangram?(str) + phrase = 'a quick movement of the enemy will jeopardize five gunboats' + refute Pangram.pangram?(phrase), "#{phrase.inspect} is NOT a pangram" end def test_another_missing_character_x skip - str = 'the quick brown fish jumps over the lazy dog' - refute Pangram.is_pangram?(str) + phrase = 'the quick brown fish jumps over the lazy dog' + refute Pangram.pangram?(phrase), "#{phrase.inspect} is NOT a pangram" end def test_pangram_with_underscores skip - str = 'the_quick_brown_fox_jumps_over_the_lazy_dog' - assert Pangram.is_pangram?(str) + phrase = 'the_quick_brown_fox_jumps_over_the_lazy_dog' + assert Pangram.pangram?(phrase), "#{phrase.inspect} IS a pangram" end def test_pangram_with_numbers skip - str = 'the 1 quick brown fox jumps over the 2 lazy dogs' - assert Pangram.is_pangram?(str) + phrase = 'the 1 quick brown fox jumps over the 2 lazy dogs' + assert Pangram.pangram?(phrase), "#{phrase.inspect} IS a pangram" end def test_missing_letters_replaced_by_numbers skip - str = '7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog' - refute Pangram.is_pangram?(str) + phrase = '7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog' + refute Pangram.pangram?(phrase), "#{phrase.inspect} is NOT a pangram" end def test_pangram_with_mixed_case_and_punctuation skip - str = '"Five quacking Zephyrs jolt my wax bed."' - assert Pangram.is_pangram?(str) + phrase = '"Five quacking Zephyrs jolt my wax bed."' + assert Pangram.pangram?(phrase), "#{phrase.inspect} IS a pangram" end def test_pangram_with_non_ascii_characters skip - str = 'Victor jagt zwölf Boxkämpfer quer über den großen Sylter Deich.' - assert Pangram.is_pangram?(str) + phrase = 'Victor jagt zwölf Boxkämpfer quer über den großen Sylter Deich.' + assert Pangram.pangram?(phrase), "#{phrase.inspect} IS a pangram" + end + + def test_panagram_in_alphabet_other_than_ascii + skip + phrase = 'Широкая электрификация южных губерний даст мощный толчок подъёму сельского хозяйства.' + refute Pangram.pangram?(phrase), "#{phrase.inspect} is NOT a pangram" end # Problems in exercism evolve over time, as we find better ways to ask @@ -67,8 +72,9 @@ def test_pangram_with_non_ascii_characters # not your solution. # # Define a constant named VERSION inside of the top level BookKeeping - # module. - # In your file, it will look like this: + # 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. @@ -76,9 +82,8 @@ def test_pangram_with_non_ascii_characters # # 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 diff --git a/lib/pangram_cases.rb b/lib/pangram_cases.rb index 4e6ebd7549..38ef126b8e 100644 --- a/lib/pangram_cases.rb +++ b/lib/pangram_cases.rb @@ -1,14 +1,29 @@ class PangramCase < OpenStruct def name - 'test_%s' % description.delete("'").gsub(/[ -]/, '_') + 'test_%s' % description.downcase.tr_s(" -'", '_').sub(/_$/,'') end - def do - 'Pangram.is_pangram?(str)' + def workload + [ + "phrase = '#{input}'\n", + " #{assertion} Pangram.pangram?(phrase), \"#{message}\"" + ].join + end + + def message + "\#{phrase.inspect} #{is_or_isnt} a pangram" + end + + def is_or_isnt + expected ? 'IS' : 'is NOT' + end + + def assertion + expected ? 'assert' : 'refute' end def skipped? - index > 0 + index.zero? ? '# skip' : 'skip' end end