Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Layouts work in AbstractController. Add support for the rspec runner …

…for T::U
  • Loading branch information...
commit 95c9718118bc0342ddb320f23b5e0a17fb12b7ad 1 parent c1aa5b0
@wycats wycats authored
View
24 actionpack/lib/action_controller/abstract/layouts.rb
@@ -13,6 +13,10 @@ def layout(layout)
_write_layout_method
end
+ def _implied_layout_name
+ name.underscore
+ end
+
def _write_layout_method
case @_layout
when String
@@ -24,8 +28,8 @@ def _write_layout_method
else
self.class_eval %{
def _layout
- if view_paths.find_by_parts?("#{controller_path}", formats, "layouts")
- "#{controller_path}"
+ if view_paths.find_by_parts?("#{_implied_layout_name}", formats, "layouts")
+ "#{_implied_layout_name}"
else
super
end
@@ -45,9 +49,12 @@ def _layout() end # This will be overwritten
def _layout_for_option(name)
case name
- when String then _layout_for_name(name)
- when true then _default_layout(true)
- when false then nil
+ when String then _layout_for_name(name)
+ when true then _default_layout(true)
+ when false, nil then nil
+ else
+ raise ArgumentError,
+ "String, true, or false, expected for `layout'; you passed #{name.inspect}"
end
end
@@ -56,7 +63,12 @@ def _layout_for_name(name)
end
def _default_layout(require_layout = false)
- _layout_for_option(_layout)
+ begin
+ _layout_for_option(_layout)
+ rescue NameError => e
+ raise NoMethodError,
+ "You specified #{@_layout.inspect} as the layout, but no such method was found"
+ end
end
end
end
View
119 actionpack/test/abstract_controller/layouts_test.rb
@@ -9,9 +9,12 @@ class Base < AbstractController::Base
use AbstractController::Layouts
self.view_paths = [ActionView::FixtureTemplate::FixturePath.new(
- "layouts/hello.erb" => "With String <%= yield %>",
- "layouts/omg.erb" => "OMGHI2U <%= yield %>",
- "layouts/with_false_layout.erb" => "False Layout <%= yield %>"
+ "layouts/hello.erb" => "With String <%= yield %>",
+ "layouts/hello_override.erb" => "With Override <%= yield %>",
+ "layouts/abstract_controller_tests/layouts/with_string_implied_child.erb" =>
+ "With Implied <%= yield %>",
+ "layouts/omg.erb" => "OMGHI2U <%= yield %>",
+ "layouts/with_false_layout.erb" => "False Layout <%= yield %>"
)]
def self.controller_path
@@ -42,6 +45,23 @@ def index
end
end
+ class WithStringChild < WithString
+ end
+
+ class WithStringOverriddenChild < WithString
+ layout "hello_override"
+ end
+
+ class WithNilChild < WithString
+ layout nil
+ end
+
+ class WithStringImpliedChild < WithString
+ end
+
+ class WithChildOfImplied < WithStringImpliedChild
+ end
+
class WithSymbol < Base
layout :hello
@@ -66,6 +86,36 @@ def no_hello
end
end
+ class WithSymbolReturningNil < Base
+ layout :nilz
+
+ def index
+ render :_template => ActionView::TextTemplate.new("Hello nilz!")
+ end
+
+ def nilz() end
+ end
+
+ class WithSymbolReturningObj < Base
+ layout :objekt
+
+ def index
+ render :_template => ActionView::TextTemplate.new("Hello nilz!")
+ end
+
+ def objekt
+ Object.new
+ end
+ end
+
+ class WithSymbolAndNoMethod < Base
+ layout :omg_no_method
+
+ def index
+ render :_template => ActionView::TextTemplate.new("Hello boom!")
+ end
+ end
+
class WithMissingLayout < Base
layout "missing"
@@ -82,6 +132,24 @@ def index
end
end
+ class WithNilLayout < Base
+ layout nil
+
+ def index
+ render :_template => ActionView::TextTemplate.new("Hello nil!")
+ end
+ end
+
+ # TODO Move to bootloader
+ AbstractController::Base.subclasses.each do |klass|
+ klass = klass.constantize
+ next unless klass < AbstractController::Layouts
+ p klass
+ klass.class_eval do
+ _write_layout_method
+ end
+ end
+
class TestBase < ActiveSupport::TestCase
test "when no layout is specified, and no default is available, render without a layout" do
result = Blank.process(:index)
@@ -103,7 +171,8 @@ class TestBase < ActiveSupport::TestCase
end
test "when layout is specified as nil, do not use a layout" do
- pending
+ result = WithNilLayout.process(:index)
+ assert_equal "Hello nil!", result.response_obj[:body]
end
test "when layout is specified as a symbol, call the requested method and use the layout returned" do
@@ -112,47 +181,43 @@ class TestBase < ActiveSupport::TestCase
end
test "when layout is specified as a symbol and the method returns nil, don't use a layout" do
- pending
+ result = WithSymbolReturningNil.process(:index)
+ assert_equal "Hello nilz!", result.response_obj[:body]
end
test "when the layout is specified as a symbol and the method doesn't exist, raise an exception" do
- pending
+ assert_raises(NoMethodError, /:nilz/) { WithSymbolAndNoMethod.process(:index) }
end
test "when the layout is specified as a symbol and the method returns something besides a string/false/nil, raise an exception" do
- pending
+ assert_raises(ArgumentError) { WithSymbolReturningObj.process(:index) }
end
test "when a child controller does not have a layout, use the parent controller layout" do
- pending
+ result = WithStringChild.process(:index)
+ assert_equal "With String Hello string!", result.response_obj[:body]
end
test "when a child controller has specified a layout, use that layout and not the parent controller layout" do
- pending
+ result = WithStringOverriddenChild.process(:index)
+ assert_equal "With Override Hello string!", result.response_obj[:body]
end
test "when a child controller has an implied layout, use that layout and not the parent controller layout" do
- pending
+ result = WithStringImpliedChild.process(:index)
+ assert_equal "With Implied Hello string!", result.response_obj[:body]
end
test "when a child controller specifies layout nil, do not use the parent layout" do
- pending
- end
-
- test "when a child controller has an implied layout, use that layout instead of the parent controller layout" do
- pending
- end
-
- 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
- pending
- end
-
- test "Raise ArgumentError if layout is called with a bad argument" do
- pending
- end
+ result = WithNilChild.process(:index)
+ assert_equal "Hello string!", result.response_obj[:body]
+ end
+
+ 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
+ result = WithChildOfImplied.process(:index)
+ assert_equal "With Implied Hello string!", result.response_obj[:body]
+ end
end
end
end
View
19 actionpack/test/new_base/test_helper.rb
@@ -70,24 +70,7 @@ class Rack::TestCase < ActiveSupport::TestCase
ActionController::Routing.use_controllers!(controllers)
end
-
- unless method_defined?(:describe)
- def self.describe(text)
- class_eval <<-RUBY_EVAL
- def self.name
- "#{text}"
- end
- RUBY_EVAL
- end
- end
-
- if defined?(Spec)
- class << self
- undef test
- alias_method :test, :it
- end
- end
-
+
def app
@app ||= ActionController::Dispatcher.new
end
View
49 activesupport/lib/active_support/testing/declarative.rb
@@ -1,18 +1,43 @@
module ActiveSupport
module Testing
module Declarative
- # test "verify something" do
- # ...
- # end
- def test(name, &block)
- test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym
- defined = instance_method(test_name) rescue false
- raise "#{test_name} is already defined in #{self}" if defined
- if block_given?
- define_method(test_name, &block)
- else
- define_method(test_name) do
- flunk "No implementation provided for #{name}"
+
+ def self.extended(klass)
+ klass.class_eval do
+
+ unless method_defined?(:describe)
+ def self.describe(text)
+ class_eval <<-RUBY_EVAL
+ def self.name
+ "#{text}"
+ end
+ RUBY_EVAL
+ end
+ end
+
+ if defined?(Spec)
+ class << self
+ alias_method :test, :it
+ end
+ end
+
+ end
+ end
+
+ unless defined?(Spec)
+ # test "verify something" do
+ # ...
+ # end
+ def test(name, &block)
+ test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym
+ defined = instance_method(test_name) rescue false
+ raise "#{test_name} is already defined in #{self}" if defined
+ if block_given?
+ define_method(test_name, &block)
+ else
+ define_method(test_name) do
+ flunk "No implementation provided for #{name}"
+ end
end
end
end
View
45 activesupport/lib/active_support/testing/pending.rb
@@ -5,31 +5,34 @@ module ActiveSupport
module Testing
module Pending
- @@pending_cases = []
- @@at_exit = false
+ unless defined?(Spec)
- def pending(description = "", &block)
- if block_given?
- failed = false
+ @@pending_cases = []
+ @@at_exit = false
- begin
- block.call
- rescue
- failed = true
- end
+ def pending(description = "", &block)
+ if block_given?
+ failed = false
- flunk("<#{description}> did not fail.") unless failed
- end
+ begin
+ block.call
+ rescue
+ failed = true
+ end
- caller[0] =~ (/(.*):(.*):in `(.*)'/)
- @@pending_cases << "#{$3} at #{$1}, line #{$2}"
- print "P"
-
- @@at_exit ||= begin
- at_exit do
- puts "\nPending Cases:"
- @@pending_cases.each do |test_case|
- puts test_case
+ flunk("<#{description}> did not fail.") unless failed
+ end
+
+ caller[0] =~ (/(.*):(.*):in `(.*)'/)
+ @@pending_cases << "#{$3} at #{$1}, line #{$2}"
+ print "P"
+
+ @@at_exit ||= begin
+ at_exit do
+ puts "\nPending Cases:"
+ @@pending_cases.each do |test_case|
+ puts test_case
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.