From cc3e108aa08401cc878380911444f31c106a9f3b Mon Sep 17 00:00:00 2001 From: meatball Date: Sat, 11 Oct 2025 22:23:07 +0200 Subject: [PATCH] Add templates for exercises batch 12 --- .../practice/triangle/.docs/instructions.md | 3 +- .../practice/triangle/.meta/test_template.erb | 14 ++ exercises/practice/triangle/triangle_test.rb | 126 +++++++------- .../two-bucket/.meta/test_template.erb | 14 ++ .../practice/two-bucket/.meta/tests.toml | 6 + .../practice/two-bucket/two_bucket_test.rb | 70 +++++--- .../practice/two-fer/.meta/test_template.erb | 13 ++ exercises/practice/two-fer/two_fer_test.rb | 12 +- .../word-count/.meta/test_template.erb | 13 ++ .../practice/word-count/word_count_test.rb | 89 +++++----- .../practice/wordy/.meta/test_template.erb | 20 +++ exercises/practice/wordy/.meta/tests.toml | 12 ++ exercises/practice/wordy/wordy_test.rb | 160 ++++++++++++++---- .../practice/yacht/.meta/test_template.erb | 13 ++ exercises/practice/yacht/yacht_test.rb | 143 ++++++++-------- 15 files changed, 472 insertions(+), 236 deletions(-) create mode 100644 exercises/practice/triangle/.meta/test_template.erb create mode 100644 exercises/practice/two-bucket/.meta/test_template.erb create mode 100644 exercises/practice/two-fer/.meta/test_template.erb create mode 100644 exercises/practice/word-count/.meta/test_template.erb create mode 100644 exercises/practice/wordy/.meta/test_template.erb create mode 100644 exercises/practice/yacht/.meta/test_template.erb diff --git a/exercises/practice/triangle/.docs/instructions.md b/exercises/practice/triangle/.docs/instructions.md index 755cb8d19d..e9b053dcd3 100644 --- a/exercises/practice/triangle/.docs/instructions.md +++ b/exercises/practice/triangle/.docs/instructions.md @@ -14,7 +14,8 @@ A _scalene_ triangle has all sides of different lengths. For a shape to be a triangle at all, all sides have to be of length > 0, and the sum of the lengths of any two sides must be greater than or equal to the length of the third side. ~~~~exercism/note -We opted to not include tests for degenerate triangles (triangles that violate these rules) to keep things simpler. +_Degenerate triangles_ are triangles where the sum of the length of two sides is **equal** to the length of the third side, e.g. `1, 1, 2`. +We opted to not include tests for degenerate triangles in this exercise. You may handle those situations if you wish to do so, or safely ignore them. ~~~~ diff --git a/exercises/practice/triangle/.meta/test_template.erb b/exercises/practice/triangle/.meta/test_template.erb new file mode 100644 index 0000000000..1cac51a844 --- /dev/null +++ b/exercises/practice/triangle/.meta/test_template.erb @@ -0,0 +1,14 @@ +require 'minitest/autorun' +require_relative 'triangle' + +class TriangleTest < Minitest::Test + <% json["cases"].each do |cases| %> + <% cases["cases"].each do |sub_case| %> + def test_<%= underscore(sub_case["description"]) %>_on_<%= underscore(cases["description"])%> + <%= skip? %> + actual = Triangle.new(<%= sub_case["input"]["sides"] %>).<%= sub_case["property"] %>? + <%= sub_case["expected"] ? "assert" : "refute" %> actual, "Expected '<%= sub_case["expected"] %>', triangle <%= sub_case["input"]["sides"] %> is <%= sub_case["expected"] ? "" : "not " %><%= sub_case["property"] %>." + end + <% end %> + <% end %> +end diff --git a/exercises/practice/triangle/triangle_test.rb b/exercises/practice/triangle/triangle_test.rb index 9489cc799f..52a9ad0baf 100644 --- a/exercises/practice/triangle/triangle_test.rb +++ b/exercises/practice/triangle/triangle_test.rb @@ -2,129 +2,129 @@ require_relative 'triangle' class TriangleTest < Minitest::Test - def test_equilateral_triangle_all_sides_are_equal + def test_all_sides_are_equal_on_equilateral_triangle # skip - triangle = Triangle.new([2, 2, 2]) - assert triangle.equilateral?, "Expected 'true', triangle [2, 2, 2] is equilateral." + actual = Triangle.new([2, 2, 2]).equilateral? + assert actual, "Expected 'true', triangle [2, 2, 2] is equilateral." end - def test_equilateral_triangle_any_side_is_unequal + def test_any_side_is_unequal_on_equilateral_triangle skip - triangle = Triangle.new([2, 3, 2]) - refute triangle.equilateral?, "Expected 'false', triangle [2, 3, 2] is not equilateral." + actual = Triangle.new([2, 3, 2]).equilateral? + refute actual, "Expected 'false', triangle [2, 3, 2] is not equilateral." end - def test_equilateral_triangle_no_sides_are_equal + def test_no_sides_are_equal_on_equilateral_triangle skip - triangle = Triangle.new([5, 4, 6]) - refute triangle.equilateral?, "Expected 'false', triangle [5, 4, 6] is not equilateral." + actual = Triangle.new([5, 4, 6]).equilateral? + refute actual, "Expected 'false', triangle [5, 4, 6] is not equilateral." end - def test_equilateral_triangle_all_zero_sides_is_not_a_triangle + def test_all_zero_sides_is_not_a_triangle_on_equilateral_triangle skip - triangle = Triangle.new([0, 0, 0]) - refute triangle.equilateral?, "Expected 'false', triangle [0, 0, 0] is not equilateral." + actual = Triangle.new([0, 0, 0]).equilateral? + refute actual, "Expected 'false', triangle [0, 0, 0] is not equilateral." end - def test_equilateral_triangle_sides_may_be_floats + def test_sides_may_be_floats_on_equilateral_triangle skip - triangle = Triangle.new([0.5, 0.5, 0.5]) - assert triangle.equilateral?, "Expected 'true', triangle [0.5, 0.5, 0.5] is equilateral." + actual = Triangle.new([0.5, 0.5, 0.5]).equilateral? + assert actual, "Expected 'true', triangle [0.5, 0.5, 0.5] is equilateral." end - def test_isosceles_triangle_last_two_sides_are_equal + def test_last_two_sides_are_equal_on_isosceles_triangle skip - triangle = Triangle.new([3, 4, 4]) - assert triangle.isosceles?, "Expected 'true', triangle [3, 4, 4] is isosceles." + actual = Triangle.new([3, 4, 4]).isosceles? + assert actual, "Expected 'true', triangle [3, 4, 4] is isosceles." end - def test_isosceles_triangle_first_two_sides_are_equal + def test_first_two_sides_are_equal_on_isosceles_triangle skip - triangle = Triangle.new([4, 4, 3]) - assert triangle.isosceles?, "Expected 'true', triangle [4, 4, 3] is isosceles." + actual = Triangle.new([4, 4, 3]).isosceles? + assert actual, "Expected 'true', triangle [4, 4, 3] is isosceles." end - def test_isosceles_triangle_first_and_last_sides_are_equal + def test_first_and_last_sides_are_equal_on_isosceles_triangle skip - triangle = Triangle.new([4, 3, 4]) - assert triangle.isosceles?, "Expected 'true', triangle [4, 3, 4] is isosceles." + actual = Triangle.new([4, 3, 4]).isosceles? + assert actual, "Expected 'true', triangle [4, 3, 4] is isosceles." end - def test_isosceles_triangle_equilateral_triangles_are_also_isosceles + def test_equilateral_triangles_are_also_isosceles_on_isosceles_triangle skip - triangle = Triangle.new([4, 4, 4]) - assert triangle.isosceles?, "Expected 'true', triangle [4, 4, 4] is isosceles." + actual = Triangle.new([4, 4, 4]).isosceles? + assert actual, "Expected 'true', triangle [4, 4, 4] is isosceles." end - def test_isosceles_triangle_no_sides_are_equal + def test_no_sides_are_equal_on_isosceles_triangle skip - triangle = Triangle.new([2, 3, 4]) - refute triangle.isosceles?, "Expected 'false', triangle [2, 3, 4] is not isosceles." + actual = Triangle.new([2, 3, 4]).isosceles? + refute actual, "Expected 'false', triangle [2, 3, 4] is not isosceles." end - def test_isosceles_triangle_first_triangle_inequality_violation + def test_first_triangle_inequality_violation_on_isosceles_triangle skip - triangle = Triangle.new([1, 1, 3]) - refute triangle.isosceles?, "Expected 'false', triangle [1, 1, 3] is not isosceles." + actual = Triangle.new([1, 1, 3]).isosceles? + refute actual, "Expected 'false', triangle [1, 1, 3] is not isosceles." end - def test_isosceles_triangle_second_triangle_inequality_violation + def test_second_triangle_inequality_violation_on_isosceles_triangle skip - triangle = Triangle.new([1, 3, 1]) - refute triangle.isosceles?, "Expected 'false', triangle [1, 3, 1] is not isosceles." + actual = Triangle.new([1, 3, 1]).isosceles? + refute actual, "Expected 'false', triangle [1, 3, 1] is not isosceles." end - def test_isosceles_triangle_third_triangle_inequality_violation + def test_third_triangle_inequality_violation_on_isosceles_triangle skip - triangle = Triangle.new([3, 1, 1]) - refute triangle.isosceles?, "Expected 'false', triangle [3, 1, 1] is not isosceles." + actual = Triangle.new([3, 1, 1]).isosceles? + refute actual, "Expected 'false', triangle [3, 1, 1] is not isosceles." end - def test_isosceles_triangle_sides_may_be_floats + def test_sides_may_be_floats_on_isosceles_triangle skip - triangle = Triangle.new([0.5, 0.4, 0.5]) - assert triangle.isosceles?, "Expected 'true', triangle [0.5, 0.4, 0.5] is isosceles." + actual = Triangle.new([0.5, 0.4, 0.5]).isosceles? + assert actual, "Expected 'true', triangle [0.5, 0.4, 0.5] is isosceles." end - def test_scalene_triangle_no_sides_are_equal + def test_no_sides_are_equal_on_scalene_triangle skip - triangle = Triangle.new([5, 4, 6]) - assert triangle.scalene?, "Expected 'true', triangle [5, 4, 6] is scalene." + actual = Triangle.new([5, 4, 6]).scalene? + assert actual, "Expected 'true', triangle [5, 4, 6] is scalene." end - def test_scalene_triangle_all_sides_are_equal + def test_all_sides_are_equal_on_scalene_triangle skip - triangle = Triangle.new([4, 4, 4]) - refute triangle.scalene?, "Expected 'false', triangle [4, 4, 4] is not scalene." + actual = Triangle.new([4, 4, 4]).scalene? + refute actual, "Expected 'false', triangle [4, 4, 4] is not scalene." end - def test_scalene_triangle_first_and_second_sides_are_equal + def test_first_and_second_sides_are_equal_on_scalene_triangle skip - triangle = Triangle.new([4, 4, 3]) - refute triangle.scalene?, "Expected 'false', triangle [4, 4, 3] is not scalene." + actual = Triangle.new([4, 4, 3]).scalene? + refute actual, "Expected 'false', triangle [4, 4, 3] is not scalene." end - def test_scalene_triangle_first_and_third_sides_are_equal + def test_first_and_third_sides_are_equal_on_scalene_triangle skip - triangle = Triangle.new([3, 4, 3]) - refute triangle.scalene?, "Expected 'false', triangle [3, 4, 3] is not scalene." + actual = Triangle.new([3, 4, 3]).scalene? + refute actual, "Expected 'false', triangle [3, 4, 3] is not scalene." end - def test_scalene_triangle_second_and_third_sides_are_equal + def test_second_and_third_sides_are_equal_on_scalene_triangle skip - triangle = Triangle.new([4, 3, 3]) - refute triangle.scalene?, "Expected 'false', triangle [4, 3, 3] is not scalene." + actual = Triangle.new([4, 3, 3]).scalene? + refute actual, "Expected 'false', triangle [4, 3, 3] is not scalene." end - def test_scalene_triangle_may_not_violate_triangle_inequality + def test_may_not_violate_triangle_inequality_on_scalene_triangle skip - triangle = Triangle.new([7, 3, 2]) - refute triangle.scalene?, "Expected 'false', triangle [7, 3, 2] is not scalene." + actual = Triangle.new([7, 3, 2]).scalene? + refute actual, "Expected 'false', triangle [7, 3, 2] is not scalene." end - def test_scalene_triangle_sides_may_be_floats + def test_sides_may_be_floats_on_scalene_triangle skip - triangle = Triangle.new([0.5, 0.4, 0.6]) - assert triangle.scalene?, "Expected 'true', triangle [0.5, 0.4, 0.6] is scalene." + actual = Triangle.new([0.5, 0.4, 0.6]).scalene? + assert actual, "Expected 'true', triangle [0.5, 0.4, 0.6] is scalene." end end diff --git a/exercises/practice/two-bucket/.meta/test_template.erb b/exercises/practice/two-bucket/.meta/test_template.erb new file mode 100644 index 0000000000..64c91d401c --- /dev/null +++ b/exercises/practice/two-bucket/.meta/test_template.erb @@ -0,0 +1,14 @@ +require 'minitest/autorun' +require_relative 'two_bucket' + +class TwoBucketTest < Minitest::Test + <% json["cases"].each do |cases| %> + def test_<%= underscore(cases["description"]) %> + <%= skip? %> + subject = TwoBucket.new(<%= cases["input"].values.inspect[1...-1] %>) + actual = [subject.moves, subject.goal_bucket, subject.other_bucket] + expected = <%= cases["expected"].values %> + assert_equal expected, actual + end + <% end %> +end diff --git a/exercises/practice/two-bucket/.meta/tests.toml b/exercises/practice/two-bucket/.meta/tests.toml index fe09b6c263..f95ab634d5 100644 --- a/exercises/practice/two-bucket/.meta/tests.toml +++ b/exercises/practice/two-bucket/.meta/tests.toml @@ -27,6 +27,12 @@ description = "Measure one step using bucket one of size 1 and bucket two of siz [eb329c63-5540-4735-b30b-97f7f4df0f84] description = "Measure using bucket one of size 2 and bucket two of size 3 - start with bucket one and end with bucket two" +[58d70152-bf2b-46bb-ad54-be58ebe94c03] +description = "Measure using bucket one much bigger than bucket two" + +[9dbe6499-caa5-4a58-b5ce-c988d71b8981] +description = "Measure using bucket one much smaller than bucket two" + [449be72d-b10a-4f4b-a959-ca741e333b72] description = "Not possible to reach the goal" include = false diff --git a/exercises/practice/two-bucket/two_bucket_test.rb b/exercises/practice/two-bucket/two_bucket_test.rb index c13e357683..5be469a956 100644 --- a/exercises/practice/two-bucket/two_bucket_test.rb +++ b/exercises/practice/two-bucket/two_bucket_test.rb @@ -2,59 +2,75 @@ require_relative 'two_bucket' class TwoBucketTest < Minitest::Test - def test_measure_using_bucket_one_of_size_3_and_bucket_two_of_size_5_start_with_bucket_one + def test_measure_using_bucket_one_of_size_3_and_bucket_two_of_size_5___start_with_bucket_one # skip subject = TwoBucket.new(3, 5, 1, "one") - assert_equal 4, subject.moves - assert_equal "one", subject.goal_bucket - assert_equal 5, subject.other_bucket + actual = [subject.moves, subject.goal_bucket, subject.other_bucket] + expected = [4, "one", 5] + assert_equal expected, actual end - def test_measure_using_bucket_one_of_size_3_and_bucket_two_of_size_5_start_with_bucket_two + def test_measure_using_bucket_one_of_size_3_and_bucket_two_of_size_5___start_with_bucket_two skip subject = TwoBucket.new(3, 5, 1, "two") - assert_equal 8, subject.moves - assert_equal "two", subject.goal_bucket - assert_equal 3, subject.other_bucket + actual = [subject.moves, subject.goal_bucket, subject.other_bucket] + expected = [8, "two", 3] + assert_equal expected, actual end - def test_measure_using_bucket_one_of_size_7_and_bucket_two_of_size_11_start_with_bucket_one + def test_measure_using_bucket_one_of_size_7_and_bucket_two_of_size_11___start_with_bucket_one skip subject = TwoBucket.new(7, 11, 2, "one") - assert_equal 14, subject.moves - assert_equal "one", subject.goal_bucket - assert_equal 11, subject.other_bucket + actual = [subject.moves, subject.goal_bucket, subject.other_bucket] + expected = [14, "one", 11] + assert_equal expected, actual end - def test_measure_using_bucket_one_of_size_7_and_bucket_two_of_size_11_start_with_bucket_two + def test_measure_using_bucket_one_of_size_7_and_bucket_two_of_size_11___start_with_bucket_two skip subject = TwoBucket.new(7, 11, 2, "two") - assert_equal 18, subject.moves - assert_equal "two", subject.goal_bucket - assert_equal 7, subject.other_bucket + actual = [subject.moves, subject.goal_bucket, subject.other_bucket] + expected = [18, "two", 7] + assert_equal expected, actual end - def test_measure_one_step_using_bucket_one_of_size_1_and_bucket_two_of_size_3_start_with_bucket_two + def test_measure_one_step_using_bucket_one_of_size_1_and_bucket_two_of_size_3___start_with_bucket_two skip subject = TwoBucket.new(1, 3, 3, "two") - assert_equal 1, subject.moves - assert_equal "two", subject.goal_bucket - assert_equal 0, subject.other_bucket + actual = [subject.moves, subject.goal_bucket, subject.other_bucket] + expected = [1, "two", 0] + assert_equal expected, actual end - def test_measure_using_bucket_one_of_size_2_and_bucket_two_of_size_3_start_with_bucket_one_and_end_with_bucket_two + def test_measure_using_bucket_one_of_size_2_and_bucket_two_of_size_3___start_with_bucket_one_and_end_with_bucket_two skip subject = TwoBucket.new(2, 3, 3, "one") - assert_equal 2, subject.moves - assert_equal "two", subject.goal_bucket - assert_equal 2, subject.other_bucket + actual = [subject.moves, subject.goal_bucket, subject.other_bucket] + expected = [2, "two", 2] + assert_equal expected, actual + end + + def test_measure_using_bucket_one_much_bigger_than_bucket_two + skip + subject = TwoBucket.new(5, 1, 2, "one") + actual = [subject.moves, subject.goal_bucket, subject.other_bucket] + expected = [6, "one", 1] + assert_equal expected, actual + end + + def test_measure_using_bucket_one_much_smaller_than_bucket_two + skip + subject = TwoBucket.new(3, 15, 9, "one") + actual = [subject.moves, subject.goal_bucket, subject.other_bucket] + expected = [6, "two", 0] + assert_equal expected, actual end def test_with_the_same_buckets_but_a_different_goal_then_it_is_possible skip subject = TwoBucket.new(6, 15, 9, "one") - assert_equal 10, subject.moves - assert_equal "two", subject.goal_bucket - assert_equal 0, subject.other_bucket + actual = [subject.moves, subject.goal_bucket, subject.other_bucket] + expected = [10, "two", 0] + assert_equal expected, actual end end diff --git a/exercises/practice/two-fer/.meta/test_template.erb b/exercises/practice/two-fer/.meta/test_template.erb new file mode 100644 index 0000000000..21789f3af5 --- /dev/null +++ b/exercises/practice/two-fer/.meta/test_template.erb @@ -0,0 +1,13 @@ +require 'minitest/autorun' +require_relative 'two_fer' + +class TwoFerTest < Minitest::Test + <% json["cases"].each do |cases| %> + def test_<%= underscore(cases["description"]) %> + <%= skip? %> + actual = TwoFer.two_fer(<%= cases["input"]["name"] ? "'#{cases["input"]["name"]}'" : "" %>) + expected = '<%= cases["expected"] %>' + assert_equal expected, actual + end + <% end %> +end diff --git a/exercises/practice/two-fer/two_fer_test.rb b/exercises/practice/two-fer/two_fer_test.rb index 2796a39360..2900106a29 100644 --- a/exercises/practice/two-fer/two_fer_test.rb +++ b/exercises/practice/two-fer/two_fer_test.rb @@ -4,16 +4,22 @@ class TwoFerTest < Minitest::Test def test_no_name_given # skip - assert_equal "One for you, one for me.", TwoFer.two_fer + actual = TwoFer.two_fer + expected = 'One for you, one for me.' + assert_equal expected, actual end def test_a_name_given skip - assert_equal "One for Alice, one for me.", TwoFer.two_fer("Alice") + actual = TwoFer.two_fer('Alice') + expected = 'One for Alice, one for me.' + assert_equal expected, actual end def test_another_name_given skip - assert_equal "One for Bob, one for me.", TwoFer.two_fer("Bob") + actual = TwoFer.two_fer('Bob') + expected = 'One for Bob, one for me.' + assert_equal expected, actual end end diff --git a/exercises/practice/word-count/.meta/test_template.erb b/exercises/practice/word-count/.meta/test_template.erb new file mode 100644 index 0000000000..c024d492ad --- /dev/null +++ b/exercises/practice/word-count/.meta/test_template.erb @@ -0,0 +1,13 @@ +require 'minitest/autorun' +require_relative 'word_count' + +class WordCountTest < Minitest::Test + <% json["cases"].each do |cases| %> + def test_<%= underscore(cases["description"]) %> + <%= skip? %> + actual = Phrase.new("<%= cases["input"]["sentence"] %>").word_count + expected = <%= cases["expected"] %> + assert_equal expected, actual + end + <% end %> +end diff --git a/exercises/practice/word-count/word_count_test.rb b/exercises/practice/word-count/word_count_test.rb index ddda5d11f8..e7a44e16f6 100644 --- a/exercises/practice/word-count/word_count_test.rb +++ b/exercises/practice/word-count/word_count_test.rb @@ -4,99 +4,104 @@ class WordCountTest < Minitest::Test def test_count_one_word # skip - phrase = Phrase.new("word") - counts = { "word" => 1 } - assert_equal counts, phrase.word_count + actual = Phrase.new("word").word_count + expected = { "word" => 1 } + assert_equal expected, actual end def test_count_one_of_each_word skip - phrase = Phrase.new("one of each") - counts = { "one" => 1, "of" => 1, "each" => 1 } - assert_equal counts, phrase.word_count + actual = Phrase.new("one of each").word_count + expected = { "one" => 1, "of" => 1, "each" => 1 } + assert_equal expected, actual end def test_multiple_occurrences_of_a_word skip - phrase = Phrase.new("one fish two fish red fish blue fish") - counts = { "one" => 1, "fish" => 4, "two" => 1, "red" => 1, "blue" => 1 } - assert_equal counts, phrase.word_count + actual = Phrase.new("one fish two fish red fish blue fish").word_count + expected = { "one" => 1, "fish" => 4, "two" => 1, "red" => 1, "blue" => 1 } + assert_equal expected, actual end def test_handles_cramped_lists skip - phrase = Phrase.new("one,two,three") - counts = { "one" => 1, "two" => 1, "three" => 1 } - assert_equal counts, phrase.word_count + actual = Phrase.new("one,two,three").word_count + expected = { "one" => 1, "two" => 1, "three" => 1 } + assert_equal expected, actual end def test_handles_expanded_lists skip - phrase = Phrase.new("one,\ntwo,\nthree") - counts = { "one" => 1, "two" => 1, "three" => 1 } - assert_equal counts, phrase.word_count + actual = Phrase.new("one, +two, +three").word_count + expected = { "one" => 1, "two" => 1, "three" => 1 } + assert_equal expected, actual end def test_ignore_punctuation skip - phrase = Phrase.new("car: carpet as java: javascript!!&@$%^&") - counts = { "car" => 1, "carpet" => 1, "as" => 1, "java" => 1, "javascript" => 1 } - assert_equal counts, phrase.word_count + actual = Phrase.new("car: carpet as java: javascript!!&@$%^&").word_count + expected = { "car" => 1, "carpet" => 1, "as" => 1, "java" => 1, "javascript" => 1 } + assert_equal expected, actual end def test_include_numbers skip - phrase = Phrase.new("testing, 1, 2 testing") - counts = { "testing" => 2, "1" => 1, "2" => 1 } - assert_equal counts, phrase.word_count + actual = Phrase.new("testing, 1, 2 testing").word_count + expected = { "testing" => 2, "1" => 1, "2" => 1 } + assert_equal expected, actual end def test_normalize_case skip - phrase = Phrase.new("go Go GO Stop stop") - counts = { "go" => 3, "stop" => 2 } - assert_equal counts, phrase.word_count + actual = Phrase.new("go Go GO Stop stop").word_count + expected = { "go" => 3, "stop" => 2 } + assert_equal expected, actual end def test_with_apostrophes skip - phrase = Phrase.new("'First: don't laugh. Then: don't cry. You're getting it.'") - counts = { "first" => 1, "don't" => 2, "laugh" => 1, "then" => 1, "cry" => 1, "you're" => 1, "getting" => 1, "it" => 1 } - assert_equal counts, phrase.word_count + actual = Phrase.new("'First: don't laugh. Then: don't cry. You're getting it.'").word_count + expected = { "first" => 1, "don't" => 2, "laugh" => 1, "then" => 1, "cry" => 1, "you're" => 1, "getting" => 1, "it" => 1 } + assert_equal expected, actual end def test_with_quotations skip - phrase = Phrase.new("Joe can't tell between 'large' and large.") - counts = { "joe" => 1, "can't" => 1, "tell" => 1, "between" => 1, "large" => 2, "and" => 1 } - assert_equal counts, phrase.word_count + actual = Phrase.new("Joe can't tell between 'large' and large.").word_count + expected = { "joe" => 1, "can't" => 1, "tell" => 1, "between" => 1, "large" => 2, "and" => 1 } + assert_equal expected, actual end def test_substrings_from_the_beginning skip - phrase = Phrase.new("Joe can't tell between app, apple and a.") - counts = { "joe" => 1, "can't" => 1, "tell" => 1, "between" => 1, "app" => 1, "apple" => 1, "and" => 1, "a" => 1 } - assert_equal counts, phrase.word_count + actual = Phrase.new("Joe can't tell between app, apple and a.").word_count + expected = { "joe" => 1, "can't" => 1, "tell" => 1, "between" => 1, "app" => 1, "apple" => 1, "and" => 1, "a" => 1 } + assert_equal expected, actual end def test_multiple_spaces_not_detected_as_a_word skip - phrase = Phrase.new(" multiple whitespaces") - counts = { "multiple" => 1, "whitespaces" => 1 } - assert_equal counts, phrase.word_count + actual = Phrase.new(" multiple whitespaces").word_count + expected = { "multiple" => 1, "whitespaces" => 1 } + assert_equal expected, actual end def test_alternating_word_separators_not_detected_as_a_word skip - phrase = Phrase.new(",\n,one,\n ,two \n 'three'") - counts = { "one" => 1, "two" => 1, "three" => 1 } - assert_equal counts, phrase.word_count + actual = Phrase.new(", +,one, + ,two + 'three'").word_count + expected = { "one" => 1, "two" => 1, "three" => 1 } + assert_equal expected, actual end def test_quotation_for_word_with_apostrophe skip - phrase = Phrase.new("can, can't, 'can't'") - counts = { "can" => 1, "can't" => 2 } - assert_equal counts, phrase.word_count + actual = Phrase.new("can, can't, 'can't'").word_count + expected = { "can" => 1, "can't" => 2 } + assert_equal expected, actual end end diff --git a/exercises/practice/wordy/.meta/test_template.erb b/exercises/practice/wordy/.meta/test_template.erb new file mode 100644 index 0000000000..ecf6fa5895 --- /dev/null +++ b/exercises/practice/wordy/.meta/test_template.erb @@ -0,0 +1,20 @@ +require 'minitest/autorun' +require_relative 'wordy' + +class WordyTest < Minitest::Test + <% json["cases"].each do |cases| %> + def test_<%= underscore(cases["description"]) %> + <%= skip? %> + <%- if cases["expected"].is_a?(Hash) && cases["expected"].key?("error") -%> + assert_raises(ArgumentError) do + problem = WordProblem.new("<%= cases["input"]["question"] %>") + problem.answer + end + <%- else -%> + actual = WordProblem.new("<%= cases["input"]["question"] %>").answer + expected = <%= cases["expected"] %> + assert_equal expected, actual + <%- end -%> + end + <% end %> +end diff --git a/exercises/practice/wordy/.meta/tests.toml b/exercises/practice/wordy/.meta/tests.toml index f812dfa98b..a0a83ed0b9 100644 --- a/exercises/practice/wordy/.meta/tests.toml +++ b/exercises/practice/wordy/.meta/tests.toml @@ -12,9 +12,21 @@ [88bf4b28-0de3-4883-93c7-db1b14aa806e] description = "just a number" +[18983214-1dfc-4ebd-ac77-c110dde699ce] +description = "just a zero" + +[607c08ee-2241-4288-916d-dae5455c87e6] +description = "just a negative number" + [bb8c655c-cf42-4dfc-90e0-152fcfd8d4e0] description = "addition" +[bb9f2082-171c-46ad-ad4e-c3f72087c1b5] +description = "addition with a left hand zero" + +[6fa05f17-405a-4742-80ae-5d1a8edb0d5d] +description = "addition with a right hand zero" + [79e49e06-c5ae-40aa-a352-7a3a01f70015] description = "more addition" diff --git a/exercises/practice/wordy/wordy_test.rb b/exercises/practice/wordy/wordy_test.rb index 1739063a6a..79cbf3380d 100644 --- a/exercises/practice/wordy/wordy_test.rb +++ b/exercises/practice/wordy/wordy_test.rb @@ -2,103 +2,199 @@ require_relative 'wordy' class WordyTest < Minitest::Test - def test_addition + def test_just_a_number # skip - problem = WordProblem.new("What is 1 plus 1?") - assert_equal(2, problem.answer) + actual = WordProblem.new("What is 5?").answer + expected = 5 + assert_equal expected, actual + end + + def test_just_a_zero + skip + actual = WordProblem.new("What is 0?").answer + expected = 0 + assert_equal expected, actual + end + + def test_just_a_negative_number + skip + actual = WordProblem.new("What is -123?").answer + expected = -123 + assert_equal expected, actual + end + + def test_addition + skip + actual = WordProblem.new("What is 1 plus 1?").answer + expected = 2 + assert_equal expected, actual + end + + def test_addition_with_a_left_hand_zero + skip + actual = WordProblem.new("What is 0 plus 2?").answer + expected = 2 + assert_equal expected, actual + end + + def test_addition_with_a_right_hand_zero + skip + actual = WordProblem.new("What is 3 plus 0?").answer + expected = 3 + assert_equal expected, actual end def test_more_addition skip - problem = WordProblem.new("What is 53 plus 2?") - assert_equal(55, problem.answer) + actual = WordProblem.new("What is 53 plus 2?").answer + expected = 55 + assert_equal expected, actual end def test_addition_with_negative_numbers skip - problem = WordProblem.new("What is -1 plus -10?") - assert_equal(-11, problem.answer) + actual = WordProblem.new("What is -1 plus -10?").answer + expected = -11 + assert_equal expected, actual end def test_large_addition skip - problem = WordProblem.new("What is 123 plus 45678?") - assert_equal(45_801, problem.answer) + actual = WordProblem.new("What is 123 plus 45678?").answer + expected = 45_801 + assert_equal expected, actual end def test_subtraction skip - problem = WordProblem.new("What is 4 minus -12?") - assert_equal(16, problem.answer) + actual = WordProblem.new("What is 4 minus -12?").answer + expected = 16 + assert_equal expected, actual end def test_multiplication skip - problem = WordProblem.new("What is -3 multiplied by 25?") - assert_equal(-75, problem.answer) + actual = WordProblem.new("What is -3 multiplied by 25?").answer + expected = -75 + assert_equal expected, actual end def test_division skip - problem = WordProblem.new("What is 33 divided by -3?") - assert_equal(-11, problem.answer) + actual = WordProblem.new("What is 33 divided by -3?").answer + expected = -11 + assert_equal expected, actual end def test_multiple_additions skip - problem = WordProblem.new("What is 1 plus 1 plus 1?") - assert_equal(3, problem.answer) + actual = WordProblem.new("What is 1 plus 1 plus 1?").answer + expected = 3 + assert_equal expected, actual end def test_addition_and_subtraction skip - problem = WordProblem.new("What is 1 plus 5 minus -2?") - assert_equal(8, problem.answer) + actual = WordProblem.new("What is 1 plus 5 minus -2?").answer + expected = 8 + assert_equal expected, actual end def test_multiple_subtraction skip - problem = WordProblem.new("What is 20 minus 4 minus 13?") - assert_equal(3, problem.answer) + actual = WordProblem.new("What is 20 minus 4 minus 13?").answer + expected = 3 + assert_equal expected, actual end def test_subtraction_then_addition skip - problem = WordProblem.new("What is 17 minus 6 plus 3?") - assert_equal(14, problem.answer) + actual = WordProblem.new("What is 17 minus 6 plus 3?").answer + expected = 14 + assert_equal expected, actual end def test_multiple_multiplication skip - problem = WordProblem.new("What is 2 multiplied by -2 multiplied by 3?") - assert_equal(-12, problem.answer) + actual = WordProblem.new("What is 2 multiplied by -2 multiplied by 3?").answer + expected = -12 + assert_equal expected, actual end def test_addition_and_multiplication skip - problem = WordProblem.new("What is -3 plus 7 multiplied by -2?") - message = "You should ignore order of precedence. -3 + 7 * -2 = -8, not #{problem.answer}" - assert_equal(-8, problem.answer, message) + actual = WordProblem.new("What is -3 plus 7 multiplied by -2?").answer + expected = -8 + assert_equal expected, actual end def test_multiple_division skip - problem = WordProblem.new("What is -12 divided by 2 divided by -3?") - assert_equal(2, problem.answer) + actual = WordProblem.new("What is -12 divided by 2 divided by -3?").answer + expected = 2 + assert_equal expected, actual end def test_unknown_operation skip - problem = WordProblem.new("What is 52 cubed?") assert_raises(ArgumentError) do + problem = WordProblem.new("What is 52 cubed?") problem.answer end end def test_non_math_question skip - problem = WordProblem.new("Who is the President of the United States?") assert_raises(ArgumentError) do + problem = WordProblem.new("Who is the President of the United States?") + problem.answer + end + end + + def test_reject_problem_missing_an_operand + skip + assert_raises(ArgumentError) do + problem = WordProblem.new("What is 1 plus?") + problem.answer + end + end + + def test_reject_problem_with_no_operands_or_operators + skip + assert_raises(ArgumentError) do + problem = WordProblem.new("What is?") + problem.answer + end + end + + def test_reject_two_operations_in_a_row + skip + assert_raises(ArgumentError) do + problem = WordProblem.new("What is 1 plus plus 2?") + problem.answer + end + end + + def test_reject_two_numbers_in_a_row + skip + assert_raises(ArgumentError) do + problem = WordProblem.new("What is 1 plus 2 1?") + problem.answer + end + end + + def test_reject_postfix_notation + skip + assert_raises(ArgumentError) do + problem = WordProblem.new("What is 1 2 plus?") + problem.answer + end + end + + def test_reject_prefix_notation + skip + assert_raises(ArgumentError) do + problem = WordProblem.new("What is plus 1 2?") problem.answer end end diff --git a/exercises/practice/yacht/.meta/test_template.erb b/exercises/practice/yacht/.meta/test_template.erb new file mode 100644 index 0000000000..a912b7620c --- /dev/null +++ b/exercises/practice/yacht/.meta/test_template.erb @@ -0,0 +1,13 @@ +require 'minitest/autorun' +require_relative 'yacht' + +class YachtTest < Minitest::Test + <% json["cases"].each do |cases| %> + def test_<%= underscore(cases["description"]) %> + <%= skip? %> + actual = Yacht.new(<%= cases["input"]["dice"] %>, '<%= cases["input"]["category"] %>').score + expected = <%= cases["expected"] %> + assert_equal expected, actual + end + <% end %> +end diff --git a/exercises/practice/yacht/yacht_test.rb b/exercises/practice/yacht/yacht_test.rb index 3e713840a7..2230b327f3 100644 --- a/exercises/practice/yacht/yacht_test.rb +++ b/exercises/practice/yacht/yacht_test.rb @@ -4,197 +4,204 @@ class YachtTest < Minitest::Test def test_yacht # skip - score = Yacht.new([5, 5, 5, 5, 5], 'yacht').score + actual = Yacht.new([5, 5, 5, 5, 5], 'yacht').score expected = 50 - assert_equal expected, score + assert_equal expected, actual end def test_not_yacht skip - score = Yacht.new([1, 3, 3, 2, 5], 'yacht').score + actual = Yacht.new([1, 3, 3, 2, 5], 'yacht').score expected = 0 - assert_equal expected, score + assert_equal expected, actual end def test_ones skip - score = Yacht.new([1, 1, 1, 3, 5], 'ones').score + actual = Yacht.new([1, 1, 1, 3, 5], 'ones').score expected = 3 - assert_equal expected, score + assert_equal expected, actual end def test_ones_out_of_order skip - score = Yacht.new([3, 1, 1, 5, 1], 'ones').score + actual = Yacht.new([3, 1, 1, 5, 1], 'ones').score expected = 3 - assert_equal expected, score + assert_equal expected, actual end def test_no_ones skip - score = Yacht.new([4, 3, 6, 5, 5], 'ones').score + actual = Yacht.new([4, 3, 6, 5, 5], 'ones').score expected = 0 - assert_equal expected, score + assert_equal expected, actual end def test_twos skip - score = Yacht.new([2, 3, 4, 5, 6], 'twos').score + actual = Yacht.new([2, 3, 4, 5, 6], 'twos').score expected = 2 - assert_equal expected, score + assert_equal expected, actual end def test_fours skip - score = Yacht.new([1, 4, 1, 4, 1], 'fours').score + actual = Yacht.new([1, 4, 1, 4, 1], 'fours').score expected = 8 - assert_equal expected, score + assert_equal expected, actual end - def test_yacht_as_threes + def test_yacht_counted_as_threes skip - score = Yacht.new([3, 3, 3, 3, 3], 'threes').score + actual = Yacht.new([3, 3, 3, 3, 3], 'threes').score expected = 15 - assert_equal expected, score + assert_equal expected, actual end - def test_yacht_of_threes_as_fives + def test_yacht_of_3s_counted_as_fives skip - score = Yacht.new([3, 3, 3, 3, 3], 'fives').score + actual = Yacht.new([3, 3, 3, 3, 3], 'fives').score expected = 0 - assert_equal expected, score + assert_equal expected, actual + end + + def test_fives + skip + actual = Yacht.new([1, 5, 3, 5, 3], 'fives').score + expected = 10 + assert_equal expected, actual end def test_sixes skip - score = Yacht.new([2, 3, 4, 5, 6], 'sixes').score + actual = Yacht.new([2, 3, 4, 5, 6], 'sixes').score expected = 6 - assert_equal expected, score + assert_equal expected, actual end def test_full_house_two_small_three_big skip - score = Yacht.new([2, 2, 4, 4, 4], 'full house').score + actual = Yacht.new([2, 2, 4, 4, 4], 'full house').score expected = 16 - assert_equal expected, score + assert_equal expected, actual end def test_full_house_three_small_two_big skip - score = Yacht.new([5, 3, 3, 5, 3], 'full house').score + actual = Yacht.new([5, 3, 3, 5, 3], 'full house').score expected = 19 - assert_equal expected, score + assert_equal expected, actual end - def test_two_pair_not_full_house + def test_two_pair_is_not_a_full_house skip - score = Yacht.new([2, 2, 4, 4, 5], 'full house').score + actual = Yacht.new([2, 2, 4, 4, 5], 'full house').score expected = 0 - assert_equal expected, score + assert_equal expected, actual end - def test_four_of_a_kind_not_full_house + def test_four_of_a_kind_is_not_a_full_house skip - score = Yacht.new([1, 4, 4, 4, 4], 'full house').score + actual = Yacht.new([1, 4, 4, 4, 4], 'full house').score expected = 0 - assert_equal expected, score + assert_equal expected, actual end - def test_yacht_not_full_house + def test_yacht_is_not_a_full_house skip - score = Yacht.new([2, 2, 2, 2, 2], 'full house').score + actual = Yacht.new([2, 2, 2, 2, 2], 'full house').score expected = 0 - assert_equal expected, score + assert_equal expected, actual end def test_four_of_a_kind skip - score = Yacht.new([6, 6, 4, 6, 6], 'four of a kind').score + actual = Yacht.new([6, 6, 4, 6, 6], 'four of a kind').score expected = 24 - assert_equal expected, score + assert_equal expected, actual end - def test_yacht_as_four_of_a_kind + def test_yacht_can_be_scored_as_four_of_a_kind skip - score = Yacht.new([3, 3, 3, 3, 3], 'four of a kind').score + actual = Yacht.new([3, 3, 3, 3, 3], 'four of a kind').score expected = 12 - assert_equal expected, score + assert_equal expected, actual end - def test_full_house_not_four_of_a_kind + def test_full_house_is_not_four_of_a_kind skip - score = Yacht.new([3, 3, 3, 5, 5], 'four of a kind').score + actual = Yacht.new([3, 3, 3, 5, 5], 'four of a kind').score expected = 0 - assert_equal expected, score + assert_equal expected, actual end def test_little_straight skip - score = Yacht.new([3, 5, 4, 1, 2], 'little straight').score + actual = Yacht.new([3, 5, 4, 1, 2], 'little straight').score expected = 30 - assert_equal expected, score + assert_equal expected, actual end - def test_little_straight_not_big_straight + def test_little_straight_as_big_straight skip - score = Yacht.new([1, 2, 3, 4, 5], 'big straight').score + actual = Yacht.new([1, 2, 3, 4, 5], 'big straight').score expected = 0 - assert_equal expected, score + assert_equal expected, actual end - def test_ordered_but_not_little_straight + def test_four_in_order_but_not_a_little_straight skip - score = Yacht.new([1, 1, 2, 3, 4], 'little straight').score + actual = Yacht.new([1, 1, 2, 3, 4], 'little straight').score expected = 0 - assert_equal expected, score + assert_equal expected, actual end - def test_no_pairs_but_not_little_straight + def test_no_pairs_but_not_a_little_straight skip - score = Yacht.new([1, 2, 3, 4, 6], 'little straight').score + actual = Yacht.new([1, 2, 3, 4, 6], 'little straight').score expected = 0 - assert_equal expected, score + assert_equal expected, actual end - def test_min_and_max_do_not_make_little_straight + def test_minimum_is_1_maximum_is_5_but_not_a_little_straight skip - score = Yacht.new([1, 1, 3, 4, 5], 'little straight').score + actual = Yacht.new([1, 1, 3, 4, 5], 'little straight').score expected = 0 - assert_equal expected, score + assert_equal expected, actual end def test_big_straight skip - score = Yacht.new([4, 6, 2, 5, 3], 'big straight').score + actual = Yacht.new([4, 6, 2, 5, 3], 'big straight').score expected = 30 - assert_equal expected, score + assert_equal expected, actual end def test_big_straight_as_little_straight skip - score = Yacht.new([6, 5, 4, 3, 2], 'little straight').score + actual = Yacht.new([6, 5, 4, 3, 2], 'little straight').score expected = 0 - assert_equal expected, score + assert_equal expected, actual end - def test_no_pairs_but_not_big_straight + def test_no_pairs_but_not_a_big_straight skip - score = Yacht.new([6, 5, 4, 3, 1], 'big straight').score + actual = Yacht.new([6, 5, 4, 3, 1], 'big straight').score expected = 0 - assert_equal expected, score + assert_equal expected, actual end def test_choice skip - score = Yacht.new([3, 3, 5, 6, 6], 'choice').score + actual = Yacht.new([3, 3, 5, 6, 6], 'choice').score expected = 23 - assert_equal expected, score + assert_equal expected, actual end def test_yacht_as_choice skip - score = Yacht.new([2, 2, 2, 2, 2], 'choice').score + actual = Yacht.new([2, 2, 2, 2, 2], 'choice').score expected = 10 - assert_equal expected, score + assert_equal expected, actual end end