# frozen_string_literal: true require "abstract_unit" class TestController < ActionController::Base end class SetupFiberedBase < ActiveSupport::TestCase def setup view_paths = ActionController::Base.view_paths @assigns = { secret: "in the sauce", name: nil } @view = ActionView::Base.with_view_paths(view_paths, @assigns) @controller_view = TestController.new.view_context end def render_body(options) @view.view_renderer.render_body(@view, options) end def buffered_render(options) body = render_body(options) string = +"" body.each do |piece| string << piece end string end end class FiberedTest < SetupFiberedBase def test_streaming_works content = [] body = render_body(template: "test/hello_world", layout: "layouts/yield") body.each do |piece| content << piece end assert_equal "
This is grand!
", buffered_render(template: "test/hello") end def test_render_with_streaming_multiple_yields_provide_and_content_for assert_equal "Yes, \nthis works\n like a charm.", buffered_render(template: "test/streaming", layout: "layouts/streaming") end def test_render_with_streaming_with_fake_yields_and_streaming_buster assert_equal "This won't look\n good.", buffered_render(template: "test/streaming_buster", layout: "layouts/streaming") end def test_render_with_nested_streaming_multiple_yields_provide_and_content_for assert_equal "?Yes, \n\nthis works\n\n? like a charm.", buffered_render(template: "test/nested_streaming", layout: "layouts/streaming") end 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 end 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