From dc0755076a386fe6aa9782dfbb6d52f77dcccd11 Mon Sep 17 00:00:00 2001 From: Geoff Hubbard Date: Sun, 21 May 2017 22:49:23 +0100 Subject: [PATCH 01/10] Remove wordy_cases_tests They just cause everything to break unnecessarily. These tests will be added back in again at the end of the refactoring. --- test/wordy_cases_test.rb | 43 ---------------------------------------- 1 file changed, 43 deletions(-) delete mode 100644 test/wordy_cases_test.rb diff --git a/test/wordy_cases_test.rb b/test/wordy_cases_test.rb deleted file mode 100644 index fa5a7c6517..0000000000 --- a/test/wordy_cases_test.rb +++ /dev/null @@ -1,43 +0,0 @@ -require 'json' -require_relative 'test_helper' -require_relative '../exercises/wordy/.meta/generator/wordy_case' - -class WordyCaseTest < Minitest::Test - def test_workload_with_expected_and_no_message - test_case = WordyCase.new(expected: 1, input: 1) - - expected_workload = [ - 'question = \'1\'', - ' assert_equal(1, WordProblem.new(question).answer)', - ].join("\n") - - assert_equal expected_workload, test_case.workload - end - - def test_workload_with_expected_and_message - test_case = WordyCase.new(expected: 1, input: 'What is -3 plus 7 multiplied by -2?') - message = test_case.send(:message) - - expected_workload = [ - "question = 'What is -3 plus 7 multiplied by -2?'", - ' answer = WordProblem.new(question).answer', - ' message = "' + message[0..-3] + '#{answer}"', - ' assert_equal(1, answer, message)', - ].join("\n") - - assert_equal expected_workload, test_case.workload - end - - def test_workload_without_expected - test_case = WordyCase.new(input: 1) - - expected_workload = [ - 'question = \'1\'', - ' assert_raises ArgumentError do', - ' WordProblem.new(question).answer', - ' end', - ].join("\n") - - assert_equal expected_workload, test_case.workload - end -end From fa156692ae048d09fa4379f900478a7559819fb9 Mon Sep 17 00:00:00 2001 From: Hilary Holz Date: Sun, 21 May 2017 22:49:23 +0100 Subject: [PATCH 02/10] inject canonical data into ExerciseCase --- lib/generator/case_values.rb | 2 +- lib/generator/exercise_case.rb | 9 +++-- lib/generator/exercise_case/assertion.rb | 8 ++--- .../alpha/.meta/generator/alpha_case.rb | 2 +- test/generator/case_values_test.rb | 35 ++++++++++++++---- .../generator/exercise_case/assertion_test.rb | 14 ++++---- test/generator/exercise_case_test.rb | 36 ++----------------- 7 files changed, 51 insertions(+), 55 deletions(-) diff --git a/lib/generator/case_values.rb b/lib/generator/case_values.rb index a7e9b6ba78..1dbed0ebe5 100644 --- a/lib/generator/case_values.rb +++ b/lib/generator/case_values.rb @@ -7,7 +7,7 @@ def initialize(case_class:) def cases(exercise_data) extract_test_cases(data: JSON.parse(exercise_data)['cases']) - .map { |test| @case_class.new(test) } + .map { |test| @case_class.new(case_data: test) } end private diff --git a/lib/generator/exercise_case.rb b/lib/generator/exercise_case.rb index edd79b6ce0..5ced3aafe8 100644 --- a/lib/generator/exercise_case.rb +++ b/lib/generator/exercise_case.rb @@ -1,13 +1,18 @@ require 'ostruct' module Generator - class ExerciseCase < OpenStruct + class ExerciseCase using Generator::Underscore include CaseHelpers include Assertion + attr_reader :case_data + def initialize(case_data:) + @case_data = case_data + end + def name - 'test_%s' % description.underscore + 'test_%s' % case_data['description'].underscore end def skipped(index) diff --git a/lib/generator/exercise_case/assertion.rb b/lib/generator/exercise_case/assertion.rb index fef1a774f5..a447cf1a96 100644 --- a/lib/generator/exercise_case/assertion.rb +++ b/lib/generator/exercise_case/assertion.rb @@ -14,7 +14,7 @@ module Assertion # "#{assert} Luhn.valid?(#{input.inspect})" # def assert - expected ? 'assert' : 'refute' + case_data['expected'] ? 'assert' : 'refute' end # generates assertions of the form @@ -29,8 +29,8 @@ def assert # assert_equal { "PigLatin.translate(#{input.inspect})" } # def assert_equal - assertion = expected.nil? ? 'assert_nil' : - "assert_equal #{expected.inspect}," + assertion = case_data['expected'].nil? ? 'assert_nil' : + "assert_equal #{case_data['expected'].inspect}," "#{assertion} #{yield}" end @@ -43,7 +43,7 @@ def assert_equal # end # def raises_error? - expected.to_i == -1 + case_data['expected'].to_i == -1 end # generates assertions of the form diff --git a/test/fixtures/xruby/exercises/alpha/.meta/generator/alpha_case.rb b/test/fixtures/xruby/exercises/alpha/.meta/generator/alpha_case.rb index 709f9d9e64..b46c95f54e 100644 --- a/test/fixtures/xruby/exercises/alpha/.meta/generator/alpha_case.rb +++ b/test/fixtures/xruby/exercises/alpha/.meta/generator/alpha_case.rb @@ -1,6 +1,6 @@ class AlphaCase < Generator::ExerciseCase def name - format('test_%s', description.downcase.gsub(/[ -]/, '_')) + format('test_%s', case_data['description'].downcase.gsub(/[ -]/, '_')) end def workload diff --git a/test/generator/case_values_test.rb b/test/generator/case_values_test.rb index a6c2d9f3ba..12fbe9fd46 100644 --- a/test/generator/case_values_test.rb +++ b/test/generator/case_values_test.rb @@ -16,14 +16,35 @@ def test_multi_level_auto_extraction ).cases(canonical_data) expected = [ - ComplexCase.new(description: 'first generic verse', property: 'verse', number: 99, - expected: '99 bottles of beer on the wall, YAAAR'), - ComplexCase.new(description: 'last generic verse', property: 'verse', number: 3, - expected: '3 bottles of beer on the wall, YAAAR'), - ComplexCase.new(description: 'first two verses', property: 'verses', beginning: 99, end: 98, - expected: "99 bottles of beer on the wall, YAR, PIRATES CAN'T COUNT") + ComplexCase.new( + case_data: { + 'description' => 'first generic verse', + 'property' => 'verse', + 'number' => 99, + 'expected' => '99 bottles of beer on the wall, YAAAR' + } + ), + ComplexCase.new( + case_data: { + 'description' => 'last generic verse', + 'property' => 'verse', + 'number' => 3, + 'expected' => '3 bottles of beer on the wall, YAAAR' + } + ), + ComplexCase.new( + case_data: { + 'description' => 'first two verses', + 'property' => 'verses', + 'beginning' => 99, + 'end' => 98, + 'expected' => "99 bottles of beer on the wall, YAR, PIRATES CAN'T COUNT" + } + ) ] - assert_equal expected, cases + assert expected.zip(cases).all? do |exp, cs| + assert_equal exp.case_data, cs.case_data + end end end end diff --git a/test/generator/exercise_case/assertion_test.rb b/test/generator/exercise_case/assertion_test.rb index ab37778cfa..2e69c077e1 100644 --- a/test/generator/exercise_case/assertion_test.rb +++ b/test/generator/exercise_case/assertion_test.rb @@ -4,43 +4,43 @@ module Generator class ExerciseCase class AssertionTest < Minitest::Test def test_assert - test_case = OpenStruct.new(expected: true) + test_case = OpenStruct.new(case_data: {'expected' => true}) test_case.extend(Assertion) assert_equal 'assert', test_case.assert end def test_refute - test_case = OpenStruct.new(expected: false) + test_case = OpenStruct.new(case_data: {'expected' => false}) test_case.extend(Assertion) assert_equal 'refute', test_case.assert end def test_assert_equal - test_case = OpenStruct.new(expected: 2) + test_case = OpenStruct.new(case_data: {'expected' => 2}) test_case.extend(Assertion) assert_equal "assert_equal 2, 4", test_case.assert_equal { 1 + 3 } end def test_assert_equal_when_nil - test_case = OpenStruct.new(expected: nil) + test_case = OpenStruct.new(case_data: {'expected' => nil}) test_case.extend(Assertion) assert_equal "assert_nil 4", test_case.assert_equal { 1 + 3 } end def test_raises_error - test_case = OpenStruct.new(expected: -1) + test_case = OpenStruct.new(case_data: {'expected' => -1}) test_case.extend(Assertion) assert test_case.raises_error? end def test_does_not_raise_error - test_case = OpenStruct.new(expected: 'cute kitties') + test_case = OpenStruct.new(case_data: {'expected' => 'cute kitties'}) test_case.extend(Assertion) refute test_case.raises_error? end def test_assert_raises - test_case = OpenStruct.new + test_case = OpenStruct.new(case_data: {}) test_case.extend(Assertion) assert_equal( "assert_raises(ArgumentError) { 4 }", diff --git a/test/generator/exercise_case_test.rb b/test/generator/exercise_case_test.rb index 4c5fd047ea..914d8f2a90 100644 --- a/test/generator/exercise_case_test.rb +++ b/test/generator/exercise_case_test.rb @@ -3,45 +3,15 @@ module Generator class ExerciseCaseTest < Minitest::Test def test_name - assert_equal 'test_foo', ExerciseCase.new(description: 'foo').name + assert_equal 'test_foo', ExerciseCase.new(case_data: {'description' => 'foo'}).name end def test_skipped_index_zero - assert_equal '# skip', ExerciseCase.new.skipped(0) + assert_equal '# skip', ExerciseCase.new(case_data: {}).skipped(0) end def test_skipped_index_nonzero - assert_equal 'skip', ExerciseCase.new.skipped(1) - end - - class MultiLineCase < ExerciseCase - def workload - indent_lines(['foo', 'bar'], 1) - end - end - def test_indent_multiline_workloads - expected = "foo\n bar" - assert_equal expected, MultiLineCase.new.workload - end - - class BlankLineCase < ExerciseCase - def workload - indent_text(2, "foo\n\nbar\n") - end - end - def test_indent_multiline_workloads_with_blank_lines - expected = "foo\n\n bar\n" - assert_equal expected, BlankLineCase.new.workload - end - - class HeredocCase < ExerciseCase - def workload - indent_heredoc(["foo", "bar"], 'TEXT', 1) - end - end - def test_heredoc - expected = "<<-TEXT\n foo\n bar\nTEXT" - assert_equal expected, HeredocCase.new.workload + assert_equal 'skip', ExerciseCase.new(case_data: {}).skipped(1) end end end From 01f71479010686473241801f631a5c30d22d1928 Mon Sep 17 00:00:00 2001 From: Geoff Hubbard Date: Sun, 21 May 2017 22:49:23 +0100 Subject: [PATCH 03/10] Rename case_data -> canonical --- lib/generator/case_values.rb | 2 +- lib/generator/exercise_case.rb | 8 ++++---- lib/generator/exercise_case/assertion.rb | 8 ++++---- .../exercises/alpha/.meta/generator/alpha_case.rb | 2 +- test/generator/case_values_test.rb | 8 ++++---- test/generator/exercise_case/assertion_test.rb | 14 +++++++------- test/generator/exercise_case_test.rb | 6 +++--- 7 files changed, 24 insertions(+), 24 deletions(-) diff --git a/lib/generator/case_values.rb b/lib/generator/case_values.rb index 1dbed0ebe5..71c85b3628 100644 --- a/lib/generator/case_values.rb +++ b/lib/generator/case_values.rb @@ -7,7 +7,7 @@ def initialize(case_class:) def cases(exercise_data) extract_test_cases(data: JSON.parse(exercise_data)['cases']) - .map { |test| @case_class.new(case_data: test) } + .map { |test| @case_class.new(canonical: test) } end private diff --git a/lib/generator/exercise_case.rb b/lib/generator/exercise_case.rb index 5ced3aafe8..744d4f183d 100644 --- a/lib/generator/exercise_case.rb +++ b/lib/generator/exercise_case.rb @@ -6,13 +6,13 @@ class ExerciseCase include CaseHelpers include Assertion - attr_reader :case_data - def initialize(case_data:) - @case_data = case_data + attr_reader :canonical + def initialize(canonical:) + @canonical = canonical end def name - 'test_%s' % case_data['description'].underscore + 'test_%s' % canonical['description'].underscore end def skipped(index) diff --git a/lib/generator/exercise_case/assertion.rb b/lib/generator/exercise_case/assertion.rb index a447cf1a96..41861b148b 100644 --- a/lib/generator/exercise_case/assertion.rb +++ b/lib/generator/exercise_case/assertion.rb @@ -14,7 +14,7 @@ module Assertion # "#{assert} Luhn.valid?(#{input.inspect})" # def assert - case_data['expected'] ? 'assert' : 'refute' + canonical['expected'] ? 'assert' : 'refute' end # generates assertions of the form @@ -29,8 +29,8 @@ def assert # assert_equal { "PigLatin.translate(#{input.inspect})" } # def assert_equal - assertion = case_data['expected'].nil? ? 'assert_nil' : - "assert_equal #{case_data['expected'].inspect}," + assertion = canonical['expected'].nil? ? 'assert_nil' : + "assert_equal #{canonical['expected'].inspect}," "#{assertion} #{yield}" end @@ -43,7 +43,7 @@ def assert_equal # end # def raises_error? - case_data['expected'].to_i == -1 + canonical['expected'].to_i == -1 end # generates assertions of the form diff --git a/test/fixtures/xruby/exercises/alpha/.meta/generator/alpha_case.rb b/test/fixtures/xruby/exercises/alpha/.meta/generator/alpha_case.rb index b46c95f54e..2d8c8cc455 100644 --- a/test/fixtures/xruby/exercises/alpha/.meta/generator/alpha_case.rb +++ b/test/fixtures/xruby/exercises/alpha/.meta/generator/alpha_case.rb @@ -1,6 +1,6 @@ class AlphaCase < Generator::ExerciseCase def name - format('test_%s', case_data['description'].downcase.gsub(/[ -]/, '_')) + format('test_%s', canonical['description'].downcase.gsub(/[ -]/, '_')) end def workload diff --git a/test/generator/case_values_test.rb b/test/generator/case_values_test.rb index 12fbe9fd46..43b400f77b 100644 --- a/test/generator/case_values_test.rb +++ b/test/generator/case_values_test.rb @@ -17,7 +17,7 @@ def test_multi_level_auto_extraction expected = [ ComplexCase.new( - case_data: { + canonical: { 'description' => 'first generic verse', 'property' => 'verse', 'number' => 99, @@ -25,7 +25,7 @@ def test_multi_level_auto_extraction } ), ComplexCase.new( - case_data: { + canonical: { 'description' => 'last generic verse', 'property' => 'verse', 'number' => 3, @@ -33,7 +33,7 @@ def test_multi_level_auto_extraction } ), ComplexCase.new( - case_data: { + canonical: { 'description' => 'first two verses', 'property' => 'verses', 'beginning' => 99, @@ -43,7 +43,7 @@ def test_multi_level_auto_extraction ) ] assert expected.zip(cases).all? do |exp, cs| - assert_equal exp.case_data, cs.case_data + assert_equal exp.canonical, cs.canonical end end end diff --git a/test/generator/exercise_case/assertion_test.rb b/test/generator/exercise_case/assertion_test.rb index 2e69c077e1..4a3b8bcd9f 100644 --- a/test/generator/exercise_case/assertion_test.rb +++ b/test/generator/exercise_case/assertion_test.rb @@ -4,43 +4,43 @@ module Generator class ExerciseCase class AssertionTest < Minitest::Test def test_assert - test_case = OpenStruct.new(case_data: {'expected' => true}) + test_case = OpenStruct.new(canonical: {'expected' => true}) test_case.extend(Assertion) assert_equal 'assert', test_case.assert end def test_refute - test_case = OpenStruct.new(case_data: {'expected' => false}) + test_case = OpenStruct.new(canonical: {'expected' => false}) test_case.extend(Assertion) assert_equal 'refute', test_case.assert end def test_assert_equal - test_case = OpenStruct.new(case_data: {'expected' => 2}) + test_case = OpenStruct.new(canonical: {'expected' => 2}) test_case.extend(Assertion) assert_equal "assert_equal 2, 4", test_case.assert_equal { 1 + 3 } end def test_assert_equal_when_nil - test_case = OpenStruct.new(case_data: {'expected' => nil}) + test_case = OpenStruct.new(canonical: {'expected' => nil}) test_case.extend(Assertion) assert_equal "assert_nil 4", test_case.assert_equal { 1 + 3 } end def test_raises_error - test_case = OpenStruct.new(case_data: {'expected' => -1}) + test_case = OpenStruct.new(canonical: {'expected' => -1}) test_case.extend(Assertion) assert test_case.raises_error? end def test_does_not_raise_error - test_case = OpenStruct.new(case_data: {'expected' => 'cute kitties'}) + test_case = OpenStruct.new(canonical: {'expected' => 'cute kitties'}) test_case.extend(Assertion) refute test_case.raises_error? end def test_assert_raises - test_case = OpenStruct.new(case_data: {}) + test_case = OpenStruct.new(canonical: {}) test_case.extend(Assertion) assert_equal( "assert_raises(ArgumentError) { 4 }", diff --git a/test/generator/exercise_case_test.rb b/test/generator/exercise_case_test.rb index 914d8f2a90..67c991d608 100644 --- a/test/generator/exercise_case_test.rb +++ b/test/generator/exercise_case_test.rb @@ -3,15 +3,15 @@ module Generator class ExerciseCaseTest < Minitest::Test def test_name - assert_equal 'test_foo', ExerciseCase.new(case_data: {'description' => 'foo'}).name + assert_equal 'test_foo', ExerciseCase.new(canonical: {'description' => 'foo'}).name end def test_skipped_index_zero - assert_equal '# skip', ExerciseCase.new(case_data: {}).skipped(0) + assert_equal '# skip', ExerciseCase.new(canonical: {}).skipped(0) end def test_skipped_index_nonzero - assert_equal 'skip', ExerciseCase.new(case_data: {}).skipped(1) + assert_equal 'skip', ExerciseCase.new(canonical: {}).skipped(1) end end end From a6e77a51e3de042423a88eb52cfa2c54fcd134f7 Mon Sep 17 00:00:00 2001 From: Geoff Hubbard Date: Sun, 21 May 2017 22:49:23 +0100 Subject: [PATCH 04/10] Turn canonical into an OpenStruct. So we can access its properties using dot notation. --- lib/generator/case_values.rb | 2 +- lib/generator/exercise_case.rb | 2 +- lib/generator/exercise_case/assertion.rb | 8 ++++---- .../exercises/alpha/.meta/generator/alpha_case.rb | 2 +- test/generator/exercise_case/assertion_test.rb | 14 +++++++------- test/generator/exercise_case_test.rb | 7 ++++--- 6 files changed, 18 insertions(+), 17 deletions(-) diff --git a/lib/generator/case_values.rb b/lib/generator/case_values.rb index 71c85b3628..27ff0a05f6 100644 --- a/lib/generator/case_values.rb +++ b/lib/generator/case_values.rb @@ -7,7 +7,7 @@ def initialize(case_class:) def cases(exercise_data) extract_test_cases(data: JSON.parse(exercise_data)['cases']) - .map { |test| @case_class.new(canonical: test) } + .map { |case_properties| @case_class.new(canonical: OpenStruct.new(case_properties)) } end private diff --git a/lib/generator/exercise_case.rb b/lib/generator/exercise_case.rb index 744d4f183d..4525844ac6 100644 --- a/lib/generator/exercise_case.rb +++ b/lib/generator/exercise_case.rb @@ -12,7 +12,7 @@ def initialize(canonical:) end def name - 'test_%s' % canonical['description'].underscore + 'test_%s' % canonical.description.underscore end def skipped(index) diff --git a/lib/generator/exercise_case/assertion.rb b/lib/generator/exercise_case/assertion.rb index 41861b148b..759764d50f 100644 --- a/lib/generator/exercise_case/assertion.rb +++ b/lib/generator/exercise_case/assertion.rb @@ -14,7 +14,7 @@ module Assertion # "#{assert} Luhn.valid?(#{input.inspect})" # def assert - canonical['expected'] ? 'assert' : 'refute' + canonical.expected ? 'assert' : 'refute' end # generates assertions of the form @@ -29,8 +29,8 @@ def assert # assert_equal { "PigLatin.translate(#{input.inspect})" } # def assert_equal - assertion = canonical['expected'].nil? ? 'assert_nil' : - "assert_equal #{canonical['expected'].inspect}," + assertion = canonical.expected.nil? ? 'assert_nil' : + "assert_equal #{canonical.expected.inspect}," "#{assertion} #{yield}" end @@ -43,7 +43,7 @@ def assert_equal # end # def raises_error? - canonical['expected'].to_i == -1 + canonical.expected.to_i == -1 end # generates assertions of the form diff --git a/test/fixtures/xruby/exercises/alpha/.meta/generator/alpha_case.rb b/test/fixtures/xruby/exercises/alpha/.meta/generator/alpha_case.rb index 2d8c8cc455..deacfe3f72 100644 --- a/test/fixtures/xruby/exercises/alpha/.meta/generator/alpha_case.rb +++ b/test/fixtures/xruby/exercises/alpha/.meta/generator/alpha_case.rb @@ -1,6 +1,6 @@ class AlphaCase < Generator::ExerciseCase def name - format('test_%s', canonical['description'].downcase.gsub(/[ -]/, '_')) + format('test_%s', canonical.description.downcase.gsub(/[ -]/, '_')) end def workload diff --git a/test/generator/exercise_case/assertion_test.rb b/test/generator/exercise_case/assertion_test.rb index 4a3b8bcd9f..d1057e6d1c 100644 --- a/test/generator/exercise_case/assertion_test.rb +++ b/test/generator/exercise_case/assertion_test.rb @@ -4,43 +4,43 @@ module Generator class ExerciseCase class AssertionTest < Minitest::Test def test_assert - test_case = OpenStruct.new(canonical: {'expected' => true}) + test_case = ExerciseCase.new(canonical: OpenStruct.new({'expected' => true})) test_case.extend(Assertion) assert_equal 'assert', test_case.assert end def test_refute - test_case = OpenStruct.new(canonical: {'expected' => false}) + test_case = ExerciseCase.new(canonical: OpenStruct.new({'expected' => false})) test_case.extend(Assertion) assert_equal 'refute', test_case.assert end def test_assert_equal - test_case = OpenStruct.new(canonical: {'expected' => 2}) + test_case = ExerciseCase.new(canonical: OpenStruct.new({'expected' => 2})) test_case.extend(Assertion) assert_equal "assert_equal 2, 4", test_case.assert_equal { 1 + 3 } end def test_assert_equal_when_nil - test_case = OpenStruct.new(canonical: {'expected' => nil}) + test_case = ExerciseCase.new(canonical: OpenStruct.new({'expected' => nil})) test_case.extend(Assertion) assert_equal "assert_nil 4", test_case.assert_equal { 1 + 3 } end def test_raises_error - test_case = OpenStruct.new(canonical: {'expected' => -1}) + test_case = ExerciseCase.new(canonical: OpenStruct.new({'expected' => -1})) test_case.extend(Assertion) assert test_case.raises_error? end def test_does_not_raise_error - test_case = OpenStruct.new(canonical: {'expected' => 'cute kitties'}) + test_case = ExerciseCase.new(canonical: OpenStruct.new({'expected' => 'cute kitties'})) test_case.extend(Assertion) refute test_case.raises_error? end def test_assert_raises - test_case = OpenStruct.new(canonical: {}) + test_case = ExerciseCase.new(canonical: OpenStruct.new({})) test_case.extend(Assertion) assert_equal( "assert_raises(ArgumentError) { 4 }", diff --git a/test/generator/exercise_case_test.rb b/test/generator/exercise_case_test.rb index 67c991d608..59cec25b0d 100644 --- a/test/generator/exercise_case_test.rb +++ b/test/generator/exercise_case_test.rb @@ -3,15 +3,16 @@ module Generator class ExerciseCaseTest < Minitest::Test def test_name - assert_equal 'test_foo', ExerciseCase.new(canonical: {'description' => 'foo'}).name + subject = ExerciseCase.new(canonical: OpenStruct.new('description' => 'foo')) + assert_equal 'test_foo', subject.name end def test_skipped_index_zero - assert_equal '# skip', ExerciseCase.new(canonical: {}).skipped(0) + assert_equal '# skip', ExerciseCase.new(canonical: nil).skipped(0) end def test_skipped_index_nonzero - assert_equal 'skip', ExerciseCase.new(canonical: {}).skipped(1) + assert_equal 'skip', ExerciseCase.new(canonical: nil).skipped(1) end end end From 3f5328a3a5457f78922969180af238ab2ea7078a Mon Sep 17 00:00:00 2001 From: Geoff Hubbard Date: Sun, 21 May 2017 22:49:23 +0100 Subject: [PATCH 05/10] Forward canonical data methods to canonical instance variable --- lib/generator/exercise_case.rb | 5 +++++ test/generator/exercise_case_test.rb | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/generator/exercise_case.rb b/lib/generator/exercise_case.rb index 4525844ac6..0264450489 100644 --- a/lib/generator/exercise_case.rb +++ b/lib/generator/exercise_case.rb @@ -18,5 +18,10 @@ def name def skipped(index) index.zero? ? '# skip' : 'skip' end + + def method_missing(sym, *args, &block) + return canonical.send(sym) if canonical.respond_to?(sym) + super(sym, *args, &block) + end end end diff --git a/test/generator/exercise_case_test.rb b/test/generator/exercise_case_test.rb index 59cec25b0d..1a02c02ca1 100644 --- a/test/generator/exercise_case_test.rb +++ b/test/generator/exercise_case_test.rb @@ -14,5 +14,20 @@ def test_skipped_index_zero def test_skipped_index_nonzero assert_equal 'skip', ExerciseCase.new(canonical: nil).skipped(1) end + + def test_forwarding_to_canonical + mock_canonical = Minitest::Mock.new + mock_canonical.expect :key, 'fake value' + subject = ExerciseCase.new(canonical: mock_canonical) + subject.key + mock_canonical.verify + end + + def test_method_mising_calls_super + subject = ExerciseCase.new(canonical: nil) + assert_raises NoMethodError do + subject.key + end + end end end From 58661b57e570857eb293b03c601c5d6ab61b9852 Mon Sep 17 00:00:00 2001 From: Geoff Hubbard Date: Sun, 21 May 2017 22:49:23 +0100 Subject: [PATCH 06/10] Add respond_to? method --- lib/generator/exercise_case.rb | 4 ++++ test/generator/exercise_case_test.rb | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/generator/exercise_case.rb b/lib/generator/exercise_case.rb index 0264450489..6278494adb 100644 --- a/lib/generator/exercise_case.rb +++ b/lib/generator/exercise_case.rb @@ -23,5 +23,9 @@ def method_missing(sym, *args, &block) return canonical.send(sym) if canonical.respond_to?(sym) super(sym, *args, &block) end + + def respond_to?(sym, include_private = false) + canonical.respond_to?(sym) || super + end end end diff --git a/test/generator/exercise_case_test.rb b/test/generator/exercise_case_test.rb index 1a02c02ca1..53edaba968 100644 --- a/test/generator/exercise_case_test.rb +++ b/test/generator/exercise_case_test.rb @@ -29,5 +29,15 @@ def test_method_mising_calls_super subject.key end end + + def test_true_respond_to? + subject = ExerciseCase.new(canonical: OpenStruct.new('key' => 'value')) + assert subject.respond_to?(:key) + end + + def test_false_respond_to? + subject = ExerciseCase.new(canonical: OpenStruct.new({})) + refute subject.respond_to?(:key) + end end end From 8844c7ae8c3bddcf9a65a8850204cc6d75081845 Mon Sep 17 00:00:00 2001 From: Geoff Hubbard Date: Sun, 21 May 2017 22:49:23 +0100 Subject: [PATCH 07/10] Repair problematic exericses --- exercises/anagram/.meta/generator/anagram_case.rb | 2 +- exercises/beer-song/.meta/generator/beer_song_case.rb | 2 +- exercises/clock/.meta/generator/clock_case.rb | 2 +- exercises/connect/.meta/generator/connect_case.rb | 3 --- exercises/connect/.meta/generator/test_template.erb | 2 +- 5 files changed, 4 insertions(+), 7 deletions(-) diff --git a/exercises/anagram/.meta/generator/anagram_case.rb b/exercises/anagram/.meta/generator/anagram_case.rb index 1cff0f77ed..161072dbd0 100644 --- a/exercises/anagram/.meta/generator/anagram_case.rb +++ b/exercises/anagram/.meta/generator/anagram_case.rb @@ -13,7 +13,7 @@ def indent_lines(code, indent = 2) end def show_comment - "# #{comment}" unless comment.nil? + "# #{comment}" if respond_to?(:comment) end def detector diff --git a/exercises/beer-song/.meta/generator/beer_song_case.rb b/exercises/beer-song/.meta/generator/beer_song_case.rb index 4b4f2e4649..fa41300405 100644 --- a/exercises/beer-song/.meta/generator/beer_song_case.rb +++ b/exercises/beer-song/.meta/generator/beer_song_case.rb @@ -17,7 +17,7 @@ def beer_song_arguments if property == 'verse' number else - "%s, %s" % [self["beginning"], self["end"]] + "%s, %s" % [self.beginning, self.end] end end end diff --git a/exercises/clock/.meta/generator/clock_case.rb b/exercises/clock/.meta/generator/clock_case.rb index 4d52ea079c..f8aa326074 100644 --- a/exercises/clock/.meta/generator/clock_case.rb +++ b/exercises/clock/.meta/generator/clock_case.rb @@ -29,7 +29,7 @@ def simple_test end def add_to_clock - " + #{add}" if add + " + #{add}" if respond_to?(:add) end end diff --git a/exercises/connect/.meta/generator/connect_case.rb b/exercises/connect/.meta/generator/connect_case.rb index e1b4b8a2fc..0bb766c1b1 100644 --- a/exercises/connect/.meta/generator/connect_case.rb +++ b/exercises/connect/.meta/generator/connect_case.rb @@ -19,7 +19,4 @@ def single_quote(string) string.inspect.tr('"', "'") end - def ignore_method_length - "# rubocop:disable MethodLength\n " if board.length > 8 - end end diff --git a/exercises/connect/.meta/generator/test_template.erb b/exercises/connect/.meta/generator/test_template.erb index 8fb2c0b11e..17ef595630 100644 --- a/exercises/connect/.meta/generator/test_template.erb +++ b/exercises/connect/.meta/generator/test_template.erb @@ -5,7 +5,7 @@ require_relative 'connect' # Common test data version: <%= canonical_data_version %> <%= abbreviated_commit_hash %> class ConnectTest < Minitest::Test <% test_cases.each_with_index do |test_case, idx| %> - <%= test_case.ignore_method_length%>def <%= test_case.name %> + def <%= test_case.name %> <%= test_case.skipped(idx) %><% test_case.test_body.each do |line| %> <%= line %><% end %> end From 144d61f072ebf6d02300735cc1e3efd6bca24f04 Mon Sep 17 00:00:00 2001 From: Geoff Hubbard Date: Sun, 21 May 2017 22:49:23 +0100 Subject: [PATCH 08/10] Revert "Remove wordy_cases_tests" This reverts commit 56192974a5b79ed45b74f9bc00bb4a77b386e40a. --- test/wordy_cases_test.rb | 43 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 test/wordy_cases_test.rb diff --git a/test/wordy_cases_test.rb b/test/wordy_cases_test.rb new file mode 100644 index 0000000000..fa5a7c6517 --- /dev/null +++ b/test/wordy_cases_test.rb @@ -0,0 +1,43 @@ +require 'json' +require_relative 'test_helper' +require_relative '../exercises/wordy/.meta/generator/wordy_case' + +class WordyCaseTest < Minitest::Test + def test_workload_with_expected_and_no_message + test_case = WordyCase.new(expected: 1, input: 1) + + expected_workload = [ + 'question = \'1\'', + ' assert_equal(1, WordProblem.new(question).answer)', + ].join("\n") + + assert_equal expected_workload, test_case.workload + end + + def test_workload_with_expected_and_message + test_case = WordyCase.new(expected: 1, input: 'What is -3 plus 7 multiplied by -2?') + message = test_case.send(:message) + + expected_workload = [ + "question = 'What is -3 plus 7 multiplied by -2?'", + ' answer = WordProblem.new(question).answer', + ' message = "' + message[0..-3] + '#{answer}"', + ' assert_equal(1, answer, message)', + ].join("\n") + + assert_equal expected_workload, test_case.workload + end + + def test_workload_without_expected + test_case = WordyCase.new(input: 1) + + expected_workload = [ + 'question = \'1\'', + ' assert_raises ArgumentError do', + ' WordProblem.new(question).answer', + ' end', + ].join("\n") + + assert_equal expected_workload, test_case.workload + end +end From e08ebb200b325b0c35050428967f7b28b9c06064 Mon Sep 17 00:00:00 2001 From: Geoff Hubbard Date: Sun, 21 May 2017 22:49:24 +0100 Subject: [PATCH 09/10] Update wordy cases and tests to use injected canonical data --- exercises/wordy/.meta/generator/wordy_case.rb | 2 +- test/wordy_cases_test.rb | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/exercises/wordy/.meta/generator/wordy_case.rb b/exercises/wordy/.meta/generator/wordy_case.rb index 18d73ec7c7..2b99daa31d 100644 --- a/exercises/wordy/.meta/generator/wordy_case.rb +++ b/exercises/wordy/.meta/generator/wordy_case.rb @@ -16,7 +16,7 @@ def indent(size, lines) end def assertion - return error_assertion unless expected + return error_assertion if expected == false return message_assertion if message "assert_equal(#{expected}, WordProblem.new(question).answer)" diff --git a/test/wordy_cases_test.rb b/test/wordy_cases_test.rb index fa5a7c6517..0adfe65aab 100644 --- a/test/wordy_cases_test.rb +++ b/test/wordy_cases_test.rb @@ -4,7 +4,7 @@ class WordyCaseTest < Minitest::Test def test_workload_with_expected_and_no_message - test_case = WordyCase.new(expected: 1, input: 1) + test_case = WordyCase.new(canonical: OpenStruct.new(expected: 1, input: 1)) expected_workload = [ 'question = \'1\'', @@ -15,7 +15,7 @@ def test_workload_with_expected_and_no_message end def test_workload_with_expected_and_message - test_case = WordyCase.new(expected: 1, input: 'What is -3 plus 7 multiplied by -2?') + test_case = WordyCase.new(canonical: OpenStruct.new(expected: 1, input: 'What is -3 plus 7 multiplied by -2?')) message = test_case.send(:message) expected_workload = [ @@ -28,8 +28,8 @@ def test_workload_with_expected_and_message assert_equal expected_workload, test_case.workload end - def test_workload_without_expected - test_case = WordyCase.new(input: 1) + def test_workload_with_expected_false + test_case = WordyCase.new(canonical: OpenStruct.new(input: 1, expected: false)) expected_workload = [ 'question = \'1\'', From 9d43beaa34602864ca2b48b9cbadd3012fc6af0d Mon Sep 17 00:00:00 2001 From: Geoff Hubbard Date: Wed, 31 May 2017 08:26:55 +0100 Subject: [PATCH 10/10] Make OpenStruct constructors use symbols. --- test/generator/exercise_case_test.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/generator/exercise_case_test.rb b/test/generator/exercise_case_test.rb index 53edaba968..2b0575f407 100644 --- a/test/generator/exercise_case_test.rb +++ b/test/generator/exercise_case_test.rb @@ -3,7 +3,7 @@ module Generator class ExerciseCaseTest < Minitest::Test def test_name - subject = ExerciseCase.new(canonical: OpenStruct.new('description' => 'foo')) + subject = ExerciseCase.new(canonical: OpenStruct.new(description: 'foo')) assert_equal 'test_foo', subject.name end @@ -31,12 +31,12 @@ def test_method_mising_calls_super end def test_true_respond_to? - subject = ExerciseCase.new(canonical: OpenStruct.new('key' => 'value')) + subject = ExerciseCase.new(canonical: OpenStruct.new(key: 'value')) assert subject.respond_to?(:key) end def test_false_respond_to? - subject = ExerciseCase.new(canonical: OpenStruct.new({})) + subject = ExerciseCase.new(canonical: OpenStruct.new()) refute subject.respond_to?(:key) end end