From 745ffcf0faca1e71e243e7396f82e454cc17f453 Mon Sep 17 00:00:00 2001 From: Bryan Mulvihill Date: Mon, 17 Oct 2016 20:43:53 -0400 Subject: [PATCH 1/3] All generators use common interface --- .ruby-version | 1 + exercises/acronym/example.tt | 6 +++--- exercises/alphametics/example.tt | 4 +--- exercises/binary/example.tt | 2 +- exercises/bracket-push/example.tt | 2 +- exercises/clock/example.tt | 2 +- exercises/connect/example.tt | 7 +++--- exercises/custom-set/example.tt | 4 ++-- exercises/gigasecond/example.tt | 23 +++++++++----------- exercises/hello-world/example.tt | 6 +++--- exercises/largest-series-product/example.tt | 8 +++---- exercises/leap/example.tt | 8 +++---- exercises/nth-prime/example.tt | 7 +++--- exercises/pangram/example.tt | 8 +++---- exercises/raindrops/example.tt | 6 +++--- exercises/rna-transcription/example.tt | 2 +- exercises/roman-numerals/example.tt | 6 +++--- exercises/run-length-encoding/example.tt | 8 +++---- exercises/two-bucket/example.tt | 6 +++--- lib/acronym_cases.rb | 10 ++++----- lib/alphametics_cases.rb | 2 +- lib/binary_cases.rb | 10 ++++----- lib/bracket_push_cases.rb | 10 ++++----- lib/clock_cases.rb | 12 +++++------ lib/connect_cases.rb | 17 +++++++-------- lib/custom_set_cases.rb | 12 +++++------ lib/difference_of_squares_cases.rb | 8 +++---- lib/gigasecond_cases.rb | 14 +++++++----- lib/hamming_cases.rb | 11 +++++----- lib/hello_world_cases.rb | 14 ++++++++---- lib/largest_series_product_cases.rb | 12 +++++------ lib/leap_cases.rb | 8 +++---- lib/nth_prime_cases.rb | 19 ++++++++++------ lib/pangram_cases.rb | 14 +++++++----- lib/raindrops_cases.rb | 10 ++++----- lib/rna_transcription_cases.rb | 2 +- lib/roman_numerals_cases.rb | 10 ++++----- lib/run_length_encoding_cases.rb | 24 +++++++-------------- lib/two_bucket_cases.rb | 6 +++--- 39 files changed, 171 insertions(+), 170 deletions(-) create mode 100644 .ruby-version diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000000..276cbf9e28 --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +2.3.0 diff --git a/exercises/acronym/example.tt b/exercises/acronym/example.tt index 69cc770a31..ea5f5dbdbd 100644 --- a/exercises/acronym/example.tt +++ b/exercises/acronym/example.tt @@ -6,9 +6,9 @@ require_relative 'acronym' # Test data version: # <%= sha1 %> class AcronymTest < Minitest::Test<% test_cases.each do |test_case| %> - def <%= test_case.name %><% if test_case.skipped? %> - skip<% end %> - assert_equal '<%= test_case.expected %>', <%= test_case.assertion %> + def <%= test_case.name %> + <%= test_case.skipped %> + <%= test_case.work_load %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/alphametics/example.tt b/exercises/alphametics/example.tt index 03d126c39f..d7829fae29 100644 --- a/exercises/alphametics/example.tt +++ b/exercises/alphametics/example.tt @@ -8,9 +8,7 @@ require_relative 'alphametics' class AlphameticsTest < Minitest::Test<% test_cases.each do |test_case| %> def <%= test_case.test_name %> <%= test_case.skipped %> - expect = <%= test_case.expect %> - actual = <%= test_case.work_load %> - assert_equal(expect, actual) + <%= test_case.work_load %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/binary/example.tt b/exercises/binary/example.tt index 4cab53b528..bc6046bbb0 100644 --- a/exercises/binary/example.tt +++ b/exercises/binary/example.tt @@ -9,7 +9,7 @@ require_relative 'binary' class BinaryTest < Minitest::Test<% test_cases.each do |test_case| %> def <%= test_case.name %> <%= test_case.skipped %> - <%= test_case.assertion %> + <%= test_case.work_load %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/bracket-push/example.tt b/exercises/bracket-push/example.tt index f70ffa6281..0f6a2cadda 100644 --- a/exercises/bracket-push/example.tt +++ b/exercises/bracket-push/example.tt @@ -8,7 +8,7 @@ require_relative 'bracket_push' class BracketsTest < Minitest::Test<% test_cases.each do |test_case| %> def <%= test_case.name %> <%= test_case.skipped %> - <%= test_case.test_body %> + <%= test_case.work_load %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/clock/example.tt b/exercises/clock/example.tt index ad39bc071e..256c6210a7 100644 --- a/exercises/clock/example.tt +++ b/exercises/clock/example.tt @@ -8,7 +8,7 @@ require_relative 'clock' class ClockTest < Minitest::Test<% test_cases.each do |test_case| %> def <%= test_case.name %> <%= test_case.skipped %> - <%= test_case.test_body %> + <%= test_case.work_load %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/connect/example.tt b/exercises/connect/example.tt index f8ba5db495..9076d599d9 100644 --- a/exercises/connect/example.tt +++ b/exercises/connect/example.tt @@ -4,10 +4,11 @@ require 'minitest/autorun' require_relative 'connect' # Test data version commit id: <%= sha1 %> -class ConnectTest < Minitest::Test<% test_cases.each do |test_case| %> +class ConnectTest < Minitest::Test +<% test_cases.each do |test_case| %> <%= test_case.ignore_method_length%>def <%= test_case.name %> - <%= test_case.skipped %><% test_case.test_body.each do |line| %> - <%= line %><% end %> + <%= test_case.skipped %> + <%= test_case.work_load.strip %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/custom-set/example.tt b/exercises/custom-set/example.tt index 2c2e3e6065..12ed763b33 100644 --- a/exercises/custom-set/example.tt +++ b/exercises/custom-set/example.tt @@ -6,9 +6,9 @@ require_relative 'custom_set' # Test data version: # <%= sha1 %> class CustomSetTest < Minitest::Test<% test_cases.each do |test_case| %> - def <%= test_case.name %> + def <%= test_case.test_name %> <%= test_case.skipped %> - <%= test_case.test_body %> + <%= test_case.work_load %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/gigasecond/example.tt b/exercises/gigasecond/example.tt index 86ebd8c89d..81f5258074 100644 --- a/exercises/gigasecond/example.tt +++ b/exercises/gigasecond/example.tt @@ -1,22 +1,19 @@ +#!/usr/bin/env ruby +gem 'minitest', '>= 5.0.0' require 'minitest/autorun' require_relative 'gigasecond' -# Test data version: <%= sha1 %> - -class GigasecondTest < Minitest::Test<% test_cases.each do |test_case| %> - def <%= test_case.name %><% if test_case.skipped? %> - skip<% end %> - result = Gigasecond.from(<%= test_case.got %>) - assert_equal <%= test_case.want %>, result +# Test data version commit id: <%= sha1 %> +class GigasecondTest < Minitest::Test +<% test_cases.each do |test_case| %> + def <%= test_case.name %> + <%= test_case.skipped %> + <%= test_case.work_load %> end <% end %> - # Test your 1Gs anniversary - def test_with_your_birthday - skip - end - <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> def test_bookkeeping - assert_equal <%= version.next %>, BookKeeping::VERSION + skip + assert_equal <%= version %>, BookKeeping::VERSION end end diff --git a/exercises/hello-world/example.tt b/exercises/hello-world/example.tt index 2dbfbd31a6..a16b53a3f9 100644 --- a/exercises/hello-world/example.tt +++ b/exercises/hello-world/example.tt @@ -15,9 +15,9 @@ end # Test data version: # <%= sha1 %> class HelloWorldTest < Minitest::Test<% test_cases.each do |test_case| %> - def <%= test_case.test_name %><% if test_case.skipped? %> - skip<% end %> - assert_equal '<%= test_case.expected %>', <%= test_case.do %> + def <%= test_case.test_name %> + <%= test_case.skipped %> + <%= test_case.work_load %> end <% end %>end diff --git a/exercises/largest-series-product/example.tt b/exercises/largest-series-product/example.tt index 4f923f201f..d299e1b8b8 100644 --- a/exercises/largest-series-product/example.tt +++ b/exercises/largest-series-product/example.tt @@ -10,10 +10,10 @@ require_relative 'largest_series_product' # rubocop:disable Metrics/LineLength # class Seriestest < Minitest::Test<% test_cases.each do |test_case| %> - def <%= test_case.name %><% if test_case.skipped? %> - skip<% end %><% if test_case.raises_error? %> - assert_raises(ArgumentError) { <%= test_case.do %> }<% else %> - assert_equal <%= test_case.expected %>, <%= test_case.do %><% end %> + def <%= test_case.test_name %> + <%= test_case.skipped %><% if test_case.raises_error? %> + assert_raises(ArgumentError) { <%= test_case.work_load %> }<% else %> + assert_equal <%= test_case.expected %>, <%= test_case.work_load %><% end %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/leap/example.tt b/exercises/leap/example.tt index b96861e8f5..6e1116f210 100644 --- a/exercises/leap/example.tt +++ b/exercises/leap/example.tt @@ -15,10 +15,10 @@ class Date end class YearTest < Minitest::Test<% test_cases.each do |test_case| %> - def <%= test_case.name %><% if test_case.skipped? %> - skip<% end %><% if test_case.expected%> - assert <%= test_case.do %>, "<%= test_case.failure_message%>"<% else %> - refute <%= test_case.do %>, "<%= test_case.failure_message%>"<% end%> + def <%= test_case.test_name %> + <%= test_case.skipped %><% if test_case.expected%> + assert <%= test_case.work_load %>, "<%= test_case.failure_message%>"<% else %> + refute <%= test_case.work_load %>, "<%= test_case.failure_message%>"<% end%> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/nth-prime/example.tt b/exercises/nth-prime/example.tt index 822d6c5d49..a184c5f066 100644 --- a/exercises/nth-prime/example.tt +++ b/exercises/nth-prime/example.tt @@ -9,10 +9,9 @@ require_relative 'nth_prime' # rubocop:disable Style/NumericLiterals # class NthPrimeTest < Minitest::Test<% test_cases.each do |test_case| %> - def <%= test_case.name %><% if test_case.skipped? %> - skip<% end %><% if test_case.raises_error? %> - assert_raises(ArgumentError) { <%= test_case.actual %> }<% else %> - assert_equal <%= test_case.expected %>, <%= test_case.actual %><% end %> + def <%= test_case.test_name %> + <%= test_case.skipped %> + <%= test_case.work_load %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/pangram/example.tt b/exercises/pangram/example.tt index cd4b8cd4df..27eb24dabb 100644 --- a/exercises/pangram/example.tt +++ b/exercises/pangram/example.tt @@ -7,11 +7,9 @@ require_relative 'pangram' # 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.test_name %> + <%= test_case.skipped %> + <%= test_case.work_load %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/raindrops/example.tt b/exercises/raindrops/example.tt index 8bacf242a9..605e226b92 100644 --- a/exercises/raindrops/example.tt +++ b/exercises/raindrops/example.tt @@ -6,9 +6,9 @@ require_relative 'raindrops' # Test data version: # <%= sha1 %> class RaindropsTest < Minitest::Test<% test_cases.each do |test_case| %> - def <%= test_case.name %><% if test_case.skipped? %> - skip<% end %> - assert_equal '<%= test_case.expected %>', <%= test_case.do %> + def <%= test_case.test_name %> + <%= test_case.skipped %> + <%= test_case.work_load %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/rna-transcription/example.tt b/exercises/rna-transcription/example.tt index 52ab06426b..77cc75f933 100644 --- a/exercises/rna-transcription/example.tt +++ b/exercises/rna-transcription/example.tt @@ -8,7 +8,7 @@ require_relative 'rna_transcription' class ComplementTest < Minitest::Test<% test_cases.each do |test_case| %> def <%= test_case.test_name %> <%= test_case.skipped %> - assert_equal '<%= test_case.expected %>', <%= test_case.work_load %> + <%= test_case.work_load %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/roman-numerals/example.tt b/exercises/roman-numerals/example.tt index c946c6a154..2e9258efbe 100644 --- a/exercises/roman-numerals/example.tt +++ b/exercises/roman-numerals/example.tt @@ -6,9 +6,9 @@ require_relative 'roman_numerals' # Test data version: # <%= sha1 %> class RomanNumeralsTest < Minitest::Test<% test_cases.each do |test_case| %> - def <%= test_case.name %><% if test_case.skipped? %> - skip<% end %> - assert_equal '<%= test_case.expected %>', <%= test_case.do %> + def <%= test_case.test_name %> + <%= test_case.skipped %> + <%= test_case.work_load %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/run-length-encoding/example.tt b/exercises/run-length-encoding/example.tt index e2ec280b1b..6848e371a9 100644 --- a/exercises/run-length-encoding/example.tt +++ b/exercises/run-length-encoding/example.tt @@ -7,11 +7,9 @@ require_relative 'run_length_encoding' # Test data version: # <%= sha1 %> class RunLengthEncodingTest < Minitest::Test<% test_cases.each do |test_case| %> - def <%= test_case.name %><% if test_case.skipped? %> - skip<% end %> - <%= test_case.assign_input %> - <%= test_case.assign_output %> - <%= test_case.assertion %> + def <%= test_case.test_name %> + <%= test_case.skipped %> + <%= test_case.work_load %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/two-bucket/example.tt b/exercises/two-bucket/example.tt index 06c9eb6caf..6b4b619e07 100644 --- a/exercises/two-bucket/example.tt +++ b/exercises/two-bucket/example.tt @@ -6,9 +6,9 @@ require_relative 'two_bucket' # Test data version: # <%= sha1 %> class TwoBucketTest < Minitest::Test<% test_cases.each do |test_case| %> - def <%= test_case.test_name %><% if test_case.skipped? %> - skip<% end %> - <%= test_case.test_body %> + def <%= test_case.test_name %> + <%= test_case.skipped %> + <%= test_case.work_load %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/lib/acronym_cases.rb b/lib/acronym_cases.rb index 67233833f6..ad5460d7ad 100644 --- a/lib/acronym_cases.rb +++ b/lib/acronym_cases.rb @@ -1,14 +1,14 @@ class AcronymCase < OpenStruct - def name + def test_name 'test_%s' % description.tr(' ', '_') end - def assertion - "Acronym.abbreviate('#{phrase}')" + def work_load + "assert_equal #{expected}, Acronym.abbreviate('#{phrase}')" end - def skipped? - index > 0 + def skipped + index.zero? ? '# skip' : 'skip' end end diff --git a/lib/alphametics_cases.rb b/lib/alphametics_cases.rb index 44cc5a664e..68d1d8bbb8 100644 --- a/lib/alphametics_cases.rb +++ b/lib/alphametics_cases.rb @@ -7,7 +7,7 @@ def test_name end def work_load - "Alphametics.new.solve('#{puzzle}')" + "assert_equal(#{expect}, Alphametics.new.solve('#{puzzle}'))" end def expect diff --git a/lib/binary_cases.rb b/lib/binary_cases.rb index 047ed65597..323e765f1d 100644 --- a/lib/binary_cases.rb +++ b/lib/binary_cases.rb @@ -1,9 +1,9 @@ class BinaryCase < OpenStruct - def name + def test_name 'test_%s' % description.gsub(/[ -]/, '_') end - def assertion + def work_load return error_assertion if raises_error? equality_assertion end @@ -15,14 +15,14 @@ def skipped private def error_assertion - "assert_raises(ArgumentError) { #{work_load} }" + "assert_raises(ArgumentError) { #{checked} }" end def equality_assertion - "assert_equal #{expected}, #{work_load}" + "assert_equal #{expected}, #{checked}" end - def work_load + def checked "Binary.new('#{binary}')#{'.to_decimal' unless raises_error?}" end diff --git a/lib/bracket_push_cases.rb b/lib/bracket_push_cases.rb index 61d425bc4f..89db7a0596 100644 --- a/lib/bracket_push_cases.rb +++ b/lib/bracket_push_cases.rb @@ -1,14 +1,14 @@ class BracketPushCase < OpenStruct - def name + def test_name 'test_%s' % description.gsub(/[ -]/, '_') end - def skipped - index.zero? ? '# skip' : 'skip' + def work_load + long_input? ? split_test : simple_test end - def test_body - long_input? ? split_test : simple_test + def skipped + index.zero? ? '# skip' : 'skip' end def long_input? diff --git a/lib/clock_cases.rb b/lib/clock_cases.rb index 5be31944b2..9fe138f2b0 100644 --- a/lib/clock_cases.rb +++ b/lib/clock_cases.rb @@ -1,15 +1,19 @@ class ClockCase < OpenStruct - def name + def test_name 'test_%s' % description .gsub(/[() -]/, '_') .gsub('=', 'is_equal_to') .chomp('_') end - def test_body + def work_load section == 'equal' ? compare_clocks : simple_test end + def skipped + index.zero? ? '# skip' : 'skip' + end + def compare_clocks "clock1 = Clock.at(#{clock1['hour']}, #{clock1['minute']}) clock2 = Clock.at(#{clock2['hour']}, #{clock2['minute']}) @@ -31,10 +35,6 @@ def assert_or_refute def add_to_clock " + #{add}" if add end - - def skipped - index.zero? ? '# skip' : 'skip' - end end ClockCases = proc do |data| diff --git a/lib/connect_cases.rb b/lib/connect_cases.rb index 3219e0c5a4..83c81fc44d 100644 --- a/lib/connect_cases.rb +++ b/lib/connect_cases.rb @@ -1,20 +1,19 @@ class ConnectCase < OpenStruct - def name + def test_name 'test_%s' % description .gsub(/[() -]/, '_') .chomp('_') .downcase end - def test_body - [ - 'board = [', - ' ' + board.map(&method(:single_quote)).join(",\n "), - ']', - 'game = Board.new(board)', - "assert_equal #{single_quote(expected)}, game.winner, " + - single_quote(description) + def work_load + <<-WL +board = [ + #{board.map(&method(:single_quote)).join(",\n ")} ] + game = Board.new(board) + assert_equal #{single_quote(expected)}, game.winner, #{single_quote(description)} + WL end def skipped diff --git a/lib/custom_set_cases.rb b/lib/custom_set_cases.rb index 3bd47c9688..ffb2247d47 100644 --- a/lib/custom_set_cases.rb +++ b/lib/custom_set_cases.rb @@ -1,12 +1,16 @@ class CustomSetCase < OpenStruct - def name + def test_name 'test_%s' % description.gsub(/ |-/, '_') end - def test_body + def work_load send section end + def skipped + index.zero? ? '# skip' : 'skip' + end + def union "set1 = CustomSet.new #{set1} set2 = CustomSet.new #{set2} @@ -66,10 +70,6 @@ def contains def assert_or_refute expected ? 'assert' : 'refute' end - - def skipped - index.zero? ? '# skip' : 'skip' - end end CustomSetCases = proc do |data| diff --git a/lib/difference_of_squares_cases.rb b/lib/difference_of_squares_cases.rb index 4baf7d4cf9..2a2b6a5dfd 100644 --- a/lib/difference_of_squares_cases.rb +++ b/lib/difference_of_squares_cases.rb @@ -1,9 +1,9 @@ class DifferenceOfSquaresCase < OpenStruct - def test_name + def name 'test_%s' % description.gsub(/[ -]/, '_') end - def do + def work_load "Squares.new(#{number}).#{action}" end @@ -12,8 +12,8 @@ def action section end - def skipped? - index > 0 + def skipped + index.zero? ? '# skip' : 'skip' end def expected_formatted diff --git a/lib/gigasecond_cases.rb b/lib/gigasecond_cases.rb index 6fed08e408..30e966071b 100644 --- a/lib/gigasecond_cases.rb +++ b/lib/gigasecond_cases.rb @@ -1,10 +1,18 @@ require 'time' class GigasecondCase < OpenStruct - def name + def test_name 'test_%s' % description.gsub(/[ :-]/, '_') end + def work_load + "assert_equal #{want}, Gigasecond.from(#{got})" + end + + def skipped + index.zero? ? '# skip' : 'skip' + end + def description send(:'#') || input end @@ -26,10 +34,6 @@ def stop_values ts = Time.parse(expected) [ts.year, ts.month, ts.day, ts.hour, ts.min, ts.sec] end - - def skipped? - index > 0 - end end GigasecondCases = proc do |data| diff --git a/lib/hamming_cases.rb b/lib/hamming_cases.rb index edbb3acd8c..b8424ff331 100644 --- a/lib/hamming_cases.rb +++ b/lib/hamming_cases.rb @@ -1,5 +1,5 @@ class HammingCase < OpenStruct - def name + def test_name 'test_%s' % description.gsub(/[ -]/, '_') end @@ -7,13 +7,12 @@ def work_load "Hamming.compute('#{strand1}', '#{strand2}')" end - def raises_error? - expected.to_i == -1 + def skipped + index.zero? ? '# skip' : 'skip' end - def skipped - index.zero? && '# skip' || - 'skip' + def raises_error? + expected.to_i == -1 end end diff --git a/lib/hello_world_cases.rb b/lib/hello_world_cases.rb index d8499d9d9e..55459e72c9 100644 --- a/lib/hello_world_cases.rb +++ b/lib/hello_world_cases.rb @@ -3,12 +3,18 @@ def test_name 'test_%s' % description.gsub(/[ -]/, '_') end - def do - defined?(name) ? "HelloWorld.hello('#{name}')" : 'HelloWorld.hello' + def work_load + assertion = + if defined?(name) + "HelloWorld.hello('#{name}')" + else + 'HelloWorld.hello' + end + "assert_equal '#{expected}', #{assertion}" end - def skipped? - index > 0 + def skipped + index.zero? ? '# skip' : 'skip' end end diff --git a/lib/largest_series_product_cases.rb b/lib/largest_series_product_cases.rb index 6c1ae8c3da..c5ee2594c3 100644 --- a/lib/largest_series_product_cases.rb +++ b/lib/largest_series_product_cases.rb @@ -1,18 +1,18 @@ class LargestSeriesProductCase < OpenStruct - def name + def test_name 'test_%s' % description.tr('()', '').tr(' -', '_').downcase end - def do + def work_load "Series.new('#{digits}').largest_product(#{span})" end - def raises_error? - expected.to_i == -1 + def skipped + index.zero? ? '# skip' : 'skip' end - def skipped? - index > 0 + def raises_error? + expected.to_i == -1 end end diff --git a/lib/leap_cases.rb b/lib/leap_cases.rb index 6857cc4cac..d570bad3dd 100644 --- a/lib/leap_cases.rb +++ b/lib/leap_cases.rb @@ -1,14 +1,14 @@ class LeapCase < OpenStruct - def name + def test_name 'test_%s' % description.downcase.gsub(/[ -]/, '_') end - def do + def work_load "Year.leap?(#{input})" end - def skipped? - index > 0 + def skipped + index.zero? ? '# skip' : 'skip' end def failure_message diff --git a/lib/nth_prime_cases.rb b/lib/nth_prime_cases.rb index bbaa736c30..41ee7cb424 100644 --- a/lib/nth_prime_cases.rb +++ b/lib/nth_prime_cases.rb @@ -1,18 +1,23 @@ class NthPrimeCase < OpenStruct - def name + def test_name 'test_%s' % description.downcase.gsub(/[ -]/, '_') end - def actual - "Prime.nth(#{input})" + def work_load + actual = "Prime.nth(#{input})" + if raises_error? + "assert_raises(ArgumentError) { #{actual} }" + else + "assert_equal #{expected}, #{actual}" + end end - def raises_error? - expected == false + def skipped + index.zero? ? '# skip' : 'skip' end - def skipped? - index > 0 + def raises_error? + expected == false end end diff --git a/lib/pangram_cases.rb b/lib/pangram_cases.rb index 4e6ebd7549..f197b3e2bc 100644 --- a/lib/pangram_cases.rb +++ b/lib/pangram_cases.rb @@ -1,14 +1,18 @@ class PangramCase < OpenStruct - def name + def test_name 'test_%s' % description.delete("'").gsub(/[ -]/, '_') end - def do - 'Pangram.is_pangram?(str)' + def work_load + if expected + "assert Pangram.is_pangram?('#{input}')" + else + "refute Pangram.is_pangram?('#{input}')" + end end - def skipped? - index > 0 + def skipped + index.zero? ? '# skip' : 'skip' end end diff --git a/lib/raindrops_cases.rb b/lib/raindrops_cases.rb index 0fd9933b15..5714f0efae 100644 --- a/lib/raindrops_cases.rb +++ b/lib/raindrops_cases.rb @@ -1,14 +1,14 @@ class RaindropsCase < OpenStruct - def name + def test_name 'test_%s' % number end - def do - "Raindrops.convert(#{number})" + def work_load + "assert_equal '#{expected}', Raindrops.convert(#{number})" end - def skipped? - index > 0 + def skipped + index.zero? ? '# skip' : 'skip' end end diff --git a/lib/rna_transcription_cases.rb b/lib/rna_transcription_cases.rb index 750373a4d9..1f1e658528 100644 --- a/lib/rna_transcription_cases.rb +++ b/lib/rna_transcription_cases.rb @@ -4,7 +4,7 @@ def test_name end def work_load - "Complement.of_dna('#{dna}')" + "assert_equal '#{expected}', Complement.of_dna('#{dna}')" end def skipped diff --git a/lib/roman_numerals_cases.rb b/lib/roman_numerals_cases.rb index dbee670108..ff853a361c 100644 --- a/lib/roman_numerals_cases.rb +++ b/lib/roman_numerals_cases.rb @@ -1,14 +1,14 @@ class RomanNumeralsCase < OpenStruct - def name + def test_name 'test_%s' % number.to_s end - def do - '%s.to_roman' % number.to_s + def work_load + "assert_equal '#{expected}', #{'%s.to_roman' % number.to_s}" end - def skipped? - index > 0 + def skipped + index.zero? ? '# skip' : 'skip' end end diff --git a/lib/run_length_encoding_cases.rb b/lib/run_length_encoding_cases.rb index 32ce39d763..fd48e07b5a 100644 --- a/lib/run_length_encoding_cases.rb +++ b/lib/run_length_encoding_cases.rb @@ -1,30 +1,22 @@ class RunLengthEncodingCase < OpenStruct - def name + def test_name 'test_%s' % cleaned_description end - def assign_input - "input = '#{input}'" - end - - def assign_output - "output = '#{expected}'" - end - - def assertion + def work_load case description when /decode.+encode/ - 'assert_equal output, - RunLengthEncoding.decode(RunLengthEncoding.encode(input))' + "assert_equal #{expected}, + RunLengthEncoding.decode(RunLengthEncoding.encode(#{input}))" when /encode/ - 'assert_equal output, RunLengthEncoding.encode(input)' + "assert_equal #{expected}, RunLengthEncoding.encode(#{input})" when /decode/ - 'assert_equal output, RunLengthEncoding.decode(input)' + "assert_equal #{expected}, RunLengthEncoding.decode(#{input})" end end - def skipped? - index > 0 + def skipped + index.zero? ? '# skip' : 'skip' end # internal diff --git a/lib/two_bucket_cases.rb b/lib/two_bucket_cases.rb index 7280475b0b..db7d0fbcb3 100644 --- a/lib/two_bucket_cases.rb +++ b/lib/two_bucket_cases.rb @@ -8,15 +8,15 @@ def do "TwoBucket.new(#{bucket_one}, #{bucket_two}, #{goal}, '#{start_bucket}')" end - def test_body + def work_load "two_bucket = #{self.do} assert_equal #{expected['moves']}, two_bucket.moves assert_equal '#{expected['goal_bucket']}', two_bucket.goal_bucket assert_equal #{expected['other_bucket']}, two_bucket.other_bucket" end - def skipped? - index > 0 + def skipped + index.zero? ? '# skip' : 'skip' end end From 3fd2c2799d35869917836b1d4c3480e84238fe41 Mon Sep 17 00:00:00 2001 From: Bryan Mulvihill Date: Tue, 18 Oct 2016 08:56:44 -0400 Subject: [PATCH 2/3] Ensuring all templates call test_name, fixes --- .ruby-version | 1 - exercises/acronym/example.tt | 4 ++-- exercises/alphametics/example.tt | 2 +- exercises/anagram/example.tt | 2 +- exercises/binary/example.tt | 4 ++-- exercises/bracket-push/example.tt | 4 ++-- exercises/clock/example.tt | 4 ++-- exercises/connect/example.tt | 6 ++--- exercises/custom-set/example.tt | 2 +- exercises/difference-of-squares/example.tt | 6 ++--- exercises/gigasecond/example.tt | 6 ++--- exercises/hamming/example.tt | 7 +++--- exercises/hello-world/example.tt | 2 +- exercises/largest-series-product/example.tt | 5 ++-- exercises/leap/example.tt | 5 ++-- exercises/nth-prime/example.tt | 2 +- exercises/pangram/example.tt | 2 +- exercises/raindrops/example.tt | 2 +- exercises/rna-transcription/example.tt | 2 +- exercises/roman-numerals/example.tt | 2 +- exercises/run-length-encoding/example.tt | 2 +- exercises/sieve/example.tt | 5 ++-- exercises/two-bucket/example.tt | 2 +- exercises/word-count/.version | 2 +- exercises/word-count/example.tt | 9 +++---- lib/acronym_cases.rb | 4 ++-- lib/alphametics_cases.rb | 8 +++++-- lib/anagram_cases.rb | 2 +- lib/binary_cases.rb | 2 +- lib/bracket_push_cases.rb | 2 +- lib/clock_cases.rb | 2 +- lib/connect_cases.rb | 4 ++-- lib/custom_set_cases.rb | 2 +- lib/difference_of_squares_cases.rb | 14 +++++------ lib/gigasecond_cases.rb | 7 ++++-- lib/hamming_cases.rb | 9 +++++-- lib/hello_world_cases.rb | 2 +- lib/largest_series_product_cases.rb | 9 +++++-- lib/leap_cases.rb | 9 +++++-- lib/nth_prime_cases.rb | 2 +- lib/pangram_cases.rb | 13 ++++++++--- lib/raindrops_cases.rb | 2 +- lib/rna_transcription_cases.rb | 2 +- lib/roman_numerals_cases.rb | 2 +- lib/run_length_encoding_cases.rb | 26 ++++++++++++++------- lib/sieve_cases.rb | 13 ++++++++--- lib/two_bucket_cases.rb | 2 +- lib/word_count_cases.rb | 14 +++++++---- 48 files changed, 141 insertions(+), 101 deletions(-) delete mode 100644 .ruby-version diff --git a/.ruby-version b/.ruby-version deleted file mode 100644 index 276cbf9e28..0000000000 --- a/.ruby-version +++ /dev/null @@ -1 +0,0 @@ -2.3.0 diff --git a/exercises/acronym/example.tt b/exercises/acronym/example.tt index ea5f5dbdbd..8f192b855d 100644 --- a/exercises/acronym/example.tt +++ b/exercises/acronym/example.tt @@ -6,9 +6,9 @@ require_relative 'acronym' # Test data version: # <%= sha1 %> class AcronymTest < Minitest::Test<% test_cases.each do |test_case| %> - def <%= test_case.name %> + def <%= test_case.test_name %> <%= test_case.skipped %> - <%= test_case.work_load %> + <%= test_case.workload %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/alphametics/example.tt b/exercises/alphametics/example.tt index d7829fae29..80c5079cb2 100644 --- a/exercises/alphametics/example.tt +++ b/exercises/alphametics/example.tt @@ -8,7 +8,7 @@ require_relative 'alphametics' class AlphameticsTest < Minitest::Test<% test_cases.each do |test_case| %> def <%= test_case.test_name %> <%= test_case.skipped %> - <%= test_case.work_load %> + <%= test_case.workload %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/anagram/example.tt b/exercises/anagram/example.tt index 26b66ba78c..710cc2bd8b 100644 --- a/exercises/anagram/example.tt +++ b/exercises/anagram/example.tt @@ -8,7 +8,7 @@ require_relative 'anagram' class AnagramTest < Minitest::Test<% test_cases.each do |test_case| %> def <%= test_case.test_name %> <%= test_case.skipped %> - <%= test_case.work_load %> + <%= test_case.workload %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/binary/example.tt b/exercises/binary/example.tt index bc6046bbb0..17ea0a05ef 100644 --- a/exercises/binary/example.tt +++ b/exercises/binary/example.tt @@ -7,9 +7,9 @@ require_relative 'binary' # Test data version: # <%= sha1 %> class BinaryTest < Minitest::Test<% test_cases.each do |test_case| %> - def <%= test_case.name %> + def <%= test_case.test_name %> <%= test_case.skipped %> - <%= test_case.work_load %> + <%= test_case.workload %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/bracket-push/example.tt b/exercises/bracket-push/example.tt index 0f6a2cadda..b9ceb42b8a 100644 --- a/exercises/bracket-push/example.tt +++ b/exercises/bracket-push/example.tt @@ -6,9 +6,9 @@ require_relative 'bracket_push' # Test data version: # <%= sha1 %> class BracketsTest < Minitest::Test<% test_cases.each do |test_case| %> - def <%= test_case.name %> + def <%= test_case.test_name %> <%= test_case.skipped %> - <%= test_case.work_load %> + <%= test_case.workload %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/clock/example.tt b/exercises/clock/example.tt index 256c6210a7..87c972c961 100644 --- a/exercises/clock/example.tt +++ b/exercises/clock/example.tt @@ -6,9 +6,9 @@ require_relative 'clock' # Test data version: # <%= sha1 %> class ClockTest < Minitest::Test<% test_cases.each do |test_case| %> - def <%= test_case.name %> + def <%= test_case.test_name %> <%= test_case.skipped %> - <%= test_case.work_load %> + <%= test_case.workload %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/connect/example.tt b/exercises/connect/example.tt index 9076d599d9..2bd88aaf0e 100644 --- a/exercises/connect/example.tt +++ b/exercises/connect/example.tt @@ -6,14 +6,14 @@ require_relative 'connect' # Test data version commit id: <%= sha1 %> class ConnectTest < Minitest::Test <% test_cases.each do |test_case| %> - <%= test_case.ignore_method_length%>def <%= test_case.name %> + <%= test_case.ignore_method_length%>def <%= test_case.test_name %> <%= test_case.skipped %> - <%= test_case.work_load.strip %> + <%= test_case.workload %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> def test_bookkeeping skip - assert_equal <%= version %>, BookKeeping::VERSION + assert_equal <%= version.next %>, BookKeeping::VERSION end end diff --git a/exercises/custom-set/example.tt b/exercises/custom-set/example.tt index 12ed763b33..f7aac9c015 100644 --- a/exercises/custom-set/example.tt +++ b/exercises/custom-set/example.tt @@ -8,7 +8,7 @@ require_relative 'custom_set' class CustomSetTest < Minitest::Test<% test_cases.each do |test_case| %> def <%= test_case.test_name %> <%= test_case.skipped %> - <%= test_case.work_load %> + <%= test_case.workload %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/difference-of-squares/example.tt b/exercises/difference-of-squares/example.tt index 9f7e2e834b..f671d7802a 100644 --- a/exercises/difference-of-squares/example.tt +++ b/exercises/difference-of-squares/example.tt @@ -6,9 +6,9 @@ require_relative 'difference_of_squares' # Test data version: # <%= sha1 %> class DifferenceOfSquaresTest < Minitest::Test<% test_cases.each do |test_case| %> - def <%= test_case.test_name %><% if test_case.skipped? %> - skip<% end %> - assert_equal <%= test_case.expected_formatted %>, <%= test_case.do %> + def <%= test_case.test_name %> + <%= test_case.skipped %> + <%= test_case.workload %> end <% end %> def test_consistent_difference diff --git a/exercises/gigasecond/example.tt b/exercises/gigasecond/example.tt index 81f5258074..fcf36feae2 100644 --- a/exercises/gigasecond/example.tt +++ b/exercises/gigasecond/example.tt @@ -6,14 +6,14 @@ require_relative 'gigasecond' # Test data version commit id: <%= sha1 %> class GigasecondTest < Minitest::Test <% test_cases.each do |test_case| %> - def <%= test_case.name %> + def <%= test_case.test_name %> <%= test_case.skipped %> - <%= test_case.work_load %> + <%= test_case.workload %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> def test_bookkeeping skip - assert_equal <%= version %>, BookKeeping::VERSION + assert_equal <%= version.next %>, BookKeeping::VERSION end end diff --git a/exercises/hamming/example.tt b/exercises/hamming/example.tt index 5f4dea5e56..e715d6d947 100644 --- a/exercises/hamming/example.tt +++ b/exercises/hamming/example.tt @@ -6,10 +6,9 @@ require_relative 'hamming' # Test data version: # <%= sha1 %> class HammingTest < Minitest::Test<% test_cases.each do |test_case| %> - def <%= test_case.name %> - <%= test_case.skipped %><% if test_case.raises_error? %> - assert_raises(ArgumentError) { <%= test_case.work_load %> }<% else %> - assert_equal <%= test_case.expected %>, <%= test_case.work_load %><% end %> + def <%= test_case.test_name %> + <%= test_case.skipped %> + <%= test_case.workload %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/hello-world/example.tt b/exercises/hello-world/example.tt index a16b53a3f9..63ac705e29 100644 --- a/exercises/hello-world/example.tt +++ b/exercises/hello-world/example.tt @@ -17,7 +17,7 @@ end class HelloWorldTest < Minitest::Test<% test_cases.each do |test_case| %> def <%= test_case.test_name %> <%= test_case.skipped %> - <%= test_case.work_load %> + <%= test_case.workload %> end <% end %>end diff --git a/exercises/largest-series-product/example.tt b/exercises/largest-series-product/example.tt index d299e1b8b8..52372be96b 100644 --- a/exercises/largest-series-product/example.tt +++ b/exercises/largest-series-product/example.tt @@ -11,9 +11,8 @@ require_relative 'largest_series_product' # class Seriestest < Minitest::Test<% test_cases.each do |test_case| %> def <%= test_case.test_name %> - <%= test_case.skipped %><% if test_case.raises_error? %> - assert_raises(ArgumentError) { <%= test_case.work_load %> }<% else %> - assert_equal <%= test_case.expected %>, <%= test_case.work_load %><% end %> + <%= test_case.skipped %> + <%= test_case.workload %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/leap/example.tt b/exercises/leap/example.tt index 6e1116f210..4c44e58707 100644 --- a/exercises/leap/example.tt +++ b/exercises/leap/example.tt @@ -16,9 +16,8 @@ end class YearTest < Minitest::Test<% test_cases.each do |test_case| %> def <%= test_case.test_name %> - <%= test_case.skipped %><% if test_case.expected%> - assert <%= test_case.work_load %>, "<%= test_case.failure_message%>"<% else %> - refute <%= test_case.work_load %>, "<%= test_case.failure_message%>"<% end%> + <%= test_case.skipped %> + <%= test_case.workload %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/nth-prime/example.tt b/exercises/nth-prime/example.tt index a184c5f066..104f1cdbd8 100644 --- a/exercises/nth-prime/example.tt +++ b/exercises/nth-prime/example.tt @@ -11,7 +11,7 @@ require_relative 'nth_prime' class NthPrimeTest < Minitest::Test<% test_cases.each do |test_case| %> def <%= test_case.test_name %> <%= test_case.skipped %> - <%= test_case.work_load %> + <%= test_case.workload %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/pangram/example.tt b/exercises/pangram/example.tt index 27eb24dabb..c0501a24ee 100644 --- a/exercises/pangram/example.tt +++ b/exercises/pangram/example.tt @@ -9,7 +9,7 @@ require_relative 'pangram' class PangramTest < Minitest::Test<% test_cases.each do |test_case| %> def <%= test_case.test_name %> <%= test_case.skipped %> - <%= test_case.work_load %> + <%= test_case.workload %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/raindrops/example.tt b/exercises/raindrops/example.tt index 605e226b92..77b4e87513 100644 --- a/exercises/raindrops/example.tt +++ b/exercises/raindrops/example.tt @@ -8,7 +8,7 @@ require_relative 'raindrops' class RaindropsTest < Minitest::Test<% test_cases.each do |test_case| %> def <%= test_case.test_name %> <%= test_case.skipped %> - <%= test_case.work_load %> + <%= test_case.workload %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/rna-transcription/example.tt b/exercises/rna-transcription/example.tt index 77cc75f933..5fa51bf178 100644 --- a/exercises/rna-transcription/example.tt +++ b/exercises/rna-transcription/example.tt @@ -8,7 +8,7 @@ require_relative 'rna_transcription' class ComplementTest < Minitest::Test<% test_cases.each do |test_case| %> def <%= test_case.test_name %> <%= test_case.skipped %> - <%= test_case.work_load %> + <%= test_case.workload %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/roman-numerals/example.tt b/exercises/roman-numerals/example.tt index 2e9258efbe..1feefde423 100644 --- a/exercises/roman-numerals/example.tt +++ b/exercises/roman-numerals/example.tt @@ -8,7 +8,7 @@ require_relative 'roman_numerals' class RomanNumeralsTest < Minitest::Test<% test_cases.each do |test_case| %> def <%= test_case.test_name %> <%= test_case.skipped %> - <%= test_case.work_load %> + <%= test_case.workload %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/run-length-encoding/example.tt b/exercises/run-length-encoding/example.tt index 6848e371a9..f27ba5d493 100644 --- a/exercises/run-length-encoding/example.tt +++ b/exercises/run-length-encoding/example.tt @@ -9,7 +9,7 @@ require_relative 'run_length_encoding' class RunLengthEncodingTest < Minitest::Test<% test_cases.each do |test_case| %> def <%= test_case.test_name %> <%= test_case.skipped %> - <%= test_case.work_load %> + <%= test_case.workload %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/sieve/example.tt b/exercises/sieve/example.tt index 80e1848262..024fbcca65 100644 --- a/exercises/sieve/example.tt +++ b/exercises/sieve/example.tt @@ -7,10 +7,9 @@ require_relative 'sieve' # <%= sha1 %> class SieveTest < Minitest::Test<% test_cases.each do |test_case| %> - def <%= test_case.name %> + def <%= test_case.test_name %> <%= test_case.skipped %> - expected = <%= test_case.expected_string %> - assert_equal expected, Sieve.new(<%= test_case.limit %>).primes + <%= test_case.workload %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/two-bucket/example.tt b/exercises/two-bucket/example.tt index 6b4b619e07..16f6293797 100644 --- a/exercises/two-bucket/example.tt +++ b/exercises/two-bucket/example.tt @@ -8,7 +8,7 @@ require_relative 'two_bucket' class TwoBucketTest < Minitest::Test<% test_cases.each do |test_case| %> def <%= test_case.test_name %> <%= test_case.skipped %> - <%= test_case.work_load %> + <%= test_case.workload %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> diff --git a/exercises/word-count/.version b/exercises/word-count/.version index d00491fd7e..bf0d87ab1b 100644 --- a/exercises/word-count/.version +++ b/exercises/word-count/.version @@ -1 +1 @@ -1 +4 \ No newline at end of file diff --git a/exercises/word-count/example.tt b/exercises/word-count/example.tt index e5ca51b921..869829dc30 100644 --- a/exercises/word-count/example.tt +++ b/exercises/word-count/example.tt @@ -6,11 +6,9 @@ require_relative 'word_count' # Test data version: # <%= sha1 %> class PhraseTest < Minitest::Test<% test_cases.each do |test_case| %> - def <%= test_case.name %><% if test_case.skipped? %> - skip<% end %> - phrase = <%= test_case.object_under_test %> - counts = <%= test_case.expected %> - assert_equal counts, phrase.word_count + def <%= test_case.test_name %> + <%= test_case.skipped %> + <%= test_case.workload %> end <% end %> <%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> @@ -19,4 +17,3 @@ class PhraseTest < Minitest::Test<% test_cases.each do |test_case| %> assert_equal <%= version.next %>, BookKeeping::VERSION end end - diff --git a/lib/acronym_cases.rb b/lib/acronym_cases.rb index ad5460d7ad..24a9977c8d 100644 --- a/lib/acronym_cases.rb +++ b/lib/acronym_cases.rb @@ -3,8 +3,8 @@ def test_name 'test_%s' % description.tr(' ', '_') end - def work_load - "assert_equal #{expected}, Acronym.abbreviate('#{phrase}')" + def workload + "assert_equal '#{expected}', Acronym.abbreviate('#{phrase}')" end def skipped diff --git a/lib/alphametics_cases.rb b/lib/alphametics_cases.rb index 68d1d8bbb8..10b20937bd 100644 --- a/lib/alphametics_cases.rb +++ b/lib/alphametics_cases.rb @@ -6,8 +6,12 @@ def test_name "test_#{description.tr(' ', '_')}" end - def work_load - "assert_equal(#{expect}, Alphametics.new.solve('#{puzzle}'))" + def workload + <<-WL.chomp +expect = #{expect} + actual = Alphametics.new.solve('#{puzzle}') + assert_equal(expect, actual) + WL end def expect diff --git a/lib/anagram_cases.rb b/lib/anagram_cases.rb index d21cb62cba..b70e666534 100644 --- a/lib/anagram_cases.rb +++ b/lib/anagram_cases.rb @@ -3,7 +3,7 @@ def test_name 'test_%s' % description.gsub(/[ -]/, '_') end - def work_load + def workload indent_lines([show_comment, detector, anagram, assert].compact) end diff --git a/lib/binary_cases.rb b/lib/binary_cases.rb index 323e765f1d..8e306691c9 100644 --- a/lib/binary_cases.rb +++ b/lib/binary_cases.rb @@ -3,7 +3,7 @@ def test_name 'test_%s' % description.gsub(/[ -]/, '_') end - def work_load + def workload return error_assertion if raises_error? equality_assertion end diff --git a/lib/bracket_push_cases.rb b/lib/bracket_push_cases.rb index 89db7a0596..3addc84bc5 100644 --- a/lib/bracket_push_cases.rb +++ b/lib/bracket_push_cases.rb @@ -3,7 +3,7 @@ def test_name 'test_%s' % description.gsub(/[ -]/, '_') end - def work_load + def workload long_input? ? split_test : simple_test end diff --git a/lib/clock_cases.rb b/lib/clock_cases.rb index 9fe138f2b0..453d69ddcc 100644 --- a/lib/clock_cases.rb +++ b/lib/clock_cases.rb @@ -6,7 +6,7 @@ def test_name .chomp('_') end - def work_load + def workload section == 'equal' ? compare_clocks : simple_test end diff --git a/lib/connect_cases.rb b/lib/connect_cases.rb index 83c81fc44d..78ecc8c9cd 100644 --- a/lib/connect_cases.rb +++ b/lib/connect_cases.rb @@ -6,8 +6,8 @@ def test_name .downcase end - def work_load - <<-WL + def workload + <<-WL.chomp board = [ #{board.map(&method(:single_quote)).join(",\n ")} ] diff --git a/lib/custom_set_cases.rb b/lib/custom_set_cases.rb index ffb2247d47..32aa6265c9 100644 --- a/lib/custom_set_cases.rb +++ b/lib/custom_set_cases.rb @@ -3,7 +3,7 @@ def test_name 'test_%s' % description.gsub(/ |-/, '_') end - def work_load + def workload send section end diff --git a/lib/difference_of_squares_cases.rb b/lib/difference_of_squares_cases.rb index 2a2b6a5dfd..1228fd9d3c 100644 --- a/lib/difference_of_squares_cases.rb +++ b/lib/difference_of_squares_cases.rb @@ -1,10 +1,14 @@ class DifferenceOfSquaresCase < OpenStruct - def name + def test_name 'test_%s' % description.gsub(/[ -]/, '_') end - def work_load - "Squares.new(#{number}).#{action}" + def workload + "assert_equal #{expected_formatted}, Squares.new(#{number}).#{action}" + end + + def skipped + index.zero? ? '# skip' : 'skip' end def action @@ -12,10 +16,6 @@ def action section end - def skipped - index.zero? ? '# skip' : 'skip' - end - def expected_formatted expected.to_s.reverse.scan(/\d{1,3}/).join('_').reverse end diff --git a/lib/gigasecond_cases.rb b/lib/gigasecond_cases.rb index 30e966071b..90961b75f5 100644 --- a/lib/gigasecond_cases.rb +++ b/lib/gigasecond_cases.rb @@ -5,8 +5,11 @@ def test_name 'test_%s' % description.gsub(/[ :-]/, '_') end - def work_load - "assert_equal #{want}, Gigasecond.from(#{got})" + def workload + <<-WL.chomp +result = Gigasecond.from(#{got}) + assert_equal #{want}, result + WL end def skipped diff --git a/lib/hamming_cases.rb b/lib/hamming_cases.rb index b8424ff331..c85012baf8 100644 --- a/lib/hamming_cases.rb +++ b/lib/hamming_cases.rb @@ -3,8 +3,13 @@ def test_name 'test_%s' % description.gsub(/[ -]/, '_') end - def work_load - "Hamming.compute('#{strand1}', '#{strand2}')" + def workload + assertion = "Hamming.compute('#{strand1}', '#{strand2}')" + if raises_error? + "assert_raises(ArgumentError) { #{assertion} }" + else + "assert_equal #{expected}, #{assertion}" + end end def skipped diff --git a/lib/hello_world_cases.rb b/lib/hello_world_cases.rb index 55459e72c9..308b9d5808 100644 --- a/lib/hello_world_cases.rb +++ b/lib/hello_world_cases.rb @@ -3,7 +3,7 @@ def test_name 'test_%s' % description.gsub(/[ -]/, '_') end - def work_load + def workload assertion = if defined?(name) "HelloWorld.hello('#{name}')" diff --git a/lib/largest_series_product_cases.rb b/lib/largest_series_product_cases.rb index c5ee2594c3..c308c7bd89 100644 --- a/lib/largest_series_product_cases.rb +++ b/lib/largest_series_product_cases.rb @@ -3,8 +3,13 @@ def test_name 'test_%s' % description.tr('()', '').tr(' -', '_').downcase end - def work_load - "Series.new('#{digits}').largest_product(#{span})" + def workload + assertion = "Series.new('#{digits}').largest_product(#{span})" + if raises_error? + "assert_raises(ArgumentError) { #{assertion} }" + else + "assert_equal #{expected}, #{assertion}" + end end def skipped diff --git a/lib/leap_cases.rb b/lib/leap_cases.rb index d570bad3dd..5e875f8c3e 100644 --- a/lib/leap_cases.rb +++ b/lib/leap_cases.rb @@ -3,8 +3,13 @@ def test_name 'test_%s' % description.downcase.gsub(/[ -]/, '_') end - def work_load - "Year.leap?(#{input})" + def workload + assertion = "Year.leap?(#{input})" + if expected + "assert #{assertion}, #{failure_message.inspect}" + else + "refute #{assertion}, #{failure_message.inspect}" + end end def skipped diff --git a/lib/nth_prime_cases.rb b/lib/nth_prime_cases.rb index 41ee7cb424..8722ba7f3c 100644 --- a/lib/nth_prime_cases.rb +++ b/lib/nth_prime_cases.rb @@ -3,7 +3,7 @@ def test_name 'test_%s' % description.downcase.gsub(/[ -]/, '_') end - def work_load + def workload actual = "Prime.nth(#{input})" if raises_error? "assert_raises(ArgumentError) { #{actual} }" diff --git a/lib/pangram_cases.rb b/lib/pangram_cases.rb index f197b3e2bc..56568e9fa9 100644 --- a/lib/pangram_cases.rb +++ b/lib/pangram_cases.rb @@ -3,11 +3,18 @@ def test_name 'test_%s' % description.delete("'").gsub(/[ -]/, '_') end - def work_load + def workload + <<-WL.chomp +str = '#{input}' + #{assertion} + WL + end + + def assertion if expected - "assert Pangram.is_pangram?('#{input}')" + "assert Pangram.is_pangram?(str)" else - "refute Pangram.is_pangram?('#{input}')" + "refute Pangram.is_pangram?(str)" end end diff --git a/lib/raindrops_cases.rb b/lib/raindrops_cases.rb index 5714f0efae..e2769a4c8c 100644 --- a/lib/raindrops_cases.rb +++ b/lib/raindrops_cases.rb @@ -3,7 +3,7 @@ def test_name 'test_%s' % number end - def work_load + def workload "assert_equal '#{expected}', Raindrops.convert(#{number})" end diff --git a/lib/rna_transcription_cases.rb b/lib/rna_transcription_cases.rb index 1f1e658528..4c7e799167 100644 --- a/lib/rna_transcription_cases.rb +++ b/lib/rna_transcription_cases.rb @@ -3,7 +3,7 @@ def test_name 'test_%s' % description.gsub(/[ -]/, '_') end - def work_load + def workload "assert_equal '#{expected}', Complement.of_dna('#{dna}')" end diff --git a/lib/roman_numerals_cases.rb b/lib/roman_numerals_cases.rb index ff853a361c..4e2f0f3106 100644 --- a/lib/roman_numerals_cases.rb +++ b/lib/roman_numerals_cases.rb @@ -3,7 +3,7 @@ def test_name 'test_%s' % number.to_s end - def work_load + def workload "assert_equal '#{expected}', #{'%s.to_roman' % number.to_s}" end diff --git a/lib/run_length_encoding_cases.rb b/lib/run_length_encoding_cases.rb index fd48e07b5a..83d292061a 100644 --- a/lib/run_length_encoding_cases.rb +++ b/lib/run_length_encoding_cases.rb @@ -3,22 +3,30 @@ def test_name 'test_%s' % cleaned_description end - def work_load + def workload + <<-WL.chomp +input = '#{input}' + output = '#{expected}' + #{assertion} + WL + end + + def skipped + index.zero? ? '# skip' : 'skip' + end + + def assertion case description when /decode.+encode/ - "assert_equal #{expected}, - RunLengthEncoding.decode(RunLengthEncoding.encode(#{input}))" + "assert_equal output, + RunLengthEncoding.decode(RunLengthEncoding.encode(input))" when /encode/ - "assert_equal #{expected}, RunLengthEncoding.encode(#{input})" + "assert_equal output, RunLengthEncoding.encode(input)" when /decode/ - "assert_equal #{expected}, RunLengthEncoding.decode(#{input})" + "assert_equal output, RunLengthEncoding.decode(input)" end end - def skipped - index.zero? ? '# skip' : 'skip' - end - # internal def cleaned_description diff --git a/lib/sieve_cases.rb b/lib/sieve_cases.rb index 01da5c4198..f877482b5d 100644 --- a/lib/sieve_cases.rb +++ b/lib/sieve_cases.rb @@ -4,15 +4,22 @@ class SieveCase < OpenStruct NEW_ARRAY_ROW = ",\n\s\s\s\s\s\s".freeze ARRAY_ELEMENTS_PER_ROW = 17.freeze - def name + def test_name 'test_%s' % description.tr(' ', '_') end + def workload + <<-WL.chomp +#{expected_string} + assert_equal expected, Sieve.new(#{limit}).primes + WL + end + def expected_string - return expected unless needs_indentation? + return "expected = #{expected}" unless needs_indentation? array_rows = expected.each_slice(ARRAY_ELEMENTS_PER_ROW).map { |elements| elements.join(', ') } - "#{OPEN_ARRAY}#{array_rows.join(NEW_ARRAY_ROW)}#{CLOSE_ARRAY}" + "expected = #{OPEN_ARRAY}#{array_rows.join(NEW_ARRAY_ROW)}#{CLOSE_ARRAY}" end def skipped diff --git a/lib/two_bucket_cases.rb b/lib/two_bucket_cases.rb index db7d0fbcb3..6f6c849fb6 100644 --- a/lib/two_bucket_cases.rb +++ b/lib/two_bucket_cases.rb @@ -8,7 +8,7 @@ def do "TwoBucket.new(#{bucket_one}, #{bucket_two}, #{goal}, '#{start_bucket}')" end - def work_load + def workload "two_bucket = #{self.do} assert_equal #{expected['moves']}, two_bucket.moves assert_equal '#{expected['goal_bucket']}', two_bucket.goal_bucket diff --git a/lib/word_count_cases.rb b/lib/word_count_cases.rb index ca4c95ef9a..98039dcc64 100644 --- a/lib/word_count_cases.rb +++ b/lib/word_count_cases.rb @@ -1,14 +1,18 @@ class WordCountCase < OpenStruct - def name + def test_name 'test_%s' % description.tr(' ', '_') end - def object_under_test - %Q(Phrase.new(#{input.inspect})) + def workload + <<-WL.chomp +phrase = Phrase.new(#{input.inspect}) + counts = #{expected} + assert_equal counts, phrase.word_count + WL end - def skipped? - index.nonzero? + def skipped + index.zero? ? '# skip' : 'skip' end end From 80ea27d494b926da153ea93d2d1950587cec30b3 Mon Sep 17 00:00:00 2001 From: Bryan Mulvihill Date: Sat, 22 Oct 2016 15:18:54 -0400 Subject: [PATCH 3/3] Update README with Exercise Generators --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 3f1bae9676..e1d3ac809c 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,16 @@ then additional inputs/outputs should be submitted to the x-common repository. Changes to the test suite (style, boilerplate, etc) will probably have to be made to `example.tt`. +### Exercise Generators + +If you wish to create a new generator, or edit an existing one, the generators currently live in the lib directory and are named `$PROBLEM_cases.rb`. For example, the hamming generator is `lib/hamming_cases.rb`. + +All generators currently adhere to a common public interface, and must define the following three methods: + +- `test_name` - Output the name of the test +- `workload` - Output the body of the test +- `skipped` - Output skip syntax + ## Pull Requests We welcome pull requests that provide fixes to existing test suites (missing