-
-
Notifications
You must be signed in to change notification settings - Fork 525
DRY test generators #566
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
DRY test generators #566
Changes from all commits
cd1c83b
33e6026
fd10aa8
9297e0a
f2cb3bb
b028f22
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'ostruct' | ||
require 'json' | ||
|
||
class ExerciseCase < OpenStruct | ||
using Generator::Underscore | ||
|
||
def name | ||
'test_%s' % description.underscore | ||
end | ||
|
||
def skipped | ||
index.zero? ? '# skip' : 'skip' | ||
end | ||
|
||
protected | ||
|
||
# used in workload, for example, as | ||
# "#{assert} Luhn.valid?(#{input.inspect})" | ||
def assert | ||
expected ? 'assert' : 'refute' | ||
end | ||
|
||
# used in workload, for example, as | ||
# assert_equal { "PigLatin.translate(#{input.inspect})" } | ||
def assert_equal | ||
"assert_equal #{expected.inspect}, #{yield}" | ||
end | ||
|
||
# used in workload, for example, as | ||
# if raises_error? | ||
# assert_raises(ArgumentError) { test_case } | ||
# else | ||
# assert_equal { test_case } | ||
# end | ||
|
||
def raises_error? | ||
expected.to_i == -1 | ||
end | ||
|
||
def assert_raises(error) | ||
"assert_raises(#{error}) { #{yield} }" | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Generator | ||
module Underscore | ||
refine String do | ||
def underscore | ||
downcase.gsub(/[- ]/, '_').gsub(/[^\w?]/, '') | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,12 @@ | ||
require 'exercise_cases' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should have been another separate commit. |
||
|
||
class OcrNumbersCase < OpenStruct | ||
def name | ||
'test_%s' % description.downcase.tr('- .', '_') | ||
end | ||
|
||
class OcrNumbersCase < ExerciseCase | ||
def workload | ||
if expected == -1 | ||
"assert_raises(ArgumentError) { #{test_case} }" | ||
if raises_error? | ||
assert_raises(ArgumentError) { test_case } | ||
else | ||
"assert_equal #{expected.inspect}, #{test_case}" | ||
assert_equal { test_case } | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely a better place for this. |
||
end | ||
|
||
def skipped | ||
index.zero? ? '# skip' : 'skip' | ||
end | ||
|
||
private | ||
|
||
def test_case | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,6 @@ | ||
require 'exercise_cases' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not entirely sure I agree with removing this line. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And it simply doesn't break anything... if that dependency goes away, no problem, it is a documentation value at this point. |
||
|
||
class PigLatinCase < OpenStruct | ||
def name | ||
'test_%s' % description.tr('- ', '__') | ||
end | ||
|
||
class PigLatinCase < ExerciseCase | ||
def workload | ||
%Q(assert_equal #{expected.inspect}, PigLatin.translate(#{input.inspect})) | ||
end | ||
|
||
def skipped | ||
index.zero? ? '# skip' : 'skip' | ||
assert_equal { "PigLatin.translate(#{input.inspect})" } | ||
end | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require_relative '../test_helper' | ||
|
||
module Generator | ||
class UnderscoreTest < MiniTest::Test | ||
using Underscore | ||
|
||
# at present, we're downcasing everything, including TLAs | ||
def test_mixed_case | ||
assert_equal 'A string with TLA'.underscore, 'a_string_with_tla' | ||
end | ||
|
||
def test_hyphenated_text | ||
assert_equal 'large distance in off-by-one strand'.underscore, 'large_distance_in_off_by_one_strand' | ||
end | ||
|
||
def test_question_mark | ||
assert_equal( | ||
'Unreadable but correctly sized inputs return ?'.underscore, | ||
'unreadable_but_correctly_sized_inputs_return_?' | ||
) | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice.