Skip to content
Merged
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
12 changes: 12 additions & 0 deletions exercises/practice/bob/.meta/test_template.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'minitest/autorun'
require_relative 'bob'

class BobTest < Minitest::Test
<% json["cases"].each do |cases| %>
def test_<%= underscore(cases["description"]) %>
<%= skip? %>
remark = <%= cases["input"]["heyBob"].dump() %>
assert_equal "<%= cases["expected"] %>", Bob.hey(remark), <%= "Bob hears #{ cases["input"]["heyBob"].dump}, and..".dump %>
end
<% end %>
end
60 changes: 30 additions & 30 deletions exercises/practice/bob/bob_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,150 +5,150 @@ class BobTest < Minitest::Test
def test_stating_something
# skip
remark = "Tom-ay-to, tom-aaaah-to."
assert_equal "Whatever.", Bob.hey(remark), 'Bob hears "Tom-ay-to, tom-aaaah-to.", and..'
assert_equal "Whatever.", Bob.hey(remark), "Bob hears \"Tom-ay-to, tom-aaaah-to.\", and.."
end

def test_shouting
skip
remark = "WATCH OUT!"
assert_equal "Whoa, chill out!", Bob.hey(remark), 'Bob hears "WATCH OUT!", and..'
assert_equal "Whoa, chill out!", Bob.hey(remark), "Bob hears \"WATCH OUT!\", and.."
end

def test_shouting_gibberish
skip
remark = "FCECDFCAAB"
assert_equal "Whoa, chill out!", Bob.hey(remark), 'Bob hears "FCECDFCAAB", and..'
assert_equal "Whoa, chill out!", Bob.hey(remark), "Bob hears \"FCECDFCAAB\", and.."
end

def test_asking_a_question
skip
remark = "Does this cryogenic chamber make me look fat?"
assert_equal "Sure.", Bob.hey(remark), 'Bob hears "Does this cryogenic chamber make me look fat?", and..'
assert_equal "Sure.", Bob.hey(remark), "Bob hears \"Does this cryogenic chamber make me look fat?\", and.."
end

def test_asking_a_numeric_question
skip
remark = "You are, what, like 15?"
assert_equal "Sure.", Bob.hey(remark), 'Bob hears "You are, what, like 15?", and..'
assert_equal "Sure.", Bob.hey(remark), "Bob hears \"You are, what, like 15?\", and.."
end

def test_asking_gibberish
skip
remark = "fffbbcbeab?"
assert_equal "Sure.", Bob.hey(remark), 'Bob hears "fffbbcbeab?", and..'
assert_equal "Sure.", Bob.hey(remark), "Bob hears \"fffbbcbeab?\", and.."
end

def test_talking_forcefully
skip
remark = "Hi there!"
assert_equal "Whatever.", Bob.hey(remark), 'Bob hears "Hi there!", and..'
assert_equal "Whatever.", Bob.hey(remark), "Bob hears \"Hi there!\", and.."
end

def test_using_acronyms_in_regular_speech
skip
remark = "It's OK if you don't want to go work for NASA."
assert_equal "Whatever.", Bob.hey(remark), %q(Bob hears "It's OK if you don't want to go work for NASA.", and..)
assert_equal "Whatever.", Bob.hey(remark), "Bob hears \"It's OK if you don't want to go work for NASA.\", and.."
end

def test_forceful_question
skip
remark = "WHAT'S GOING ON?"
assert_equal "Calm down, I know what I'm doing!", Bob.hey(remark), %q(Bob hears "WHAT'S GOING ON?", and..)
assert_equal "Calm down, I know what I'm doing!", Bob.hey(remark), "Bob hears \"WHAT'S GOING ON?\", and.."
end

def test_shouting_numbers
skip
remark = "1, 2, 3 GO!"
assert_equal "Whoa, chill out!", Bob.hey(remark), 'Bob hears "1, 2, 3 GO!", and..'
assert_equal "Whoa, chill out!", Bob.hey(remark), "Bob hears \"1, 2, 3 GO!\", and.."
end

def test_no_letters
skip
remark = "1, 2, 3"
assert_equal "Whatever.", Bob.hey(remark), 'Bob hears "1, 2, 3", and..'
assert_equal "Whatever.", Bob.hey(remark), "Bob hears \"1, 2, 3\", and.."
end

