Skip to content

Commit

Permalink
Add to_string case helper and use in place of heredoc in transpose_ca…
Browse files Browse the repository at this point in the history
…se.rb
  • Loading branch information
jbergenson committed Aug 5, 2019
1 parent 541f766 commit 76fd723
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 179 deletions.
4 changes: 2 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,8 @@
"slug": "flatten-array",
"uuid": "2df8ed82-2a04-4112-a17b-7813bcdc0e84",
"core": false,
"unlocked_by": "hello-world",
"difficulty": 1,
"unlocked_by": "series",
"difficulty": 3,
"topics": [
"arrays",
"recursion"
Expand Down
12 changes: 3 additions & 9 deletions exercises/transpose/.meta/generator/transpose_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@ class TransposeCase < Generator::ExerciseCase

def workload
[
"input = #{indent_heredoc(input_lines, 'INPUT', 2, delimiter_mod)}",
"input = #{to_string(lines)}",
"",
"expected = #{indent_heredoc(expected, 'EXPECTED', 2, delimiter_mod)}",
"expected = #{to_string(expected)}",
"",
"assert_equal expected, Transpose.transpose(input)",
"assert_equal expected, Transpose.transpose(input)"
]
end

private

def delimiter_mod
".strip"
end

end
197 changes: 29 additions & 168 deletions exercises/transpose/transpose_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,238 +5,99 @@
class TransposeTest < Minitest::Test
def test_empty_string
# skip
input = <<~INPUT.strip
input = ""

INPUT

expected = <<~EXPECTED.strip
EXPECTED
expected = ""

assert_equal expected, Transpose.transpose(input)
end

def test_two_characters_in_a_row
skip
input = <<~INPUT.strip
A1
INPUT
input = "A1"

expected = <<~EXPECTED.strip
A
1
EXPECTED
expected = "A\n1"

assert_equal expected, Transpose.transpose(input)
end

def test_two_characters_in_a_column
skip
input = <<~INPUT.strip
A
1
INPUT
input = "A\n1"

expected = <<~EXPECTED.strip
A1
EXPECTED
expected = "A1"

assert_equal expected, Transpose.transpose(input)
end

def test_simple
skip
input = <<~INPUT.strip
ABC
123
INPUT
input = "ABC\n123"

expected = <<~EXPECTED.strip
A1
B2
C3
EXPECTED
expected = "A1\nB2\nC3"

assert_equal expected, Transpose.transpose(input)
end

def test_single_line
skip
input = <<~INPUT.strip
Single line.
INPUT

expected = <<~EXPECTED.strip
S
i
n
g
l
e
l
i
n
e
.
EXPECTED
input = "Single line."

expected = "S\ni\nn\ng\nl\ne\n \nl\ni\nn\ne\n."

assert_equal expected, Transpose.transpose(input)
end

def test_first_line_longer_than_second_line
skip
input = <<~INPUT.strip
The fourth line.
The fifth line.
INPUT

expected = <<~EXPECTED.strip
TT
hh
ee
ff
oi
uf
rt
th
h
l
li
in
ne
e.
.
EXPECTED
input = "The fourth line.\nThe fifth line."

expected = "TT\nhh\nee\n \nff\noi\nuf\nrt\nth\nh \n l\nli\nin\nne\ne.\n."

assert_equal expected, Transpose.transpose(input)
end

def test_second_line_longer_than_first_line
skip
input = <<~INPUT.strip
The first line.
The second line.
INPUT

expected = <<~EXPECTED.strip
TT
hh
ee
fs
ie
rc
so
tn
d
l
il
ni
en
.e
.
EXPECTED
input = "The first line.\nThe second line."

expected = "TT\nhh\nee\n \nfs\nie\nrc\nso\ntn\n d\nl \nil\nni\nen\n.e\n ."

assert_equal expected, Transpose.transpose(input)
end

def test_mixed_line_length
skip
input = <<~INPUT.strip
The longest line.
A long line.
A longer line.
A line.
INPUT

expected = <<~EXPECTED.strip
TAAA
h
elll
ooi
lnnn
ogge
n e.
glr
ei
snl
tei
.n
l e
i .
n
e
.
EXPECTED
input = "The longest line.\nA long line.\nA longer line.\nA line."

expected = "TAAA\nh \nelll\n ooi\nlnnn\nogge\nn e.\nglr\nei \nsnl\ntei\n .n\nl e\ni .\nn\ne\n."

assert_equal expected, Transpose.transpose(input)
end

def test_square
skip
input = <<~INPUT.strip
HEART
EMBER
ABUSE
RESIN
TREND
INPUT

expected = <<~EXPECTED.strip
HEART
EMBER
ABUSE
RESIN
TREND
EXPECTED
input = "HEART\nEMBER\nABUSE\nRESIN\nTREND"

expected = "HEART\nEMBER\nABUSE\nRESIN\nTREND"

assert_equal expected, Transpose.transpose(input)
end

def test_rectangle
skip
input = <<~INPUT.strip
FRACTURE
OUTLINED
BLOOMING
SEPTETTE
INPUT

expected = <<~EXPECTED.strip
FOBS
RULE
ATOP
CLOT
TIME
UNIT
RENT
EDGE
EXPECTED
input = "FRACTURE\nOUTLINED\nBLOOMING\nSEPTETTE"

expected = "FOBS\nRULE\nATOP\nCLOT\nTIME\nUNIT\nRENT\nEDGE"

assert_equal expected, Transpose.transpose(input)
end

def test_triangle
skip
input = <<~INPUT.strip
T
EE
AAA
SSSS
EEEEE
RRRRRR
INPUT

expected = <<~EXPECTED.strip
TEASER
EASER
ASER
SER
ER
R
EXPECTED
input = "T\nEE\nAAA\nSSSS\nEEEEE\nRRRRRR"

expected = "TEASER\n EASER\n ASER\n SER\n ER\n R"

assert_equal expected, Transpose.transpose(input)
end
Expand Down
9 changes: 9 additions & 0 deletions lib/generator/exercise_case/case_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ def indent_heredoc(lines, delimiter, depth = 0, delimiter_method = nil)
].join("\n")
end

# combine array of string elements into a single string
# as part of workload with optional separator
#
# example usage: to_string(["foo", "bar"])
# example output: "foo\nbar"
def to_string(phrases, separator="\n")
phrases.join(separator).inspect
end

def underscore(number)
fail ArgumentError, "#{number.inspect} is not an Integer" unless number.is_a? Integer
number.to_s.reverse.gsub(/...(?=.)/, '\&_').reverse
Expand Down
27 changes: 27 additions & 0 deletions test/generator/exercise_case/case_helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,37 @@ def workload
indent_heredoc(["foo", "bar"], 'TEXT', 1)
end
end

def test_heredoc
expected = "<<~TEXT\n foo\n bar\nTEXT"
assert_equal expected, HeredocCase.new.workload
end

class ToStringCase
include CaseHelpers

def workload(phrases)
to_string(phrases)
end
end

def test_to_string_with_two_strings
phrases = ["foo", "bar"]
expected = "foo\nbar".inspect
assert_equal expected, ToStringCase.new.workload(phrases)
end

def test_to_string_with_string_and_integer
phrases = ["foo", 123]
expected = "foo\n123".inspect
assert_equal expected, ToStringCase.new.workload(phrases)
end

def test_to_string_with_string_and_nil
phrases = ["foo", nil]
expected = "foo\n".inspect
assert_equal expected, ToStringCase.new.workload(phrases)
end
end
end
end

0 comments on commit 76fd723

Please sign in to comment.