-
-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test generator for "binary" exercise (#394)
Automatically generate "binary" exercise tests
- Loading branch information
1 parent
e8f0138
commit f319c72
Showing
5 changed files
with
121 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |