-
-
Notifications
You must be signed in to change notification settings - Fork 546
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #474 from favrik/triangle-generated-exercises
Add triangle test generator
- Loading branch information
Showing
5 changed files
with
167 additions
and
62 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 @@ | ||
1 |
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 | ||
gem 'minitest', '>= 5.0.0' | ||
require 'minitest/autorun' | ||
require_relative 'triangle' | ||
|
||
# Test data version: | ||
# <%= sha1 %> | ||
class TriangleTest < Minitest::Test<% test_cases.each do |test_case| %> | ||
def <%= test_case.test_name %> | ||
<%= test_case.skipped %> | ||
<%= test_case.workload %> | ||
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
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,51 @@ | ||
class TriangleCase < OpenStruct | ||
def test_name | ||
initial = description.downcase | ||
replaced = initial.gsub(/(true|false)/, expected_type) | ||
if initial.eql?(replaced) && !initial.include?(triangle) | ||
replaced = triangle + ' triangle ' + initial | ||
end | ||
'test_%s' % replaced.tr_s(', -', '_') | ||
end | ||
|
||
def workload | ||
[ | ||
"triangle = Triangle.new(#{sides})", | ||
indent("#{assert_or_refute} triangle.#{triangle}?, #{failure_message}") | ||
].join("\n") | ||
end | ||
|
||
def indent(line) | ||
' ' * 4 + line | ||
end | ||
|
||
def assert_or_refute | ||
expected ? 'assert' : 'refute' | ||
end | ||
|
||
def failure_message | ||
%Q("Expected '#{expected}', #{expected_type}.") | ||
end | ||
|
||
def expected_type | ||
"triangle is #{expected ? '' : 'not '}#{triangle}" | ||
end | ||
|
||
def skipped | ||
index.zero? ? '# skip' : 'skip' | ||
end | ||
end | ||
|
||
TriangleCases = proc do |data| | ||
i = 0 | ||
cases = [] | ||
data = JSON.parse(data).select { |key, value| key.to_s.match(/[^#]+/) } | ||
data.keys.each do |triangle| | ||
data[triangle]['cases'].each do |row| | ||
row = row.merge(row.merge('index' => i, 'triangle' => triangle)) | ||
cases << TriangleCase.new(row) | ||
i += 1 | ||
end | ||
end | ||
cases | ||
end |