From cfa28d56639d8a0ff3b4e78e55371fc6070cbab5 Mon Sep 17 00:00:00 2001 From: Tommy Schaefer Date: Thu, 14 Jul 2016 14:57:01 -0600 Subject: [PATCH 1/4] Automatically generate "binary" exercise tests --- bin/generate-binary | 7 ++++ exercises/binary/.version | 1 + exercises/binary/binary_test.rb | 28 ++++++++++---- exercises/binary/example.rb | 2 +- exercises/binary/example.tt | 20 ++++++++++ lib/binary_cases.rb | 65 +++++++++++++++++++++++++++++++++ 6 files changed, 114 insertions(+), 9 deletions(-) create mode 100755 bin/generate-binary create mode 100644 exercises/binary/.version create mode 100644 exercises/binary/example.tt create mode 100644 lib/binary_cases.rb diff --git a/bin/generate-binary b/bin/generate-binary new file mode 100755 index 0000000000..629f213f13 --- /dev/null +++ b/bin/generate-binary @@ -0,0 +1,7 @@ +#!/usr/bin/env ruby + +require_relative '../lib/helper' +require 'generator' +require 'binary_cases' + +Generator.new('binary', BinaryCases).generate diff --git a/exercises/binary/.version b/exercises/binary/.version new file mode 100644 index 0000000000..e440e5c842 --- /dev/null +++ b/exercises/binary/.version @@ -0,0 +1 @@ +3 \ No newline at end of file diff --git a/exercises/binary/binary_test.rb b/exercises/binary/binary_test.rb index 384e9c1c71..3b2436797d 100755 --- a/exercises/binary/binary_test.rb +++ b/exercises/binary/binary_test.rb @@ -1,14 +1,19 @@ #!/usr/bin/env ruby +# encoding: utf-8 gem 'minitest', '>= 5.0.0' require 'minitest/autorun' require_relative 'binary' +# Test data version: +# 676c54a class BinaryTest < Minitest::Test def test_binary_0_is_decimal_0 + # skip assert_equal 0, Binary.new('0').to_decimal end def test_binary_1_is_decimal_1 + skip assert_equal 1, Binary.new('1').to_decimal end @@ -50,22 +55,29 @@ def test_binary_ignores_leading_zeros def test_invalid_binary_numbers_raise_an_error skip %w(012 10nope nope10 10nope10 001\ nope 2).each do |input| - assert_raises ArgumentError do - Binary.new(input) - end + assert_raises(ArgumentError) { Binary.new(input) } end 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. - # If you're curious, read more about constants on RubyDoc: + # 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 diff --git a/exercises/binary/example.rb b/exercises/binary/example.rb index 80464cd24b..5c78c01d08 100644 --- a/exercises/binary/example.rb +++ b/exercises/binary/example.rb @@ -1,5 +1,5 @@ module BookKeeping - VERSION = 2 + VERSION = 3 end class Binary diff --git a/exercises/binary/example.tt b/exercises/binary/example.tt new file mode 100644 index 0000000000..4cab53b528 --- /dev/null +++ b/exercises/binary/example.tt @@ -0,0 +1,20 @@ +#!/usr/bin/env ruby +# encoding: utf-8 +gem 'minitest', '>= 5.0.0' +require 'minitest/autorun' +require_relative 'binary' + +# Test data version: +# <%= sha1 %> +class BinaryTest < Minitest::Test<% test_cases.each do |test_case| %> + def <%= test_case.name %> + <%= test_case.skipped %> + <%= test_case.assertion %> + end +<% end %> +<%= IO.read(XRUBY_LIB + '/bookkeeping.md') %> + def test_bookkeeping + skip + assert_equal <%= version.next %>, BookKeeping::VERSION + end +end diff --git a/lib/binary_cases.rb b/lib/binary_cases.rb new file mode 100644 index 0000000000..c414cf0cd3 --- /dev/null +++ b/lib/binary_cases.rb @@ -0,0 +1,65 @@ +class BinaryCase < OpenStruct + def name + 'test_%s' % description.gsub(/[ -]/, '_') + end + + def assertion + return compound_assertion if multiple_assertions? + Assertion.new("'#{binary}'", expected).to_s + end + + def skipped + index.zero? ? '# skip' : 'skip' + end + + private + + def multiple_assertions? + binary.is_a?(Array) + end + + def compound_assertion + inputs = binary.map { |e| e.gsub(' ', '\ ') }.join(' ') + %(%w(#{inputs}).each do |input| + #{Assertion.new('input', expected)} + end) + end + + class Assertion + def initialize(initialization_value, expected) + @initialization_value = initialization_value + @expected = expected + end + + def to_s + return error_assertion if raises_error? + equality_assertion + end + + private + + attr_reader :initialization_value, :expected + + def error_assertion + "assert_raises(ArgumentError) { #{work_load} }" + end + + def equality_assertion + "assert_equal #{expected}, #{work_load}" + end + + def work_load + "Binary.new(#{initialization_value})#{'.to_decimal' unless raises_error?}" + end + + def raises_error? + expected.to_i == -1 + end + end +end + +BinaryCases = proc do |data| + JSON.parse(data)['cases'].map.with_index do |row, i| + BinaryCase.new(row.merge('index' => i)) + end +end From b052e2cdd372f0ae60c9f8c9daa797cba0ed52e1 Mon Sep 17 00:00:00 2001 From: Tommy Schaefer Date: Sun, 24 Jul 2016 19:31:24 -0500 Subject: [PATCH 2/4] Update binary "cases" key to be "decimal" --- lib/binary_cases.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/binary_cases.rb b/lib/binary_cases.rb index c414cf0cd3..bf3fa5e374 100644 --- a/lib/binary_cases.rb +++ b/lib/binary_cases.rb @@ -59,7 +59,7 @@ def raises_error? end BinaryCases = proc do |data| - JSON.parse(data)['cases'].map.with_index do |row, i| + JSON.parse(data)['decimal'].map.with_index do |row, i| BinaryCase.new(row.merge('index' => i)) end end From 6a2c96f42103ccbd19f91f8ff99556c2746466cf Mon Sep 17 00:00:00 2001 From: Tommy Schaefer Date: Sun, 24 Jul 2016 19:38:57 -0500 Subject: [PATCH 3/4] Use updated test data to generate new tests --- exercises/binary/binary_test.rb | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/exercises/binary/binary_test.rb b/exercises/binary/binary_test.rb index 3b2436797d..addd84e09c 100755 --- a/exercises/binary/binary_test.rb +++ b/exercises/binary/binary_test.rb @@ -5,7 +5,7 @@ require_relative 'binary' # Test data version: -# 676c54a +# acdf673 class BinaryTest < Minitest::Test def test_binary_0_is_decimal_0 # skip @@ -52,9 +52,16 @@ def test_binary_ignores_leading_zeros assert_equal 31, Binary.new('000011111').to_decimal end - def test_invalid_binary_numbers_raise_an_error + def test_numbers_other_than_one_and_zero_raise_an_error skip - %w(012 10nope nope10 10nope10 001\ nope 2).each do |input| + %w(012 2).each do |input| + assert_raises(ArgumentError) { Binary.new(input) } + end + end + + def test_containing_letters_raises_an_error + skip + %w(10nope nope10 10nope10 001\ nope).each do |input| assert_raises(ArgumentError) { Binary.new(input) } end end From 3d7c4cdcad5efed5b0139720c4a206b412812748 Mon Sep 17 00:00:00 2001 From: Tommy Schaefer Date: Sun, 24 Jul 2016 19:50:03 -0500 Subject: [PATCH 4/4] Change version from 3 to 2 --- exercises/binary/.version | 2 +- exercises/binary/binary_test.rb | 4 ++-- exercises/binary/example.rb | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/exercises/binary/.version b/exercises/binary/.version index e440e5c842..d8263ee986 100644 --- a/exercises/binary/.version +++ b/exercises/binary/.version @@ -1 +1 @@ -3 \ No newline at end of file +2 \ No newline at end of file diff --git a/exercises/binary/binary_test.rb b/exercises/binary/binary_test.rb index addd84e09c..fd0158245b 100755 --- a/exercises/binary/binary_test.rb +++ b/exercises/binary/binary_test.rb @@ -5,7 +5,7 @@ require_relative 'binary' # Test data version: -# acdf673 +# dd43e66 class BinaryTest < Minitest::Test def test_binary_0_is_decimal_0 # skip @@ -85,6 +85,6 @@ def test_containing_letters_raises_an_error def test_bookkeeping skip - assert_equal 3, BookKeeping::VERSION + assert_equal 2, BookKeeping::VERSION end end diff --git a/exercises/binary/example.rb b/exercises/binary/example.rb index 5c78c01d08..80464cd24b 100644 --- a/exercises/binary/example.rb +++ b/exercises/binary/example.rb @@ -1,5 +1,5 @@ module BookKeeping - VERSION = 3 + VERSION = 2 end class Binary