2009-06-21 12:05:43 -04:00
|
|
|
require 'abstract_unit'
|
|
|
|
|
|
|
|
class CaptureHelperTest < ActionView::TestCase
|
|
|
|
def setup
|
|
|
|
super
|
2010-03-15 19:22:22 -04:00
|
|
|
@av = ActionView::Base.new
|
2009-06-21 12:05:43 -04:00
|
|
|
@_content_for = Hash.new {|h,k| h[k] = "" }
|
|
|
|
end
|
|
|
|
|
2010-03-16 15:33:38 -04:00
|
|
|
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
|
|
|
|
|
2010-03-16 16:05:01 -04:00
|
|
|
def test_capture_captures_the_value_returned_by_the_block_if_the_temporary_buffer_is_blank
|
2010-03-16 15:33:38 -04:00
|
|
|
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
|
|
|
|
|
2010-10-11 14:39:12 -04:00
|
|
|
def test_capture_escapes_html
|
2010-11-02 18:02:55 -04:00
|
|
|
string = @av.capture { '<em>bar</em>' }
|
|
|
|
assert_equal '<em>bar</em>', string
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_capture_doesnt_escape_twice
|
|
|
|
string = @av.capture { '<em>bar</em>'.html_safe }
|
|
|
|
assert_equal '<em>bar</em>', string
|
2010-10-11 14:39:12 -04:00
|
|
|
end
|
|
|
|
|
2009-06-21 12:05:43 -04:00
|
|
|
def test_content_for
|
|
|
|
assert ! content_for?(:title)
|
|
|
|
content_for :title, 'title'
|
|
|
|
assert content_for?(:title)
|
|
|
|
assert ! content_for?(:something_else)
|
|
|
|
end
|
2010-03-15 13:29:21 -04:00
|
|
|
|
2010-03-15 19:22:22 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
unless RUBY_VERSION < '1.9'
|
|
|
|
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.
|
2010-03-16 16:05:01 -04:00
|
|
|
alt_encoding = alt_encoding(@av.output_buffer)
|
2010-03-15 19:22:22 -04:00
|
|
|
@av.output_buffer.force_encoding(alt_encoding)
|
|
|
|
|
|
|
|
@av.with_output_buffer do
|
2010-05-16 03:34:46 -04:00
|
|
|
assert_equal alt_encoding, @av.output_buffer.encoding
|
2010-03-15 19:22:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-15 13:29:21 -04:00
|
|
|
def test_with_output_buffer_does_not_assume_there_is_an_output_buffer
|
2010-03-15 19:22:22 -04:00
|
|
|
assert_nil @av.output_buffer
|
|
|
|
assert_equal "", @av.with_output_buffer {}
|
2010-03-15 13:29:21 -04:00
|
|
|
end
|
2010-03-16 16:05:01 -04:00
|
|
|
|
|
|
|
def test_flush_output_buffer_concats_output_buffer_to_response
|
|
|
|
view = view_with_controller
|
|
|
|
assert_equal [], view.response.body_parts
|
|
|
|
|
|
|
|
view.output_buffer << 'OMG'
|
|
|
|
view.flush_output_buffer
|
|
|
|
assert_equal ['OMG'], view.response.body_parts
|
|
|
|
assert_equal '', view.output_buffer
|
|
|
|
|
|
|
|
view.output_buffer << 'foobar'
|
|
|
|
view.flush_output_buffer
|
|
|
|
assert_equal ['OMG', 'foobar'], view.response.body_parts
|
|
|
|
assert_equal '', view.output_buffer
|
|
|
|
end
|
|
|
|
|
|
|
|
unless RUBY_VERSION < '1.9'
|
|
|
|
def test_flush_output_buffer_preserves_the_encoding_of_the_output_buffer
|
|
|
|
view = view_with_controller
|
|
|
|
alt_encoding = alt_encoding(view.output_buffer)
|
|
|
|
view.output_buffer.force_encoding(alt_encoding)
|
|
|
|
flush_output_buffer
|
|
|
|
assert_equal alt_encoding, view.output_buffer.encoding
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def alt_encoding(output_buffer)
|
|
|
|
output_buffer.encoding == Encoding::US_ASCII ? Encoding::UTF_8 : Encoding::US_ASCII
|
|
|
|
end
|
|
|
|
|
|
|
|
def view_with_controller
|
2010-07-25 04:21:16 -04:00
|
|
|
TestController.new.view_context.tap do |view|
|
2010-03-16 16:05:01 -04:00
|
|
|
view.output_buffer = ActionView::OutputBuffer.new
|
|
|
|
end
|
|
|
|
end
|
2009-06-21 12:05:43 -04:00
|
|
|
end
|