diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-12-07 14:39:52 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-12-07 14:39:52 +0000 |
commit | 7f9827e4b3e76f215fa9fd36f24fd8e1fdef9866 (patch) | |
tree | 1a8a83e8ca857731116ed37273b53d6812afd011 /test | |
parent | 586d91ce65404e06f26167ef98e6c5a949722920 (diff) | |
download | ruby-7f9827e4b3e76f215fa9fd36f24fd8e1fdef9866.tar.gz |
parse.y: indented hereoc
* parse.y: add heredoc <<~ syntax. [Feature #9098]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52916 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r-- | test/ripper/test_parser_events.rb | 13 | ||||
-rw-r--r-- | test/ripper/test_sexp.rb | 35 | ||||
-rw-r--r-- | test/ruby/test_syntax.rb | 88 |
3 files changed, 136 insertions, 0 deletions
diff --git a/test/ripper/test_parser_events.rb b/test/ripper/test_parser_events.rb index 55485b68d0..540d36e4d9 100644 --- a/test/ripper/test_parser_events.rb +++ b/test/ripper/test_parser_events.rb @@ -431,6 +431,19 @@ class TestRipper::ParserEvents < Test::Unit::TestCase assert_equal("heredoc1\nheredoc2\n", heredoc, bug1921) end + def test_heredoc_dedent + thru_heredoc_dedent = false + str = width = nil + tree = parse("<""<~EOS\n heredoc\nEOS\n", :on_heredoc_dedent) {|e, s, w| + thru_heredoc_dedent = true + str = s + width = w + } + assert_equal true, thru_heredoc_dedent + assert_match(/string_content\(\), heredoc\n/, tree) + assert_equal(1, width) + end + def test_massign thru_massign = false parse("a, b = 1, 2", :on_massign) {thru_massign = true} diff --git a/test/ripper/test_sexp.rb b/test/ripper/test_sexp.rb index 8fc17fdd4a..557ae9b423 100644 --- a/test/ripper/test_sexp.rb +++ b/test/ripper/test_sexp.rb @@ -38,6 +38,27 @@ class TestRipper::Sexp < Test::Unit::TestCase assert_equal "foo\n", search_sexp(:@tstring_content, sexp)[1] end + def test_squiggly_heredoc + sexp = Ripper.sexp("<<~eot\n asdf\neot") + assert_equal "asdf\n", search_sexp(:@tstring_content, sexp)[1] + end + + def test_squiggly_heredoc_with_interpolated_expression + sexp1 = Ripper.sexp(<<-eos) +<<-eot +a\#{1}z +eot + eos + + sexp2 = Ripper.sexp(<<-eos) +<<~eot + a\#{1}z +eot + eos + + assert_equal clear_pos(sexp1), clear_pos(sexp2) + end + def search_sexp(sym, sexp) return sexp if !sexp or sexp[0] == sym sexp.find do |e| @@ -46,4 +67,18 @@ class TestRipper::Sexp < Test::Unit::TestCase end end end + + def clear_pos(sexp) + return sexp if !sexp + sexp.each do |e| + if Array === e + if e.size == 3 and Array === (last = e.last) and + last.size == 2 and Integer === last[0] and Integer === last[1] + last.clear + else + clear_pos(e) + end + end + end + end end if ripper_test diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb index e2cd389a07..82af7817e7 100644 --- a/test/ruby/test_syntax.rb +++ b/test/ruby/test_syntax.rb @@ -475,6 +475,94 @@ e" assert_equal(expected, actual, "#{Bug7559}: ") end + def test_dedented_heredoc_without_indentation + assert_equal(" y\nz\n", <<~eos) + y +z + eos + end + + def test_dedented_heredoc_with_indentation + assert_equal(" a\nb\n", <<~eos) + a + b + eos + end + + def test_dedented_heredoc_with_blank_less_indented_line + # the blank line has two leading spaces + result = eval("<<~eos\n" \ + " a\n" \ + " \n" \ + " b\n" \ + " eos\n") + assert_equal("a\n\nb\n", result) + end + + def test_dedented_heredoc_with_blank_less_indented_line_escaped + result = eval("<<~eos\n" \ + " a\n" \ + "\\ \\ \n" \ + " b\n" \ + " eos\n") + assert_equal(" a\n \n b\n", result) + end + + def test_dedented_heredoc_with_blank_more_indented_line + # the blank line has six leading spaces + result = eval("<<~eos\n" \ + " a\n" \ + " \n" \ + " b\n" \ + " eos\n") + assert_equal("a\n \nb\n", result) + end + + def test_dedented_heredoc_with_blank_more_indented_line_escaped + result = eval("<<~eos\n" \ + " a\n" \ + "\\ \\ \\ \\ \\ \\ \n" \ + " b\n" \ + " eos\n") + assert_equal(" a\n \n b\n", result) + end + + def test_dedented_heredoc_with_empty_line +result = eval("<<~eos\n" \ + " This would contain specially formatted text.\n" \ + "\n" \ + " That might span many lines\n" \ + " eos\n") + assert_equal(<<-eos, result) +This would contain specially formatted text. + +That might span many lines + eos + end + + def test_dedented_heredoc_with_interpolated_expression + result = eval(" <<~eos\n" \ + " #{1}a\n" \ + " zy\n" \ + " eos\n") + assert_equal(<<-eos, result) + #{1}a +zy + eos + end + + def test_dedented_heredoc_with_interpolated_string + w = "" + result = eval("<<~eos\n" \ + " \#{w} a\n" \ + " zy\n" \ + " eos\n") + assert_equal(<<-eos, result) +#{w} a + zy + eos + end + def test_lineno_after_heredoc bug7559 = '[ruby-dev:46737]' expected, _, actual = __LINE__, <<eom, __LINE__ |