It's a best-practice question. There are obvious ways to do that, none of them just seem quite right.
Quite often I need to test that some multi-line string gets produced. This normally breaks indentation making everything look like a mess:
class TestHelloWorld < Test::Unit::TestCase
def test_hello
assert_equal <<EOS, hello_world
Hello, world!
World greets you
EOS
end
end
With <<-
I can indent here doc marker, but it doesn't strip indentation inside heredoc, it still looks horrible.
class TestHelloWorld < Test::Unit::TestCase
def test_hello
assert_equal <<-EOS, hello_world
Hello, world!
World greets you
EOS
end
end
This lets me indent but readability of test line suffers. This gsub
really doesn't feel right here.
class TestHelloWorld < Test::Unit::TestCase
def test_hello
assert_equal <<-EOS.gsub(/^ {6}/, ""), hello_world
Hello, world!
World greets you
EOS
end
end
Is there any way to test such multi-line strings that's really readable?
gsub
out of sight by patchingString
, leaving the here string more readable. – Electrothermics