aboutsummaryrefslogtreecommitdiffstats
path: root/actionview/test/template/capture_helper_test.rb
blob: 1e099d482c21cc61bc0a3349e83886f2e3c812bd (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
require 'abstract_unit'

class CaptureHelperTest < ActionView::TestCase
  def setup
    super
    @av = ActionView::Base.new
    @view_flow = ActionView::OutputFlow.new
  end

  def test_capture_captures_the_temporary_output_buffer_in_its_block
    assert_nil @av.output_buffer
    string = @av.capture do
      @av.output_buffer << 'foo'
      @av.output_buffer << 'bar'
    end
    assert_nil @av.output_buffer
    assert_equal 'foobar', string
  end

  def test_capture_captures_the_value_returned_by_the_block_if_the_temporary_buffer_is_blank
    string = @av.capture('foo', 'bar') do |a, b|
      a + b
    end
    assert_equal 'foobar', string
  end

  def test_capture_returns_nil_if_the_returned_value_is_not_a_string
    assert_nil @av.capture { 1 }
  end

  def test_capture_escapes_html
    string = @av.capture { '<em>bar</em>' }
    assert_equal '&lt;em&gt;bar&lt;/em&gt;', string
  end

  def test_capture_doesnt_escape_twice
    string = @av.capture { '&lt;em&gt;bar&lt;/em&gt;'.html_safe }
    assert_equal '&lt;em&gt;bar&lt;/em&gt;', string
  end

  def test_capture_used_for_read
    content_for :foo, "foo"
    assert_equal "foo", content_for(:foo)

    content_for(:bar){ "bar" }
    assert_equal "bar", content_for(:bar)
  end

  def test_content_for_with_multiple_calls
    assert ! content_for?(:title)
    content_for :title, 'foo'
    content_for :title, 'bar'
    assert_equal 'foobar', content_for(:title)
  end

  def test_content_for_with_multiple_calls_and_flush
    assert ! content_for?(:title)
    content_for :title, 'foo'
    content_for :title, 'bar', flush: true
    assert_equal 'bar', content_for(:title)
  end

  def test_content_for_with_block
    assert ! content_for?(:title)
    content_for :title do
      output_buffer << 'foo'
      output_buffer << 'bar'
      nil
    end
    assert_equal 'foobar', content_for(:title)
  end

  def test_content_for_with_block_and_multiple_calls_with_flush
    assert ! content_for?(:title)
    content_for :title do
      'foo'
    end
    content_for :title, flush: true do
      'bar'
    end
    assert_equal 'bar', content_for(:title)
  end

  def test_content_for_with_block_and_multiple_calls_with_flush_nil_content
    assert ! content_for?(:title)
    content_for :title do
      'foo'
    end
    content_for :title, nil, flush: true do
      'bar'
    end
    assert_equal 'bar', content_for(:title)
  end

  def test_content_for_with_block_and_multiple_calls_without_flush
    assert ! content_for?(:title)
    content_for :title do
      'foo'
    end
    content_for :title, flush: false do
      'bar'
    end
    assert_equal 'foobar', content_for(:title)
  end

  def test_content_for_with_whitespace_block
    assert ! content_for?(:title)
    content_for :title, 'foo'
    content_for :title do
      output_buffer << "  \n  "
      nil
    end
    content_for :title, 'bar'
    assert_equal 'foobar', content_for(:title)
  end

  def test_content_for_with_whitespace_block_and_flush
    assert ! content_for?(:title)
    content_for :title, 'foo'
    content_for :title, flush: true do
      output_buffer << "  \n  "
      nil
    end
    content_for :title, 'bar', flush: true
    assert_equal 'bar', content_for(:title)
  end

  def test_content_for_returns_nil_when_writing
    assert ! content_for?(:title)
    assert_equal nil, content_for(:title, 'foo')
    assert_equal nil, content_for(:title) { output_buffer << 'bar'; nil }
    assert_equal nil, content_for(:title) { output_buffer << "  \n  "; nil }
    assert_equal 'foobar', content_for(:title)
    assert_equal nil, content_for(:title, 'foo', flush: true)
    assert_equal nil, content_for(:title, flush: true) { output_buffer << 'bar'; nil }
    assert_equal nil, content_for(:title, flush: true) { output_buffer << "  \n  "; nil }
    assert_equal 'bar', content_for(:title)
  end

  def test_content_for_returns_nil_when_content_missing
    assert_equal nil, content_for(:some_missing_key)
  end

  def test_content_for_question_mark
    assert ! content_for?(:title)
    content_for :title, 'title'
    assert content_for?(:title)
    assert ! content_for?(:something_else)
  end

  def test_provide
    assert !content_for?(:title)
    provide :title, "hi"
    assert content_for?(:title)
    assert_equal "hi", content_for(:title)
    provide :title, "<p>title</p>"
    assert_equal "hi&lt;p&gt;title&lt;/p&gt;", content_for(:title)

    @view_flow = ActionView::OutputFlow.new
    provide :title, "hi"
    provide :title, "<p>title</p>".html_safe
    assert_equal "hi<p>title</p>", content_for(:title)
  end

  def test_with_output_buffer_swaps_the_output_buffer_given_no_argument
    assert_nil @av.output_buffer
    buffer = @av.with_output_buffer do
      @av.output_buffer << '.'
    end
    assert_equal '.', buffer
    assert_nil @av.output_buffer
  end

  def test_with_output_buffer_swaps_the_output_buffer_with_an_argument
    assert_nil @av.output_buffer
    buffer = ActionView::OutputBuffer.new('.')
    @av.with_output_buffer(buffer) do
      @av.output_buffer << '.'
    end
    assert_equal '..', buffer
    assert_nil @av.output_buffer
  end

  def test_with_output_buffer_restores_the_output_buffer
    buffer = ActionView::OutputBuffer.new
    @av.output_buffer = buffer
    @av.with_output_buffer do
      @av.output_buffer << '.'
    end
    assert buffer.equal?(@av.output_buffer)
  end

  def test_with_output_buffer_sets_proper_encoding
    @av.output_buffer = ActionView::OutputBuffer.new

    # Ensure we set the output buffer to an encoding different than the default one.
    alt_encoding = alt_encoding(@av.output_buffer)
    @av.output_buffer.force_encoding(alt_encoding)

    @av.with_output_buffer do
      assert_equal alt_encoding, @av.output_buffer.encoding
    end
  end

  def test_with_output_buffer_does_not_assume_there_is_an_output_buffer
    assert_nil @av.output_buffer
    assert_equal "", @av.with_output_buffer {}
  end

  def alt_encoding(output_buffer)
    output_buffer.encoding == Encoding::US_ASCII ? Encoding::UTF_8 : Encoding::US_ASCII
  end
end