Skip to content
Merged

Sync #1778

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion bin/generate
Original file line number Diff line number Diff line change
Expand Up @@ -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 %<exercise>s, does not match the current file'

Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions exercises/practice/acronym/.meta/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion exercises/practice/acronym/.meta/test_template.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions exercises/practice/acronym/acronym_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 26 additions & 0 deletions exercises/practice/affine-cipher/.meta/test_template.erb
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions exercises/practice/all-your-base/.meta/test_template.erb
Original file line number Diff line number Diff line change
@@ -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
57 changes: 33 additions & 24 deletions exercises/practice/all-your-base/all_your_base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
19 changes: 19 additions & 0 deletions exercises/practice/allergies/.meta/test_template.erb
Original file line number Diff line number Diff line change
@@ -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
Loading