diff --git a/bin/generate b/bin/generate index fd6f32f277..4966be4299 100755 --- a/bin/generate +++ b/bin/generate @@ -9,6 +9,10 @@ def exercises .select { |file| File.directory? File.join('./exercises/practice', file) } end +def underscore(str) + str.gsub(/[^\w\s-]/, '').gsub(/[-\s]/, '_').downcase +end + class VerificationError < StandardError MESSAGE = 'The result generated for %s, does not match the current file' @@ -39,7 +43,7 @@ end parser.on('--verify', 'Verify all exercises') do exercises.each do |exercise| if File.exist?("./exercises/practice/#{exercise}/.meta/test_template.erb") - current_code = File.read("./exercises/practice/#{exercise}/#{exercise}_test.rb") + current_code = File.read("./exercises/practice/#{exercise}/#{underscore(exercise)}_test.rb") f = File.new("./exercises/practice/#{exercise}/temp_test.rb", 'w+') Generator.new(exercise).generate(f.path) generated_code = f.read diff --git a/exercises/practice/acronym/.meta/example.rb b/exercises/practice/acronym/.meta/example.rb index d0230dd623..7ff6892849 100644 --- a/exercises/practice/acronym/.meta/example.rb +++ b/exercises/practice/acronym/.meta/example.rb @@ -7,9 +7,7 @@ def self.abbreviate(phrase) end.join end - def self.each_word(phrase) - phrase.scan(/[A-Z]+[a-z]*|[a-z]+/) do |word| - yield word - end + def self.each_word(phrase, &block) + phrase.scan(/[A-Za-z]+(?:'[A-Za-z]+)*/, &block) end end diff --git a/exercises/practice/acronym/.meta/test_template.erb b/exercises/practice/acronym/.meta/test_template.erb index 66c45804f7..c2b235f152 100644 --- a/exercises/practice/acronym/.meta/test_template.erb +++ b/exercises/practice/acronym/.meta/test_template.erb @@ -5,7 +5,7 @@ class AcronymTest < Minitest::Test <% json["cases"].each do |cases| %> def test_<%= underscore(cases["description"]) %> <%= skip? %> - assert_equal '<%= cases["expected"] %>', <%= camel_case(json["exercise"]) %>.<%= underscore(cases["property"]) %>('<%= cases["input"]["phrase"] %>') + assert_equal '<%= cases["expected"] %>', <%= camel_case(json["exercise"]) %>.<%= underscore(cases["property"]) %>('<%= cases["input"]["phrase"].gsub("'", "\\\\'") %>') end <% end %> end diff --git a/exercises/practice/acronym/acronym_test.rb b/exercises/practice/acronym/acronym_test.rb index 7dde064850..0716d5b5b8 100644 --- a/exercises/practice/acronym/acronym_test.rb +++ b/exercises/practice/acronym/acronym_test.rb @@ -36,4 +36,14 @@ def test_consecutive_delimiters skip assert_equal 'SIMUFTA', Acronym.abbreviate('Something - I made up from thin air') end + + def test_apostrophes + skip + assert_equal 'HC', Acronym.abbreviate('Halley\'s Comet') + end + + def test_underscore_emphasis + skip + assert_equal 'TRNT', Acronym.abbreviate('The Road _Not_ Taken') + end end diff --git a/exercises/practice/affine-cipher/.meta/test_template.erb b/exercises/practice/affine-cipher/.meta/test_template.erb new file mode 100644 index 0000000000..0a62fdb4e3 --- /dev/null +++ b/exercises/practice/affine-cipher/.meta/test_template.erb @@ -0,0 +1,26 @@ +require 'minitest/autorun' +require_relative 'affine_cipher' + +class AffineCipherTest < Minitest::Test +<% json["cases"].each do |group| %> + <% group["cases"].each do |sub_case| %> + def test_<%= underscore(sub_case["description"]) %> + <%= skip? %> + <%- if sub_case["expected"].is_a?(Hash) && sub_case["expected"].key?("error") -%> + assert_raises(ArgumentError) { Affine.new(<%= sub_case["input"]["key"]["a"] %>, <%= sub_case["input"]["key"]["b"] %>) } + <%- else -%> + cipher = Affine.new(<%= sub_case["input"]["key"]["a"] %>, <%= sub_case["input"]["key"]["b"] %>) + <%- if sub_case["property"] == "encode" -%> + plaintext = '<%= sub_case["input"]["phrase"] %>' + ciphertext = '<%= sub_case["expected"] %>' + assert_equal ciphertext, cipher.encode(plaintext) + <%- elsif sub_case["property"] == "decode" -%> + ciphertext = '<%= sub_case["input"]["phrase"] %>' + plaintext = '<%= sub_case["expected"] %>' + assert_equal plaintext, cipher.decode(ciphertext) + <%- end -%> + <%- end -%> + end + <% end %> +<% end %> +end diff --git a/exercises/practice/all-your-base/.meta/test_template.erb b/exercises/practice/all-your-base/.meta/test_template.erb new file mode 100644 index 0000000000..b1260006f0 --- /dev/null +++ b/exercises/practice/all-your-base/.meta/test_template.erb @@ -0,0 +1,26 @@ +require 'minitest/autorun' +require_relative 'all_your_base' + +class AllYourBaseTest < Minitest::Test +<% json["cases"].each do |cases| %> + def test_<%= underscore(cases["description"]) %> + <%= skip? %> + digits = <%= cases["input"]["digits"] %> + input_base = <%= cases["input"]["inputBase"] %> + output_base = <%= cases["input"]["outputBase"] %> + <% if cases["expected"].is_a?(Hash) && cases["expected"].key?("error") %> + assert_raises(ArgumentError) do + BaseConverter.convert(input_base, digits, output_base) + end + <% else %>expected = <%= cases["expected"] %> + + converted = BaseConverter.convert(input_base, digits, output_base) + + hint = "Input base: #{input_base}, output base #{output_base}. " \ + "Expected #{expected} but got #{converted}." + + assert_equal expected, converted, hint + <% end %> + end + <% end %> +end diff --git a/exercises/practice/all-your-base/all_your_base_test.rb b/exercises/practice/all-your-base/all_your_base_test.rb index 3ef5f434e5..c565a75aec 100644 --- a/exercises/practice/all-your-base/all_your_base_test.rb +++ b/exercises/practice/all-your-base/all_your_base_test.rb @@ -11,8 +11,8 @@ def test_single_bit_one_to_decimal converted = BaseConverter.convert(input_base, digits, output_base) - hint = "Input base: 2, output base 10. " \ - "Expected #{expected} but got #{converted}." + hint = "Input base: #{input_base}, output base #{output_base}. " \ + "Expected #{expected} but got #{converted}." assert_equal expected, converted, hint end @@ -26,8 +26,8 @@ def test_binary_to_single_decimal converted = BaseConverter.convert(input_base, digits, output_base) - hint = "Input base: 2, output base 10. " \ - "Expected #{expected} but got #{converted}." + hint = "Input base: #{input_base}, output base #{output_base}. " \ + "Expected #{expected} but got #{converted}." assert_equal expected, converted, hint end @@ -41,8 +41,8 @@ def test_single_decimal_to_binary converted = BaseConverter.convert(input_base, digits, output_base) - hint = "Input base: 10, output base 2. " \ - "Expected #{expected} but got #{converted}." + hint = "Input base: #{input_base}, output base #{output_base}. " \ + "Expected #{expected} but got #{converted}." assert_equal expected, converted, hint end @@ -56,8 +56,8 @@ def test_binary_to_multiple_decimal converted = BaseConverter.convert(input_base, digits, output_base) - hint = "Input base: 2, output base 10. " \ - "Expected #{expected} but got #{converted}." + hint = "Input base: #{input_base}, output base #{output_base}. " \ + "Expected #{expected} but got #{converted}." assert_equal expected, converted, hint end @@ -71,8 +71,8 @@ def test_decimal_to_binary converted = BaseConverter.convert(input_base, digits, output_base) - hint = "Input base: 10, output base 2. " \ - "Expected #{expected} but got #{converted}." + hint = "Input base: #{input_base}, output base #{output_base}. " \ + "Expected #{expected} but got #{converted}." assert_equal expected, converted, hint end @@ -86,8 +86,8 @@ def test_trinary_to_hexadecimal converted = BaseConverter.convert(input_base, digits, output_base) - hint = "Input base: 3, output base 16. " \ - "Expected #{expected} but got #{converted}." + hint = "Input base: #{input_base}, output base #{output_base}. " \ + "Expected #{expected} but got #{converted}." assert_equal expected, converted, hint end @@ -101,8 +101,8 @@ def test_hexadecimal_to_trinary converted = BaseConverter.convert(input_base, digits, output_base) - hint = "Input base: 16, output base 3. " \ - "Expected #{expected} but got #{converted}." + hint = "Input base: #{input_base}, output base #{output_base}. " \ + "Expected #{expected} but got #{converted}." assert_equal expected, converted, hint end @@ -116,8 +116,8 @@ def test_15_bit_integer converted = BaseConverter.convert(input_base, digits, output_base) - hint = "Input base: 97, output base 73. " \ - "Expected #{expected} but got #{converted}." + hint = "Input base: #{input_base}, output base #{output_base}. " \ + "Expected #{expected} but got #{converted}." assert_equal expected, converted, hint end @@ -131,8 +131,8 @@ def test_empty_list converted = BaseConverter.convert(input_base, digits, output_base) - hint = "Input base: 2, output base 10. " \ - "Expected #{expected} but got #{converted}." + hint = "Input base: #{input_base}, output base #{output_base}. " \ + "Expected #{expected} but got #{converted}." assert_equal expected, converted, hint end @@ -146,8 +146,8 @@ def test_single_zero converted = BaseConverter.convert(input_base, digits, output_base) - hint = "Input base: 10, output base 2. " \ - "Expected #{expected} but got #{converted}." + hint = "Input base: #{input_base}, output base #{output_base}. " \ + "Expected #{expected} but got #{converted}." assert_equal expected, converted, hint end @@ -161,8 +161,8 @@ def test_multiple_zeros converted = BaseConverter.convert(input_base, digits, output_base) - hint = "Input base: 10, output base 2. " \ - "Expected #{expected} but got #{converted}." + hint = "Input base: #{input_base}, output base #{output_base}. " \ + "Expected #{expected} but got #{converted}." assert_equal expected, converted, hint end @@ -176,8 +176,8 @@ def test_leading_zeros converted = BaseConverter.convert(input_base, digits, output_base) - hint = "Input base: 7, output base 10. " \ - "Expected #{expected} but got #{converted}." + hint = "Input base: #{input_base}, output base #{output_base}. " \ + "Expected #{expected} but got #{converted}." assert_equal expected, converted, hint end @@ -187,6 +187,7 @@ def test_input_base_is_one digits = [0] input_base = 1 output_base = 10 + assert_raises(ArgumentError) do BaseConverter.convert(input_base, digits, output_base) end @@ -197,6 +198,7 @@ def test_input_base_is_zero digits = [] input_base = 0 output_base = 10 + assert_raises(ArgumentError) do BaseConverter.convert(input_base, digits, output_base) end @@ -207,6 +209,7 @@ def test_input_base_is_negative digits = [1] input_base = -2 output_base = 10 + assert_raises(ArgumentError) do BaseConverter.convert(input_base, digits, output_base) end @@ -217,6 +220,7 @@ def test_negative_digit digits = [1, -1, 1, 0, 1, 0] input_base = 2 output_base = 10 + assert_raises(ArgumentError) do BaseConverter.convert(input_base, digits, output_base) end @@ -227,6 +231,7 @@ def test_invalid_positive_digit digits = [1, 2, 1, 0, 1, 0] input_base = 2 output_base = 10 + assert_raises(ArgumentError) do BaseConverter.convert(input_base, digits, output_base) end @@ -237,6 +242,7 @@ def test_output_base_is_one digits = [1, 0, 1, 0, 1, 0] input_base = 2 output_base = 1 + assert_raises(ArgumentError) do BaseConverter.convert(input_base, digits, output_base) end @@ -247,6 +253,7 @@ def test_output_base_is_zero digits = [7] input_base = 10 output_base = 0 + assert_raises(ArgumentError) do BaseConverter.convert(input_base, digits, output_base) end @@ -257,6 +264,7 @@ def test_output_base_is_negative digits = [1] input_base = 2 output_base = -7 + assert_raises(ArgumentError) do BaseConverter.convert(input_base, digits, output_base) end @@ -267,6 +275,7 @@ def test_both_bases_are_negative digits = [1] input_base = -2 output_base = -7 + assert_raises(ArgumentError) do BaseConverter.convert(input_base, digits, output_base) end diff --git a/exercises/practice/allergies/.meta/test_template.erb b/exercises/practice/allergies/.meta/test_template.erb new file mode 100644 index 0000000000..70ddced656 --- /dev/null +++ b/exercises/practice/allergies/.meta/test_template.erb @@ -0,0 +1,19 @@ +require 'minitest/autorun' +require_relative 'allergies' + +class AllergiesTest < Minitest::Test +<% json["cases"].each do |group| %> + <% group["cases"].each do |sub_case| %> + def test_<%= underscore(group["description"]) %>_<%= underscore(sub_case["description"]) %> + <%= skip? %> + allergies = Allergies.new(<%= sub_case["input"]["score"] %>) + <%- if sub_case["property"] == "allergicTo" -%> + <%= sub_case["expected"] ? "assert" : "refute" %> allergies.allergic_to?('<%= sub_case["input"]["item"] %>'), 'Tom is<%= sub_case["expected"] ? "" : " not" %> allergic, but it says he is<%= sub_case["expected"] ? " not" : "" %>.' + <%- else -%> + expected = %w[<%= sub_case["expected"].join(" ") %>] + assert_equal expected, allergies.list + <% end %> + end + <% end %> + <% end %> +end diff --git a/exercises/practice/allergies/allergies_test.rb b/exercises/practice/allergies/allergies_test.rb index d5d244968f..f9e6e27c7c 100644 --- a/exercises/practice/allergies/allergies_test.rb +++ b/exercises/practice/allergies/allergies_test.rb @@ -5,300 +5,310 @@ class AllergiesTest < Minitest::Test def test_testing_for_eggs_allergy_not_allergic_to_anything # skip allergies = Allergies.new(0) - refute allergies.allergic_to?("eggs"), "Tom is not allergic, but it says he is." + refute allergies.allergic_to?('eggs'), 'Tom is not allergic, but it says he is.' end def test_testing_for_eggs_allergy_allergic_only_to_eggs skip allergies = Allergies.new(1) - assert allergies.allergic_to?("eggs"), "Tom is allergic, but it says he is not." + assert allergies.allergic_to?('eggs'), 'Tom is allergic, but it says he is not.' end def test_testing_for_eggs_allergy_allergic_to_eggs_and_something_else skip allergies = Allergies.new(3) - assert allergies.allergic_to?("eggs"), "Tom is allergic, but it says he is not." + assert allergies.allergic_to?('eggs'), 'Tom is allergic, but it says he is not.' end def test_testing_for_eggs_allergy_allergic_to_something_but_not_eggs skip allergies = Allergies.new(2) - refute allergies.allergic_to?("eggs"), "Tom is not allergic, but it says he is." + refute allergies.allergic_to?('eggs'), 'Tom is not allergic, but it says he is.' end def test_testing_for_eggs_allergy_allergic_to_everything skip allergies = Allergies.new(255) - assert allergies.allergic_to?("eggs"), "Tom is allergic, but it says he is not." + assert allergies.allergic_to?('eggs'), 'Tom is allergic, but it says he is not.' end def test_testing_for_peanuts_allergy_not_allergic_to_anything skip allergies = Allergies.new(0) - refute allergies.allergic_to?("peanuts"), "Tom is not allergic, but it says he is." + refute allergies.allergic_to?('peanuts'), 'Tom is not allergic, but it says he is.' end def test_testing_for_peanuts_allergy_allergic_only_to_peanuts skip allergies = Allergies.new(2) - assert allergies.allergic_to?("peanuts"), "Tom is allergic, but it says he is not." + assert allergies.allergic_to?('peanuts'), 'Tom is allergic, but it says he is not.' end def test_testing_for_peanuts_allergy_allergic_to_peanuts_and_something_else skip allergies = Allergies.new(7) - assert allergies.allergic_to?("peanuts"), "Tom is allergic, but it says he is not." + assert allergies.allergic_to?('peanuts'), 'Tom is allergic, but it says he is not.' end def test_testing_for_peanuts_allergy_allergic_to_something_but_not_peanuts skip allergies = Allergies.new(5) - refute allergies.allergic_to?("peanuts"), "Tom is not allergic, but it says he is." + refute allergies.allergic_to?('peanuts'), 'Tom is not allergic, but it says he is.' end def test_testing_for_peanuts_allergy_allergic_to_everything skip allergies = Allergies.new(255) - assert allergies.allergic_to?("peanuts"), "Tom is allergic, but it says he is not." + assert allergies.allergic_to?('peanuts'), 'Tom is allergic, but it says he is not.' end def test_testing_for_shellfish_allergy_not_allergic_to_anything skip allergies = Allergies.new(0) - refute allergies.allergic_to?("shellfish"), "Tom is not allergic, but it says he is." + refute allergies.allergic_to?('shellfish'), 'Tom is not allergic, but it says he is.' end def test_testing_for_shellfish_allergy_allergic_only_to_shellfish skip allergies = Allergies.new(4) - assert allergies.allergic_to?("shellfish"), "Tom is allergic, but it says he is not." + assert allergies.allergic_to?('shellfish'), 'Tom is allergic, but it says he is not.' end def test_testing_for_shellfish_allergy_allergic_to_shellfish_and_something_else skip allergies = Allergies.new(14) - assert allergies.allergic_to?("shellfish"), "Tom is allergic, but it says he is not." + assert allergies.allergic_to?('shellfish'), 'Tom is allergic, but it says he is not.' end def test_testing_for_shellfish_allergy_allergic_to_something_but_not_shellfish skip allergies = Allergies.new(10) - refute allergies.allergic_to?("shellfish"), "Tom is not allergic, but it says he is." + refute allergies.allergic_to?('shellfish'), 'Tom is not allergic, but it says he is.' end def test_testing_for_shellfish_allergy_allergic_to_everything skip allergies = Allergies.new(255) - assert allergies.allergic_to?("shellfish"), "Tom is allergic, but it says he is not." + assert allergies.allergic_to?('shellfish'), 'Tom is allergic, but it says he is not.' end def test_testing_for_strawberries_allergy_not_allergic_to_anything skip allergies = Allergies.new(0) - refute allergies.allergic_to?("strawberries"), "Tom is not allergic, but it says he is." + refute allergies.allergic_to?('strawberries'), 'Tom is not allergic, but it says he is.' end def test_testing_for_strawberries_allergy_allergic_only_to_strawberries skip allergies = Allergies.new(8) - assert allergies.allergic_to?("strawberries"), "Tom is allergic, but it says he is not." + assert allergies.allergic_to?('strawberries'), 'Tom is allergic, but it says he is not.' end def test_testing_for_strawberries_allergy_allergic_to_strawberries_and_something_else skip allergies = Allergies.new(28) - assert allergies.allergic_to?("strawberries"), "Tom is allergic, but it says he is not." + assert allergies.allergic_to?('strawberries'), 'Tom is allergic, but it says he is not.' end def test_testing_for_strawberries_allergy_allergic_to_something_but_not_strawberries skip allergies = Allergies.new(20) - refute allergies.allergic_to?("strawberries"), "Tom is not allergic, but it says he is." + refute allergies.allergic_to?('strawberries'), 'Tom is not allergic, but it says he is.' end def test_testing_for_strawberries_allergy_allergic_to_everything skip allergies = Allergies.new(255) - assert allergies.allergic_to?("strawberries"), "Tom is allergic, but it says he is not." + assert allergies.allergic_to?('strawberries'), 'Tom is allergic, but it says he is not.' end def test_testing_for_tomatoes_allergy_not_allergic_to_anything skip allergies = Allergies.new(0) - refute allergies.allergic_to?("tomatoes"), "Tom is not allergic, but it says he is." + refute allergies.allergic_to?('tomatoes'), 'Tom is not allergic, but it says he is.' end def test_testing_for_tomatoes_allergy_allergic_only_to_tomatoes skip allergies = Allergies.new(16) - assert allergies.allergic_to?("tomatoes"), "Tom is allergic, but it says he is not." + assert allergies.allergic_to?('tomatoes'), 'Tom is allergic, but it says he is not.' end def test_testing_for_tomatoes_allergy_allergic_to_tomatoes_and_something_else skip allergies = Allergies.new(56) - assert allergies.allergic_to?("tomatoes"), "Tom is allergic, but it says he is not." + assert allergies.allergic_to?('tomatoes'), 'Tom is allergic, but it says he is not.' end def test_testing_for_tomatoes_allergy_allergic_to_something_but_not_tomatoes skip allergies = Allergies.new(40) - refute allergies.allergic_to?("tomatoes"), "Tom is not allergic, but it says he is." + refute allergies.allergic_to?('tomatoes'), 'Tom is not allergic, but it says he is.' end def test_testing_for_tomatoes_allergy_allergic_to_everything skip allergies = Allergies.new(255) - assert allergies.allergic_to?("tomatoes"), "Tom is allergic, but it says he is not." + assert allergies.allergic_to?('tomatoes'), 'Tom is allergic, but it says he is not.' end def test_testing_for_chocolate_allergy_not_allergic_to_anything skip allergies = Allergies.new(0) - refute allergies.allergic_to?("chocolate"), "Tom is not allergic, but it says he is." + refute allergies.allergic_to?('chocolate'), 'Tom is not allergic, but it says he is.' end def test_testing_for_chocolate_allergy_allergic_only_to_chocolate skip allergies = Allergies.new(32) - assert allergies.allergic_to?("chocolate"), "Tom is allergic, but it says he is not." + assert allergies.allergic_to?('chocolate'), 'Tom is allergic, but it says he is not.' end def test_testing_for_chocolate_allergy_allergic_to_chocolate_and_something_else skip allergies = Allergies.new(112) - assert allergies.allergic_to?("chocolate"), "Tom is allergic, but it says he is not." + assert allergies.allergic_to?('chocolate'), 'Tom is allergic, but it says he is not.' end def test_testing_for_chocolate_allergy_allergic_to_something_but_not_chocolate skip allergies = Allergies.new(80) - refute allergies.allergic_to?("chocolate"), "Tom is not allergic, but it says he is." + refute allergies.allergic_to?('chocolate'), 'Tom is not allergic, but it says he is.' end def test_testing_for_chocolate_allergy_allergic_to_everything skip allergies = Allergies.new(255) - assert allergies.allergic_to?("chocolate"), "Tom is allergic, but it says he is not." + assert allergies.allergic_to?('chocolate'), 'Tom is allergic, but it says he is not.' end def test_testing_for_pollen_allergy_not_allergic_to_anything skip allergies = Allergies.new(0) - refute allergies.allergic_to?("pollen"), "Tom is not allergic, but it says he is." + refute allergies.allergic_to?('pollen'), 'Tom is not allergic, but it says he is.' end def test_testing_for_pollen_allergy_allergic_only_to_pollen skip allergies = Allergies.new(64) - assert allergies.allergic_to?("pollen"), "Tom is allergic, but it says he is not." + assert allergies.allergic_to?('pollen'), 'Tom is allergic, but it says he is not.' end def test_testing_for_pollen_allergy_allergic_to_pollen_and_something_else skip allergies = Allergies.new(224) - assert allergies.allergic_to?("pollen"), "Tom is allergic, but it says he is not." + assert allergies.allergic_to?('pollen'), 'Tom is allergic, but it says he is not.' end def test_testing_for_pollen_allergy_allergic_to_something_but_not_pollen skip allergies = Allergies.new(160) - refute allergies.allergic_to?("pollen"), "Tom is not allergic, but it says he is." + refute allergies.allergic_to?('pollen'), 'Tom is not allergic, but it says he is.' end def test_testing_for_pollen_allergy_allergic_to_everything skip allergies = Allergies.new(255) - assert allergies.allergic_to?("pollen"), "Tom is allergic, but it says he is not." + assert allergies.allergic_to?('pollen'), 'Tom is allergic, but it says he is not.' end def test_testing_for_cats_allergy_not_allergic_to_anything skip allergies = Allergies.new(0) - refute allergies.allergic_to?("cats"), "Tom is not allergic, but it says he is." + refute allergies.allergic_to?('cats'), 'Tom is not allergic, but it says he is.' end def test_testing_for_cats_allergy_allergic_only_to_cats skip allergies = Allergies.new(128) - assert allergies.allergic_to?("cats"), "Tom is allergic, but it says he is not." + assert allergies.allergic_to?('cats'), 'Tom is allergic, but it says he is not.' end def test_testing_for_cats_allergy_allergic_to_cats_and_something_else skip allergies = Allergies.new(192) - assert allergies.allergic_to?("cats"), "Tom is allergic, but it says he is not." + assert allergies.allergic_to?('cats'), 'Tom is allergic, but it says he is not.' end def test_testing_for_cats_allergy_allergic_to_something_but_not_cats skip allergies = Allergies.new(64) - refute allergies.allergic_to?("cats"), "Tom is not allergic, but it says he is." + refute allergies.allergic_to?('cats'), 'Tom is not allergic, but it says he is.' end def test_testing_for_cats_allergy_allergic_to_everything skip allergies = Allergies.new(255) - assert allergies.allergic_to?("cats"), "Tom is allergic, but it says he is not." + assert allergies.allergic_to?('cats'), 'Tom is allergic, but it says he is not.' end def test_list_when_no_allergies skip - expected = [] - assert_equal expected, Allergies.new(0).list + allergies = Allergies.new(0) + expected = %w[] + assert_equal expected, allergies.list end def test_list_when_just_eggs skip - expected = ["eggs"] - assert_equal expected, Allergies.new(1).list + allergies = Allergies.new(1) + expected = %w[eggs] + assert_equal expected, allergies.list end def test_list_when_just_peanuts skip - expected = ["peanuts"] - assert_equal expected, Allergies.new(2).list + allergies = Allergies.new(2) + expected = %w[peanuts] + assert_equal expected, allergies.list end def test_list_when_just_strawberries skip - expected = ["strawberries"] - assert_equal expected, Allergies.new(8).list + allergies = Allergies.new(8) + expected = %w[strawberries] + assert_equal expected, allergies.list end def test_list_when_eggs_and_peanuts skip + allergies = Allergies.new(3) expected = %w[eggs peanuts] - assert_equal expected, Allergies.new(3).list + assert_equal expected, allergies.list end def test_list_when_more_than_eggs_but_not_peanuts skip + allergies = Allergies.new(5) expected = %w[eggs shellfish] - assert_equal expected, Allergies.new(5).list + assert_equal expected, allergies.list end def test_list_when_lots_of_stuff skip + allergies = Allergies.new(248) expected = %w[strawberries tomatoes chocolate pollen cats] - assert_equal expected, Allergies.new(248).list + assert_equal expected, allergies.list end def test_list_when_everything skip + allergies = Allergies.new(255) expected = %w[eggs peanuts shellfish strawberries tomatoes chocolate pollen cats] - assert_equal expected, Allergies.new(255).list + assert_equal expected, allergies.list end def test_list_when_no_allergen_score_parts skip + allergies = Allergies.new(509) expected = %w[eggs shellfish strawberries tomatoes chocolate pollen cats] - assert_equal expected, Allergies.new(509).list + assert_equal expected, allergies.list end def test_list_when_no_allergen_score_parts_without_highest_valid_score skip - expected = ["eggs"] - assert_equal expected, Allergies.new(257).list + allergies = Allergies.new(257) + expected = %w[eggs] + assert_equal expected, allergies.list end end diff --git a/exercises/practice/anagram/.meta/test_template.erb b/exercises/practice/anagram/.meta/test_template.erb new file mode 100644 index 0000000000..003e246495 --- /dev/null +++ b/exercises/practice/anagram/.meta/test_template.erb @@ -0,0 +1,15 @@ +require 'minitest/autorun' +require_relative 'anagram' + +class AnagramTest < Minitest::Test +<% json["cases"].each do |cases| %> + def test_<%= underscore(cases["description"]) %> + <%= skip? %> + detector = Anagram.new('<%= cases["input"]["subject"] %>') + anagrams = detector.match(%w[<%= cases["input"]["candidates"].join(" ") %>]) + expected = %w[<%= cases["expected"].join(" ") %>] + + assert_equal expected, anagrams + end + <% end %> +end diff --git a/exercises/practice/anagram/.meta/tests.toml b/exercises/practice/anagram/.meta/tests.toml index 4d90562705..8b680daca3 100644 --- a/exercises/practice/anagram/.meta/tests.toml +++ b/exercises/practice/anagram/.meta/tests.toml @@ -81,6 +81,8 @@ reimplements = "a0705568-628c-4b55-9798-82e4acde51ca" [a6854f66-eec1-4afd-a137-62ef2870c051] description = "handles case of greek letters" +include = false [fd3509e5-e3ba-409d-ac3d-a9ac84d13296] description = "different characters may have the same bytes" +include = false diff --git a/exercises/practice/anagram/anagram_test.rb b/exercises/practice/anagram/anagram_test.rb index cd5f4fc98c..93bbdd3a9a 100644 --- a/exercises/practice/anagram/anagram_test.rb +++ b/exercises/practice/anagram/anagram_test.rb @@ -4,129 +4,145 @@ class AnagramTest < Minitest::Test def test_no_matches # skip - detector = Anagram.new("diaper") + detector = Anagram.new('diaper') anagrams = detector.match(%w[hello world zombies pants]) - expected = [] + expected = %w[] + assert_equal expected, anagrams end def test_detects_two_anagrams skip - detector = Anagram.new("solemn") + detector = Anagram.new('solemn') anagrams = detector.match(%w[lemons cherry melons]) expected = %w[lemons melons] + assert_equal expected, anagrams end def test_does_not_detect_anagram_subsets skip - detector = Anagram.new("good") + detector = Anagram.new('good') anagrams = detector.match(%w[dog goody]) - expected = [] + expected = %w[] + assert_equal expected, anagrams end def test_detects_anagram skip - detector = Anagram.new("listen") + detector = Anagram.new('listen') anagrams = detector.match(%w[enlists google inlets banana]) - expected = ["inlets"] + expected = %w[inlets] + assert_equal expected, anagrams end def test_detects_three_anagrams skip - detector = Anagram.new("allergy") + detector = Anagram.new('allergy') anagrams = detector.match(%w[gallery ballerina regally clergy largely leading]) expected = %w[gallery regally largely] + assert_equal expected, anagrams end def test_detects_multiple_anagrams_with_different_case skip - detector = Anagram.new("nose") + detector = Anagram.new('nose') anagrams = detector.match(%w[Eons ONES]) expected = %w[Eons ONES] + assert_equal expected, anagrams end def test_does_not_detect_non_anagrams_with_identical_checksum skip - detector = Anagram.new("mass") - anagrams = detector.match(["last"]) - expected = [] + detector = Anagram.new('mass') + anagrams = detector.match(%w[last]) + expected = %w[] + assert_equal expected, anagrams end def test_detects_anagrams_case_insensitively skip - detector = Anagram.new("Orchestra") + detector = Anagram.new('Orchestra') anagrams = detector.match(%w[cashregister Carthorse radishes]) - expected = ["Carthorse"] + expected = %w[Carthorse] + assert_equal expected, anagrams end def test_detects_anagrams_using_case_insensitive_subject skip - detector = Anagram.new("Orchestra") + detector = Anagram.new('Orchestra') anagrams = detector.match(%w[cashregister carthorse radishes]) - expected = ["carthorse"] + expected = %w[carthorse] + assert_equal expected, anagrams end def test_detects_anagrams_using_case_insensitive_possible_matches skip - detector = Anagram.new("orchestra") + detector = Anagram.new('orchestra') anagrams = detector.match(%w[cashregister Carthorse radishes]) - expected = ["Carthorse"] + expected = %w[Carthorse] + assert_equal expected, anagrams end def test_does_not_detect_an_anagram_if_the_original_word_is_repeated skip - detector = Anagram.new("go") - anagrams = detector.match(["go Go GO"]) - expected = [] + detector = Anagram.new('go') + anagrams = detector.match(%w[goGoGO]) + expected = %w[] + assert_equal expected, anagrams end def test_anagrams_must_use_all_letters_exactly_once skip - detector = Anagram.new("tapper") - anagrams = detector.match(["patter"]) - expected = [] + detector = Anagram.new('tapper') + anagrams = detector.match(%w[patter]) + expected = %w[] + assert_equal expected, anagrams end def test_words_are_not_anagrams_of_themselves skip - detector = Anagram.new("BANANA") - anagrams = detector.match(["BANANA"]) - expected = [] + detector = Anagram.new('BANANA') + anagrams = detector.match(%w[BANANA]) + expected = %w[] + assert_equal expected, anagrams end def test_words_are_not_anagrams_of_themselves_even_if_letter_case_is_partially_different skip - detector = Anagram.new("BANANA") - anagrams = detector.match(["Banana"]) - expected = [] + detector = Anagram.new('BANANA') + anagrams = detector.match(%w[Banana]) + expected = %w[] + assert_equal expected, anagrams end def test_words_are_not_anagrams_of_themselves_even_if_letter_case_is_completely_different skip - detector = Anagram.new("BANANA") - anagrams = detector.match(["banana"]) - expected = [] + detector = Anagram.new('BANANA') + anagrams = detector.match(%w[banana]) + expected = %w[] + assert_equal expected, anagrams end def test_words_other_than_themselves_can_be_anagrams skip - detector = Anagram.new("LISTEN") + detector = Anagram.new('LISTEN') anagrams = detector.match(%w[LISTEN Silent]) - expected = ["Silent"] + expected = %w[Silent] + assert_equal expected, anagrams end end diff --git a/exercises/practice/armstrong-numbers/.meta/test_template.erb b/exercises/practice/armstrong-numbers/.meta/test_template.erb new file mode 100644 index 0000000000..a39a63b2a7 --- /dev/null +++ b/exercises/practice/armstrong-numbers/.meta/test_template.erb @@ -0,0 +1,11 @@ +require 'minitest/autorun' +require_relative 'armstrong_numbers' + +class ArmstrongNumbersTest < Minitest::Test +<% json["cases"].each do |cases| %> + def test_<%= underscore(cases["description"]) %> + <%= skip? %> + <%= cases["expected"] ? "assert_includes" : "refute_includes" %> ArmstrongNumbers, <%= cases["input"]["number"] %> + end + <% end %> +end diff --git a/exercises/practice/atbash-cipher/.meta/test_template.erb b/exercises/practice/atbash-cipher/.meta/test_template.erb new file mode 100644 index 0000000000..f0f0ac9e1e --- /dev/null +++ b/exercises/practice/atbash-cipher/.meta/test_template.erb @@ -0,0 +1,21 @@ +require 'minitest/autorun' +require_relative 'armstrong_numbers' + +class ArmstrongNumbersTest < Minitest::Test +<% json["cases"].each do |cases| %> + <% cases["cases"].each do |sub_case| %> + def test_<%= underscore(sub_case["description"]) %> + <%= skip? %> + <%- if sub_case["property"] == "encode" -%> + plaintext = '<%= sub_case["input"]["phrase"] %>' + ciphertext = '<%= sub_case["expected"] %>' + assert_equal ciphertext, Atbash.encode(plaintext) + <%- else -%> + ciphertext = '<%= sub_case["input"]["phrase"] %>' + plaintext = '<%= sub_case["expected"] %>' + assert_equal plaintext, Atbash.decode(ciphertext) + <% end %> + end + <% end %> +<% end %> +end diff --git a/exercises/practice/bank-account/.meta/test_template.erb b/exercises/practice/bank-account/.meta/test_template.erb new file mode 100644 index 0000000000..7bda18768f --- /dev/null +++ b/exercises/practice/bank-account/.meta/test_template.erb @@ -0,0 +1,43 @@ +require 'minitest/autorun' +require_relative 'bank_account' + +class BankAccountTest < Minitest::Test +<% json["cases"].each do |cases| %> + def test_<%= underscore(cases["description"]) %> + <%= skip? %> + bank_account = BankAccount.new + <%- cases["input"]["operations"][...-1].each do |operation| -%> + <%- if operation["operation"] == "open" -%> + bank_account.open + <%- elsif operation["operation"] == "close" -%> + bank_account.close + <%- elsif operation["operation"] == "deposit" -%> + bank_account.deposit(<%= operation["amount"] %>) + <%- elsif operation["operation"] == "withdraw" -%> + bank_account.withdraw(<%= operation["amount"] %>) + <%- end -%> + <%- end -%> + <%- if cases["expected"].is_a?(Hash) && cases["expected"].key?("error") -%> + assert_raises(ArgumentError, <%- if cases["expected"]["error"] == "account not open" && cases["input"]["operations"][-1]["operation"] == "balance" -%> + "You can't check the balance of a closed account" + <%- elsif cases["expected"]["error"] == "account not open" && cases["input"]["operations"][-1]["operation"] == "withdraw" -%> + "You can't withdraw money into a closed account" + <%- elsif cases["expected"]["error"] == "account not open" && cases["input"]["operations"][-1]["operation"] == "deposit" -%> + "You can't deposit money into a closed account" + <%- elsif cases["expected"]["error"] == "account not open" -%> + "You can't close an already closed account" + <%- elsif cases["expected"]["error"] == "account already open" -%> + "You can't open an already open account" + <%- elsif cases["expected"]["error"] == "amount must be less than balance" -%> + "You can't withdraw more than you have" + <%- elsif cases["expected"]["error"] == "amount must be greater than 0" && cases["input"]["operations"][-1]["operation"] == "withdraw" -%> + "You can't withdraw a negative amount" + <%- elsif cases["expected"]["error"] == "amount must be greater than 0" -%> + "You can't deposit a negative amount" + <%- end -%> ) { bank_account.<%= cases["input"]["operations"][-1]["operation"]%>(<%= cases["input"]["operations"][-1]["amount"] %>) } + <%- else -%> + assert_equal <%= cases["expected"] %>, bank_account.balance + <%- end -%> + end +<% end %> +end diff --git a/exercises/practice/bank-account/.meta/tests.toml b/exercises/practice/bank-account/.meta/tests.toml index 655ea7ae5a..1704a08c5a 100644 --- a/exercises/practice/bank-account/.meta/tests.toml +++ b/exercises/practice/bank-account/.meta/tests.toml @@ -59,4 +59,4 @@ description = "Cannot deposit negative" [ba0c1e0b-0f00-416f-8097-a7dfc97871ff] description = "Can handle concurrent transactions" -include = false \ No newline at end of file +include = false diff --git a/exercises/practice/bank-account/bank_account_test.rb b/exercises/practice/bank-account/bank_account_test.rb index edc44b4559..926b987c6d 100644 --- a/exercises/practice/bank-account/bank_account_test.rb +++ b/exercises/practice/bank-account/bank_account_test.rb @@ -6,7 +6,7 @@ def test_newly_opened_account_has_zero_balance # skip bank_account = BankAccount.new bank_account.open - assert_equal bank_account.balance, 0 + assert_equal 0, bank_account.balance end def test_single_deposit diff --git a/exercises/practice/binary-search/.meta/test_template.erb b/exercises/practice/binary-search/.meta/test_template.erb new file mode 100644 index 0000000000..27b55b5a33 --- /dev/null +++ b/exercises/practice/binary-search/.meta/test_template.erb @@ -0,0 +1,16 @@ +require 'minitest/autorun' +require_relative 'binary_search' + +class BinarySearchTest < Minitest::Test +<% json["cases"].each do |cases| %> + def test_<%= underscore(cases["description"]) %> + <%= skip? %> + binary = BinarySearch.new(<%= cases["input"]["array"] %>) + <%- if cases["expected"].is_a?(Hash) && cases["expected"].key?("error") -%> + assert_nil binary.search_for(<%= cases["input"]["value"] %>) + <%- else -%> + assert_equal <%= cases["expected"] %>, binary.search_for(<%= cases["input"]["value"] %>) + <%- end -%> + end + <% end %> +end diff --git a/exercises/practice/binary-search/binary_search_test.rb b/exercises/practice/binary-search/binary_search_test.rb index bc928a666c..6f9c9ebd4b 100644 --- a/exercises/practice/binary-search/binary_search_test.rb +++ b/exercises/practice/binary-search/binary_search_test.rb @@ -44,13 +44,13 @@ def test_identifies_that_a_value_is_not_included_in_the_array assert_nil binary.search_for(7) end - def test_a_value_smaller_than_the_array_s_smallest_value_is_not_found + def test_a_value_smaller_than_the_arrays_smallest_value_is_not_found skip binary = BinarySearch.new([1, 3, 4, 6, 8, 9, 11]) assert_nil binary.search_for(0) end - def test_a_value_larger_than_the_array_s_largest_value_is_not_found + def test_a_value_larger_than_the_arrays_largest_value_is_not_found skip binary = BinarySearch.new([1, 3, 4, 6, 8, 9, 11]) assert_nil binary.search_for(13) diff --git a/generatorv2/lib/generator.rb b/generatorv2/lib/generator.rb index a0d669e929..5f15b0b43c 100644 --- a/generatorv2/lib/generator.rb +++ b/generatorv2/lib/generator.rb @@ -21,17 +21,17 @@ def generate(result_path = "./exercises/practice/#{@exercise}/#{underscore(@exer additional_json(json) json["cases"] = remove_tests(uuid, json) status = proc { status } - template = ERB.new File.read("./exercises/practice/#{@exercise}/.meta/test_template.erb") + template = ERB.new(File.read("./exercises/practice/#{@exercise}/.meta/test_template.erb"), trim_mode: '-') result = template.result(binding) File.write(result_path, result) RuboCop::CLI.new. - run(['-x', '-c', '.rubocop.yml', '-o', NullDevice.path, result_path]) + run(['-a', '-c', '.rubocop.yml', '-o', NullDevice.path, result_path]) end def underscore(str) - str.gsub(/[-\s]/, '_').downcase + str.gsub(/[^\w\s-]/, '').gsub(' ', ' ').gsub(/[-\s]/, '_').downcase end def camel_case(str) diff --git a/generatorv2/lib/utils.rb b/generatorv2/lib/utils.rb index d6b80971e6..a728848e8c 100644 --- a/generatorv2/lib/utils.rb +++ b/generatorv2/lib/utils.rb @@ -47,7 +47,7 @@ def additional_json(json) def remove_tests(uuid, json) json["cases"].each_with_object([]) do |x, acc| if x["cases"] - acc << { "cases" => remove_tests(uuid, x) } + acc << { "cases" => remove_tests(uuid, x), "description" => x["description"] } elsif uuid.include?(x["uuid"]) acc << x end diff --git a/generatorv2/test/utils_test.rb b/generatorv2/test/utils_test.rb index 235021882c..944e0b66b8 100644 --- a/generatorv2/test/utils_test.rb +++ b/generatorv2/test/utils_test.rb @@ -22,6 +22,16 @@ def test_underscore_with_two_words Generator.new("two-fer").underscore("two-fer") end + def test_underscore_with_special_characters + assert_equal "two_fer", + Generator.new("two-fer").underscore("two,!@#$%^&*()-fer") + end + + def test_underscore_with_special_characters_should_not_create_multiple_spaces + assert_equal "two_fer", + Generator.new("two-fer").underscore("two = fer") + end + def test_first_time_includes_hastag assert_equal "# skip", Generator.new("acronym").skip?