Skip to content

Instantly share code, notes, and snippets.

@danielfone
Forked from avdi/unindent.rb
Last active August 29, 2015 14:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielfone/eacaf4a1f1d7f2ad425f to your computer and use it in GitHub Desktop.
Save danielfone/eacaf4a1f1d7f2ad425f to your computer and use it in GitHub Desktop.
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
@danielfone
Copy link
Author

Tests from activesupport and unindent

@Peeja
Copy link

Peeja commented Sep 17, 2014

Whoa…

@niclas
Copy link

niclas commented Sep 28, 2014

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment