From bb75f8107f51466418afc450dfd9b7865b7248bf Mon Sep 17 00:00:00 2001 From: Tommy Schaefer Date: Mon, 19 Dec 2016 08:09:44 -0600 Subject: [PATCH] Generate wordy tests --- exercises/wordy/.meta/.version | 1 + exercises/wordy/example.rb | 4 ++ exercises/wordy/example.tt | 17 ++++++ exercises/wordy/wordy_test.rb | 101 +++++++++++++++++++++----------- lib/wordy_cases.rb | 67 ++++++++++++++++++++++ test/wordy_cases_test.rb | 102 +++++++++++++++++++++++++++++++++ 6 files changed, 257 insertions(+), 35 deletions(-) create mode 100644 exercises/wordy/.meta/.version create mode 100644 exercises/wordy/example.tt create mode 100644 lib/wordy_cases.rb create mode 100644 test/wordy_cases_test.rb diff --git a/exercises/wordy/.meta/.version b/exercises/wordy/.meta/.version new file mode 100644 index 0000000000..56a6051ca2 --- /dev/null +++ b/exercises/wordy/.meta/.version @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git a/exercises/wordy/example.rb b/exercises/wordy/example.rb index 6c522dcaad..b072bdb403 100644 --- a/exercises/wordy/example.rb +++ b/exercises/wordy/example.rb @@ -1,3 +1,7 @@ +module BookKeeping + VERSION = 1 +end + class WordProblem attr_reader :question def initialize(question) diff --git a/exercises/wordy/example.tt b/exercises/wordy/example.tt new file mode 100644 index 0000000000..c109ab9814 --- /dev/null +++ b/exercises/wordy/example.tt @@ -0,0 +1,17 @@ +require 'minitest/autorun' +require_relative 'wordy' + +# Test data version: <%= sha1 %> +class WordyTest < 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 diff --git a/exercises/wordy/wordy_test.rb b/exercises/wordy/wordy_test.rb index ed6e3ed3f2..46312f0c9b 100755 --- a/exercises/wordy/wordy_test.rb +++ b/exercises/wordy/wordy_test.rb @@ -1,97 +1,128 @@ -#!/usr/bin/env ruby -gem 'minitest', '>= 5.0.0' require 'minitest/autorun' require_relative 'wordy' -class WordProblemTest < Minitest::Test - def test_add_1 - assert_equal 2, WordProblem.new('What is 1 plus 1?').answer +# Test data version: aa12f2e +class WordyTest < Minitest::Test + def test_addition + # skip + question = 'What is 1 plus 1?' + assert_equal(2, WordProblem.new(question).answer) end - def test_add_2 + def test_more_addition skip - assert_equal 55, WordProblem.new('What is 53 plus 2?').answer + question = 'What is 53 plus 2?' + assert_equal(55, WordProblem.new(question).answer) end - def test_add_negative_numbers + def test_addition_with_negative_numbers skip - assert_equal(-11, WordProblem.new('What is -1 plus -10?').answer) + question = 'What is -1 plus -10?' + assert_equal(-11, WordProblem.new(question).answer) end - def test_add_more_digits + def test_large_addition skip - assert_equal 45_801, WordProblem.new('What is 123 plus 45678?').answer + question = 'What is 123 plus 45678?' + assert_equal(45801, WordProblem.new(question).answer) end - def test_subtract + def test_subtraction skip - assert_equal 16, WordProblem.new('What is 4 minus -12?').answer + question = 'What is 4 minus -12?' + assert_equal(16, WordProblem.new(question).answer) end - def test_multiply + def test_multiplication skip - assert_equal(-75, WordProblem.new('What is -3 multiplied by 25?').answer) + question = 'What is -3 multiplied by 25?' + assert_equal(-75, WordProblem.new(question).answer) end - def test_divide + def test_division skip - assert_equal(-11, WordProblem.new('What is 33 divided by -3?').answer) + question = 'What is 33 divided by -3?' + assert_equal(-11, WordProblem.new(question).answer) end - def test_add_twice + def test_multiple_additions skip question = 'What is 1 plus 1 plus 1?' - assert_equal 3, WordProblem.new(question).answer + assert_equal(3, WordProblem.new(question).answer) end - def test_add_then_subtract + def test_addition_and_subtraction skip question = 'What is 1 plus 5 minus -2?' - assert_equal 8, WordProblem.new(question).answer + assert_equal(8, WordProblem.new(question).answer) end - def test_subtract_twice + def test_multiple_subtraction skip question = 'What is 20 minus 4 minus 13?' - assert_equal 3, WordProblem.new(question).answer + assert_equal(3, WordProblem.new(question).answer) end - def test_subtract_then_add + def test_subtraction_then_addition skip question = 'What is 17 minus 6 plus 3?' - assert_equal 14, WordProblem.new(question).answer + assert_equal(14, WordProblem.new(question).answer) end - def test_multiply_twice + def test_multiple_multiplication skip question = 'What is 2 multiplied by -2 multiplied by 3?' assert_equal(-12, WordProblem.new(question).answer) end - def test_add_then_multiply + def test_addition_and_multiplication skip question = 'What is -3 plus 7 multiplied by -2?' - message = 'You should ignore order of precedence. -3 + 7 * -2 = -8, not -17' - assert_equal(-8, WordProblem.new(question).answer, message) + answer = WordProblem.new(question).answer + message = "You should ignore order of precedence. -3 + 7 * -2 = -8, not #{answer}" + assert_equal(-8, answer, message) end - def test_divide_twice + def test_multiple_division skip question = 'What is -12 divided by 2 divided by -3?' - assert_equal 2, WordProblem.new(question).answer + assert_equal(2, WordProblem.new(question).answer) end - def test_too_advanced + def test_unknown_operation skip + question = 'What is 52 cubed?' assert_raises ArgumentError do - WordProblem.new('What is 53 cubed?').answer + WordProblem.new(question).answer end end - def test_irrelevant + def test_non_math_question skip + question = 'Who is the President of the United States?' assert_raises ArgumentError do - WordProblem.new('Who is the president of the United States?').answer + WordProblem.new(question).answer end end + + # Problems in exercism evolve over time, as we find better ways to ask + # questions. + # The version number refers to the version of the problem you solved, + # not your solution. + # + # Define a constant named VERSION inside of the top level BookKeeping + # module, which may be placed near the end of your file. + # + # In your file, it will look like this: + # + # module BookKeeping + # VERSION = 1 # Where the version number matches the one in the test. + # end + # + # If you are curious, read more about constants on RubyDoc: + # http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html + def test_bookkeeping + skip + assert_equal 1, BookKeeping::VERSION + end end diff --git a/lib/wordy_cases.rb b/lib/wordy_cases.rb new file mode 100644 index 0000000000..15642f02a6 --- /dev/null +++ b/lib/wordy_cases.rb @@ -0,0 +1,67 @@ +class WordyCase < OpenStruct + def test_name + 'test_%s' % description.downcase.tr(' ', '_') + end + + def workload + [ + "question = '#{input}'", + indent(4, assertion), + ].join("\n") + end + + def skipped + index.zero? ? '# skip' : 'skip' + end + + private + + def indent(size, lines) + lines.lines.each_with_object('') { |line, obj| obj << ' ' * size + line } + end + + def assertion + return error_assertion unless expected + return message_assertion if message + + "assert_equal(#{expected}, WordProblem.new(question).answer)" + end + + def error_assertion + [ + 'assert_raises ArgumentError do', + indent(2, 'WordProblem.new(question).answer'), + 'end', + ].join("\n") + end + + def message_assertion + [ + 'answer = WordProblem.new(question).answer', + "message = \"#{message % '#{answer}'}\"", + "assert_equal(#{expected}, answer, message)", + ].join("\n") + end +end + +class WordyCase::PreProcessor + class << self + def call(row) + row.merge('message' => message_for(row)) + end + + private + + def message_for(row) + return unless row['input'] == 'What is -3 plus 7 multiplied by -2?' + + 'You should ignore order of precedence. -3 + 7 * -2 = -8, not %s' + end + end +end + +WordyCases = proc do |data| + JSON.parse(data)['cases'].map.with_index do |row, i| + WordyCase.new(WordyCase::PreProcessor.call(row).merge(index: i)) + end +end diff --git a/test/wordy_cases_test.rb b/test/wordy_cases_test.rb new file mode 100644 index 0000000000..12307353e2 --- /dev/null +++ b/test/wordy_cases_test.rb @@ -0,0 +1,102 @@ +require_relative 'test_helper' + +class WordyCaseTest < Minitest::Test + def test_test_name + test_case = WordyCase.new(description: 'description') + + assert_equal 'test_description', test_case.test_name + end + + def test_test_name_with_description_with_spaces + test_case = WordyCase.new(description: 'description with spaces') + + assert_equal 'test_description_with_spaces', test_case.test_name + end + + def test_skipped_with_zero_index + test_case = WordyCase.new(index: 0) + + assert_equal '# skip', test_case.skipped + end + + def test_skipped_with_non_zero_index + test_case = WordyCase.new(index: 1) + + assert_equal 'skip', test_case.skipped + end + + 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: 1, message: 'test %s') + + expected_workload = [ + 'question = \'1\'', + ' answer = WordProblem.new(question).answer', + ' message = "test #{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 + +class WordyCasePrProcessorTest < Minitest::Test + def test_call_as_non_special_case + row = { 'input' => '' } + processed_row = WordyCase::PreProcessor.call(row) + + assert_equal({ 'input' => '', 'message' => nil }, processed_row) + end + + def test_call_as_special_case + row = { 'input' => 'What is -3 plus 7 multiplied by -2?' } + processed_row = WordyCase::PreProcessor.call(row) + expected_row = { + 'input' => 'What is -3 plus 7 multiplied by -2?', + 'message' => 'You should ignore order of precedence. -3 + 7 * -2 = -8, not %s', + } + + assert_equal expected_row, processed_row + end +end + +class WordyCasesTest < Minitest::Test + def test_call + json = { + cases: [ + { description: 'test 1' }, + { description: 'test 2' }, + ] + }.to_json + + expected_cases = [ + WordyCase.new(description: 'test 1', message: nil, index: 0), + WordyCase.new(description: 'test 2', message: nil, index: 1), + ] + + assert_equal expected_cases, WordyCases.call(json) + end +end