def test_question_with_no_letters
skip
remark = "4?"
assert_equal "Sure.", Bob.hey(remark), 'Bob hears "4?", and..'
assert_equal "Sure.", Bob.hey(remark), "Bob hears \"4?\", and.."
end

def test_shouting_with_special_characters
skip
remark = "ZOMG THE %^*@\#$(*^ ZOMBIES ARE COMING!!11!!1!"
assert_equal "Whoa, chill out!", Bob.hey(remark), %q{Bob hears "ZOMG THE %^*@\#$(*^ ZOMBIES ARE COMING!!11!!1!", and..}
assert_equal "Whoa, chill out!", Bob.hey(remark), "Bob hears \"ZOMG THE %^*@\\\#$(*^ ZOMBIES ARE COMING!!11!!1!\", and.."
end

def test_shouting_with_no_exclamation_mark
skip
remark = "I HATE THE DENTIST"
assert_equal "Whoa, chill out!", Bob.hey(remark), 'Bob hears "I HATE THE DENTIST", and..'
assert_equal "Whoa, chill out!", Bob.hey(remark), "Bob hears \"I HATE THE DENTIST\", and.."
end

def test_statement_containing_question_mark
skip
remark = "Ending with ? means a question."
assert_equal "Whatever.", Bob.hey(remark), 'Bob hears "Ending with ? means a question.", and..'
assert_equal "Whatever.", Bob.hey(remark), "Bob hears \"Ending with ? means a question.\", and.."
end

def test_non_letters_with_question
skip
remark = ":) ?"
assert_equal "Sure.", Bob.hey(remark), 'Bob hears ":) ?", and..'
assert_equal "Sure.", Bob.hey(remark), "Bob hears \":) ?\", and.."
end

def test_prattling_on
skip
remark = "Wait! Hang on. Are you going to be OK?"
assert_equal "Sure.", Bob.hey(remark), 'Bob hears "Wait! Hang on. Are you going to be OK?", and..'
assert_equal "Sure.", Bob.hey(remark), "Bob hears \"Wait! Hang on. Are you going to be OK?\", and.."
end

def test_silence
skip
remark = ""
assert_equal "Fine. Be that way!", Bob.hey(remark), 'Bob hears "", and..'
assert_equal "Fine. Be that way!", Bob.hey(remark), "Bob hears \"\", and.."
end

def test_prolonged_silence
skip
remark = " "
assert_equal "Fine. Be that way!", Bob.hey(remark), 'Bob hears " ", and..'
assert_equal "Fine. Be that way!", Bob.hey(remark), "Bob hears \" \", and.."
end

def test_alternate_silence
skip
remark = "\t\t\t\t\t\t\t\t\t\t"
assert_equal "Fine. Be that way!", Bob.hey(remark), %q(Bob hears "\t\t\t\t\t\t\t\t\t\t", and..)
end

def test_multiple_line_question
skip
remark = "\nDoes this cryogenic chamber make me look fat?\nNo."
assert_equal "Whatever.", Bob.hey(remark), %q(Bob hears "\nDoes this cryogenic chamber make me look fat?\nNo.", and..)
assert_equal "Fine. Be that way!", Bob.hey(remark), "Bob hears \"\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\", and.."
end

def test_starting_with_whitespace
skip
remark = " hmmmmmmm..."
assert_equal "Whatever.", Bob.hey(remark), 'Bob hears " hmmmmmmm...", and..'
assert_equal "Whatever.", Bob.hey(remark), "Bob hears \" hmmmmmmm...\", and.."
end

def test_ending_with_whitespace
skip
remark = "Okay if like my spacebar quite a bit? "
assert_equal "Sure.", Bob.hey(remark), 'Bob hears "Okay if like my spacebar quite a bit? ", and..'
assert_equal "Sure.", Bob.hey(remark), "Bob hears \"Okay if like my spacebar quite a bit? \", and.."
end

def test_other_whitespace
skip
remark = "\n\r \t"
assert_equal "Fine. Be that way!", Bob.hey(remark), %q(Bob hears "\n\r \t", and..)
assert_equal "Fine. Be that way!", Bob.hey(remark), "Bob hears \"\\n\\r \\t\", and.."
end

def test_non_question_ending_with_whitespace
skip
remark = "This is a statement ending with whitespace "
assert_equal "Whatever.", Bob.hey(remark), 'Bob hears "This is a statement ending with whitespace ", and..'
assert_equal "Whatever.", Bob.hey(remark), "Bob hears \"This is a statement ending with whitespace \", and.."
end

