aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/template/erb/handlers_test.rb
blob: 7cd9c7fbcb7ba4a25f82f1b154b1994b9554ee87 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
require "abstract_unit"

class HandlersTest < ActiveSupport::TestCase
  HANDLER  = ActionView::Template::Handlers::ERB
  Template = Struct.new(:source)

  extend ActiveSupport::Testing::Declarative

  test "content is not trimmed without a trim mode" do
    with_erb_trim_mode nil do
      assert_equal("   \ntest", render(" <% 'IGNORED' %>  \ntest"))
    end
  end

  test "content around tags is trimmed if the trim mode includes a dash" do
    with_erb_trim_mode '-' do
      assert_equal("test", render(" <% 'IGNORED' %>  \ntest"))
    end
  end

  test "percent lines are normal content without a trim mode" do
    with_erb_trim_mode nil do
      assert_equal( "% if false\noops\n% end\n",
                    render("% if false\noops\n% end\n") )
    end
  end

  test "percent lines count as ruby if trim mode includes a percent" do
    with_erb_trim_mode "%" do
      assert_equal("", render("% if false\noops\n% end\n"))
    end
  end

  test "both trim modes can be used at the same time" do
    with_erb_trim_mode "%-" do
      assert_equal( "test", render( "% if false\noops\n% end\n" +
                                    " <% 'IGNORED' %>  \ntest" ) )
    end
  end

  private

  def with_erb_trim_mode(mode)
    @old_erb_trim_mode    = HANDLER.erb_trim_mode
    HANDLER.erb_trim_mode = mode
    yield
  ensure
    HANDLER.erb_trim_mode = @old_erb_trim_mode
  end

  def render(template)
    eval("output_buffer = nil; " + HANDLER.call(Template.new(template)))
  end
end