From 2d1ae1ff3f55c89322e41c21bd2dbd147c65442a Mon Sep 17 00:00:00 2001 From: meatball Date: Mon, 13 Oct 2025 22:11:15 +0200 Subject: [PATCH] Add templates for exercises batch 13 --- .../practice/connect/.meta/test_template.erb | 16 ++ exercises/practice/connect/connect_test.rb | 50 ++-- .../nucleotide-count/.meta/test_template.erb | 19 ++ .../nucleotide-count/nucleotide_count_test.rb | 52 ++--- .../ocr-numbers/.meta/test_template.erb | 20 ++ .../practice/ocr-numbers/ocr_numbers_test.rb | 214 ++++++++++-------- .../proverb/.meta/additional_tests.json | 37 +++ exercises/practice/proverb/.meta/example.rb | 19 +- .../practice/proverb/.meta/test_template.erb | 21 ++ exercises/practice/proverb/.meta/tests.toml | 6 + exercises/practice/proverb/proverb_test.rb | 93 +++++--- .../tournament/.meta/test_template.erb | 21 ++ .../practice/tournament/tournament_test.rb | 48 +++- 13 files changed, 416 insertions(+), 200 deletions(-) create mode 100644 exercises/practice/connect/.meta/test_template.erb create mode 100644 exercises/practice/nucleotide-count/.meta/test_template.erb create mode 100644 exercises/practice/ocr-numbers/.meta/test_template.erb create mode 100644 exercises/practice/proverb/.meta/additional_tests.json create mode 100644 exercises/practice/proverb/.meta/test_template.erb create mode 100644 exercises/practice/tournament/.meta/test_template.erb diff --git a/exercises/practice/connect/.meta/test_template.erb b/exercises/practice/connect/.meta/test_template.erb new file mode 100644 index 0000000000..31dd936d16 --- /dev/null +++ b/exercises/practice/connect/.meta/test_template.erb @@ -0,0 +1,16 @@ +require 'minitest/autorun' +require_relative 'connect' + +class ConnectTest < Minitest::Test +<% json["cases"].each do |cases| %> + def test_<%= underscore(cases["description"]) %> + <%= skip? %> + expected = '<%= cases["expected"] %>' + board = [ + '<%= cases["input"]["board"].join("', \n'") %>' + ].map { |row| row.gsub(/^ */, '') } + actual = Board.new(board).winner + assert_equal expected, actual, '<%= cases["description"] %>' + end + <% end %> +end diff --git a/exercises/practice/connect/connect_test.rb b/exercises/practice/connect/connect_test.rb index 718be9f374..0724c36d4d 100644 --- a/exercises/practice/connect/connect_test.rb +++ b/exercises/practice/connect/connect_test.rb @@ -4,6 +4,7 @@ class ConnectTest < Minitest::Test def test_an_empty_board_has_no_winner # skip + expected = '' board = [ '. . . . .', ' . . . . .', @@ -11,42 +12,46 @@ def test_an_empty_board_has_no_winner ' . . . . .', ' . . . . .' ].map { |row| row.gsub(/^ */, '') } - game = Board.new(board) - assert_equal '', game.winner, 'an empty board has no winner' + actual = Board.new(board).winner + assert_equal expected, actual, 'an empty board has no winner' end def test_x_can_win_on_a_1x1_board skip + expected = 'X' board = [ 'X' ].map { |row| row.gsub(/^ */, '') } - game = Board.new(board) - assert_equal 'X', game.winner, 'X can win on a 1x1 board' + actual = Board.new(board).winner + assert_equal expected, actual, 'X can win on a 1x1 board' end def test_o_can_win_on_a_1x1_board skip + expected = 'O' board = [ 'O' ].map { |row| row.gsub(/^ */, '') } - game = Board.new(board) - assert_equal 'O', game.winner, 'O can win on a 1x1 board' + actual = Board.new(board).winner + assert_equal expected, actual, 'O can win on a 1x1 board' end def test_only_edges_does_not_make_a_winner skip + expected = '' board = [ 'O O O X', ' X . . X', ' X . . X', ' X O O O' ].map { |row| row.gsub(/^ */, '') } - game = Board.new(board) - assert_equal '', game.winner, 'only edges does not make a winner' + actual = Board.new(board).winner + assert_equal expected, actual, 'only edges does not make a winner' end def test_illegal_diagonal_does_not_make_a_winner skip + expected = '' board = [ 'X O . .', ' O X X X', @@ -54,12 +59,13 @@ def test_illegal_diagonal_does_not_make_a_winner ' . O X .', ' X X O O' ].map { |row| row.gsub(/^ */, '') } - game = Board.new(board) - assert_equal '', game.winner, 'illegal diagonal does not make a winner' + actual = Board.new(board).winner + assert_equal expected, actual, 'illegal diagonal does not make a winner' end def test_nobody_wins_crossing_adjacent_angles skip + expected = '' board = [ 'X . . .', ' . X O .', @@ -67,12 +73,13 @@ def test_nobody_wins_crossing_adjacent_angles ' . O . X', ' . . O .' ].map { |row| row.gsub(/^ */, '') } - game = Board.new(board) - assert_equal '', game.winner, 'nobody wins crossing adjacent angles' + actual = Board.new(board).winner + assert_equal expected, actual, 'nobody wins crossing adjacent angles' end def test_x_wins_crossing_from_left_to_right skip + expected = 'X' board = [ '. O . .', ' O X X X', @@ -80,12 +87,13 @@ def test_x_wins_crossing_from_left_to_right ' X X O X', ' . O X .' ].map { |row| row.gsub(/^ */, '') } - game = Board.new(board) - assert_equal 'X', game.winner, 'X wins crossing from left to right' + actual = Board.new(board).winner + assert_equal expected, actual, 'X wins crossing from left to right' end def test_o_wins_crossing_from_top_to_bottom skip + expected = 'O' board = [ '. O . .', ' O X X X', @@ -93,12 +101,13 @@ def test_o_wins_crossing_from_top_to_bottom ' X X O X', ' . O X .' ].map { |row| row.gsub(/^ */, '') } - game = Board.new(board) - assert_equal 'O', game.winner, 'O wins crossing from top to bottom' + actual = Board.new(board).winner + assert_equal expected, actual, 'O wins crossing from top to bottom' end def test_x_wins_using_a_convoluted_path skip + expected = 'X' board = [ '. X X . .', ' X . X . X', @@ -106,12 +115,13 @@ def test_x_wins_using_a_convoluted_path ' . X X . .', ' O O O O O' ].map { |row| row.gsub(/^ */, '') } - game = Board.new(board) - assert_equal 'X', game.winner, 'X wins using a convoluted path' + actual = Board.new(board).winner + assert_equal expected, actual, 'X wins using a convoluted path' end def test_x_wins_using_a_spiral_path skip + expected = 'X' board = [ 'O X X X X X X X X', ' O X O O O O O O O', @@ -123,7 +133,7 @@ def test_x_wins_using_a_spiral_path ' O O O O O O O X O', ' X X X X X X X X O' ].map { |row| row.gsub(/^ */, '') } - game = Board.new(board) - assert_equal 'X', game.winner, 'X wins using a spiral path' + actual = Board.new(board).winner + assert_equal expected, actual, 'X wins using a spiral path' end end diff --git a/exercises/practice/nucleotide-count/.meta/test_template.erb b/exercises/practice/nucleotide-count/.meta/test_template.erb new file mode 100644 index 0000000000..a04566738e --- /dev/null +++ b/exercises/practice/nucleotide-count/.meta/test_template.erb @@ -0,0 +1,19 @@ +require 'minitest/autorun' +require_relative 'nucleotide_count' + +class NucleotideTest < 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 + Nucleotide.from_dna('<%= cases["input"]["strand"] %>') + end + <%- else -%> + expected = <%= cases["expected"] %> + actual = Nucleotide.from_dna('<%= cases["input"]["strand"] %>').histogram + assert_equal expected, actual + <%- end -%> + end +<% end %> +end diff --git a/exercises/practice/nucleotide-count/nucleotide_count_test.rb b/exercises/practice/nucleotide-count/nucleotide_count_test.rb index 0dd5dee6fc..9a90784107 100644 --- a/exercises/practice/nucleotide-count/nucleotide_count_test.rb +++ b/exercises/practice/nucleotide-count/nucleotide_count_test.rb @@ -2,52 +2,38 @@ require_relative 'nucleotide_count' class NucleotideTest < Minitest::Test - def test_empty_dna_strand_has_no_adenosine - assert_equal 0, Nucleotide.from_dna('').count('A') + def test_empty_strand + # skip + expected = { "A" => 0, "C" => 0, "G" => 0, "T" => 0 } + actual = Nucleotide.from_dna('').histogram + assert_equal expected, actual end - def test_repetitive_cytidine_gets_counted + def test_can_count_one_nucleotide_in_single_character_input skip - assert_equal 5, Nucleotide.from_dna('CCCCC').count('C') + expected = { "A" => 0, "C" => 0, "G" => 1, "T" => 0 } + actual = Nucleotide.from_dna('G').histogram + assert_equal expected, actual end - def test_counts_only_thymidine + def test_strand_with_repeated_nucleotide skip - assert_equal 1, Nucleotide.from_dna('GGGGGTAACCCGG').count('T') + expected = { "A" => 0, "C" => 0, "G" => 7, "T" => 0 } + actual = Nucleotide.from_dna('GGGGGGG').histogram + assert_equal expected, actual end - def test_counts_a_nucleotide_only_once + def test_strand_with_multiple_nucleotides skip - dna = Nucleotide.from_dna('CGATTGGG') - dna.count('T') - dna.count('T') - assert_equal 2, dna.count('T') + expected = { "A" => 20, "C" => 12, "G" => 17, "T" => 21 } + actual = Nucleotide.from_dna('AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC').histogram + assert_equal expected, actual end - def test_empty_dna_strand_has_no_nucleotides - skip - expected = { 'A' => 0, 'T' => 0, 'C' => 0, 'G' => 0 } - assert_equal expected, Nucleotide.from_dna('').histogram - end - - def test_repetitive_sequence_has_only_guanosine - skip - expected = { 'A' => 0, 'T' => 0, 'C' => 0, 'G' => 8 } - assert_equal expected, Nucleotide.from_dna('GGGGGGGG').histogram - end - - def test_counts_all_nucleotides - skip - s = 'AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC' - dna = Nucleotide.from_dna(s) - expected = { 'A' => 20, 'T' => 21, 'G' => 17, 'C' => 12 } - assert_equal expected, dna.histogram - end - - def test_validates_dna + def test_strand_with_invalid_nucleotides skip assert_raises ArgumentError do - Nucleotide.from_dna('JOHNNYAPPLESEED') + Nucleotide.from_dna('AGXXACT') end end end diff --git a/exercises/practice/ocr-numbers/.meta/test_template.erb b/exercises/practice/ocr-numbers/.meta/test_template.erb new file mode 100644 index 0000000000..47b0b8b07a --- /dev/null +++ b/exercises/practice/ocr-numbers/.meta/test_template.erb @@ -0,0 +1,20 @@ +require 'minitest/autorun' +require_relative 'ocr_numbers' + +class OcrNumbersTest < Minitest::Test +<% json["cases"].each do |cases| %> + def test_<%= underscore(cases["description"]) %> + <%= skip? %> + input = ['<%= cases["input"]["rows"].join("', \n'") %>'].join("\n") + <%- if cases["expected"].is_a?(Hash) && cases["expected"].key?("error") -%> + assert_raises(ArgumentError) do + OcrNumbers.convert(input) + end + <%- else -%> + expected = '<%= cases["expected"] %>' + actual = OcrNumbers.convert(input) + assert_equal expected, actual + <%- end -%> + end +<% end %> +end diff --git a/exercises/practice/ocr-numbers/ocr_numbers_test.rb b/exercises/practice/ocr-numbers/ocr_numbers_test.rb index bb0f1f8003..6acaeb4344 100644 --- a/exercises/practice/ocr-numbers/ocr_numbers_test.rb +++ b/exercises/practice/ocr-numbers/ocr_numbers_test.rb @@ -4,36 +4,42 @@ class OcrNumbersTest < Minitest::Test def test_recognizes_0 # skip - input = [" _ ", - "| |", - "|_|", - " "].join("\n") - assert_equal "0", OcrNumbers.convert(input) + input = [' _ ', + '| |', + '|_|', + ' '].join("\n") + expected = '0' + actual = OcrNumbers.convert(input) + assert_equal expected, actual end def test_recognizes_1 skip - input = [" ", - " |", - " |", - " "].join("\n") - assert_equal "1", OcrNumbers.convert(input) + input = [' ', + ' |', + ' |', + ' '].join("\n") + expected = '1' + actual = OcrNumbers.convert(input) + assert_equal expected, actual end - def test_unreadable_but_correctly_sized_inputs_return_question_mark + def test_unreadable_but_correctly_sized_inputs_return_ skip - input = [" ", - " _", - " |", - " "].join("\n") - assert_equal "?", OcrNumbers.convert(input) + input = [' ', + ' _', + ' |', + ' '].join("\n") + expected = '?' + actual = OcrNumbers.convert(input) + assert_equal expected, actual end def test_input_with_a_number_of_lines_that_is_not_a_multiple_of_four_raises_an_error skip - input = [" _ ", - "| |", - " "].join("\n") + input = [' _ ', + '| |', + ' '].join("\n") assert_raises(ArgumentError) do OcrNumbers.convert(input) end @@ -41,10 +47,10 @@ def test_input_with_a_number_of_lines_that_is_not_a_multiple_of_four_raises_an_e def test_input_with_a_number_of_columns_that_is_not_a_multiple_of_three_raises_an_error skip - input = [" ", - " |", - " |", - " "].join("\n") + input = [' ', + ' |', + ' |', + ' '].join("\n") assert_raises(ArgumentError) do OcrNumbers.convert(input) end @@ -52,117 +58,141 @@ def test_input_with_a_number_of_columns_that_is_not_a_multiple_of_three_raises_a def test_recognizes_110101100 skip - input = [" _ _ _ _ ", - " | || | || | | || || |", - " | ||_| ||_| | ||_||_|", - " "].join("\n") - assert_equal "110101100", OcrNumbers.convert(input) + input = [' _ _ _ _ ', + ' | || | || | | || || |', + ' | ||_| ||_| | ||_||_|', + ' '].join("\n") + expected = '110101100' + actual = OcrNumbers.convert(input) + assert_equal expected, actual end - def test_garbled_numbers_in_a_string_are_replaced_with_question_mark + def test_garbled_numbers_in_a_string_are_replaced_with_ skip - input = [" _ _ _ ", - " | || | || | || || |", - " | | _| ||_| | ||_||_|", - " "].join("\n") - assert_equal "11?10?1?0", OcrNumbers.convert(input) + input = [' _ _ _ ', + ' | || | || | || || |', + ' | | _| ||_| | ||_||_|', + ' '].join("\n") + expected = '11?10?1?0' + actual = OcrNumbers.convert(input) + assert_equal expected, actual end def test_recognizes_2 skip - input = [" _ ", - " _|", - "|_ ", - " "].join("\n") - assert_equal "2", OcrNumbers.convert(input) + input = [' _ ', + ' _|', + '|_ ', + ' '].join("\n") + expected = '2' + actual = OcrNumbers.convert(input) + assert_equal expected, actual end def test_recognizes_3 skip - input = [" _ ", - " _|", - " _|", - " "].join("\n") - assert_equal "3", OcrNumbers.convert(input) + input = [' _ ', + ' _|', + ' _|', + ' '].join("\n") + expected = '3' + actual = OcrNumbers.convert(input) + assert_equal expected, actual end def test_recognizes_4 skip - input = [" ", - "|_|", - " |", - " "].join("\n") - assert_equal "4", OcrNumbers.convert(input) + input = [' ', + '|_|', + ' |', + ' '].join("\n") + expected = '4' + actual = OcrNumbers.convert(input) + assert_equal expected, actual end def test_recognizes_5 skip - input = [" _ ", - "|_ ", - " _|", - " "].join("\n") - assert_equal "5", OcrNumbers.convert(input) + input = [' _ ', + '|_ ', + ' _|', + ' '].join("\n") + expected = '5' + actual = OcrNumbers.convert(input) + assert_equal expected, actual end def test_recognizes_6 skip - input = [" _ ", - "|_ ", - "|_|", - " "].join("\n") - assert_equal "6", OcrNumbers.convert(input) + input = [' _ ', + '|_ ', + '|_|', + ' '].join("\n") + expected = '6' + actual = OcrNumbers.convert(input) + assert_equal expected, actual end def test_recognizes_7 skip - input = [" _ ", - " |", - " |", - " "].join("\n") - assert_equal "7", OcrNumbers.convert(input) + input = [' _ ', + ' |', + ' |', + ' '].join("\n") + expected = '7' + actual = OcrNumbers.convert(input) + assert_equal expected, actual end def test_recognizes_8 skip - input = [" _ ", - "|_|", - "|_|", - " "].join("\n") - assert_equal "8", OcrNumbers.convert(input) + input = [' _ ', + '|_|', + '|_|', + ' '].join("\n") + expected = '8' + actual = OcrNumbers.convert(input) + assert_equal expected, actual end def test_recognizes_9 skip - input = [" _ ", - "|_|", - " _|", - " "].join("\n") - assert_equal "9", OcrNumbers.convert(input) + input = [' _ ', + '|_|', + ' _|', + ' '].join("\n") + expected = '9' + actual = OcrNumbers.convert(input) + assert_equal expected, actual end def test_recognizes_string_of_decimal_numbers skip - input = [" _ _ _ _ _ _ _ _ ", - " | _| _||_||_ |_ ||_||_|| |", - " ||_ _| | _||_| ||_| _||_|", - " "].join("\n") - assert_equal "1234567890", OcrNumbers.convert(input) + input = [' _ _ _ _ _ _ _ _ ', + ' | _| _||_||_ |_ ||_||_|| |', + ' ||_ _| | _||_| ||_| _||_|', + ' '].join("\n") + expected = '1234567890' + actual = OcrNumbers.convert(input) + assert_equal expected, actual end def test_numbers_separated_by_empty_lines_are_recognized_lines_are_joined_by_commas skip - input = [" _ _ ", - " | _| _|", - " ||_ _|", - " ", - " _ _ ", - "|_||_ |_ ", - " | _||_|", - " ", - " _ _ _ ", - " ||_||_|", - " ||_| _|", - " "].join("\n") - assert_equal "123,456,789", OcrNumbers.convert(input) + input = [' _ _ ', + ' | _| _|', + ' ||_ _|', + ' ', + ' _ _ ', + '|_||_ |_ ', + ' | _||_|', + ' ', + ' _ _ _ ', + ' ||_||_|', + ' ||_| _|', + ' '].join("\n") + expected = '123,456,789' + actual = OcrNumbers.convert(input) + assert_equal expected, actual end end diff --git a/exercises/practice/proverb/.meta/additional_tests.json b/exercises/practice/proverb/.meta/additional_tests.json new file mode 100644 index 0000000000..e44afbc2cd --- /dev/null +++ b/exercises/practice/proverb/.meta/additional_tests.json @@ -0,0 +1,37 @@ +{ "cases": + [ + { + "uuid": "4d420abc-5359-4a6c-b6a8-05edcd9463c9", + "description": "an optional qualifier in the final consequence", + "property": "recite", + "input": { + "strings": ["nail", "shoe", "horse", "rider", "message", "battle", "kingdom"], + "qualifier": "horseshoe" + }, + "expected": [ + "For want of a nail the shoe was lost.", + "For want of a shoe the horse was lost.", + "For want of a horse the rider was lost.", + "For want of a rider the message was lost.", + "For want of a message the battle was lost.", + "For want of a battle the kingdom was lost.", + "And all for the want of a horseshoe nail." + ] + }, + { + "uuid": "f610c8cc-b29e-485c-a82c-e9a9b337bd78", + "description": "proverb is same each time", + "property": "consistency", + "input": { + "strings": ["nail", "shoe"], + "qualifier": "horseshoe" + }, + "expected": [ + "For want of a horseshoe nail the shoe was lost.", + "For want of a horseshoe shoe the horse was lost.", + "For want of a horseshoe horse the rider was lost.", + "And all for the want of a horseshoe nail." + ] + } + ] +} diff --git a/exercises/practice/proverb/.meta/example.rb b/exercises/practice/proverb/.meta/example.rb index eaa8fe4957..541e8ebd6c 100644 --- a/exercises/practice/proverb/.meta/example.rb +++ b/exercises/practice/proverb/.meta/example.rb @@ -2,15 +2,18 @@ class Proverb attr_reader :chain, :options def initialize(*chain) - if chain.last.is_a? Hash - @options = chain.pop - else - @options = {} - end + @options = if chain.last.is_a? Hash + chain.pop + else + {} + end @chain = chain end def to_s + return "" if chain.empty? + return conclusion[1..] if chain.size <= 1 + chain_of_events + conclusion end @@ -25,14 +28,14 @@ def causes_and_effects end def consequence(cause, effect) - 'For want of a %s the %s was lost.' % [cause, effect] + format('For want of a %s the %s was lost.', cause, effect) end def qualifier - options[:qualifier] ? '%s ' % options[:qualifier] : '' + options[:qualifier] ? '%s ' % options[:qualifier] : '' end def conclusion - "\nAnd all for the want of a %s%s." % [qualifier, chain.first] + format("\nAnd all for the want of a %s%s.", qualifier, chain.first) end end diff --git a/exercises/practice/proverb/.meta/test_template.erb b/exercises/practice/proverb/.meta/test_template.erb new file mode 100644 index 0000000000..481a33aa08 --- /dev/null +++ b/exercises/practice/proverb/.meta/test_template.erb @@ -0,0 +1,21 @@ +require 'minitest/autorun' +require_relative 'proverb' + +class ProverbTest < Minitest::Test +<% json["cases"].each do |cases| %> + def test_<%= underscore(cases["description"]) %> + #<%= skip? %> + chain = %w[<%= cases["input"]["strings"].join(" ") %>] + proverb = Proverb.new(*chain, <%= cases["input"].key?("qualifier") ? "qualifier: '#{cases["input"]["qualifier"]}'" : "" %>) + <%- if cases["property"] == "consistency" %> + actual = proverb.to_s + expected = proverb.to_s + assert_equal expected, actual + <%- else -%> + actual = proverb.to_s + expected = "<%= cases["expected"].join("\\n\" \\\n\t\t\t\"") %>" + assert_equal expected, actual + <%- end -%> + end + <% end %> +end diff --git a/exercises/practice/proverb/.meta/tests.toml b/exercises/practice/proverb/.meta/tests.toml index dc92a0c96c..8031a5854c 100644 --- a/exercises/practice/proverb/.meta/tests.toml +++ b/exercises/practice/proverb/.meta/tests.toml @@ -26,3 +26,9 @@ description = "full proverb" [c1eefa5a-e8d9-41c7-91d4-99fab6d6b9f7] description = "four pieces modernized" + +[4d420abc-5359-4a6c-b6a8-05edcd9463c9] +description = "an optional qualifier in the final consequence" + +[f610c8cc-b29e-485c-a82c-e9a9b337bd78] +description = "proverb is same each time" diff --git a/exercises/practice/proverb/proverb_test.rb b/exercises/practice/proverb/proverb_test.rb index dc812e85bb..2f1ee18983 100644 --- a/exercises/practice/proverb/proverb_test.rb +++ b/exercises/practice/proverb/proverb_test.rb @@ -2,71 +2,94 @@ require_relative 'proverb' class ProverbTest < Minitest::Test - def test_a_single_consequence - proverb = Proverb.new('nail', 'shoe') - expected = "For want of a nail the shoe was lost.\n" \ - 'And all for the want of a nail.' - assert_equal expected, proverb.to_s + def test_zero_pieces + ## skip + chain = %w[] + proverb = Proverb.new(*chain) + actual = proverb.to_s + expected = "" + assert_equal expected, actual end - def test_a_short_chain_of_consequences - skip - proverb = Proverb.new('nail', 'shoe', 'horse') - expected = "For want of a nail the shoe was lost.\n" \ - "For want of a shoe the horse was lost.\n" \ - 'And all for the want of a nail.' - assert_equal expected, proverb.to_s + def test_one_piece + # skip + chain = %w[nail] + proverb = Proverb.new(*chain) + actual = proverb.to_s + expected = "And all for the want of a nail." + assert_equal expected, actual end - def test_a_longer_chain_of_consequences - skip - proverb = Proverb.new('nail', 'shoe', 'horse', 'rider') + def test_two_pieces + # skip + chain = %w[nail shoe] + proverb = Proverb.new(*chain) + actual = proverb.to_s expected = "For want of a nail the shoe was lost.\n" \ - "For want of a shoe the horse was lost.\n" \ - "For want of a horse the rider was lost.\n" \ - 'And all for the want of a nail.' - assert_equal expected, proverb.to_s + "And all for the want of a nail." + assert_equal expected, actual end - def test_proverb_does_not_hard_code_the_rhyme_dictionary - skip - proverb = Proverb.new('key', 'value') - expected = "For want of a key the value was lost.\n" \ - 'And all for the want of a key.' - assert_equal expected, proverb.to_s + def test_three_pieces + # skip + chain = %w[nail shoe horse] + proverb = Proverb.new(*chain) + actual = proverb.to_s + expected = "For want of a nail the shoe was lost.\n" \ + "For want of a shoe the horse was lost.\n" \ + "And all for the want of a nail." + assert_equal expected, actual end - def test_the_whole_proverb - skip + def test_full_proverb + # skip chain = %w[nail shoe horse rider message battle kingdom] proverb = Proverb.new(*chain) + actual = proverb.to_s expected = "For want of a nail the shoe was lost.\n" \ "For want of a shoe the horse was lost.\n" \ "For want of a horse the rider was lost.\n" \ "For want of a rider the message was lost.\n" \ "For want of a message the battle was lost.\n" \ "For want of a battle the kingdom was lost.\n" \ - 'And all for the want of a nail.' - assert_equal expected, proverb.to_s + "And all for the want of a nail." + assert_equal expected, actual + end + + def test_four_pieces_modernized + # skip + chain = %w[pin gun soldier battle] + proverb = Proverb.new(*chain) + actual = proverb.to_s + expected = "For want of a pin the gun was lost.\n" \ + "For want of a gun the soldier was lost.\n" \ + "For want of a soldier the battle was lost.\n" \ + "And all for the want of a pin." + assert_equal expected, actual end def test_an_optional_qualifier_in_the_final_consequence - skip + # skip chain = %w[nail shoe horse rider message battle kingdom] proverb = Proverb.new(*chain, qualifier: 'horseshoe') + actual = proverb.to_s expected = "For want of a nail the shoe was lost.\n" \ "For want of a shoe the horse was lost.\n" \ "For want of a horse the rider was lost.\n" \ "For want of a rider the message was lost.\n" \ "For want of a message the battle was lost.\n" \ "For want of a battle the kingdom was lost.\n" \ - 'And all for the want of a horseshoe nail.' - assert_equal expected, proverb.to_s + "And all for the want of a horseshoe nail." + assert_equal expected, actual end def test_proverb_is_same_each_time - skip - proverb = Proverb.new('nail', 'shoe') - assert_equal proverb.to_s, proverb.to_s + # skip + chain = %w[nail shoe] + proverb = Proverb.new(*chain, qualifier: 'horseshoe') + + actual = proverb.to_s + expected = proverb.to_s + assert_equal expected, actual end end diff --git a/exercises/practice/tournament/.meta/test_template.erb b/exercises/practice/tournament/.meta/test_template.erb new file mode 100644 index 0000000000..338b37c09e --- /dev/null +++ b/exercises/practice/tournament/.meta/test_template.erb @@ -0,0 +1,21 @@ +require 'minitest/autorun' +require_relative 'tournament' + +class TournamentTest < Minitest::Test +<% json["cases"].each do |cases| %> + def test_<%= underscore(cases["description"]) %> + <%= skip? %> + input = <<~INPUT +<%= cases["input"]["rows"].join("\n") %> + INPUT + + actual = Tournament.tally(input) + + expected = <<~TALLY + <%= cases["expected"].join("\n ") %> + TALLY + + assert_equal expected, actual + end + <% end %> +end diff --git a/exercises/practice/tournament/tournament_test.rb b/exercises/practice/tournament/tournament_test.rb index b95998e622..43fddc08fe 100644 --- a/exercises/practice/tournament/tournament_test.rb +++ b/exercises/practice/tournament/tournament_test.rb @@ -8,11 +8,13 @@ def test_just_the_header_if_no_input INPUT + actual = Tournament.tally(input) + expected = <<~TALLY Team | MP | W | D | L | P TALLY - assert_equal expected, Tournament.tally(input) + assert_equal expected, actual end def test_a_win_is_three_points_a_loss_is_zero_points @@ -21,13 +23,15 @@ def test_a_win_is_three_points_a_loss_is_zero_points Allegoric Alaskans;Blithering Badgers;win INPUT + actual = Tournament.tally(input) + expected = <<~TALLY Team | MP | W | D | L | P Allegoric Alaskans | 1 | 1 | 0 | 0 | 3 Blithering Badgers | 1 | 0 | 0 | 1 | 0 TALLY - assert_equal expected, Tournament.tally(input) + assert_equal expected, actual end def test_a_win_can_also_be_expressed_as_a_loss @@ -36,13 +40,15 @@ def test_a_win_can_also_be_expressed_as_a_loss Blithering Badgers;Allegoric Alaskans;loss INPUT + actual = Tournament.tally(input) + expected = <<~TALLY Team | MP | W | D | L | P Allegoric Alaskans | 1 | 1 | 0 | 0 | 3 Blithering Badgers | 1 | 0 | 0 | 1 | 0 TALLY - assert_equal expected, Tournament.tally(input) + assert_equal expected, actual end def test_a_different_team_can_win @@ -51,13 +57,15 @@ def test_a_different_team_can_win Blithering Badgers;Allegoric Alaskans;win INPUT + actual = Tournament.tally(input) + expected = <<~TALLY Team | MP | W | D | L | P Blithering Badgers | 1 | 1 | 0 | 0 | 3 Allegoric Alaskans | 1 | 0 | 0 | 1 | 0 TALLY - assert_equal expected, Tournament.tally(input) + assert_equal expected, actual end def test_a_draw_is_one_point_each @@ -66,13 +74,15 @@ def test_a_draw_is_one_point_each Allegoric Alaskans;Blithering Badgers;draw INPUT + actual = Tournament.tally(input) + expected = <<~TALLY Team | MP | W | D | L | P Allegoric Alaskans | 1 | 0 | 1 | 0 | 1 Blithering Badgers | 1 | 0 | 1 | 0 | 1 TALLY - assert_equal expected, Tournament.tally(input) + assert_equal expected, actual end def test_there_can_be_more_than_one_match @@ -82,13 +92,15 @@ def test_there_can_be_more_than_one_match Allegoric Alaskans;Blithering Badgers;win INPUT + actual = Tournament.tally(input) + expected = <<~TALLY Team | MP | W | D | L | P Allegoric Alaskans | 2 | 2 | 0 | 0 | 6 Blithering Badgers | 2 | 0 | 0 | 2 | 0 TALLY - assert_equal expected, Tournament.tally(input) + assert_equal expected, actual end def test_there_can_be_more_than_one_winner @@ -98,13 +110,15 @@ def test_there_can_be_more_than_one_winner Allegoric Alaskans;Blithering Badgers;win INPUT + actual = Tournament.tally(input) + expected = <<~TALLY Team | MP | W | D | L | P Allegoric Alaskans | 2 | 1 | 0 | 1 | 3 Blithering Badgers | 2 | 1 | 0 | 1 | 3 TALLY - assert_equal expected, Tournament.tally(input) + assert_equal expected, actual end def test_there_can_be_more_than_two_teams @@ -115,6 +129,8 @@ def test_there_can_be_more_than_two_teams Courageous Californians;Allegoric Alaskans;loss INPUT + actual = Tournament.tally(input) + expected = <<~TALLY Team | MP | W | D | L | P Allegoric Alaskans | 2 | 2 | 0 | 0 | 6 @@ -122,7 +138,7 @@ def test_there_can_be_more_than_two_teams Courageous Californians | 2 | 0 | 0 | 2 | 0 TALLY - assert_equal expected, Tournament.tally(input) + assert_equal expected, actual end def test_typical_input @@ -136,6 +152,8 @@ def test_typical_input Allegoric Alaskans;Courageous Californians;win INPUT + actual = Tournament.tally(input) + expected = <<~TALLY Team | MP | W | D | L | P Devastating Donkeys | 3 | 2 | 1 | 0 | 7 @@ -144,7 +162,7 @@ def test_typical_input Courageous Californians | 3 | 0 | 1 | 2 | 1 TALLY - assert_equal expected, Tournament.tally(input) + assert_equal expected, actual end def test_incomplete_competition_not_all_pairs_have_played @@ -156,6 +174,8 @@ def test_incomplete_competition_not_all_pairs_have_played Allegoric Alaskans;Courageous Californians;win INPUT + actual = Tournament.tally(input) + expected = <<~TALLY Team | MP | W | D | L | P Allegoric Alaskans | 3 | 2 | 0 | 1 | 6 @@ -164,7 +184,7 @@ def test_incomplete_competition_not_all_pairs_have_played Devastating Donkeys | 1 | 0 | 0 | 1 | 0 TALLY - assert_equal expected, Tournament.tally(input) + assert_equal expected, actual end def test_ties_broken_alphabetically @@ -178,6 +198,8 @@ def test_ties_broken_alphabetically Allegoric Alaskans;Courageous Californians;draw INPUT + actual = Tournament.tally(input) + expected = <<~TALLY Team | MP | W | D | L | P Allegoric Alaskans | 3 | 2 | 1 | 0 | 7 @@ -186,7 +208,7 @@ def test_ties_broken_alphabetically Devastating Donkeys | 3 | 0 | 1 | 2 | 1 TALLY - assert_equal expected, Tournament.tally(input) + assert_equal expected, actual end def test_ensure_points_sorted_numerically @@ -199,12 +221,14 @@ def test_ensure_points_sorted_numerically Blithering Badgers;Devastating Donkeys;win INPUT + actual = Tournament.tally(input) + expected = <<~TALLY Team | MP | W | D | L | P Devastating Donkeys | 5 | 4 | 0 | 1 | 12 Blithering Badgers | 5 | 1 | 0 | 4 | 3 TALLY - assert_equal expected, Tournament.tally(input) + assert_equal expected, actual end end