2017-07-23 11:36:41 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:50:17 -04:00
|
|
|
require "abstract_unit"
|
2009-06-21 12:05:43 -04:00
|
|
|
|
|
|
|
class CaptureHelperTest < ActionView::TestCase
|
|
|
|
def setup
|
|
|
|
super
|
2019-01-29 18:17:52 -05:00
|
|
|
@av = ActionView::Base.empty
|
2011-05-01 06:16:31 -04:00
|
|
|
@view_flow = ActionView::OutputFlow.new
|
2009-06-21 12:05:43 -04:00
|
|
|
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
|
2016-08-06 12:50:17 -04:00
|
|
|
@av.output_buffer << "foo"
|
|
|
|
@av.output_buffer << "bar"
|
2010-03-16 15:33:38 -04:00
|
|
|
end
|
|
|
|
assert_nil @av.output_buffer
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "foobar", string
|
2010-03-16 15:33:38 -04:00
|
|
|
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
|
2016-08-06 12:50:17 -04:00
|
|
|
string = @av.capture("foo", "bar") do |a, b|
|
2010-03-16 15:33:38 -04:00
|
|
|
a + b
|
|
|
|
end
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "foobar", string
|
2010-03-16 15:33:38 -04:00
|
|
|
end
|
|
|
|
|
2014-12-15 18:20:54 -05:00
|
|
|
def test_capture_returns_nil_if_the_returned_value_is_not_a_string
|
|
|
|
assert_nil @av.capture { 1 }
|
2010-03-16 15:33:38 -04:00
|
|
|
end
|
|
|
|
|
2010-10-11 14:39:12 -04:00
|
|
|
def test_capture_escapes_html
|
2016-08-06 12:50:17 -04:00
|
|
|
string = @av.capture { "<em>bar</em>" }
|
|
|
|
assert_equal "<em>bar</em>", string
|
2010-11-02 18:02:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_capture_doesnt_escape_twice
|
2016-08-06 12:50:17 -04:00
|
|
|
string = @av.capture { raw("<em>bar</em>") }
|
|
|
|
assert_equal "<em>bar</em>", string
|
2010-10-11 14:39:12 -04:00
|
|
|
end
|
|
|
|
|
2018-09-25 02:15:35 -04:00
|
|
|
def test_content_for_used_for_read
|
2011-05-10 04:22:09 -04:00
|
|
|
content_for :foo, "foo"
|
|
|
|
assert_equal "foo", content_for(:foo)
|
|
|
|
|
2016-08-16 03:30:11 -04:00
|
|
|
content_for(:bar) { "bar" }
|
2011-05-10 04:22:09 -04:00
|
|
|
assert_equal "bar", content_for(:bar)
|
|
|
|
end
|
|
|
|
|
2011-09-05 09:10:12 -04:00
|
|
|
def test_content_for_with_multiple_calls
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not content_for?(:title)
|
2016-08-06 12:50:17 -04:00
|
|
|
content_for :title, "foo"
|
2021-05-07 11:27:48 -04:00
|
|
|
content_for :title, :bar
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "foobar", content_for(:title)
|
2011-09-05 09:10:12 -04:00
|
|
|
end
|
|
|
|
|
2011-12-29 15:58:55 -05:00
|
|
|
def test_content_for_with_multiple_calls_and_flush
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not content_for?(:title)
|
2016-08-06 12:50:17 -04:00
|
|
|
content_for :title, "foo"
|
2021-05-07 11:27:48 -04:00
|
|
|
content_for :title, :bar, flush: true
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "bar", content_for(:title)
|
2011-12-29 15:58:55 -05:00
|
|
|
end
|
|
|
|
|
2011-09-05 09:10:12 -04:00
|
|
|
def test_content_for_with_block
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not content_for?(:title)
|
2011-09-05 09:10:12 -04:00
|
|
|
content_for :title do
|
2016-08-06 12:50:17 -04:00
|
|
|
output_buffer << "foo"
|
|
|
|
output_buffer << "bar"
|
2011-09-05 09:10:12 -04:00
|
|
|
nil
|
|
|
|
end
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "foobar", content_for(:title)
|
2011-09-05 09:10:12 -04:00
|
|
|
end
|
|
|
|
|
2011-12-29 15:58:55 -05:00
|
|
|
def test_content_for_with_block_and_multiple_calls_with_flush
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not content_for?(:title)
|
2011-12-29 15:58:55 -05:00
|
|
|
content_for :title do
|
2016-08-06 12:50:17 -04:00
|
|
|
"foo"
|
2011-12-29 15:58:55 -05:00
|
|
|
end
|
2012-07-24 15:19:14 -04:00
|
|
|
content_for :title, flush: true do
|
2016-08-06 12:50:17 -04:00
|
|
|
"bar"
|
2011-12-29 15:58:55 -05:00
|
|
|
end
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "bar", content_for(:title)
|
2011-12-29 15:58:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_content_for_with_block_and_multiple_calls_with_flush_nil_content
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not content_for?(:title)
|
2011-12-29 15:58:55 -05:00
|
|
|
content_for :title do
|
2016-08-06 12:50:17 -04:00
|
|
|
"foo"
|
2011-12-29 15:58:55 -05:00
|
|
|
end
|
2012-07-24 15:19:14 -04:00
|
|
|
content_for :title, nil, flush: true do
|
2016-08-06 12:50:17 -04:00
|
|
|
"bar"
|
2011-12-29 15:58:55 -05:00
|
|
|
end
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "bar", content_for(:title)
|
2011-12-29 15:58:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_content_for_with_block_and_multiple_calls_without_flush
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not content_for?(:title)
|
2011-12-29 15:58:55 -05:00
|
|
|
content_for :title do
|
2016-08-06 12:50:17 -04:00
|
|
|
"foo"
|
2011-12-29 15:58:55 -05:00
|
|
|
end
|
2012-07-24 15:19:14 -04:00
|
|
|
content_for :title, flush: false do
|
2016-08-06 12:50:17 -04:00
|
|
|
"bar"
|
2011-12-29 15:58:55 -05:00
|
|
|
end
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "foobar", content_for(:title)
|
2011-12-29 15:58:55 -05:00
|
|
|
end
|
|
|
|
|
2011-09-05 09:10:12 -04:00
|
|
|
def test_content_for_with_whitespace_block
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not content_for?(:title)
|
2016-08-06 12:50:17 -04:00
|
|
|
content_for :title, "foo"
|
2011-09-05 09:10:12 -04:00
|
|
|
content_for :title do
|
|
|
|
output_buffer << " \n "
|
|
|
|
nil
|
|
|
|
end
|
2016-08-06 12:50:17 -04:00
|
|
|
content_for :title, "bar"
|
|
|
|
assert_equal "foobar", content_for(:title)
|
2011-09-05 09:10:12 -04:00
|
|
|
end
|
|
|
|
|
2011-12-29 15:58:55 -05:00
|
|
|
def test_content_for_with_whitespace_block_and_flush
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not content_for?(:title)
|
2016-08-06 12:50:17 -04:00
|
|
|
content_for :title, "foo"
|
2012-07-24 15:19:14 -04:00
|
|
|
content_for :title, flush: true do
|
2011-12-29 15:58:55 -05:00
|
|
|
output_buffer << " \n "
|
|
|
|
nil
|
|
|
|
end
|
2016-08-06 12:50:17 -04:00
|
|
|
content_for :title, "bar", flush: true
|
|
|
|
assert_equal "bar", content_for(:title)
|
2011-12-29 15:58:55 -05:00
|
|
|
end
|
|
|
|
|
2011-09-05 09:10:12 -04:00
|
|
|
def test_content_for_returns_nil_when_writing
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not content_for?(:title)
|
2016-12-24 12:29:52 -05:00
|
|
|
assert_nil content_for(:title, "foo")
|
|
|
|
assert_nil content_for(:title) { output_buffer << "bar"; nil }
|
|
|
|
assert_nil content_for(:title) { output_buffer << " \n "; nil }
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "foobar", content_for(:title)
|
2016-12-24 12:29:52 -05:00
|
|
|
assert_nil content_for(:title, "foo", flush: true)
|
|
|
|
assert_nil content_for(:title, flush: true) { output_buffer << "bar"; nil }
|
|
|
|
assert_nil content_for(:title, flush: true) { output_buffer << " \n "; nil }
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "bar", content_for(:title)
|
2011-09-05 09:10:12 -04:00
|
|
|
end
|
|
|
|
|
2013-02-21 17:22:27 -05:00
|
|
|
def test_content_for_returns_nil_when_content_missing
|
2016-12-24 12:29:52 -05:00
|
|
|
assert_nil content_for(:some_missing_key)
|
2013-02-21 17:22:27 -05:00
|
|
|
end
|
|
|
|
|
2011-05-10 04:22:09 -04:00
|
|
|
def test_content_for_question_mark
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not content_for?(:title)
|
2016-08-06 12:50:17 -04:00
|
|
|
content_for :title, "title"
|
2009-06-21 12:05:43 -04:00
|
|
|
assert content_for?(:title)
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not content_for?(:something_else)
|
2009-06-21 12:05:43 -04:00
|
|
|
end
|
2010-03-15 13:29:21 -04:00
|
|
|
|
2015-05-06 09:31:49 -04:00
|
|
|
def test_content_for_should_be_html_safe_after_flush_empty
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not content_for?(:title)
|
2015-05-06 09:31:49 -04:00
|
|
|
content_for :title do
|
2016-08-06 12:50:17 -04:00
|
|
|
content_tag(:p, "title")
|
2015-05-06 09:31:49 -04:00
|
|
|
end
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate content_for(:title), :html_safe?
|
2015-05-06 09:31:49 -04:00
|
|
|
content_for :title, "", flush: true
|
|
|
|
content_for(:title) do
|
2016-08-06 12:50:17 -04:00
|
|
|
content_tag(:p, "title")
|
2015-05-06 09:31:49 -04:00
|
|
|
end
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate content_for(:title), :html_safe?
|
2015-05-06 09:31:49 -04:00
|
|
|
end
|
|
|
|
|
2011-04-16 04:50:33 -04:00
|
|
|
def test_provide
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not content_for?(:title)
|
2011-04-16 05:34:07 -04:00
|
|
|
provide :title, "hi"
|
2011-04-16 04:50:33 -04:00
|
|
|
assert content_for?(:title)
|
2011-05-10 04:22:09 -04:00
|
|
|
assert_equal "hi", content_for(:title)
|
2011-04-16 04:50:33 -04:00
|
|
|
provide :title, "<p>title</p>"
|
2011-05-10 04:22:09 -04:00
|
|
|
assert_equal "hi<p>title</p>", content_for(:title)
|
2011-04-16 05:34:07 -04:00
|
|
|
|
2011-05-01 06:16:31 -04:00
|
|
|
@view_flow = ActionView::OutputFlow.new
|
2021-05-07 11:27:48 -04:00
|
|
|
provide :title, :hi
|
2016-01-20 01:55:06 -05:00
|
|
|
provide :title, raw("<p>title</p>")
|
2011-05-10 04:22:09 -04:00
|
|
|
assert_equal "hi<p>title</p>", content_for(:title)
|
2011-04-16 04:50:33 -04:00
|
|
|
end
|
|
|
|
|
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
|
2016-08-06 12:50:17 -04:00
|
|
|
@av.output_buffer << "."
|
2010-03-15 19:22:22 -04:00
|
|
|
end
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal ".", buffer
|
2010-03-15 19:22:22 -04:00
|
|
|
assert_nil @av.output_buffer
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_with_output_buffer_swaps_the_output_buffer_with_an_argument
|
|
|
|
assert_nil @av.output_buffer
|
2016-08-06 12:50:17 -04:00
|
|
|
buffer = ActionView::OutputBuffer.new(".")
|
2010-03-15 19:22:22 -04:00
|
|
|
@av.with_output_buffer(buffer) do
|
2016-08-06 12:50:17 -04:00
|
|
|
@av.output_buffer << "."
|
2010-03-15 19:22:22 -04:00
|
|
|
end
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "..", buffer
|
2010-03-15 19:22:22 -04:00
|
|
|
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
|
2016-08-06 12:50:17 -04:00
|
|
|
@av.output_buffer << "."
|
2010-03-15 19:22:22 -04:00
|
|
|
end
|
|
|
|
assert buffer.equal?(@av.output_buffer)
|
|
|
|
end
|
|
|
|
|
2011-12-21 03:04:54 -05:00
|
|
|
def test_with_output_buffer_sets_proper_encoding
|
|
|
|
@av.output_buffer = ActionView::OutputBuffer.new
|
2010-03-15 19:22:22 -04:00
|
|
|
|
2011-12-21 03:04:54 -05:00
|
|
|
# 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)
|
2010-03-15 19:22:22 -04:00
|
|
|
|
2011-12-21 03:04:54 -05:00
|
|
|
@av.with_output_buffer do
|
|
|
|
assert_equal alt_encoding, @av.output_buffer.encoding
|
2010-03-15 19:22:22 -04:00
|
|
|
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
|
2018-09-25 13:18:20 -04:00
|
|
|
assert_equal "", @av.with_output_buffer { }
|
2010-03-15 13:29:21 -04:00
|
|
|
end
|
2010-03-16 16:05:01 -04:00
|
|
|
|
|
|
|
def alt_encoding(output_buffer)
|
|
|
|
output_buffer.encoding == Encoding::US_ASCII ? Encoding::UTF_8 : Encoding::US_ASCII
|
|
|
|
end
|
2009-06-21 12:05:43 -04:00
|
|
|
end
|