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..d8263ee986 --- /dev/null +++ b/exercises/binary/.version @@ -0,0 +1 @@ +2 \ No newline at end of file diff --git a/exercises/binary/binary_test.rb b/exercises/binary/binary_test.rb index 384e9c1c71..fd0158245b 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: +# dd43e66 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 @@ -47,23 +52,37 @@ 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 2).each do |input| + assert_raises(ArgumentError) { Binary.new(input) } + end + end + + def test_containing_letters_raises_an_error skip - %w(012 10nope nope10 10nope10 001\ nope 2).each do |input| - assert_raises ArgumentError do - Binary.new(input) - end + %w(10nope nope10 10nope10 001\ nope).each do |input| + 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 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..bf3fa5e374 --- /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)['decimal'].map.with_index do |row, i| + BinaryCase.new(row.merge('index' => i)) + end +end