2017-07-23 11:36:41 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:50:17 -04:00
|
|
|
require "abstract_unit"
|
2011-04-15 19:09:05 -04:00
|
|
|
|
|
|
|
class TestController < ActionController::Base
|
|
|
|
end
|
|
|
|
|
2017-11-25 13:37:33 -05:00
|
|
|
class SetupFiberedBase < ActiveSupport::TestCase
|
2011-04-15 19:09:05 -04:00
|
|
|
def setup
|
2019-02-08 17:43:22 -05:00
|
|
|
ActionView::LookupContext::DetailsKey.clear
|
|
|
|
|
2011-04-15 19:09:05 -04:00
|
|
|
view_paths = ActionController::Base.view_paths
|
2019-01-23 17:19:50 -05:00
|
|
|
|
2016-08-06 13:36:34 -04:00
|
|
|
@assigns = { secret: "in the sauce", name: nil }
|
2019-01-23 17:19:50 -05:00
|
|
|
@view = ActionView::Base.with_empty_template_cache.with_view_paths(view_paths, @assigns)
|
2011-04-15 19:09:05 -04:00
|
|
|
@controller_view = TestController.new.view_context
|
|
|
|
end
|
|
|
|
|
2011-05-01 07:39:15 -04:00
|
|
|
def render_body(options)
|
|
|
|
@view.view_renderer.render_body(@view, options)
|
|
|
|
end
|
|
|
|
|
2011-04-16 04:31:55 -04:00
|
|
|
def buffered_render(options)
|
2011-05-01 07:39:15 -04:00
|
|
|
body = render_body(options)
|
2018-05-17 04:32:27 -04:00
|
|
|
string = +""
|
2011-04-16 04:31:55 -04:00
|
|
|
body.each do |piece|
|
|
|
|
string << piece
|
|
|
|
end
|
|
|
|
string
|
|
|
|
end
|
2017-11-25 13:37:33 -05:00
|
|
|
end
|
2011-04-16 04:31:55 -04:00
|
|
|
|
2017-11-25 13:37:33 -05:00
|
|
|
class FiberedTest < SetupFiberedBase
|
2011-04-16 04:50:33 -04:00
|
|
|
def test_streaming_works
|
|
|
|
content = []
|
2016-08-06 13:36:34 -04:00
|
|
|
body = render_body(template: "test/hello_world", layout: "layouts/yield")
|
2011-04-16 04:50:33 -04:00
|
|
|
|
|
|
|
body.each do |piece|
|
|
|
|
content << piece
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "<title>", content[0]
|
|
|
|
assert_equal "", content[1]
|
|
|
|
assert_equal "</title>\n", content[2]
|
|
|
|
assert_equal "Hello world!", content[3]
|
|
|
|
assert_equal "\n", content[4]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_file
|
2020-05-05 15:58:55 -04:00
|
|
|
assert_equal "Hello world!", buffered_render(file: File.absolute_path("../fixtures/test/hello_world.erb", __dir__))
|
2011-04-16 04:50:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_partial
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_equal "only partial", buffered_render(partial: "test/partial_only")
|
2011-04-16 04:50:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_inline
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_equal "Hello, World!", buffered_render(inline: "Hello, World!")
|
2011-04-16 04:50:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_without_layout
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_equal "Hello world!", buffered_render(template: "test/hello_world")
|
2011-04-15 19:09:05 -04:00
|
|
|
end
|
|
|
|
|
2011-04-16 04:50:33 -04:00
|
|
|
def test_render_with_layout
|
2011-04-15 19:09:05 -04:00
|
|
|
assert_equal %(<title></title>\nHello world!\n),
|
2016-08-06 13:36:34 -04:00
|
|
|
buffered_render(template: "test/hello_world", layout: "layouts/yield")
|
2011-04-15 19:09:05 -04:00
|
|
|
end
|
2011-04-16 04:50:33 -04:00
|
|
|
|
|
|
|
def test_render_with_layout_which_has_render_inline
|
|
|
|
assert_equal %(welcome\nHello world!\n),
|
2016-08-06 13:36:34 -04:00
|
|
|
buffered_render(template: "test/hello_world", layout: "layouts/yield_with_render_inline_inside")
|
2011-04-16 04:50:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_with_layout_which_renders_another_partial
|
|
|
|
assert_equal %(partial html\nHello world!\n),
|
2016-08-06 13:36:34 -04:00
|
|
|
buffered_render(template: "test/hello_world", layout: "layouts/yield_with_render_partial_inside")
|
2011-04-16 04:50:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_with_nested_layout
|
|
|
|
assert_equal %(<title>title</title>\n\n<div id="column">column</div>\n<div id="content">content</div>\n),
|
2016-08-06 13:36:34 -04:00
|
|
|
buffered_render(template: "test/nested_layout", layout: "layouts/yield")
|
2011-04-16 04:50:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_with_file_in_layout
|
|
|
|
assert_equal %(\n<title>title</title>\n\n),
|
2016-08-06 13:36:34 -04:00
|
|
|
buffered_render(template: "test/layout_render_file")
|
2011-04-16 04:50:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_with_handler_without_streaming_support
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_match "<p>This is grand!</p>", buffered_render(template: "test/hello")
|
2011-04-16 04:50:33 -04:00
|
|
|
end
|
2011-04-16 05:22:36 -04:00
|
|
|
|
|
|
|
def test_render_with_streaming_multiple_yields_provide_and_content_for
|
2011-04-16 05:34:07 -04:00
|
|
|
assert_equal "Yes, \nthis works\n like a charm.",
|
2016-08-06 13:36:34 -04:00
|
|
|
buffered_render(template: "test/streaming", layout: "layouts/streaming")
|
2011-04-16 05:22:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_with_streaming_with_fake_yields_and_streaming_buster
|
2011-04-16 05:34:07 -04:00
|
|
|
assert_equal "This won't look\n good.",
|
2016-08-06 13:36:34 -04:00
|
|
|
buffered_render(template: "test/streaming_buster", layout: "layouts/streaming")
|
2011-04-16 05:22:36 -04:00
|
|
|
end
|
2011-04-16 05:34:07 -04:00
|
|
|
|
|
|
|
def test_render_with_nested_streaming_multiple_yields_provide_and_content_for
|
|
|
|
assert_equal "?Yes, \n\nthis works\n\n? like a charm.",
|
2016-08-06 13:36:34 -04:00
|
|
|
buffered_render(template: "test/nested_streaming", layout: "layouts/streaming")
|
2011-04-16 05:34:07 -04:00
|
|
|
end
|
|
|
|
|
2015-03-01 02:20:27 -05:00
|
|
|
def test_render_with_streaming_and_capture
|
|
|
|
assert_equal "Yes, \n this works\n like a charm.",
|
|
|
|
buffered_render(template: "test/streaming", layout: "layouts/streaming_with_capture")
|
|
|
|
end
|
2011-12-30 14:52:40 -05:00
|
|
|
end
|
2017-11-25 13:37:33 -05:00
|
|
|
|
|
|
|
class FiberedWithLocaleTest < SetupFiberedBase
|
|
|
|
def setup
|
|
|
|
@old_locale = I18n.locale
|
|
|
|
I18n.locale = "da"
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
I18n.locale = @old_locale
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_with_streaming_and_locale
|
|
|
|
assert_equal "layout.locale: da\nview.locale: da\n\n",
|
|
|
|
buffered_render(template: "test/streaming_with_locale", layout: "layouts/streaming_with_locale")
|
|
|
|
end
|
|
|
|
end
|