From c6402c6be58f17746c688f327fc6858227d442ae Mon Sep 17 00:00:00 2001 From: Hilary Holz Date: Fri, 28 Apr 2017 14:14:39 -0700 Subject: [PATCH] keep integers integers! --- exercises/etl/etl_test.rb | 98 ++++++++++++++++----------------- lib/etl_cases.rb | 38 ++++++------- lib/generator/exercise_cases.rb | 4 +- 3 files changed, 65 insertions(+), 75 deletions(-) diff --git a/exercises/etl/etl_test.rb b/exercises/etl/etl_test.rb index 2aeba1d515..9b19cfc32e 100755 --- a/exercises/etl/etl_test.rb +++ b/exercises/etl/etl_test.rb @@ -8,87 +8,83 @@ class EtlTest < Minitest::Test def test_a_single_letter # skip old = { - '1' => ["A"], + 1 => ["A"] } expected = { - 'a' => '1', + 'a' => 1 } - assert_equal expected, ETL.transform(old) end def test_single_score_with_multiple_letters skip old = { - '1' => ["A", "E", "I", "O", "U"], + 1 => ["A", "E", "I", "O", "U"] } expected = { - 'a' => '1', - 'e' => '1', - 'i' => '1', - 'o' => '1', - 'u' => '1', + 'a' => 1, + 'e' => 1, + 'i' => 1, + 'o' => 1, + 'u' => 1 } - assert_equal expected, ETL.transform(old) end def test_multiple_scores_with_multiple_letters skip old = { - '1' => ["A", "E"], - '2' => ["D", "G"], + 1 => ["A", "E"], + 2 => ["D", "G"] } expected = { - 'a' => '1', - 'd' => '2', - 'e' => '1', - 'g' => '2', + 'a' => 1, + 'd' => 2, + 'e' => 1, + 'g' => 2 } - assert_equal expected, ETL.transform(old) end def test_multiple_scores_with_differing_numbers_of_letters skip old = { - '1' => ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"], - '2' => ["D", "G"], - '3' => ["B", "C", "M", "P"], - '4' => ["F", "H", "V", "W", "Y"], - '5' => ["K"], - '8' => ["J", "X"], - '10' => ["Q", "Z"], + 1 => ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"], + 2 => ["D", "G"], + 3 => ["B", "C", "M", "P"], + 4 => ["F", "H", "V", "W", "Y"], + 5 => ["K"], + 8 => ["J", "X"], + 10 => ["Q", "Z"] } expected = { - 'a' => '1', - 'b' => '3', - 'c' => '3', - 'd' => '2', - 'e' => '1', - 'f' => '4', - 'g' => '2', - 'h' => '4', - 'i' => '1', - 'j' => '8', - 'k' => '5', - 'l' => '1', - 'm' => '3', - 'n' => '1', - 'o' => '1', - 'p' => '3', - 'q' => '10', - 'r' => '1', - 's' => '1', - 't' => '1', - 'u' => '1', - 'v' => '4', - 'w' => '4', - 'x' => '8', - 'y' => '4', - 'z' => '10', + 'a' => 1, + 'b' => 3, + 'c' => 3, + 'd' => 2, + 'e' => 1, + 'f' => 4, + 'g' => 2, + 'h' => 4, + 'i' => 1, + 'j' => 8, + 'k' => 5, + 'l' => 1, + 'm' => 3, + 'n' => 1, + 'o' => 1, + 'p' => 3, + 'q' => 10, + 'r' => 1, + 's' => 1, + 't' => 1, + 'u' => 1, + 'v' => 4, + 'w' => 4, + 'x' => 8, + 'y' => 4, + 'z' => 10 } - assert_equal expected, ETL.transform(old) end diff --git a/lib/etl_cases.rb b/lib/etl_cases.rb index 48bde9b9c5..f348281dde 100644 --- a/lib/etl_cases.rb +++ b/lib/etl_cases.rb @@ -2,33 +2,27 @@ class EtlCase < ExerciseCase def workload - [ - "old = #{format_hash(integerize_keys(input))}", - " expected = #{format_hash(expected)}\n", - indent(4, assertion), - ].join("\n") + indent_lines([ + "old = {\n #{format(input)}\n }", + "expected = {\n #{format(expected)}\n }", + "assert_equal expected, ETL.transform(old)" + ], 4) end private - def indent(size, text) - text.lines.each_with_object('') { |line, obj| obj << ' ' * size + line } - end - - def assertion - "assert_equal expected, ETL.transform(old)" - end - - def integerize_keys(input) - input.reduce({}) { |hash, (k, v)| hash[k.to_i] = v; hash } - end - - def format_hash(hash) - middle = hash.each_pair.with_object('') do |(k, v), obj| - value = v.class == Array ? v : "'#{v}'" - obj << " '#{k}' => #{value},\n " + def format(obj) + case + when obj.respond_to?(:each_pair) + indent_lines( + obj.each_with_object([]) {|(k, v), string| string << "#{format(k)} => #{format(v)}" }, + 6, + ",\n" + ) + when obj.respond_to?(:each) then obj + when obj.to_s =~ /\d+/ then obj.to_i + else %Q('#{obj}') end - "{\n #{middle} }" end end diff --git a/lib/generator/exercise_cases.rb b/lib/generator/exercise_cases.rb index a4c6c99c0c..07c4e8a1f8 100644 --- a/lib/generator/exercise_cases.rb +++ b/lib/generator/exercise_cases.rb @@ -21,8 +21,8 @@ def skipped # "#{assert} Isogram.is_isogram?(string)" # ], 4 # ) - def indent_lines(code, depth) - code.join("\n" + ' ' * depth) + def indent_lines(code, depth, separator = "\n") + code.join(separator + ' ' * depth) end # used in workload, for example, as