Skip to content

Commit

Permalink
Merge pull request #591 from hilary/exercise_cases_helper_methods
Browse files Browse the repository at this point in the history
generators: Exercise cases helper methods

    moved ExerciseCase under the Generator module (and gave thanks for sed)
    moved the assert helper methods out into their own module, for ease of testing and because ExerciseCase was becoming bloated.
    added test coverage for ExerciseCase
  • Loading branch information
Insti authored May 2, 2017
2 parents 7045d99 + ea4721c commit 463c264
Show file tree
Hide file tree
Showing 45 changed files with 216 additions and 94 deletions.
2 changes: 1 addition & 1 deletion exercises/acronym/.meta/generator/acronym_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class AcronymCase < ExerciseCase
class AcronymCase < Generator::ExerciseCase

def workload
assert_equal { "Acronym.abbreviate('#{phrase}')" }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class AllYourBaseCase < ExerciseCase
class AllYourBaseCase < Generator::ExerciseCase

def workload
indent(4, (assignments + assertion).join("\n")) + "\n"
Expand Down
2 changes: 1 addition & 1 deletion exercises/alphametics/.meta/generator/alphametics_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class AlphameticsCase < ExerciseCase
class AlphameticsCase < Generator::ExerciseCase
def workload
body =
"input = %s\n" % input,
Expand Down
2 changes: 1 addition & 1 deletion exercises/anagram/.meta/generator/anagram_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class AnagramCase < ExerciseCase
class AnagramCase < Generator::ExerciseCase

def workload
indent_lines([show_comment, detector, anagram, wanted, assert].compact)
Expand Down
2 changes: 1 addition & 1 deletion exercises/beer-song/.meta/generator/beer_song_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class BeerSongCase < ExerciseCase
class BeerSongCase < Generator::ExerciseCase

def workload
"assert_equal expected, #{beer_song}"
Expand Down
2 changes: 1 addition & 1 deletion exercises/binary/.meta/generator/binary_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class BinaryCase < ExerciseCase
class BinaryCase < Generator::ExerciseCase

def workload
raises_error? ? error_assertion : equality_assertion
Expand Down
2 changes: 1 addition & 1 deletion exercises/bowling/.meta/generator/bowling_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class BowlingCase < ExerciseCase
class BowlingCase < Generator::ExerciseCase

def workload
indent_lines(assert)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class BracketPushCase < ExerciseCase
class BracketPushCase < Generator::ExerciseCase

def workload
long_input? ? split_test : simple_test
Expand Down
2 changes: 1 addition & 1 deletion exercises/clock/.meta/generator/clock_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class ClockCase < ExerciseCase
class ClockCase < Generator::ExerciseCase
def name
'test_%s' % description
.gsub(/[() -]/, '_')
Expand Down
2 changes: 1 addition & 1 deletion exercises/connect/.meta/generator/connect_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class ConnectCase < ExerciseCase
class ConnectCase < Generator::ExerciseCase

