ruby-changes:53455
From: k0kubun <ko1@a...>
Date: Mon, 12 Nov 2018 09:37:20 +0900 (JST)
Subject: [ruby-changes:53455] k0kubun:r65671 (trunk): erb.rb: warn invalid trim_mode [Misc #15294]
k0kubun 2018-11-12 09:37:15 +0900 (Mon, 12 Nov 2018) New Revision: 65671 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=65671 Log: erb.rb: warn invalid trim_mode [Misc #15294] From: Justin Collins <justin@p...> Modified files: trunk/lib/erb.rb trunk/test/erb/test_erb.rb Index: test/erb/test_erb.rb =================================================================== --- test/erb/test_erb.rb (revision 65670) +++ test/erb/test_erb.rb (revision 65671) @@ -235,6 +235,28 @@ EOS https://github.com/ruby/ruby/blob/trunk/test/erb/test_erb.rb#L235 assert_equal("line\r\n" * 3, erb.result) end + def test_invalid_trim_mode + assert_warning(/#{__FILE__}:#{__LINE__ + 1}/) do + @erb.new("", trim_mode: 'abc-def') + end + + assert_warning(/Invalid ERB trim mode/) do + @erb.new("", trim_mode: 'abc-def') + end + + assert_warning(/Invalid ERB trim mode/) do + @erb.new("", trim_mode: '%<') + end + + assert_warning(/Invalid ERB trim mode/) do + @erb.new("", trim_mode: '%<>-') + end + + assert_warning(/Invalid ERB trim mode/) do + @erb.new("", trim_mode: 3) + end + end + def test_run out = StringIO.new orig, $stdout = $stdout, out Index: lib/erb.rb =================================================================== --- lib/erb.rb (revision 65670) +++ lib/erb.rb (revision 65671) @@ -665,9 +665,13 @@ class ERB https://github.com/ruby/ruby/blob/trunk/lib/erb.rb#L665 return [false, '>'] when 2 return [false, '<>'] - when 0 + when 0, nil return [false, nil] when String + unless mode.match?(/\A(%|-|>|<>){1,2}\z/) + warn_invalid_trim_mode(mode, uplevel: 5) + end + perc = mode.include?('%') if mode.include?('-') return [perc, '-'] @@ -679,6 +683,7 @@ class ERB https://github.com/ruby/ruby/blob/trunk/lib/erb.rb#L683 [perc, nil] end else + warn_invalid_trim_mode(mode, uplevel: 5) return [false, nil] end end @@ -730,6 +735,10 @@ class ERB https://github.com/ruby/ruby/blob/trunk/lib/erb.rb#L735 end return enc, frozen end + + def warn_invalid_trim_mode(mode, uplevel:) + warn "Invalid ERB trim mode: #{mode.inspect} (trim_mode: nil, 0, 1, 2, or String composed of '%' and/or '-', '>', '<>')", uplevel: uplevel + 1 + end end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/