tweak String#undent so that only leading whitespace up to the first indentation level is gsubbed, change test_undent to reflect change and add test_undent_nested to test new behavior

This commit is contained in:
William Woodruff 2015-08-26 16:50:36 -04:00 committed by Xu Cheng
parent 3b88c070c6
commit b0d5e17906
2 changed files with 15 additions and 2 deletions

View File

@ -1,6 +1,6 @@
class String class String
def undent def undent
gsub(/^.{#{(slice(/^ +/) || '').length}}/, "") gsub(/^[ \t]{#{(slice(/^[ \t]+/) || '').length}}/, "")
end end
# eg: # eg:

View File

@ -8,7 +8,7 @@ class StringTest < Homebrew::TestCase
....my friend over ....my friend over
there there
EOS EOS
assert_equal "hi\nmy friend over\nthere\n", undented assert_equal "hi\n....my friend over\nthere\n", undented
end end
def test_undent_not_indented def test_undent_not_indented
@ -18,4 +18,17 @@ I'm not indented
EOS EOS
assert_equal "hi\nI'm not indented\n", undented assert_equal "hi\nI'm not indented\n", undented
end end
def test_undent_nested
nest = <<-EOS.undent
goodbye
EOS
undented = <<-EOS.undent
hello
#{nest}
EOS
assert_equal "hello\ngoodbye\n\n", undented
end
end end