diff options
Diffstat (limited to 'actionview/test/actionpack')
8 files changed, 654 insertions, 336 deletions
diff --git a/actionview/test/actionpack/abstract/abstract_controller_test.rb b/actionview/test/actionpack/abstract/abstract_controller_test.rb index 490932fef0..4d4e2b8ef2 100644 --- a/actionview/test/actionpack/abstract/abstract_controller_test.rb +++ b/actionview/test/actionpack/abstract/abstract_controller_test.rb @@ -1,9 +1,10 @@ -require 'abstract_unit' -require 'set' +# frozen_string_literal: true + +require "abstract_unit" +require "set" module AbstractController module Testing - # Test basic dispatching. # ==== # * Call process @@ -38,12 +39,12 @@ module AbstractController def render(options = {}) if options.is_a?(String) - options = {:_template_name => options} + options = { _template_name: options } end super end - append_view_path File.expand_path(File.join(File.dirname(__FILE__), "views")) + append_view_path File.expand_path("views", __dir__) end class Me2 < RenderingController @@ -65,11 +66,11 @@ module AbstractController end def rendering_to_body - self.response_body = render_to_body :template => "naked_render" + self.response_body = render_to_body template: "naked_render" end def rendering_to_string - self.response_body = render_to_string :template => "naked_render" + self.response_body = render_to_string template: "naked_render" end end @@ -114,13 +115,13 @@ module AbstractController # * self._prefix is used when defined class PrefixedViews < RenderingController private - def self.prefix - name.underscore - end + def self.prefix + name.underscore + end - def _prefixes - [self.class.prefix] - end + def _prefixes + [self.class.prefix] + end end class Me3 < PrefixedViews @@ -153,7 +154,7 @@ module AbstractController class OverridingLocalPrefixes < AbstractController::Base include AbstractController::Rendering include ActionView::Rendering - append_view_path File.expand_path(File.join(File.dirname(__FILE__), "views")) + append_view_path File.expand_path("views", __dir__) def index render @@ -189,19 +190,19 @@ module AbstractController include ActionView::Layouts private - def self.layout(formats) - find_template(name.underscore, {:formats => formats}, :_prefixes => ["layouts"]) - rescue ActionView::MissingTemplate - begin - find_template("application", {:formats => formats}, :_prefixes => ["layouts"]) + def self.layout(formats) + find_template(name.underscore, { formats: formats }, { _prefixes: ["layouts"] }) rescue ActionView::MissingTemplate + begin + find_template("application", { formats: formats }, { _prefixes: ["layouts"] }) + rescue ActionView::MissingTemplate + end end - end - def render_to_body(options = {}) - options[:_layout] = options[:layout] || _default_layout({}) - super - end + def render_to_body(options = {}) + options[:_layout] = options[:layout] || _default_layout({}) + super + end end class Me4 < WithLayouts @@ -228,14 +229,14 @@ module AbstractController end class ActionMissingRespondToActionController < AbstractController::Base - # No actions + # No actions private def action_missing(action_name) self.response_body = "success" end end - class RespondToActionController < AbstractController::Base; + class RespondToActionController < AbstractController::Base def index() self.response_body = "success" end def fail() self.response_body = "fail" end @@ -248,7 +249,6 @@ module AbstractController end class TestRespondToAction < ActiveSupport::TestCase - def assert_dispatch(klass, body = "success", action = :index) controller = klass.new controller.process(action) @@ -277,18 +277,16 @@ module AbstractController end class Me6 < AbstractController::Base - self.action_methods + action_methods def index end end class TestActionMethodsReloading < ActiveSupport::TestCase - test "action_methods should be reloaded after defining a new method" do assert_equal Set.new(["index"]), Me6.action_methods end end - end end diff --git a/actionview/test/actionpack/abstract/helper_test.rb b/actionview/test/actionpack/abstract/helper_test.rb index 7d346e917d..480ff60ba2 100644 --- a/actionview/test/actionpack/abstract/helper_test.rb +++ b/actionview/test/actionpack/abstract/helper_test.rb @@ -1,17 +1,18 @@ -require 'abstract_unit' +# frozen_string_literal: true -ActionController::Base.helpers_path = File.expand_path('../../../fixtures/helpers', __FILE__) +require "abstract_unit" + +ActionController::Base.helpers_path = File.expand_path("../../fixtures/helpers", __dir__) module AbstractController module Testing - class ControllerWithHelpers < AbstractController::Base include AbstractController::Helpers include AbstractController::Rendering include ActionView::Rendering def with_module - render :inline => "Module <%= included_method %>" + render inline: "Module <%= included_method %>" end end @@ -31,11 +32,11 @@ module AbstractController helper :abc def with_block - render :inline => "Hello <%= helpery_test %>" + render inline: "Hello <%= helpery_test %>" end def with_symbol - render :inline => "I respond to bare_a: <%= respond_to?(:bare_a) %>" + render inline: "I respond to bare_a: <%= respond_to?(:bare_a) %>" end end @@ -52,7 +53,7 @@ module AbstractController class AbstractInvalidHelpers < AbstractHelpers include ActionController::Helpers - path = File.expand_path('../../../fixtures/helpers_missing', __FILE__) + path = File.expand_path("../../fixtures/helpers_missing", __dir__) $:.unshift(path) self.helpers_path = path end @@ -110,7 +111,7 @@ module AbstractController class InvalidHelpersTest < ActiveSupport::TestCase def test_controller_raise_error_about_real_require_problem e = assert_raise(LoadError) { AbstractInvalidHelpers.helper(:invalid_require) } - assert_equal "No such file to load -- very_invalid_file_name", e.message + assert_equal "No such file to load -- very_invalid_file_name.rb", e.message end def test_controller_raise_error_about_missing_helper diff --git a/actionview/test/actionpack/abstract/layouts_test.rb b/actionview/test/actionpack/abstract/layouts_test.rb index a6786d9b6b..1146e6f64b 100644 --- a/actionview/test/actionpack/abstract/layouts_test.rb +++ b/actionview/test/actionpack/abstract/layouts_test.rb @@ -1,8 +1,9 @@ -require 'abstract_unit' +# frozen_string_literal: true + +require "abstract_unit" module AbstractControllerTests module Layouts - # Base controller for these tests class Base < AbstractController::Base include AbstractController::Rendering @@ -12,7 +13,9 @@ module AbstractControllerTests abstract! self.view_paths = [ActionView::FixtureResolver.new( + "some/template.erb" => "hello <%= foo %> bar", "layouts/hello.erb" => "With String <%= yield %>", + "layouts/hello_locals.erb" => "With String <%= yield %>", "layouts/hello_override.erb" => "With Override <%= yield %>", "layouts/overwrite.erb" => "Overwrite <%= yield %>", "layouts/with_false_layout.erb" => "False Layout <%= yield %>", @@ -28,7 +31,15 @@ module AbstractControllerTests self.view_paths = [] def index - render :template => ActionView::Template::Text.new("Hello blank!") + render template: ActionView::Template::Text.new("Hello blank!") + end + end + + class WithStringLocals < Base + layout "hello_locals" + + def index + render template: "some/template", locals: { foo: "less than 3" } end end @@ -36,23 +47,27 @@ module AbstractControllerTests layout "hello" def index - render :template => ActionView::Template::Text.new("Hello string!") + render template: ActionView::Template::Text.new("Hello string!") + end + + def action_has_layout_false + render template: ActionView::Template::Text.new("Hello string!") end def overwrite_default - render :template => ActionView::Template::Text.new("Hello string!"), :layout => :default + render template: ActionView::Template::Text.new("Hello string!"), layout: :default end def overwrite_false - render :template => ActionView::Template::Text.new("Hello string!"), :layout => false + render template: ActionView::Template::Text.new("Hello string!"), layout: false end def overwrite_string - render :template => ActionView::Template::Text.new("Hello string!"), :layout => "overwrite" + render template: ActionView::Template::Text.new("Hello string!"), layout: "overwrite" end def overwrite_skip - render :text => "Hello text!" + render plain: "Hello text!" end end @@ -78,11 +93,11 @@ module AbstractControllerTests layout proc { "overwrite" } def index - render :template => ActionView::Template::Text.new("Hello proc!") + render template: ActionView::Template::Text.new("Hello proc!") end end - class WithProcReturningNil < Base + class WithProcReturningNil < WithString layout proc { nil } def index @@ -90,11 +105,19 @@ module AbstractControllerTests end end + class WithProcReturningFalse < WithString + layout proc { false } + + def index + render template: ActionView::Template::Text.new("Hello false!") + end + end + class WithZeroArityProc < Base layout proc { "overwrite" } def index - render :template => ActionView::Template::Text.new("Hello zero arity proc!") + render template: ActionView::Template::Text.new("Hello zero arity proc!") end end @@ -107,7 +130,7 @@ module AbstractControllerTests } def index - render :template => ActionView::Template::Text.new("Hello again zero arity proc!") + render template: ActionView::Template::Text.new("Hello again zero arity proc!") end end @@ -115,7 +138,7 @@ module AbstractControllerTests layout :hello def index - render :template => ActionView::Template::Text.new("Hello symbol!") + render template: ActionView::Template::Text.new("Hello symbol!") end private def hello @@ -127,7 +150,7 @@ module AbstractControllerTests layout :nilz def index - render :template => ActionView::Template::Text.new("Hello nilz!") + render template: ActionView::Template::Text.new("Hello nilz!") end def nilz() end @@ -137,7 +160,7 @@ module AbstractControllerTests layout :objekt def index - render :template => ActionView::Template::Text.new("Hello nilz!") + render template: ActionView::Template::Text.new("Hello nilz!") end def objekt @@ -149,7 +172,7 @@ module AbstractControllerTests layout :no_method def index - render :template => ActionView::Template::Text.new("Hello boom!") + render template: ActionView::Template::Text.new("Hello boom!") end end @@ -157,7 +180,7 @@ module AbstractControllerTests layout "missing" def index - render :template => ActionView::Template::Text.new("Hello missing!") + render template: ActionView::Template::Text.new("Hello missing!") end end @@ -165,7 +188,7 @@ module AbstractControllerTests layout false def index - render :template => ActionView::Template::Text.new("Hello false!") + render template: ActionView::Template::Text.new("Hello false!") end end @@ -173,34 +196,81 @@ module AbstractControllerTests layout nil def index - render :template => ActionView::Template::Text.new("Hello nil!") + render template: ActionView::Template::Text.new("Hello nil!") end end class WithOnlyConditional < WithStringImpliedChild - layout "overwrite", :only => :show + layout "overwrite", only: :show def index - render :template => ActionView::Template::Text.new("Hello index!") + render template: ActionView::Template::Text.new("Hello index!") end def show - render :template => ActionView::Template::Text.new("Hello show!") + render template: ActionView::Template::Text.new("Hello show!") end end + class WithOnlyConditionalFlipped < WithOnlyConditional + layout "hello_override", only: :index + end + + class WithOnlyConditionalFlippedAndInheriting < WithOnlyConditional + layout nil, only: :index + end + class WithExceptConditional < WithStringImpliedChild - layout "overwrite", :except => :show + layout "overwrite", except: :show def index - render :template => ActionView::Template::Text.new("Hello index!") + render template: ActionView::Template::Text.new("Hello index!") end def show - render :template => ActionView::Template::Text.new("Hello show!") + render template: ActionView::Template::Text.new("Hello show!") + end + end + + class AbstractWithString < Base + layout "hello" + abstract! + end + + class AbstractWithStringChild < AbstractWithString + def index + render template: ActionView::Template::Text.new("Hello abstract child!") end end + class AbstractWithStringChildDefaultsToInherited < AbstractWithString + layout nil + + def index + render template: ActionView::Template::Text.new("Hello abstract child!") + end + end + + class WithConditionalOverride < WithString + layout "overwrite", only: :overwritten + + def non_overwritten + render template: ActionView::Template::Text.new("Hello non overwritten!") + end + + def overwritten + render template: ActionView::Template::Text.new("Hello overwritten!") + end + end + + class WithConditionalOverrideFlipped < WithConditionalOverride + layout "hello_override", only: :non_overwritten + end + + class WithConditionalOverrideFlippedAndInheriting < WithConditionalOverride + layout nil, only: :non_overwritten + end + class TestBase < ActiveSupport::TestCase test "when no layout is specified, and no default is available, render without a layout" do controller = Blank.new @@ -208,6 +278,31 @@ module AbstractControllerTests assert_equal "Hello blank!", controller.response_body end + test "with locals" do + controller = WithStringLocals.new + controller.process(:index) + assert_equal "With String hello less than 3 bar", controller.response_body + end + + test "cache should not grow when locals change for a string template" do + cache = WithString.view_paths.paths.first.instance_variable_get(:@cache) + + controller = WithString.new + controller.process(:index) # heat the cache + + size = cache.size + + 10.times do |x| + controller = WithString.new + controller.define_singleton_method :index do + render template: ActionView::Template::Text.new("Hello string!"), locals: { :"x#{x}" => :omg } + end + controller.process(:index) + end + + assert_equal size, cache.size + end + test "when layout is specified as a string, render with that layout" do controller = WithString.new controller.process(:index) @@ -255,7 +350,7 @@ module AbstractControllerTests end test "when layout is specified as a proc, do not leak any methods into controller's action_methods" do - assert_equal Set.new(['index']), WithProc.action_methods + assert_equal Set.new(["index"]), WithProc.action_methods end test "when layout is specified as a proc, call it and use the layout returned" do @@ -264,10 +359,16 @@ module AbstractControllerTests assert_equal "Overwrite Hello proc!", controller.response_body end - test "when layout is specified as a proc and the proc returns nil, don't use a layout" do + test "when layout is specified as a proc and the proc returns nil, use inherited layout" do controller = WithProcReturningNil.new controller.process(:index) - assert_equal "Hello nil!", controller.response_body + assert_equal "With String Hello nil!", controller.response_body + end + + test "when layout is specified as a proc and the proc returns false, use no layout instead of inherited layout" do + controller = WithProcReturningFalse.new + controller.process(:index) + assert_equal "Hello false!", controller.response_body end test "when layout is specified as a proc without parameters it works just the same" do @@ -322,16 +423,28 @@ module AbstractControllerTests test "when a grandchild has no layout specified, the child has an implied layout, and the " \ "parent has specified a layout, use the child controller layout" do - controller = WithChildOfImplied.new - controller.process(:index) - assert_equal "With Implied Hello string!", controller.response_body + controller = WithChildOfImplied.new + controller.process(:index) + assert_equal "With Implied Hello string!", controller.response_body end test "when a grandchild has nil layout specified, the child has an implied layout, and the " \ - "parent has specified a layout, use the child controller layout" do - controller = WithGrandChildOfImplied.new - controller.process(:index) - assert_equal "With Grand Child Hello string!", controller.response_body + "parent has specified a layout, use the grand child controller layout" do + controller = WithGrandChildOfImplied.new + controller.process(:index) + assert_equal "With Grand Child Hello string!", controller.response_body + end + + test "a child inherits layout from abstract controller" do + controller = AbstractWithStringChild.new + controller.process(:index) + assert_equal "With String Hello abstract child!", controller.response_body + end + + test "a child inherits layout from abstract controller2" do + controller = AbstractWithStringChildDefaultsToInherited.new + controller.process(:index) + assert_equal "With String Hello abstract child!", controller.response_body end test "raises an exception when specifying layout true" do @@ -356,6 +469,30 @@ module AbstractControllerTests assert_equal "With Implied Hello index!", controller.response_body end + test "when specify an :only option which match current action name and is opposite from parent controller" do + controller = WithOnlyConditionalFlipped.new + controller.process(:show) + assert_equal "With Implied Hello show!", controller.response_body + end + + test "when specify an :only option which does not match current action name and is opposite from parent controller" do + controller = WithOnlyConditionalFlipped.new + controller.process(:index) + assert_equal "With Override Hello index!", controller.response_body + end + + test "when specify to inherit and an :only option which match current action name and is opposite from parent controller" do + controller = WithOnlyConditionalFlippedAndInheriting.new + controller.process(:show) + assert_equal "With Implied Hello show!", controller.response_body + end + + test "when specify to inherit and an :only option which does not match current action name and is opposite from parent controller" do + controller = WithOnlyConditionalFlippedAndInheriting.new + controller.process(:index) + assert_equal "Overwrite Hello index!", controller.response_body + end + test "when specify an :except option which match current action name" do controller = WithExceptConditional.new controller.process(:show) @@ -368,10 +505,46 @@ module AbstractControllerTests assert_equal "Overwrite Hello index!", controller.response_body end + test "when specify overwrite as an :only option which match current action name" do + controller = WithConditionalOverride.new + controller.process(:overwritten) + assert_equal "Overwrite Hello overwritten!", controller.response_body + end + + test "when specify overwrite as an :only option which does not match current action name" do + controller = WithConditionalOverride.new + controller.process(:non_overwritten) + assert_equal "Hello non overwritten!", controller.response_body + end + + test "when specify overwrite as an :only option which match current action name and is opposite from parent controller" do + controller = WithConditionalOverrideFlipped.new + controller.process(:overwritten) + assert_equal "Hello overwritten!", controller.response_body + end + + test "when specify overwrite as an :only option which does not match current action name and is opposite from parent controller" do + controller = WithConditionalOverrideFlipped.new + controller.process(:non_overwritten) + assert_equal "With Override Hello non overwritten!", controller.response_body + end + + test "when specify to inherit and overwrite as an :only option which match current action name and is opposite from parent controller" do + controller = WithConditionalOverrideFlippedAndInheriting.new + controller.process(:overwritten) + assert_equal "Hello overwritten!", controller.response_body + end + + test "when specify to inherit and overwrite as an :only option which does not match current action name and is opposite from parent controller" do + controller = WithConditionalOverrideFlippedAndInheriting.new + controller.process(:non_overwritten) + assert_equal "Overwrite Hello non overwritten!", controller.response_body + end + test "layout for anonymous controller" do klass = Class.new(WithString) do def index - render :text => 'index', :layout => true + render plain: "index", layout: true end end @@ -379,6 +552,17 @@ module AbstractControllerTests controller.process(:index) assert_equal "With String index", controller.response_body end + + test "when layout is disabled with #action_has_layout? returning false, render no layout" do + controller = WithString.new + controller.instance_eval do + def action_has_layout? + false + end + end + controller.process(:action_has_layout_false) + assert_equal "Hello string!", controller.response_body + end end end end diff --git a/actionview/test/actionpack/abstract/render_test.rb b/actionview/test/actionpack/abstract/render_test.rb index d09f91c1e2..d863548a5c 100644 --- a/actionview/test/actionpack/abstract/render_test.rb +++ b/actionview/test/actionpack/abstract/render_test.rb @@ -1,8 +1,9 @@ -require 'abstract_unit' +# frozen_string_literal: true + +require "abstract_unit" module AbstractController module Testing - class ControllerRenderer < AbstractController::Base include AbstractController::Rendering include ActionView::Rendering @@ -21,19 +22,19 @@ module AbstractController )] def template - render :template => "template" + render template: "template" end def file - render :file => "some/file" + render file: "some/file" end def inline - render :inline => "With <%= :Inline %>" + render inline: "With <%= :Inline %>" end def text - render :text => "With Text" + render plain: "With Text" end def default @@ -54,7 +55,6 @@ module AbstractController end class TestRenderer < ActiveSupport::TestCase - def setup @controller = ControllerRenderer.new end diff --git a/actionview/test/actionpack/controller/capture_test.rb b/actionview/test/actionpack/controller/capture_test.rb index f8387b27b0..d02125bafa 100644 --- a/actionview/test/actionpack/controller/capture_test.rb +++ b/actionview/test/actionpack/controller/capture_test.rb @@ -1,30 +1,32 @@ -require 'abstract_unit' -require 'active_support/logger' +# frozen_string_literal: true + +require "abstract_unit" +require "active_support/logger" class CaptureController < ActionController::Base - self.view_paths = [ File.dirname(__FILE__) + '/../../fixtures/actionpack' ] + self.view_paths = [ File.expand_path("../../fixtures/actionpack", __dir__) ] def self.controller_name; "test"; end def self.controller_path; "test"; end def content_for @title = nil - render :layout => "talk_from_action" + render layout: "talk_from_action" end def content_for_with_parameter @title = nil - render :layout => "talk_from_action" + render layout: "talk_from_action" end def content_for_concatenated @title = nil - render :layout => "talk_from_action" + render layout: "talk_from_action" end def non_erb_block_content_for @title = nil - render :layout => "talk_from_action" + render layout: "talk_from_action" end def proper_block_detection @@ -35,6 +37,15 @@ end class CaptureTest < ActionController::TestCase tests CaptureController + with_routes do + get :content_for, to: "test#content_for" + get :capturing, to: "test#capturing" + get :proper_block_detection, to: "test#proper_block_detection" + get :non_erb_block_content_for, to: "test#non_erb_block_content_for" + get :content_for_concatenated, to: "test#content_for_concatenated" + get :content_for_with_parameter, to: "test#content_for_with_parameter" + end + def setup super # enable a logger so that (e.g.) the benchmarking stuff runs, so we can get @@ -54,7 +65,7 @@ class CaptureTest < ActionController::TestCase assert_equal expected_content_for_output, @response.body end - def test_should_concatentate_content_for + def test_should_concatenate_content_for get :content_for_concatenated assert_equal expected_content_for_output, @response.body end diff --git a/actionview/test/actionpack/controller/layout_test.rb b/actionview/test/actionpack/controller/layout_test.rb index 64bc4c41d6..6d5c97b7fd 100644 --- a/actionview/test/actionpack/controller/layout_test.rb +++ b/actionview/test/actionpack/controller/layout_test.rb @@ -1,15 +1,17 @@ -require 'abstract_unit' -require 'active_support/core_ext/array/extract_options' +# frozen_string_literal: true + +require "abstract_unit" +require "active_support/core_ext/array/extract_options" # The view_paths array must be set on Base and not LayoutTest so that LayoutTest's inherited # method has access to the view_paths array when looking for a layout to automatically assign. old_load_paths = ActionController::Base.view_paths -ActionController::Base.view_paths = [ File.dirname(__FILE__) + '/../../fixtures/actionpack/layout_tests/' ] +ActionController::Base.view_paths = [ File.expand_path("../../fixtures/actionpack/layout_tests", __dir__) ] class LayoutTest < ActionController::Base - def self.controller_path; 'views' end - def self._implied_layout_name; to_s.underscore.gsub(/_controller$/, '') ; end + def self.controller_path; "views" end + def self._implied_layout_name; to_s.underscore.gsub(/_controller$/, "") ; end self.view_paths = ActionController::Base.view_paths.dup end @@ -46,6 +48,10 @@ end class LayoutAutoDiscoveryTest < ActionController::TestCase include TemplateHandlerHelper + with_routes do + get :hello, to: "views#hello" + end + def setup super @request.host = "www.nextangle.com" @@ -54,13 +60,13 @@ class LayoutAutoDiscoveryTest < ActionController::TestCase def test_application_layout_is_default_when_no_controller_match @controller = ProductController.new get :hello - assert_equal 'layout_test.erb hello.erb', @response.body + assert_equal "layout_test.erb hello.erb", @response.body end def test_controller_name_layout_name_match @controller = ItemController.new get :hello - assert_equal 'item.erb hello.erb', @response.body + assert_equal "item.erb hello.erb", @response.body end def test_third_party_template_library_auto_discovers_layout @@ -68,20 +74,20 @@ class LayoutAutoDiscoveryTest < ActionController::TestCase @controller = ThirdPartyTemplateLibraryController.new get :hello assert_response :success - assert_equal 'layouts/third_party_template_library.mab', @response.body + assert_equal "layouts/third_party_template_library.mab", @response.body end end def test_namespaced_controllers_auto_detect_layouts1 @controller = ControllerNameSpace::NestedController.new get :hello - assert_equal 'controller_name_space/nested.erb hello.erb', @response.body + assert_equal "controller_name_space/nested.erb hello.erb", @response.body end def test_namespaced_controllers_auto_detect_layouts2 @controller = MultipleExtensions.new get :hello - assert_equal 'multiple_extensions.html.erb hello.erb', @response.body.strip + assert_equal "multiple_extensions.html.erb hello.erb", @response.body.strip end end @@ -91,16 +97,16 @@ end class StreamingLayoutController < LayoutTest def render(*args) options = args.extract_options! - super(*args, options.merge(:stream => true)) + super(*args, options.merge(stream: true)) end end class AbsolutePathLayoutController < LayoutTest - layout File.expand_path(File.expand_path(__FILE__) + '/../../../fixtures/actionpack/layout_tests/layouts/layout_test') + layout File.expand_path("../../fixtures/actionpack/layout_tests/layouts/layout_test", __dir__) end class HasOwnLayoutController < LayoutTest - layout 'item' + layout "item" end class HasNilLayoutSymbol < LayoutTest @@ -117,28 +123,28 @@ end class PrependsViewPathController < LayoutTest def hello - prepend_view_path File.dirname(__FILE__) + '/../../fixtures/actionpack/layout_tests/alt/' - render :layout => 'alt' + prepend_view_path File.expand_path("../../fixtures/actionpack/layout_tests/alt", __dir__) + render layout: "alt" end end class OnlyLayoutController < LayoutTest - layout 'item', :only => "hello" + layout "item", only: "hello" end class ExceptLayoutController < LayoutTest - layout 'item', :except => "goodbye" + layout "item", except: "goodbye" end class SetsLayoutInRenderController < LayoutTest def hello - render :layout => 'third_party_template_library' + render layout: "third_party_template_library" end end class RendersNoLayoutController < LayoutTest def hello - render :layout => false + render layout: false end end @@ -146,78 +152,83 @@ class LayoutSetInResponseTest < ActionController::TestCase include ActionView::Template::Handlers include TemplateHandlerHelper + with_routes do + get :hello, to: "views#hello" + get :hello, to: "views#goodbye" + end + def test_layout_set_when_using_default_layout @controller = DefaultLayoutController.new get :hello - assert_includes @response.body, 'layout_test.erb' + assert_includes @response.body, "layout_test.erb" end def test_layout_set_when_using_streaming_layout @controller = StreamingLayoutController.new get :hello - assert_includes @response.body, 'layout_test.erb' + assert_includes @response.body, "layout_test.erb" end def test_layout_set_when_set_in_controller @controller = HasOwnLayoutController.new get :hello - assert_includes @response.body, 'item.erb' + assert_includes @response.body, "item.erb" end def test_layout_symbol_set_in_controller_returning_nil_falls_back_to_default @controller = HasNilLayoutSymbol.new get :hello - assert_includes @response.body, 'layout_test.erb' + assert_includes @response.body, "layout_test.erb" end def test_layout_proc_set_in_controller_returning_nil_falls_back_to_default @controller = HasNilLayoutProc.new get :hello - assert_includes @response.body, 'layout_test.erb' + assert_includes @response.body, "layout_test.erb" end def test_layout_only_exception_when_included @controller = OnlyLayoutController.new get :hello - assert_includes @response.body, 'item.erb' + assert_includes @response.body, "item.erb" end def test_layout_only_exception_when_excepted @controller = OnlyLayoutController.new get :goodbye - assert_not_includes @response.body, 'item.erb' + assert_not_includes @response.body, "item.erb" end def test_layout_except_exception_when_included @controller = ExceptLayoutController.new get :hello - assert_includes @response.body, 'item.erb' + assert_includes @response.body, "item.erb" end def test_layout_except_exception_when_excepted @controller = ExceptLayoutController.new get :goodbye - assert_not_includes @response.body, 'item.erb' + assert_not_includes @response.body, "item.erb" end def test_layout_set_when_using_render with_template_handler :mab, lambda { |template| template.source.inspect } do @controller = SetsLayoutInRenderController.new get :hello - assert_includes @response.body, 'layouts/third_party_template_library.mab' + assert_includes @response.body, "layouts/third_party_template_library.mab" end end def test_layout_is_not_set_when_none_rendered @controller = RendersNoLayoutController.new get :hello - assert_equal 'hello.erb', @response.body + assert_equal "hello.erb", @response.body end def test_layout_is_picked_from_the_controller_instances_view_path @controller = PrependsViewPathController.new get :hello - assert_includes @response.body, 'alt.erb' + assert_includes @response.body, "alt.erb" end def test_absolute_pathed_layout @@ -232,6 +243,10 @@ class SetsNonExistentLayoutFile < LayoutTest end class LayoutExceptionRaisedTest < ActionController::TestCase + with_routes do + get :hello, to: "views#hello" + end + def test_exception_raised_when_layout_file_not_found @controller = SetsNonExistentLayoutFile.new assert_raise(ActionView::MissingTemplate) { get :hello } @@ -240,11 +255,15 @@ end class LayoutStatusIsRendered < LayoutTest def hello - render :status => 401 + render status: 401 end end class LayoutStatusIsRenderedTest < ActionController::TestCase + with_routes do + get :hello, to: "views#hello" + end + def test_layout_status_is_rendered @controller = LayoutStatusIsRendered.new get :hello @@ -252,17 +271,21 @@ class LayoutStatusIsRenderedTest < ActionController::TestCase end end -unless RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ +unless Gem.win_platform? class LayoutSymlinkedTest < LayoutTest layout "symlinked/symlinked_layout" end class LayoutSymlinkedIsRenderedTest < ActionController::TestCase + with_routes do + get :hello, to: "views#hello" + end + def test_symlinked_layout_is_rendered @controller = LayoutSymlinkedTest.new get :hello assert_response 200 - assert_includes @response.body, 'This is my layout' + assert_includes @response.body, "This is my layout" end end end diff --git a/actionview/test/actionpack/controller/render_test.rb b/actionview/test/actionpack/controller/render_test.rb index ebaf39a12d..204903c60c 100644 --- a/actionview/test/actionpack/controller/render_test.rb +++ b/actionview/test/actionpack/controller/render_test.rb @@ -1,45 +1,12 @@ -require 'abstract_unit' -require 'active_model' +# frozen_string_literal: true -class ApplicationController < ActionController::Base - self.view_paths = File.join(FIXTURE_LOAD_PATH, "actionpack") -end - -class Customer < Struct.new(:name, :id) - extend ActiveModel::Naming - include ActiveModel::Conversion - - undef_method :to_json - - def to_xml(options={}) - if options[:builder] - options[:builder].name name - else - "<name>#{name}</name>" - end - end - - def to_js(options={}) - "name: #{name.inspect}" - end - alias :to_text :to_js - - def errors - [] - end - - def persisted? - id.present? - end - - def cache_key - name.to_s - end -end +require "abstract_unit" +require "active_model" +require "controller/fake_models" module Quiz - #Models - class Question < Struct.new(:name, :id) + # Models + Question = Struct.new(:name, :id) do extend ActiveModel::Naming include ActiveModel::Conversion @@ -49,27 +16,24 @@ module Quiz end # Controller - class QuestionsController < ApplicationController + class QuestionsController < ActionController::Base def new - render :partial => Quiz::Question.new("Namespaced Partial") + render partial: Quiz::Question.new("Namespaced Partial") end end end -class BadCustomer < Customer; end -class GoodCustomer < Customer; end - module Fun - class GamesController < ApplicationController + class GamesController < ActionController::Base def hello_world; end def nested_partial_with_form_builder - render :partial => ActionView::Helpers::FormBuilder.new(:post, nil, view_context, {}) + render partial: ActionView::Helpers::FormBuilder.new(:post, nil, view_context, {}) end end end -class TestController < ApplicationController +class TestController < ActionController::Base protect_from_forgery before_action :set_variable_for_layout @@ -90,7 +54,7 @@ class TestController < ApplicationController end def hello_world_file - render :file => File.expand_path("../../../fixtures/actionpack/hello", __FILE__), :formats => [:html] + render file: File.expand_path("../../fixtures/actionpack/hello", __dir__), formats: [:html] end # :ported: @@ -110,27 +74,27 @@ class TestController < ApplicationController # :ported: def render_template_in_top_directory - render :template => 'shared' + render template: "shared" end # :deprecated: def render_template_in_top_directory_with_slash - render '/shared' + render "/shared" end # :ported: def render_hello_world_from_variable @person = "david" - render :text => "hello #{@person}" + render plain: "hello #{@person}" end # :ported: def render_action_hello_world - render :action => "hello_world" + render action: "hello_world" end def render_action_upcased_hello_world - render :action => "Hello_world" + render action: "Hello_world" end def render_action_hello_world_as_string @@ -138,100 +102,100 @@ class TestController < ApplicationController end def render_action_hello_world_with_symbol - render :action => :hello_world + render action: :hello_world end # :ported: def render_text_hello_world - render :text => "hello world" + render plain: "hello world" end # :ported: def render_text_hello_world_with_layout @variable_for_layout = ", I am here!" - render :text => "hello world", :layout => true + render plain: "hello world", layout: true end def hello_world_with_layout_false - render :layout => false + render layout: false end # :ported: def render_file_with_instance_variables - @secret = 'in the sauce' - path = File.join(File.dirname(__FILE__), '../../fixtures/test/render_file_with_ivar') - render :file => path + @secret = "in the sauce" + path = File.expand_path("../../fixtures/test/render_file_with_ivar", __dir__) + render file: path end # :ported: def render_file_not_using_full_path - @secret = 'in the sauce' - render :file => 'test/render_file_with_ivar' + @secret = "in the sauce" + render file: "test/render_file_with_ivar" end def render_file_not_using_full_path_with_dot_in_path - @secret = 'in the sauce' - render :file => 'test/dot.directory/render_file_with_ivar' + @secret = "in the sauce" + render file: "test/dot.directory/render_file_with_ivar" end def render_file_using_pathname - @secret = 'in the sauce' - render :file => Pathname.new(File.dirname(__FILE__)).join('..', '..', 'fixtures', 'test', 'dot.directory', 'render_file_with_ivar') + @secret = "in the sauce" + render file: Pathname.new(__dir__).join("..", "..", "fixtures", "test", "dot.directory", "render_file_with_ivar") end def render_file_from_template - @secret = 'in the sauce' - @path = File.expand_path(File.join(File.dirname(__FILE__), '../../fixtures/test/render_file_with_ivar')) + @secret = "in the sauce" + @path = File.expand_path("../../fixtures/test/render_file_with_ivar", __dir__) end def render_file_with_locals - path = File.join(File.dirname(__FILE__), '../../fixtures/test/render_file_with_locals') - render :file => path, :locals => {:secret => 'in the sauce'} + path = File.expand_path("../../fixtures/test/render_file_with_locals", __dir__) + render file: path, locals: { secret: "in the sauce" } end def render_file_as_string_with_locals - path = File.expand_path(File.join(File.dirname(__FILE__), '../../fixtures/test/render_file_with_locals')) - render file: path, :locals => {:secret => 'in the sauce'} + path = File.expand_path("../../fixtures/test/render_file_with_locals", __dir__) + render file: path, locals: { secret: "in the sauce" } end def accessing_request_in_template - render :inline => "Hello: <%= request.host %>" + render inline: "Hello: <%= request.host %>" end def accessing_logger_in_template - render :inline => "<%= logger.class %>" + render inline: "<%= logger.class %>" end def accessing_action_name_in_template - render :inline => "<%= action_name %>" + render inline: "<%= action_name %>" end def accessing_controller_name_in_template - render :inline => "<%= controller_name %>" + render inline: "<%= controller_name %>" end # :ported: def render_custom_code - render :text => "hello world", :status => 404 + render plain: "hello world", status: 404 end # :ported: def render_text_with_nil - render :text => nil + render plain: nil end # :ported: def render_text_with_false - render :text => false + render plain: false end def render_text_with_resource - render :text => Customer.new("David") + render plain: Customer.new("David") end # :ported: def render_nothing_with_appendix - render :text => "appended" + render plain: "appended" end # This test is testing 3 things: @@ -240,7 +204,7 @@ class TestController < ApplicationController # setting content type def render_xml_hello @name = "David" - render :template => "test/hello" + render template: "test/hello" end def render_xml_hello_as_string_template @@ -249,7 +213,7 @@ class TestController < ApplicationController end def render_line_offset - render :inline => '<% raise %>', :locals => {:foo => 'bar'} + render inline: "<% raise %>", locals: { foo: "bar" } end def heading @@ -262,49 +226,49 @@ class TestController < ApplicationController # :ported: def blank_response - render :text => ' ' + render plain: " " end # :ported: def layout_test - render :action => "hello_world" + render action: "hello_world" end # :ported: def builder_layout_test @name = nil - render :action => "hello", :layout => "layouts/builder" + render action: "hello", layout: "layouts/builder" end # :move: test this in Action View def builder_partial_test - render :action => "hello_world_container" + render action: "hello_world_container" end # :ported: def partials_list - @test_unchanged = 'hello' + @test_unchanged = "hello" @customers = [ Customer.new("david"), Customer.new("mary") ] - render :action => "list" + render action: "list" end def partial_only - render :partial => true + render partial: true end def hello_in_a_string @customers = [ Customer.new("david"), Customer.new("mary") ] - render :text => "How's there? " + render_to_string(:template => "test/list") + render plain: "How's there? " + render_to_string(template: "test/list") end def accessing_params_in_template - render :inline => "Hello: <%= params[:name] %>" + render inline: "Hello: <%= params[:name] %>" end def accessing_local_assigns_in_inline_template name = params[:local_name] - render :inline => "<%= 'Goodbye, ' + local_name %>", - :locals => { :local_name => name } + render inline: "<%= 'Goodbye, ' + local_name %>", + locals: { local_name: name } end def render_implicit_html_template_from_xhr_request @@ -320,7 +284,7 @@ class TestController < ApplicationController end def render_to_string_test - @foo = render_to_string :inline => "this is a test" + @foo = render_to_string inline: "this is a test" end def default_render @@ -333,27 +297,27 @@ class TestController < ApplicationController end def render_action_hello_world_as_symbol - render :action => :hello_world + render action: :hello_world end def layout_test_with_different_layout - render :action => "hello_world", :layout => "standard" + render action: "hello_world", layout: "standard" end def layout_test_with_different_layout_and_string_action - render "hello_world", :layout => "standard" + render "hello_world", layout: "standard" end def layout_test_with_different_layout_and_symbol_action - render :hello_world, :layout => "standard" + render :hello_world, layout: "standard" end def rendering_without_layout - render :action => "hello_world", :layout => false + render action: "hello_world", layout: false end def layout_overriding_layout - render :action => "hello_world", :layout => "standard" + render action: "hello_world", layout: "standard" end def rendering_nothing_on_layout @@ -362,40 +326,40 @@ class TestController < ApplicationController def render_to_string_with_assigns @before = "i'm before the render" - render_to_string :text => "foo" + render_to_string plain: "foo" @after = "i'm after the render" - render :template => "test/hello_world" + render template: "test/hello_world" end def render_to_string_with_exception - render_to_string :file => "exception that will not be caught - this will certainly not work" + render_to_string file: "exception that will not be caught - this will certainly not work" end def render_to_string_with_caught_exception @before = "i'm before the render" begin - render_to_string :file => "exception that will be caught- hope my future instance vars still work!" + render_to_string file: "exception that will be caught- hope my future instance vars still work!" rescue end @after = "i'm after the render" - render :template => "test/hello_world" + render template: "test/hello_world" end def accessing_params_in_template_with_layout - render :layout => true, :inline => "Hello: <%= params[:name] %>" + render layout: true, inline: "Hello: <%= params[:name] %>" end # :ported: def render_with_explicit_template - render :template => "test/hello_world" + render template: "test/hello_world" end def render_with_explicit_unescaped_template - render :template => "test/h*llo_world" + render template: "test/h*llo_world" end def render_with_explicit_escaped_template - render :template => "test/hello,world" + render template: "test/hello,world" end def render_with_explicit_string_template @@ -404,47 +368,47 @@ class TestController < ApplicationController # :ported: def render_with_explicit_template_with_locals - render :template => "test/render_file_with_locals", :locals => { :secret => 'area51' } + render template: "test/render_file_with_locals", locals: { secret: "area51" } end # :ported: def double_render - render :text => "hello" - render :text => "world" + render plain: "hello" + render plain: "world" end def double_redirect - redirect_to :action => "double_render" - redirect_to :action => "double_render" + redirect_to action: "double_render" + redirect_to action: "double_render" end def render_and_redirect - render :text => "hello" - redirect_to :action => "double_render" + render plain: "hello" + redirect_to action: "double_render" end def render_to_string_and_render - @stuff = render_to_string :text => "here is some cached stuff" - render :text => "Hi web users! #{@stuff}" + @stuff = render_to_string plain: "here is some cached stuff" + render plain: "Hi web users! #{@stuff}" end def render_to_string_with_inline_and_render - render_to_string :inline => "<%= 'dlrow olleh'.reverse %>" - render :template => "test/hello_world" + render_to_string inline: "<%= 'dlrow olleh'.reverse %>" + render template: "test/hello_world" end def rendering_with_conflicting_local_vars @name = "David" - render :action => "potential_conflicts" + render action: "potential_conflicts" end def hello_world_from_rxml_using_action - render :action => "hello_world_from_rxml", :handlers => [:builder] + render action: "hello_world_from_rxml", handlers: [:builder] end # :deprecated: def hello_world_from_rxml_using_template - render :template => "test/hello_world_from_rxml", :handlers => [:builder] + render template: "test/hello_world_from_rxml", handlers: [:builder] end def action_talk_to_layout @@ -454,134 +418,138 @@ class TestController < ApplicationController # :addressed: def render_text_with_assigns @hello = "world" - render :text => "foo" + render plain: "foo" end def render_with_assigns_option - render inline: '<%= @hello %>', assigns: { hello: "world" } + render inline: "<%= @hello %>", assigns: { hello: "world" } end def yield_content_for - render :action => "content_for", :layout => "yield" + render action: "content_for", layout: "yield" end def render_content_type_from_body - response.content_type = Mime::RSS - render :text => "hello world!" + response.content_type = Mime[:rss] + render body: "hello world!" end def render_using_layout_around_block - render :action => "using_layout_around_block" + render action: "using_layout_around_block" end def render_using_layout_around_block_in_main_layout_and_within_content_for_layout - render :action => "using_layout_around_block", :layout => "layouts/block_with_layout" + render action: "using_layout_around_block", layout: "layouts/block_with_layout" end def partial_formats_html - render :partial => 'partial', :formats => [:html] + render partial: "partial", formats: [:html] end def partial - render :partial => 'partial' + render partial: "partial" end def partial_html_erb - render :partial => 'partial_html_erb' + render partial: "partial_html_erb" end def render_to_string_with_partial - @partial_only = render_to_string :partial => "partial_only" - @partial_with_locals = render_to_string :partial => "customer", :locals => { :customer => Customer.new("david") } - render :template => "test/hello_world" + @partial_only = render_to_string partial: "partial_only" + @partial_with_locals = render_to_string partial: "customer", locals: { customer: Customer.new("david") } + render template: "test/hello_world" end def render_to_string_with_template_and_html_partial - @text = render_to_string :template => "test/with_partial", :formats => [:text] - @html = render_to_string :template => "test/with_partial", :formats => [:html] - render :template => "test/with_html_partial" + @text = render_to_string template: "test/with_partial", formats: [:text] + @html = render_to_string template: "test/with_partial", formats: [:html] + render template: "test/with_html_partial" end def render_to_string_and_render_with_different_formats - @html = render_to_string :template => "test/with_partial", :formats => [:html] - render :template => "test/with_partial", :formats => [:text] + @html = render_to_string template: "test/with_partial", formats: [:html] + render template: "test/with_partial", formats: [:text] end def render_template_within_a_template_with_other_format - render :template => "test/with_xml_template", - :formats => [:html], - :layout => "with_html_partial" + render template: "test/with_xml_template", + formats: [:html], + layout: "with_html_partial" end def partial_with_counter - render :partial => "counter", :locals => { :counter_counter => 5 } + render partial: "counter", locals: { counter_counter: 5 } end def partial_with_locals - render :partial => "customer", :locals => { :customer => Customer.new("david") } + render partial: "customer", locals: { customer: Customer.new("david") } + end + + def partial_with_string_locals + render partial: "customer", locals: { "customer" => Customer.new("david") } end def partial_with_form_builder - render :partial => ActionView::Helpers::FormBuilder.new(:post, nil, view_context, {}) + render partial: ActionView::Helpers::FormBuilder.new(:post, nil, view_context, {}) end def partial_with_form_builder_subclass - render :partial => LabellingFormBuilder.new(:post, nil, view_context, {}) + render partial: LabellingFormBuilder.new(:post, nil, view_context, {}) end def partial_collection - render :partial => "customer", :collection => [ Customer.new("david"), Customer.new("mary") ] + render partial: "customer", collection: [ Customer.new("david"), Customer.new("mary") ] end def partial_collection_with_as - render :partial => "customer_with_var", :collection => [ Customer.new("david"), Customer.new("mary") ], :as => :customer + render partial: "customer_with_var", collection: [ Customer.new("david"), Customer.new("mary") ], as: :customer end def partial_collection_with_iteration - render partial: "customer_iteration", collection: [ Customer.new("david"), Customer.new("mary"), Customer.new('christine') ] + render partial: "customer_iteration", collection: [ Customer.new("david"), Customer.new("mary"), Customer.new("christine") ] end def partial_collection_with_as_and_iteration - render partial: "customer_iteration_with_as", collection: [ Customer.new("david"), Customer.new("mary"), Customer.new('christine') ], as: :client + render partial: "customer_iteration_with_as", collection: [ Customer.new("david"), Customer.new("mary"), Customer.new("christine") ], as: :client end def partial_collection_with_counter - render :partial => "customer_counter", :collection => [ Customer.new("david"), Customer.new("mary") ] + render partial: "customer_counter", collection: [ Customer.new("david"), Customer.new("mary") ] end def partial_collection_with_as_and_counter - render :partial => "customer_counter_with_as", :collection => [ Customer.new("david"), Customer.new("mary") ], :as => :client + render partial: "customer_counter_with_as", collection: [ Customer.new("david"), Customer.new("mary") ], as: :client end def partial_collection_with_locals - render :partial => "customer_greeting", :collection => [ Customer.new("david"), Customer.new("mary") ], :locals => { :greeting => "Bonjour" } + render partial: "customer_greeting", collection: [ Customer.new("david"), Customer.new("mary") ], locals: { greeting: "Bonjour" } end def partial_collection_with_spacer - render :partial => "customer", :spacer_template => "partial_only", :collection => [ Customer.new("david"), Customer.new("mary") ] + render partial: "customer", spacer_template: "partial_only", collection: [ Customer.new("david"), Customer.new("mary") ] end def partial_collection_with_spacer_which_uses_render - render :partial => "customer", :spacer_template => "partial_with_partial", :collection => [ Customer.new("david"), Customer.new("mary") ] + render partial: "customer", spacer_template: "partial_with_partial", collection: [ Customer.new("david"), Customer.new("mary") ] end def partial_collection_shorthand_with_locals - render :partial => [ Customer.new("david"), Customer.new("mary") ], :locals => { :greeting => "Bonjour" } + render partial: [ Customer.new("david"), Customer.new("mary") ], locals: { greeting: "Bonjour" } end def partial_collection_shorthand_with_different_types_of_records - render :partial => [ + render partial: [ BadCustomer.new("mark"), GoodCustomer.new("craig"), BadCustomer.new("john"), GoodCustomer.new("zach"), GoodCustomer.new("brandon"), BadCustomer.new("dan") ], - :locals => { :greeting => "Bonjour" } + locals: { greeting: "Bonjour" } end def empty_partial_collection - render :partial => "customer", :collection => [] + render partial: "customer", collection: [] end def partial_collection_shorthand_with_different_types_of_records_with_counter @@ -589,15 +557,15 @@ class TestController < ApplicationController end def missing_partial - render :partial => 'thisFileIsntHere' + render partial: "thisFileIsntHere" end def partial_with_hash_object - render :partial => "hash_object", :object => {:first_name => "Sam"} + render partial: "hash_object", object: { first_name: "Sam" } end def partial_with_nested_object - render :partial => "quiz/questions/question", :object => Quiz::Question.new("first") + render partial: "quiz/questions/question", object: Quiz::Question.new("first") end def partial_with_nested_object_shorthand @@ -605,24 +573,24 @@ class TestController < ApplicationController end def partial_hash_collection - render :partial => "hash_object", :collection => [ {:first_name => "Pratik"}, {:first_name => "Amy"} ] + render partial: "hash_object", collection: [ { first_name: "Pratik" }, { first_name: "Amy" } ] end def partial_hash_collection_with_locals - render :partial => "hash_greeting", :collection => [ {:first_name => "Pratik"}, {:first_name => "Amy"} ], :locals => { :greeting => "Hola" } + render partial: "hash_greeting", collection: [ { first_name: "Pratik" }, { first_name: "Amy" } ], locals: { greeting: "Hola" } end def partial_with_implicit_local_assignment @customer = Customer.new("Marcel") - render :partial => "customer" + render partial: "customer" end def render_call_to_partial_with_layout - render :action => "calling_partial_with_layout" + render action: "calling_partial_with_layout" end def render_call_to_partial_with_layout_in_main_layout_and_within_content_for_layout - render :action => "calling_partial_with_layout", :layout => "layouts/partial_with_layout" + render action: "calling_partial_with_layout", layout: "layouts/partial_with_layout" end before_action only: :render_with_filters do @@ -631,7 +599,7 @@ class TestController < ApplicationController # Ensure that the before filter is executed *before* self.formats is set. def render_with_filters - render :action => :formatted_xml_erb + render action: :formatted_xml_erb end private @@ -642,7 +610,7 @@ class TestController < ApplicationController def determine_layout case action_name - when "hello_world", "layout_test", "rendering_without_layout", + when "hello_world", "layout_test", "rendering_without_layout", "rendering_nothing_on_layout", "render_text_hello_world", "render_text_hello_world_with_layout", "hello_world_with_layout_false", @@ -652,11 +620,11 @@ class TestController < ApplicationController "render_with_explicit_string_template", "update_page", "update_page_with_instance_variables" - "layouts/standard" - when "action_talk_to_layout", "layout_overriding_layout" - "layouts/talk_from_action" - when "render_implicit_html_template_from_xhr_request" - (request.xhr? ? 'layouts/xhr' : 'layouts/standard') + "layouts/standard" + when "action_talk_to_layout", "layout_overriding_layout" + "layouts/talk_from_action" + when "render_implicit_html_template_from_xhr_request" + (request.xhr? ? "layouts/xhr" : "layouts/standard") end end end @@ -664,6 +632,124 @@ end class RenderTest < ActionController::TestCase tests TestController + with_routes do + get :"hyphen-ated", to: "test#hyphen-ated" + get :accessing_action_name_in_template, to: "test#accessing_action_name_in_template" + get :accessing_controller_name_in_template, to: "test#accessing_controller_name_in_template" + get :accessing_local_assigns_in_inline_template, to: "test#accessing_local_assigns_in_inline_template" + get :accessing_logger_in_template, to: "test#accessing_logger_in_template" + get :accessing_params_in_template, to: "test#accessing_params_in_template" + get :accessing_params_in_template_with_layout, to: "test#accessing_params_in_template_with_layout" + get :accessing_request_in_template, to: "test#accessing_request_in_template" + get :action_talk_to_layout, to: "test#action_talk_to_layout" + get :builder_layout_test, to: "test#builder_layout_test" + get :builder_partial_test, to: "test#builder_partial_test" + get :clone, to: "test#clone" + get :determine_layout, to: "test#determine_layout" + get :double_redirect, to: "test#double_redirect" + get :double_render, to: "test#double_render" + get :empty_partial_collection, to: "test#empty_partial_collection" + get :formatted_html_erb, to: "test#formatted_html_erb" + get :formatted_xml_erb, to: "test#formatted_xml_erb" + get :greeting, to: "test#greeting" + get :hello_in_a_string, to: "test#hello_in_a_string" + get :hello_world, to: "fun/games#hello_world" + get :hello_world, to: "test#hello_world" + get :hello_world_file, to: "test#hello_world_file" + get :hello_world_from_rxml_using_action, to: "test#hello_world_from_rxml_using_action" + get :hello_world_from_rxml_using_template, to: "test#hello_world_from_rxml_using_template" + get :hello_world_with_layout_false, to: "test#hello_world_with_layout_false" + get :layout_overriding_layout, to: "test#layout_overriding_layout" + get :layout_test, to: "test#layout_test" + get :layout_test_with_different_layout, to: "test#layout_test_with_different_layout" + get :layout_test_with_different_layout_and_string_action, to: "test#layout_test_with_different_layout_and_string_action" + get :layout_test_with_different_layout_and_symbol_action, to: "test#layout_test_with_different_layout_and_symbol_action" + get :missing_partial, to: "test#missing_partial" + get :nested_partial_with_form_builder, to: "fun/games#nested_partial_with_form_builder" + get :new, to: "quiz/questions#new" + get :partial, to: "test#partial" + get :partial_collection, to: "test#partial_collection" + get :partial_collection_shorthand_with_different_types_of_records, to: "test#partial_collection_shorthand_with_different_types_of_records" + get :partial_collection_shorthand_with_locals, to: "test#partial_collection_shorthand_with_locals" + get :partial_collection_with_as, to: "test#partial_collection_with_as" + get :partial_collection_with_as_and_counter, to: "test#partial_collection_with_as_and_counter" + get :partial_collection_with_as_and_iteration, to: "test#partial_collection_with_as_and_iteration" + get :partial_collection_with_counter, to: "test#partial_collection_with_counter" + get :partial_collection_with_iteration, to: "test#partial_collection_with_iteration" + get :partial_collection_with_locals, to: "test#partial_collection_with_locals" + get :partial_collection_with_spacer, to: "test#partial_collection_with_spacer" + get :partial_collection_with_spacer_which_uses_render, to: "test#partial_collection_with_spacer_which_uses_render" + get :partial_formats_html, to: "test#partial_formats_html" + get :partial_hash_collection, to: "test#partial_hash_collection" + get :partial_hash_collection_with_locals, to: "test#partial_hash_collection_with_locals" + get :partial_html_erb, to: "test#partial_html_erb" + get :partial_only, to: "test#partial_only" + get :partial_with_counter, to: "test#partial_with_counter" + get :partial_with_form_builder, to: "test#partial_with_form_builder" + get :partial_with_form_builder_subclass, to: "test#partial_with_form_builder_subclass" + get :partial_with_hash_object, to: "test#partial_with_hash_object" + get :partial_with_locals, to: "test#partial_with_locals" + get :partial_with_nested_object, to: "test#partial_with_nested_object" + get :partial_with_nested_object_shorthand, to: "test#partial_with_nested_object_shorthand" + get :partial_with_string_locals, to: "test#partial_with_string_locals" + get :partials_list, to: "test#partials_list" + get :render_action_hello_world, to: "test#render_action_hello_world" + get :render_action_hello_world_as_string, to: "test#render_action_hello_world_as_string" + get :render_action_hello_world_with_symbol, to: "test#render_action_hello_world_with_symbol" + get :render_action_upcased_hello_world, to: "test#render_action_upcased_hello_world" + get :render_and_redirect, to: "test#render_and_redirect" + get :render_call_to_partial_with_layout, to: "test#render_call_to_partial_with_layout" + get :render_call_to_partial_with_layout_in_main_layout_and_within_content_for_layout, to: "test#render_call_to_partial_with_layout_in_main_layout_and_within_content_for_layout" + get :render_custom_code, to: "test#render_custom_code" + get :render_file_as_string_with_locals, to: "test#render_file_as_string_with_locals" + get :render_file_from_template, to: "test#render_file_from_template" + get :render_file_not_using_full_path, to: "test#render_file_not_using_full_path" + get :render_file_not_using_full_path_with_dot_in_path, to: "test#render_file_not_using_full_path_with_dot_in_path" + get :render_file_using_pathname, to: "test#render_file_using_pathname" + get :render_file_with_instance_variables, to: "test#render_file_with_instance_variables" + get :render_file_with_locals, to: "test#render_file_with_locals" + get :render_hello_world, to: "test#render_hello_world" + get :render_hello_world_from_variable, to: "test#render_hello_world_from_variable" + get :render_hello_world_with_forward_slash, to: "test#render_hello_world_with_forward_slash" + get :render_implicit_html_template_from_xhr_request, to: "test#render_implicit_html_template_from_xhr_request" + get :render_implicit_js_template_without_layout, to: "test#render_implicit_js_template_without_layout" + get :render_line_offset, to: "test#render_line_offset" + get :render_nothing_with_appendix, to: "test#render_nothing_with_appendix" + get :render_template_in_top_directory, to: "test#render_template_in_top_directory" + get :render_template_in_top_directory_with_slash, to: "test#render_template_in_top_directory_with_slash" + get :render_template_within_a_template_with_other_format, to: "test#render_template_within_a_template_with_other_format" + get :render_text_hello_world, to: "test#render_text_hello_world" + get :render_text_hello_world_with_layout, to: "test#render_text_hello_world_with_layout" + get :render_text_with_assigns, to: "test#render_text_with_assigns" + get :render_text_with_false, to: "test#render_text_with_false" + get :render_text_with_nil, to: "test#render_text_with_nil" + get :render_text_with_resource, to: "test#render_text_with_resource" + get :render_to_string_and_render, to: "test#render_to_string_and_render" + get :render_to_string_and_render_with_different_formats, to: "test#render_to_string_and_render_with_different_formats" + get :render_to_string_test, to: "test#render_to_string_test" + get :render_to_string_with_assigns, to: "test#render_to_string_with_assigns" + get :render_to_string_with_caught_exception, to: "test#render_to_string_with_caught_exception" + get :render_to_string_with_exception, to: "test#render_to_string_with_exception" + get :render_to_string_with_inline_and_render, to: "test#render_to_string_with_inline_and_render" + get :render_to_string_with_partial, to: "test#render_to_string_with_partial" + get :render_to_string_with_template_and_html_partial, to: "test#render_to_string_with_template_and_html_partial" + get :render_using_layout_around_block, to: "test#render_using_layout_around_block" + get :render_using_layout_around_block_in_main_layout_and_within_content_for_layout, to: "test#render_using_layout_around_block_in_main_layout_and_within_content_for_layout" + get :render_with_assigns_option, to: "test#render_with_assigns_option" + get :render_with_explicit_escaped_template, to: "test#render_with_explicit_escaped_template" + get :render_with_explicit_string_template, to: "test#render_with_explicit_string_template" + get :render_with_explicit_template, to: "test#render_with_explicit_template" + get :render_with_explicit_template_with_locals, to: "test#render_with_explicit_template_with_locals" + get :render_with_explicit_unescaped_template, to: "test#render_with_explicit_unescaped_template" + get :render_with_filters, to: "test#render_with_filters" + get :render_xml_hello, to: "test#render_xml_hello" + get :render_xml_hello_as_string_template, to: "test#render_xml_hello_as_string_template" + get :rendering_nothing_on_layout, to: "test#rendering_nothing_on_layout" + get :rendering_with_conflicting_local_vars, to: "test#rendering_with_conflicting_local_vars" + get :rendering_without_layout, to: "test#rendering_without_layout" + get :yield_content_for, to: "test#yield_content_for" + end + def setup # enable a logger so that (e.g.) the benchmarking stuff runs, so we can get # a more accurate simulation of what happens in "real life". @@ -672,10 +758,15 @@ class RenderTest < ActionController::TestCase ActionView::Base.logger = ActiveSupport::Logger.new(nil) @request.host = "www.nextangle.com" + + @old_view_paths = ActionController::Base.view_paths + ActionController::Base.view_paths = File.join(FIXTURE_LOAD_PATH, "actionpack") end def teardown ActionView::Base.logger = nil + + ActionController::Base.view_paths = @old_view_paths end # :ported: @@ -726,11 +817,6 @@ class RenderTest < ActionController::TestCase assert_equal "Elastica", @response.body end - def test_render_process - get :render_action_hello_world_as_string - assert_equal ["Hello world!"], @controller.process(:render_action_hello_world_as_string) - end - # :ported: def test_render_from_variable get :render_hello_world_from_variable @@ -770,13 +856,13 @@ class RenderTest < ActionController::TestCase # :ported: def test_do_with_render_text_and_layout get :render_text_hello_world_with_layout - assert_equal "<html>hello world, I am here!</html>", @response.body + assert_equal "{{hello world, I am here!}}\n", @response.body end # :ported: def test_do_with_render_action_and_layout_false get :hello_world_with_layout_false - assert_equal 'Hello world!', @response.body + assert_equal "Hello world!", @response.body end # :ported: @@ -831,27 +917,27 @@ class RenderTest < ActionController::TestCase get :render_custom_code assert_response 404 assert_response :missing - assert_equal 'hello world', @response.body + assert_equal "hello world", @response.body end # :ported: def test_render_text_with_nil get :render_text_with_nil assert_response 200 - assert_equal '', @response.body + assert_equal "", @response.body end # :ported: def test_render_text_with_false get :render_text_with_false - assert_equal 'false', @response.body + assert_equal "false", @response.body end # :ported: def test_render_nothing_with_appendix get :render_nothing_with_appendix assert_response 200 - assert_equal 'appended', @response.body + assert_equal "appended", @response.body end def test_render_text_with_resource @@ -946,7 +1032,7 @@ class RenderTest < ActionController::TestCase def test_render_to_string_inline get :render_to_string_with_inline_and_render - assert_equal 'Hello world!', @response.body + assert_equal "Hello world!", @response.body end # :ported: @@ -979,24 +1065,24 @@ class RenderTest < ActionController::TestCase def test_should_render_formatted_template get :formatted_html_erb - assert_equal 'formatted html erb', @response.body + assert_equal "formatted html erb", @response.body end def test_should_render_formatted_html_erb_template get :formatted_xml_erb - assert_equal '<test>passed formatted html erb</test>', @response.body + assert_equal "<test>passed formatted html erb</test>", @response.body end def test_should_render_formatted_html_erb_template_with_bad_accepts_header @request.env["HTTP_ACCEPT"] = "; a=dsf" get :formatted_xml_erb - assert_equal '<test>passed formatted html erb</test>', @response.body + assert_equal "<test>passed formatted html erb</test>", @response.body end def test_should_render_formatted_html_erb_template_with_faulty_accepts_header @request.accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*" get :formatted_xml_erb - assert_equal '<test>passed formatted html erb</test>', @response.body + assert_equal "<test>passed formatted html erb</test>", @response.body end def test_layout_test_with_different_layout @@ -1026,7 +1112,7 @@ class RenderTest < ActionController::TestCase def test_rendering_nothing_on_layout get :rendering_nothing_on_layout - assert_equal '', @response.body + assert_equal "", @response.body end def test_render_to_string_doesnt_break_assigns @@ -1108,7 +1194,7 @@ class RenderTest < ActionController::TestCase def test_render_text_with_assigns_option get :render_with_assigns_option - assert_equal 'world', response.body + assert_equal "world", response.body end # :ported: @@ -1122,7 +1208,7 @@ class RenderTest < ActionController::TestCase assert_equal "<title>Putting stuff in the title!</title>\nGreat stuff!\n", @response.body end - def test_overwritting_rendering_relative_file_with_extension + def test_overwriting_rendering_relative_file_with_extension get :hello_world_from_rxml_using_template assert_equal "<html>\n <p>Hello</p>\n</html>\n", @response.body @@ -1206,6 +1292,11 @@ class RenderTest < ActionController::TestCase assert_equal "Hello: david", @response.body end + def test_partial_with_string_locals + get :partial_with_string_locals + assert_equal "Hello: david", @response.body + end + def test_partial_with_form_builder get :partial_with_form_builder assert_equal "<label for=\"post_title\">Title</label>\n", @response.body diff --git a/actionview/test/actionpack/controller/view_paths_test.rb b/actionview/test/actionpack/controller/view_paths_test.rb index 2dd27358f7..7f3fe0fa08 100644 --- a/actionview/test/actionpack/controller/view_paths_test.rb +++ b/actionview/test/actionpack/controller/view_paths_test.rb @@ -1,4 +1,6 @@ -require 'abstract_unit' +# frozen_string_literal: true + +require "abstract_unit" class ViewLoadPathsTest < ActionController::TestCase class TestController < ActionController::Base @@ -7,7 +9,7 @@ class ViewLoadPathsTest < ActionController::TestCase before_action :add_view_path, only: :hello_world_at_request_time def hello_world() end - def hello_world_at_request_time() render(:action => 'hello_world') end + def hello_world_at_request_time() render(action: "hello_world") end private def add_view_path @@ -17,16 +19,22 @@ class ViewLoadPathsTest < ActionController::TestCase module Test class SubController < ActionController::Base - layout 'test/sub' - def hello_world; render(:template => 'test/hello_world'); end + layout "test/sub" + def hello_world; render(template: "test/hello_world"); end end end + with_routes do + get :hello_world, to: "test#hello_world" + get :hello_world_at_request_time, to: "test#hello_world_at_request_time" + end + def setup - @request = ActionController::TestRequest.create - @response = ActionController::TestResponse.new @controller = TestController.new + @request = ActionController::TestRequest.create(@controller.class) + @response = ActionDispatch::TestResponse.new @paths = TestController.view_paths + super end def teardown @@ -34,7 +42,7 @@ class ViewLoadPathsTest < ActionController::TestCase end def expand(array) - array.map {|x| File.expand_path(x.to_s)} + array.map { |x| File.expand_path(x.to_s) } end def assert_paths(*paths) @@ -47,7 +55,7 @@ class ViewLoadPathsTest < ActionController::TestCase end def test_controller_appends_view_path_correctly - @controller.append_view_path 'foo' + @controller.append_view_path "foo" assert_paths(FIXTURE_LOAD_PATH, "foo") @controller.append_view_path(%w(bar baz)) @@ -58,7 +66,7 @@ class ViewLoadPathsTest < ActionController::TestCase end def test_controller_prepends_view_path_correctly - @controller.prepend_view_path 'baz' + @controller.prepend_view_path "baz" assert_paths("baz", FIXTURE_LOAD_PATH) @controller.prepend_view_path(%w(foo bar)) @@ -72,7 +80,7 @@ class ViewLoadPathsTest < ActionController::TestCase @controller.instance_variable_set :@template, ActionView::Base.new(TestController.view_paths, {}, @controller) class_view_paths = TestController.view_paths - @controller.append_view_path 'foo' + @controller.append_view_path "foo" assert_paths FIXTURE_LOAD_PATH, "foo" @controller.append_view_path(%w(bar baz)) @@ -84,7 +92,7 @@ class ViewLoadPathsTest < ActionController::TestCase @controller.instance_variable_set :@template, ActionView::Base.new(TestController.view_paths, {}, @controller) class_view_paths = TestController.view_paths - @controller.prepend_view_path 'baz' + @controller.prepend_view_path "baz" assert_paths "baz", FIXTURE_LOAD_PATH @controller.prepend_view_path(%w(foo bar)) @@ -107,6 +115,10 @@ class ViewLoadPathsTest < ActionController::TestCase def test_view_paths_override_for_layouts_in_controllers_with_a_module @controller = Test::SubController.new + with_routes do + get :hello_world, to: "view_load_paths_test/test/sub#hello_world" + end + Test::SubController.view_paths = [ "#{FIXTURE_LOAD_PATH}/override", FIXTURE_LOAD_PATH, "#{FIXTURE_LOAD_PATH}/override2" ] get :hello_world assert_response :success @@ -131,10 +143,8 @@ class ViewLoadPathsTest < ActionController::TestCase "Decorated body", template.identifier, template.handler, - { - :virtual_path => template.virtual_path, - :format => template.formats - } + virtual_path: template.virtual_path, + format: template.formats ) end end @@ -157,14 +167,14 @@ class ViewLoadPathsTest < ActionController::TestCase class C < ActionController::Base; end } - A.view_paths = ['a/path'] + A.view_paths = ["a/path"] assert_paths A, "a/path" assert_paths A, *B.view_paths assert_paths C, *original_load_paths C.view_paths = [] - assert_nothing_raised { C.append_view_path 'c/path' } + assert_nothing_raised { C.append_view_path "c/path" } assert_paths C, "c/path" end |