Project

General

Profile

Misc #15294 ยป erb_trim_mode_warning.patch

Add warnings for invalid ERB trim modes - jsc (Justin Collins), 11/09/2018 09:02 PM

View differences:

lib/erb.rb
668 668
      when 0
669 669
        return [false, nil]
670 670
      when String
671
        warn "Invalid ERB trim mode: #{mode.inspect}", uplevel: 5 unless mode.match?(/\A(%|-|>|<>){1,2}\z/)
672

  
671 673
        perc = mode.include?('%')
672 674
        if mode.include?('-')
673 675
          return [perc, '-']
......
679 681
          [perc, nil]
680 682
        end
681 683
      else
684
        warn "Invalid ERB trim mode: #{mode.inspect}", uplevel: 5 unless mode.nil?
682 685
        return [false, nil]
683 686
      end
684 687
    end
test/erb/test_erb.rb
235 235
    assert_equal("line\r\n" * 3, erb.result)
236 236
  end
237 237

  
238
  def test_invalid_trim_mode
239
    assert_warning(/Invalid ERB trim mode/) do
240
      @erb.new("", trim_mode: 'abc-def')
241
    end
242

  
243
    assert_warning(/Invalid ERB trim mode/) do
244
      @erb.new("", trim_mode: '%<')
245
    end
246

  
247
    assert_warning(/Invalid ERB trim mode/) do
248
      @erb.new("", trim_mode: '%<>-')
249
    end
250

  
251
    assert_warning(/Invalid ERB trim mode/) do
252
      @erb.new("", trim_mode: 3)
253
    end
254
  end
255

  
238 256
  def test_run
239 257
    out = StringIO.new
240 258
    orig, $stdout = $stdout, out