def test_multiple_line_question
skip
remark = "\nDoes this cryogenic chamber make\n me look fat?"
assert_equal "Sure.", Bob.hey(remark), "Bob hears \"\\nDoes this cryogenic chamber make\\n me look fat?\", and.."
end
end
12 changes: 12 additions & 0 deletions exercises/practice/book-store/.meta/test_template.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'minitest/autorun'
require_relative 'book_store'

class BookStoreTest < Minitest::Test
<% json["cases"].each do |cases| %>
def test_<%= underscore(cases["description"]) %>
<%= skip? %>
basket = <%= cases["input"]["basket"] %>
assert_in_delta <%= cases["expected"] / 100.0 %>0, BookStore.calculate_price(basket), 0.001
end
<% end %>
end
18 changes: 18 additions & 0 deletions exercises/practice/bottle-song/.meta/test_template.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'minitest/autorun'
require_relative 'bottle_song'

class BottleSongTest < Minitest::Test
<% json["cases"].each do |cases| %>
<% cases["cases"].each do |sub_case| %>
<% sub_case["cases"].each do |sub_sub_case| %>
def test_<%= underscore(sub_sub_case["description"]) %>
<%= skip? %>
expected = <<~TEXT
<%= sub_sub_case["expected"].join("\n") %>
TEXT
assert_equal expected, BottleSong.recite(<%= sub_sub_case["input"]["startBottles"] %>, <%= sub_sub_case["input"]["takeDown"] %>)
end
<% end %>
<% end %>
<% end %>
end
2 changes: 1 addition & 1 deletion exercises/practice/bottle-song/bottle_song_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require_relative 'bottle_song'

class BottleSongTest < Minitest::Test
def test_single_verse
def test_first_generic_verse
# skip
expected = <<~TEXT
Ten green bottles hanging on the wall,
Expand Down
26 changes: 26 additions & 0 deletions exercises/practice/bowling/.meta/test_template.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'minitest/autorun'
require_relative 'bowling'

class BowlingTest < Minitest::Test
<% json["cases"].each do |cases| %>
def test_<%= underscore(cases["description"]) %>
<%= skip? %>
game = Game.new
rolls = <%= cases["input"]["previousRolls"] %>
rolls.each { |pins| game.roll(pins) }
<%- if cases["property"] == "score" -%>
<%- if cases["expected"].is_a?(Hash) && cases["expected"].key?("error") -%>
assert_raises Game::BowlingError do
game.score
end
<%- else -%>
assert_equal <%= cases["expected"] %>, game.score
<%- end -%>
<%- elsif cases["property"] == "roll" -%>
assert_raises Game::BowlingError do
game.roll(<%= cases["input"]["roll"] %>)
end
<%- end -%>
end
<% end %>
end
26 changes: 26 additions & 0 deletions exercises/practice/change/.meta/test_template.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'minitest/autorun'
require_relative 'change'

class ChangeTest < Minitest::Test
<% json["cases"].each do |cases| %>
def test_<%= underscore(cases["description"]) %>
<%= skip? %>
<%- if cases["expected"].is_a?(Hash) && cases["expected"].key?("error") -%>
<%- if cases["expected"]["error"] == "can't make target with given coins" -%>
assert_raises(Change::ImpossibleCombinationError) do
<%- elsif cases["expected"]["error"] == "target can't be negative" -%>
assert_raises(Change::NegativeTargetError) do
<%- end -%>
Change.generate(<%= cases["input"]["coins"] %>, <%= cases["input"]["target"] %>)
end
<%- else -%>
<%- if cases["expected"].empty? -%>
assert_empty Change.generate(<%= cases["input"]["coins"] %>, <%= cases["input"]["target"] %>)
<%- else -%>
expected = <%= cases["expected"] %>
assert_equal expected, Change.generate(<%= cases["input"]["coins"] %>, <%= cases["input"]["target"] %>)
<%- end -%>
<%- end -%>
end
<% end %>
end
27 changes: 20 additions & 7 deletions exercises/practice/change/change_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,32 @@
class ChangeTest < Minitest::Test
def test_change_for_1_cent
# skip
assert_equal [1], Change.generate([1, 5, 10, 25], 1)
expected = [1]
assert_equal expected, Change.generate([1, 5, 10, 25], 1)
end

