mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add more test coverage to layouts
[Rafael Mendonça França + Nick Sutterer + thedarkone]
This commit is contained in:
parent
0ce7eae741
commit
ce5538be68
1 changed files with 152 additions and 4 deletions
|
@ -49,6 +49,10 @@ module AbstractControllerTests
|
|||
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
|
||||
end
|
||||
|
@ -92,7 +96,7 @@ module AbstractControllerTests
|
|||
end
|
||||
end
|
||||
|
||||
class WithProcReturningNil < Base
|
||||
class WithProcReturningNil < WithString
|
||||
layout proc { nil }
|
||||
|
||||
def index
|
||||
|
@ -100,6 +104,14 @@ 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" }
|
||||
|
||||
|
@ -199,6 +211,14 @@ module AbstractControllerTests
|
|||
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
|
||||
|
||||
|
@ -211,6 +231,45 @@ module AbstractControllerTests
|
|||
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
|
||||
|
@ -299,10 +358,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
|
||||
|
@ -363,12 +428,24 @@ module AbstractControllerTests
|
|||
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
|
||||
"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
|
||||
assert_raises ArgumentError do
|
||||
Object.class_eval do
|
||||
|
@ -391,6 +468,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)
|
||||
|
@ -403,6 +504,42 @@ 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
|
||||
|
@ -414,6 +551,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
|
||||
|
|
Loading…
Reference in a new issue