-
-
Save danielfone/eacaf4a1f1d7f2ad425f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TEXT = <<EOF | |
See, the interesting thing about this text | |
is that while it seems like the first line defines an indent | |
it's actually the last line which has the smallest indent | |
there are also some blank lines | |
both with and without extra spaces in them | |
and it just goes on and on | |
this text | |
and starts to repeat itself | |
See, the interesting thing about this text | |
is that while it seems like the first line defines an indent | |
it's actually the last line which has the smallest indent | |
there are also some blank lines | |
both with and without extra spaces in them | |
and it just goes on and on | |
this text | |
and starts to repeat itself | |
See, the interesting thing about this text | |
is that while it seems like the first line defines an indent | |
it's actually the last line which has the smallest indent | |
there are also some blank lines | |
both with and without extra spaces in them | |
and it just goes on and on | |
this text | |
and starts to repeat itself | |
The End. | |
EOF | |
EXPECTED_TEXT = <<EOF | |
See, the interesting thing about this text | |
is that while it seems like the first line defines an indent | |
it's actually the last line which has the smallest indent | |
there are also some blank lines | |
both with and without extra spaces in them | |
and it just goes on and on | |
this text | |
and starts to repeat itself | |
See, the interesting thing about this text | |
is that while it seems like the first line defines an indent | |
it's actually the last line which has the smallest indent | |
there are also some blank lines | |
both with and without extra spaces in them | |
and it just goes on and on | |
this text | |
and starts to repeat itself | |
See, the interesting thing about this text | |
is that while it seems like the first line defines an indent | |
it's actually the last line which has the smallest indent | |
there are also some blank lines | |
both with and without extra spaces in them | |
and it just goes on and on | |
this text | |
and starts to repeat itself | |
The End. | |
EOF | |
require "minitest/autorun" | |
require "minitest" | |
require 'minitest/pride' | |
class String | |
def strip_heredoc | |
gsub /^#{scan(/^[ \t]*\b/).min}/,'' | |
end | |
end | |
class TestUnindent < MiniTest::Test | |
def test_strip_heredoc | |
assert_equal EXPECTED_TEXT, TEXT.strip_heredoc | |
assert_equal "abc", "\s\sabc".strip_heredoc # removes space indentation | |
assert_equal "abc", "\tabc".strip_heredoc # removes tab indentation | |
assert_equal "abc", "\t\s\sabc".strip_heredoc # removes space/tab indentation | |
assert_equal "" , "".strip_heredoc # handles empty strings | |
assert_equal "abc\nabc" , "\tabc\n\tabc" .strip_heredoc # removes space/tab indentation | |
assert_equal "abc\n\tabc" , "\tabc\n\t\tabc" .strip_heredoc # keeps relative indentation | |
assert_equal "\nabc\n\n\tabc\n", "\n\tabc\n\n\t\tabc\n".strip_heredoc # ignores blank lines for indent calculation | |
end | |
def test_strip_heredoc_on_an_empty_string | |
assert_equal '', ''.strip_heredoc | |
end | |
def test_strip_heredoc_on_a_string_with_no_lines | |
assert_equal 'x', 'x'.strip_heredoc | |
assert_equal 'x', ' x'.strip_heredoc | |
end | |
def test_strip_heredoc_on_a_heredoc_with_no_margin | |
assert_equal "foo\nbar", "foo\nbar".strip_heredoc | |
assert_equal "foo\n bar", "foo\n bar".strip_heredoc | |
end | |
def test_strip_heredoc_on_a_regular_indented_heredoc | |
assert_equal "foo\n bar\nbaz\n", <<-EOS.strip_heredoc | |
foo | |
bar | |
baz | |
EOS | |
end | |
def test_strip_heredoc_on_a_regular_indented_heredoc_with_blank_lines | |
assert_equal "foo\n bar\n\nbaz\n", <<-EOS.strip_heredoc | |
foo | |
bar | |
baz | |
EOS | |
end | |
end |
Whoa…
👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tests from activesupport and unindent