def test_body
[
Expand Down
2 changes: 1 addition & 1 deletion exercises/custom-set/.meta/generator/custom_set_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class CustomSetCase < ExerciseCase
class CustomSetCase < Generator::ExerciseCase

def workload
send property
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class DifferenceOfSquaresCase < ExerciseCase
class DifferenceOfSquaresCase < Generator::ExerciseCase

def workload
%Q(assert_equal #{expected_formatted}, Squares.new(#{number}).#{action})
Expand Down
2 changes: 1 addition & 1 deletion exercises/dominoes/.meta/generator/dominoes_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class DominoesCase < ExerciseCase
class DominoesCase < Generator::ExerciseCase
def name
'test_%s' % description.gsub("can't", 'can not').gsub(/[= -]+/, '_')
end
Expand Down
2 changes: 1 addition & 1 deletion exercises/etl/.meta/generator/etl_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class EtlCase < ExerciseCase
class EtlCase < Generator::ExerciseCase
def workload
indent_lines([
"old = {\n #{format(input)}\n }",
Expand Down
2 changes: 1 addition & 1 deletion exercises/gigasecond/.meta/generator/gigasecond_cases.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'generator/exercise_cases'
require 'time'

class GigasecondCase < ExerciseCase
class GigasecondCase < Generator::ExerciseCase
def workload
%Q(assert_equal #{want}, Gigasecond.from(#{got}))
end
Expand Down
2 changes: 1 addition & 1 deletion exercises/grains/.meta/generator/grains_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class GrainsCase < ExerciseCase
class GrainsCase < Generator::ExerciseCase

def workload
send("#{property}_workload")
Expand Down
2 changes: 1 addition & 1 deletion exercises/hamming/.meta/generator/hamming_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class HammingCase < ExerciseCase
class HammingCase < Generator::ExerciseCase
def workload
if raises_error?
assert_raises(ArgumentError) { test_case }
Expand Down
2 changes: 1 addition & 1 deletion exercises/hello-world/.meta/generator/hello_world_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class HelloWorldCase < ExerciseCase
class HelloWorldCase < Generator::ExerciseCase

def workload
assert_equal { "HelloWorld.hello" }
Expand Down
2 changes: 1 addition & 1 deletion exercises/isogram/.meta/generator/isogram_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class IsogramCase < ExerciseCase
class IsogramCase < Generator::ExerciseCase

def workload
indent_lines(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class LargestSeriesProductCase < ExerciseCase
class LargestSeriesProductCase < Generator::ExerciseCase

def workload
if raises_error?
Expand Down
2 changes: 1 addition & 1 deletion exercises/leap/.meta/generator/leap_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class LeapCase < ExerciseCase
class LeapCase < Generator::ExerciseCase

def workload
"#{assert} Year.leap?(#{input.inspect})"
Expand Down
2 changes: 1 addition & 1 deletion exercises/luhn/.meta/generator/luhn_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class LuhnCase < ExerciseCase
class LuhnCase < Generator::ExerciseCase
def workload
"#{assert} Luhn.valid?(#{input.inspect})"
end
Expand Down
2 changes: 1 addition & 1 deletion exercises/nth-prime/.meta/generator/nth_prime_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class NthPrimeCase < ExerciseCase
class NthPrimeCase < Generator::ExerciseCase

def workload
if raises_error?
Expand Down
2 changes: 1 addition & 1 deletion exercises/ocr-numbers/.meta/generator/ocr_numbers_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class OcrNumbersCase < ExerciseCase
class OcrNumbersCase < Generator::ExerciseCase
def workload
if raises_error?
assert_raises(ArgumentError) { test_case }
Expand Down
2 changes: 1 addition & 1 deletion exercises/pangram/.meta/generator/pangram_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class PangramCase < ExerciseCase
class PangramCase < Generator::ExerciseCase
def workload
[
"phrase = '#{input}'",
Expand Down
2 changes: 1 addition & 1 deletion exercises/pig-latin/.meta/generator/pig_latin_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class PigLatinCase < ExerciseCase
class PigLatinCase < Generator::ExerciseCase
def workload
assert_equal { "PigLatin.translate(#{input.inspect})" }
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class QueenAttackCase < ExerciseCase
class QueenAttackCase < Generator::ExerciseCase

def workload
property == 'create' ? create_workload : attack_workload
Expand Down
2 changes: 1 addition & 1 deletion exercises/raindrops/.meta/generator/raindrops_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class RaindropsCase < ExerciseCase
class RaindropsCase < Generator::ExerciseCase

def workload
assert_equal { "Raindrops.convert(#{number})" }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class RnaTranscriptionCase < ExerciseCase
class RnaTranscriptionCase < Generator::ExerciseCase

def workload
"assert_equal '#{expected}', Complement.of_dna('#{dna}')"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class RomanNumeralsCase < ExerciseCase
class RomanNumeralsCase < Generator::ExerciseCase
def name
'test_%s' % number.to_s
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class RunLengthEncodingCase < ExerciseCase
class RunLengthEncodingCase < Generator::ExerciseCase

def workload
indent_lines([
Expand Down
2 changes: 1 addition & 1 deletion exercises/say/.meta/generator/say_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class SayCase < ExerciseCase
class SayCase < Generator::ExerciseCase

def workload
[
Expand Down
2 changes: 1 addition & 1 deletion exercises/sieve/.meta/generator/sieve_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class SieveCase < ExerciseCase
class SieveCase < Generator::ExerciseCase
OPEN_ARRAY = "[\n\s\s\s\s\s\s".freeze
CLOSE_ARRAY = "\n\s\s\s\s]".freeze
NEW_ARRAY_ROW = ",\n\s\s\s\s\s\s".freeze
Expand Down
2 changes: 1 addition & 1 deletion exercises/tournament/.meta/generator/tournament_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class TournamentCase < ExerciseCase
class TournamentCase < Generator::ExerciseCase

def workload
'Tournament.tally(input)'
Expand Down
2 changes: 1 addition & 1 deletion exercises/transpose/.meta/generator/transpose_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class TransposeCase < ExerciseCase
class TransposeCase < Generator::ExerciseCase

def workload
'Transpose.transpose(input)'
Expand Down
2 changes: 1 addition & 1 deletion exercises/triangle/.meta/generator/triangle_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class TriangleCase < ExerciseCase
class TriangleCase < Generator::ExerciseCase
def name
initial = description.downcase
replaced = initial.gsub(/(true|false)/, expected_type)
Expand Down
2 changes: 1 addition & 1 deletion exercises/two-bucket/.meta/generator/two_bucket_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class TwoBucketCase < ExerciseCase
class TwoBucketCase < Generator::ExerciseCase
def name
"test_bucket_one_size_#{bucket_one}_bucket_two_"\
"size_#{bucket_two}_goal_#{goal}_start_with_bucket_#{start_bucket}"
Expand Down
2 changes: 1 addition & 1 deletion exercises/word-count/.meta/generator/word_count_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class WordCountCase < ExerciseCase
class WordCountCase < Generator::ExerciseCase

def workload
indent_lines([
Expand Down
2 changes: 1 addition & 1 deletion exercises/wordy/.meta/generator/wordy_cases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'generator/exercise_cases'

class WordyCase < ExerciseCase
class WordyCase < Generator::ExerciseCase

def workload
[
Expand Down
33 changes: 33 additions & 0 deletions lib/generator/exercise_case/assertion.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Generator
class ExerciseCase < OpenStruct
module Assertion

# "#{assert} Luhn.valid?(#{input.inspect})"
def assert
expected ? 'assert' : 'refute'
end

# e.g.,
# assert_equal { "PigLatin.translate(#{input.inspect})" }
def assert_equal
"assert_equal #{expected.inspect}, #{yield}"
end

# e.g.,
# if raises_error?
# assert_raises(ArgumentError) { test_case }
# else
# assert_equal { test_case }
# end
def raises_error?
expected.to_i == -1
end

# e.g.,
# assert_raises(ArgumentError) { test_case }
def assert_raises(error)
"assert_raises(#{error}) { #{yield} }"
end
end
end
end
Loading

0 comments on commit 463c264

Please sign in to comment.