Skip to content

Commit

Permalink
keep integers integers!
Browse files Browse the repository at this point in the history
  • Loading branch information
hilary committed Apr 28, 2017
1 parent ec15c5b commit c6402c6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 75 deletions.
98 changes: 47 additions & 51 deletions exercises/etl/etl_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
38 changes: 16 additions & 22 deletions lib/etl_cases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions lib/generator/exercise_cases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c6402c6

Please sign in to comment.