aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/template/erb/tag_helper_test.rb
blob: b5b7ae87de834dfb7b8ccc13431bdb7195f60d1d (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
require "abstract_unit"

module ERBTest
  class ViewContext
    mock_controller = Class.new do
      include SharedTestRoutes.url_helpers
    end

    include ActionView::Helpers::TagHelper
    include ActionView::Helpers::JavaScriptHelper
    include ActionView::Helpers::FormHelper

    attr_accessor :output_buffer

    def protect_against_forgery?() false end

    define_method(:controller) do
      mock_controller.new
    end
  end

  class DeprecatedViewContext < ViewContext
    # include ActionView::Helpers::DeprecatedBlockHelpers
  end

  module SharedTagHelpers
    extend ActiveSupport::Testing::Declarative

    def render_content(start, inside)
      template = block_helper(start, inside)
      ActionView::Template::Handlers::Erubis.new(template).evaluate(context.new)
    end

    def maybe_deprecated
      if @deprecated
        assert_deprecated { yield }
      else
        yield
      end
    end

    test "percent equals works for content_tag and does not require parenthesis on method call" do
      maybe_deprecated { assert_equal "<div>Hello world</div>", render_content("content_tag :div", "Hello world") }
    end

    test "percent equals works for javascript_tag" do
      expected_output = "<script type=\"text/javascript\">\n//<![CDATA[\nalert('Hello')\n//]]>\n</script>"
      maybe_deprecated { assert_equal expected_output, render_content("javascript_tag", "alert('Hello')") }
    end

    test "percent equals works for javascript_tag with options" do
      expected_output = "<script id=\"the_js_tag\" type=\"text/javascript\">\n//<![CDATA[\nalert('Hello')\n//]]>\n</script>"
      maybe_deprecated { assert_equal expected_output, render_content("javascript_tag(:id => 'the_js_tag')", "alert('Hello')") }
    end

    test "percent equals works with form tags" do
      expected_output = "<form action=\"foo\" method=\"post\">hello</form>"
      maybe_deprecated { assert_equal expected_output, render_content("form_tag('foo')", "<%= 'hello' %>") }
    end

    test "percent equals works with fieldset tags" do
      expected_output = "<fieldset><legend>foo</legend>hello</fieldset>"
      maybe_deprecated { assert_equal expected_output, render_content("field_set_tag('foo')", "<%= 'hello' %>") }
    end
  end

  class TagHelperTest < ActiveSupport::TestCase
    def context
      ViewContext
    end

    def block_helper(str, rest)
      "<%= #{str} do %>#{rest}<% end %>"
    end

    include SharedTagHelpers
  end

  class DeprecatedTagHelperTest < ActiveSupport::TestCase
    def context
      DeprecatedViewContext
    end

    def block_helper(str, rest)
      "<% __in_erb_template=true %><% #{str} do %>#{rest}<% end %>"
    end

    def setup
      @deprecated = true
    end

    include SharedTagHelpers
  end
end