def test_single_coin_change
skip
assert_equal [25], Change.generate([1, 5, 10, 25, 100], 25)
expected = [25]
assert_equal expected, Change.generate([1, 5, 10, 25, 100], 25)
end

def test_multiple_coin_change
skip
assert_equal [5, 10], Change.generate([1, 5, 10, 25, 100], 15)
expected = [5, 10]
assert_equal expected, Change.generate([1, 5, 10, 25, 100], 15)
end

def test_change_with_lilliputian_coins
skip
assert_equal [4, 4, 15], Change.generate([1, 4, 15, 20, 50], 23)
expected = [4, 4, 15]
assert_equal expected, Change.generate([1, 4, 15, 20, 50], 23)
end

def test_change_with_lower_elbonia_coins
skip
assert_equal [21, 21, 21], Change.generate([1, 5, 10, 21, 25], 63)
expected = [21, 21, 21]
assert_equal expected, Change.generate([1, 5, 10, 21, 25], 63)
end

def test_large_target_values
Expand All @@ -35,12 +40,20 @@ def test_large_target_values

def test_possible_change_without_unit_coins_available
skip
assert_equal [2, 2, 2, 5, 10], Change.generate([2, 5, 10, 20, 50], 21)
expected = [2, 2, 2, 5, 10]
assert_equal expected, Change.generate([2, 5, 10, 20, 50], 21)
end

def test_another_possible_change_without_unit_coins_available
skip
assert_equal [4, 4, 4, 5, 5, 5], Change.generate([4, 5], 27)
expected = [4, 4, 4, 5, 5, 5]
assert_equal expected, Change.generate([4, 5], 27)
end

def test_a_greedy_approach_is_not_optimal
skip
expected = [10, 10]
assert_equal expected, Change.generate([1, 10, 11], 20)
end

def test_no_coins_make_0_change
Expand Down
25 changes: 25 additions & 0 deletions exercises/practice/clock/.meta/test_template.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'minitest/autorun'
require_relative 'clock'

class ClockTest < Minitest::Test
<% json["cases"].each do |cases| %>
<% cases["cases"].each do |sub_case| %>
def test_<%= underscore(sub_case["description"]) %>
<%= skip? %>
<%- if sub_case["property"] == "create" -%>
assert_equal '<%= sub_case["expected"] %>', Clock.new(<%= sub_case["input"]["hour"] != 0 ? "hour: #{sub_case["input"]["hour"]}" : "" %><%= sub_case["input"]["minute"] != 0 && sub_case["input"]["hour"] != 0 ? ", " : "" %><%= sub_case["input"]["minute"] != 0 ? "minute: #{sub_case["input"]["minute"]}" : "" %>).to_s
<%- elsif sub_case["property"] == "add" -%>
clock = Clock.new(<%= sub_case["input"]["hour"] != 0 ? "hour: #{sub_case["input"]["hour"]}" : "" %><%= sub_case["input"]["minute"] != 0 && sub_case["input"]["hour"] != 0 ? ", " : "" %><%= sub_case["input"]["minute"] != 0 ? "minute: #{sub_case["input"]["minute"]}" : "" %>)
assert_equal '<%= sub_case["expected"] %>', (clock + Clock.new(minute: <%= sub_case["input"]["value"]%>)).to_s
<%- elsif sub_case["property"] == "subtract" -%>
clock = Clock.new(<%= sub_case["input"]["hour"] != 0 ? "hour: #{sub_case["input"]["hour"]}" : "" %><%= sub_case["input"]["minute"] != 0 && sub_case["input"]["hour"] != 0 ? ", " : "" %><%= sub_case["input"]["minute"] != 0 ? "minute: #{sub_case["input"]["minute"]}" : "" %>)
assert_equal '<%= sub_case["expected"] %>', (clock - Clock.new(minute: <%= sub_case["input"]["value"]%>)).to_s
<%- elsif sub_case["property"] == "equal" -%>
clock1 = Clock.new(hour: <%= sub_case["input"]["clock1"]["hour"] %>, minute: <%= sub_case["input"]["clock1"]["minute"] %>)
clock2 = Clock.new(hour: <%= sub_case["input"]["clock2"]["hour"] %>, minute: <%= sub_case["input"]["clock2"]["minute"] %>)
<%= sub_case["expected"] ? "assert_equal" : "refute_equal"%> clock1, clock2
<%- end -%>
end
<% end %>
<% end %>
end
Loading