Skip to content
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

Add test generator for "binary" exercise #394

Merged
merged 4 commits into from
Jul 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions bin/generate-binary
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env ruby

require_relative '../lib/helper'
require 'generator'
require 'binary_cases'

Generator.new('binary', BinaryCases).generate
1 change: 1 addition & 0 deletions exercises/binary/.version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
37 changes: 28 additions & 9 deletions exercises/binary/binary_test.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions exercises/binary/example.tt
Original file line number Diff line number Diff line change
@@ -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
65 changes: 65 additions & 0 deletions lib/binary_cases.rb
Original file line number Diff line number Diff line change
@@ -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