diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2009-09-21 21:14:04 +0100 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-09-21 21:14:04 +0100 |
commit | 340be9bddd8e5902e0218a0101a40a17a4afd558 (patch) | |
tree | ef4de25f3f8eb610dc2235f0762b01cb1d464efd /actionpack/test/abstract | |
parent | b31cdb55422226cd45a2234a4b54986f1f611151 (diff) | |
parent | 1bbb9b2db05730194edfd7d2cef9f5fcb9d79e50 (diff) | |
download | rails-340be9bddd8e5902e0218a0101a40a17a4afd558.tar.gz rails-340be9bddd8e5902e0218a0101a40a17a4afd558.tar.bz2 rails-340be9bddd8e5902e0218a0101a40a17a4afd558.zip |
Merge commit 'mainstream/master'
Diffstat (limited to 'actionpack/test/abstract')
14 files changed, 772 insertions, 0 deletions
diff --git a/actionpack/test/abstract/abstract_controller_test.rb b/actionpack/test/abstract/abstract_controller_test.rb new file mode 100644 index 0000000000..524381509d --- /dev/null +++ b/actionpack/test/abstract/abstract_controller_test.rb @@ -0,0 +1,245 @@ +require 'abstract_unit' + +module AbstractController + module Testing + + # Test basic dispatching. + # ==== + # * Call process + # * Test that the response_body is set correctly + class SimpleController < AbstractController::Base + end + + class Me < SimpleController + def index + self.response_body = "Hello world" + "Something else" + end + end + + class TestBasic < ActiveSupport::TestCase + test "dispatching works" do + controller = Me.new + controller.process(:index) + assert_equal "Hello world", controller.response_body + end + end + + # Test Render mixin + # ==== + class RenderingController < AbstractController::Base + include ::AbstractController::RenderingController + + def _prefix() end + + def render(options = {}) + if options.is_a?(String) + options = {:_template_name => options} + end + + options[:_prefix] = _prefix + super + end + + append_view_path File.expand_path(File.join(File.dirname(__FILE__), "views")) + end + + class Me2 < RenderingController + def index + render "index.erb" + end + + def action_with_ivars + @my_ivar = "Hello" + render "action_with_ivars.erb" + end + + def naked_render + render + end + + def rendering_to_body + self.response_body = render_to_body :_template_name => "naked_render.erb" + end + + def rendering_to_string + self.response_body = render_to_string :_template_name => "naked_render.erb" + end + end + + class TestRenderingController < ActiveSupport::TestCase + def setup + @controller = Me2.new + end + + test "rendering templates works" do + @controller.process(:index) + assert_equal "Hello from index.erb", @controller.response_body + end + + test "rendering passes ivars to the view" do + @controller.process(:action_with_ivars) + assert_equal "Hello from index_with_ivars.erb", @controller.response_body + end + + test "rendering with no template name" do + @controller.process(:naked_render) + assert_equal "Hello from naked_render.erb", @controller.response_body + end + + test "rendering to a rack body" do + @controller.process(:rendering_to_body) + assert_equal "Hello from naked_render.erb", @controller.response_body + end + + test "rendering to a string" do + @controller.process(:rendering_to_string) + assert_equal "Hello from naked_render.erb", @controller.response_body + end + end + + # Test rendering with prefixes + # ==== + # * self._prefix is used when defined + class PrefixedViews < RenderingController + private + def self.prefix + name.underscore + end + + def _prefix + self.class.prefix + end + end + + class Me3 < PrefixedViews + def index + render + end + + def formatted + self.formats = [:html] + render + end + end + + class TestPrefixedViews < ActiveSupport::TestCase + def setup + @controller = Me3.new + end + + test "templates are located inside their 'prefix' folder" do + @controller.process(:index) + assert_equal "Hello from me3/index.erb", @controller.response_body + end + + test "templates included their format" do + @controller.process(:formatted) + assert_equal "Hello from me3/formatted.html.erb", @controller.response_body + end + end + + # Test rendering with layouts + # ==== + # self._layout is used when defined + class WithLayouts < PrefixedViews + include Layouts + + private + def self.layout(formats) + begin + find_template(name.underscore, {:formats => formats}, :_prefix => "layouts") + rescue ActionView::MissingTemplate + begin + find_template("application", {:formats => formats}, :_prefix => "layouts") + rescue ActionView::MissingTemplate + end + end + end + + def render_to_body(options = {}) + options[:_layout] = options[:layout] || _default_layout({}) + super + end + end + + class Me4 < WithLayouts + def index + render + end + end + + class Me5 < WithLayouts + def index + render + end + end + + class TestLayouts < ActiveSupport::TestCase + test "layouts are included" do + controller = Me4.new + result = controller.process(:index) + assert_equal "Me4 Enter : Hello from me4/index.erb : Exit", controller.response_body + end + end + + # respond_to_action?(action_name) + # ==== + # * A method can be used as an action only if this method + # returns true when passed the method name as an argument + # * Defaults to true in AbstractController + class DefaultRespondToActionController < AbstractController::Base + def index() self.response_body = "success" end + end + + class ActionMissingRespondToActionController < AbstractController::Base + # No actions + private + def action_missing(action_name) + self.response_body = "success" + end + end + + class RespondToActionController < AbstractController::Base; + def index() self.response_body = "success" end + + def fail() self.response_body = "fail" end + + private + + def method_for_action(action_name) + action_name.to_s != "fail" && action_name + end + end + + class TestRespondToAction < ActiveSupport::TestCase + + def assert_dispatch(klass, body = "success", action = :index) + controller = klass.new + controller.process(action) + assert_equal body, controller.response_body + end + + test "an arbitrary method is available as an action by default" do + assert_dispatch DefaultRespondToActionController, "success", :index + end + + test "raises ActionNotFound when method does not exist and action_missing is not defined" do + assert_raise(ActionNotFound) { DefaultRespondToActionController.new.process(:fail) } + end + + test "dispatches to action_missing when method does not exist and action_missing is defined" do + assert_dispatch ActionMissingRespondToActionController, "success", :ohai + end + + test "a method is available as an action if respond_to_action? returns true" do + assert_dispatch RespondToActionController, "success", :index + end + + test "raises ActionNotFound if method is defined but respond_to_action? returns false" do + assert_raise(ActionNotFound) { RespondToActionController.new.process(:fail) } + end + end + + end +end diff --git a/actionpack/test/abstract/callbacks_test.rb b/actionpack/test/abstract/callbacks_test.rb new file mode 100644 index 0000000000..0ce1dc506b --- /dev/null +++ b/actionpack/test/abstract/callbacks_test.rb @@ -0,0 +1,238 @@ +require 'abstract_unit' + +module AbstractController + module Testing + + class ControllerWithCallbacks < AbstractController::Base + include AbstractController::Callbacks + end + + class Callback1 < ControllerWithCallbacks + set_callback :process_action, :before, :first + + def first + @text = "Hello world" + end + + def index + self.response_body = @text + end + end + + class TestCallbacks1 < ActiveSupport::TestCase + test "basic callbacks work" do + controller = Callback1.new + result = controller.process(:index) + assert_equal "Hello world", controller.response_body + end + end + + class Callback2 < ControllerWithCallbacks + before_filter :first + after_filter :second + around_filter :aroundz + + def first + @text = "Hello world" + end + + def second + @second = "Goodbye" + end + + def aroundz + @aroundz = "FIRST" + yield + @aroundz << "SECOND" + end + + def index + self.response_body = @text + end + end + + class TestCallbacks2 < ActiveSupport::TestCase + def setup + @controller = Callback2.new + end + + test "before_filter works" do + result = @controller.process(:index) + assert_equal "Hello world", @controller.response_body + end + + test "after_filter works" do + @controller.process(:index) + assert_equal "Goodbye", @controller.instance_variable_get("@second") + end + + test "around_filter works" do + @controller.process(:index) + assert_equal "FIRSTSECOND", @controller.instance_variable_get("@aroundz") + end + end + + class Callback3 < ControllerWithCallbacks + before_filter do |c| + c.instance_variable_set("@text", "Hello world") + end + + after_filter do |c| + c.instance_variable_set("@second", "Goodbye") + end + + def index + self.response_body = @text + end + end + + class TestCallbacks3 < ActiveSupport::TestCase + def setup + @controller = Callback3.new + end + + test "before_filter works with procs" do + result = @controller.process(:index) + assert_equal "Hello world", @controller.response_body + end + + test "after_filter works with procs" do + result = @controller.process(:index) + assert_equal "Goodbye", @controller.instance_variable_get("@second") + end + end + + class CallbacksWithConditions < ControllerWithCallbacks + before_filter :list, :only => :index + before_filter :authenticate, :except => :index + + def index + self.response_body = @list.join(", ") + end + + def sekrit_data + self.response_body = (@list + [@authenticated]).join(", ") + end + + private + def list + @list = ["Hello", "World"] + end + + def authenticate + @list = [] + @authenticated = "true" + end + end + + class TestCallbacksWithConditions < ActiveSupport::TestCase + def setup + @controller = CallbacksWithConditions.new + end + + test "when :only is specified, a before filter is triggered on that action" do + @controller.process(:index) + assert_equal "Hello, World", @controller.response_body + end + + test "when :only is specified, a before filter is not triggered on other actions" do + @controller.process(:sekrit_data) + assert_equal "true", @controller.response_body + end + + test "when :except is specified, an after filter is not triggered on that action" do + result = @controller.process(:index) + assert_nil @controller.instance_variable_get("@authenticated") + end + end + + class CallbacksWithArrayConditions < ControllerWithCallbacks + before_filter :list, :only => [:index, :listy] + before_filter :authenticate, :except => [:index, :listy] + + def index + self.response_body = @list.join(", ") + end + + def sekrit_data + self.response_body = (@list + [@authenticated]).join(", ") + end + + private + def list + @list = ["Hello", "World"] + end + + def authenticate + @list = [] + @authenticated = "true" + end + end + + class TestCallbacksWithArrayConditions < ActiveSupport::TestCase + def setup + @controller = CallbacksWithArrayConditions.new + end + + test "when :only is specified with an array, a before filter is triggered on that action" do + result = @controller.process(:index) + assert_equal "Hello, World", @controller.response_body + end + + test "when :only is specified with an array, a before filter is not triggered on other actions" do + result = @controller.process(:sekrit_data) + assert_equal "true", @controller.response_body + end + + test "when :except is specified with an array, an after filter is not triggered on that action" do + result = @controller.process(:index) + assert_nil @controller.instance_variable_get("@authenticated") + end + end + + class ChangedConditions < Callback2 + before_filter :first, :only => :index + + def not_index + self.response_body = @text.to_s + end + end + + class TestCallbacksWithChangedConditions < ActiveSupport::TestCase + def setup + @controller = ChangedConditions.new + end + + test "when a callback is modified in a child with :only, it works for the :only action" do + result = @controller.process(:index) + assert_equal "Hello world", @controller.response_body + end + + test "when a callback is modified in a child with :only, it does not work for other actions" do + result = @controller.process(:not_index) + assert_equal "", @controller.response_body + end + end + + class SetsResponseBody < ControllerWithCallbacks + before_filter :set_body + + def index + self.response_body = "Fail" + end + + def set_body + self.response_body = "Success" + end + end + + class TestHalting < ActiveSupport::TestCase + test "when a callback sets the response body, the action should not be invoked" do + controller = SetsResponseBody.new + controller.process(:index) + assert_equal "Success", controller.response_body + end + end + + end +end diff --git a/actionpack/test/abstract/helper_test.rb b/actionpack/test/abstract/helper_test.rb new file mode 100644 index 0000000000..5a363c9aa5 --- /dev/null +++ b/actionpack/test/abstract/helper_test.rb @@ -0,0 +1,44 @@ +require 'abstract_unit' + +module AbstractController + module Testing + + class ControllerWithHelpers < AbstractController::Base + include AbstractController::RenderingController + include Helpers + + def render(string) + super(:_template_name => string) + end + + append_view_path File.expand_path(File.join(File.dirname(__FILE__), "views")) + end + + module HelperyTest + def included_method + "Included" + end + end + + class MyHelpers1 < ControllerWithHelpers + helper(HelperyTest) do + def helpery_test + "World" + end + end + + def index + render "helper_test.erb" + end + end + + class TestHelpers < ActiveSupport::TestCase + def test_helpers + controller = MyHelpers1.new + controller.process(:index) + assert_equal "Hello World : Included", controller.response_body + end + end + + end +end diff --git a/actionpack/test/abstract/layouts_test.rb b/actionpack/test/abstract/layouts_test.rb new file mode 100644 index 0000000000..453d31826e --- /dev/null +++ b/actionpack/test/abstract/layouts_test.rb @@ -0,0 +1,235 @@ +require 'abstract_unit' +require 'active_support/core_ext/class/removal' + +module AbstractControllerTests + module Layouts + + # Base controller for these tests + class Base < AbstractController::Base + include AbstractController::RenderingController + include AbstractController::Layouts + + self.view_paths = [ActionView::FixtureResolver.new( + "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 + @controller_path ||= self.name.sub(/Controller$/, '').underscore + end + + def controller_path() self.class.controller_path end + + def render_to_body(options) + options[:_layout] = _default_layout({}) + super + end + end + + class Blank < Base + self.view_paths = [] + + def index + render :_template => ActionView::TextTemplate.new("Hello blank!") + end + end + + class WithString < Base + layout "hello" + + def index + render :_template => ActionView::TextTemplate.new("Hello string!") + 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 + + def index + render :_template => ActionView::TextTemplate.new("Hello symbol!") + end + private + def hello + "omg" + end + end + + class WithSymbolReturningString < Base + layout :no_hello + + def index + render :_template => ActionView::TextTemplate.new("Hello missing symbol!") + end + private + def no_hello + nil + 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" + + def index + render :_template => ActionView::TextTemplate.new("Hello missing!") + end + end + + class WithFalseLayout < Base + layout false + + def index + render :_template => ActionView::TextTemplate.new("Hello false!") + end + end + + class WithNilLayout < Base + layout nil + + def index + render :_template => ActionView::TextTemplate.new("Hello nil!") + end + end + + class TestBase < ActiveSupport::TestCase + test "when no layout is specified, and no default is available, render without a layout" do + controller = Blank.new + controller.process(:index) + assert_equal "Hello blank!", controller.response_body + end + + test "when layout is specified as a string, render with that layout" do + controller = WithString.new + controller.process(:index) + assert_equal "With String Hello string!", controller.response_body + end + + test "when layout is specified as a string, but the layout is missing, raise an exception" do + assert_raises(ActionView::MissingTemplate) { WithMissingLayout.new.process(:index) } + end + + test "when layout is specified as false, do not use a layout" do + controller = WithFalseLayout.new + controller.process(:index) + assert_equal "Hello false!", controller.response_body + end + + test "when layout is specified as nil, do not use a layout" do + controller = WithNilLayout.new + controller.process(:index) + assert_equal "Hello nil!", controller.response_body + end + + test "when layout is specified as a symbol, call the requested method and use the layout returned" do + controller = WithSymbol.new + controller.process(:index) + assert_equal "OMGHI2U Hello symbol!", controller.response_body + end + + test "when layout is specified as a symbol and the method returns nil, don't use a layout" do + controller = WithSymbolReturningNil.new + controller.process(:index) + assert_equal "Hello nilz!", controller.response_body + end + + test "when the layout is specified as a symbol and the method doesn't exist, raise an exception" do + assert_raises(NoMethodError, /:nilz/) { WithSymbolAndNoMethod.new.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 + assert_raises(ArgumentError) { WithSymbolReturningObj.new.process(:index) } + end + + test "when a child controller does not have a layout, use the parent controller layout" do + controller = WithStringChild.new + controller.process(:index) + assert_equal "With String Hello string!", controller.response_body + end + + test "when a child controller has specified a layout, use that layout and not the parent controller layout" do + controller = WithStringOverriddenChild.new + controller.process(:index) + assert_equal "With Override Hello string!", controller.response_body + end + + test "when a child controller has an implied layout, use that layout and not the parent controller layout" do + controller = WithStringImpliedChild.new + controller.process(:index) + assert_equal "With Implied Hello string!", controller.response_body + end + + test "when a child controller specifies layout nil, do not use the parent layout" do + controller = WithNilChild.new + controller.process(:index) + assert_equal "Hello string!", controller.response_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 + controller = WithChildOfImplied.new + controller.process(:index) + assert_equal "With Implied Hello string!", controller.response_body + end + + test "raises an exception when specifying layout true" do + assert_raises ArgumentError do + Object.class_eval do + class ::BadOmgFailLolLayout < AbstractControllerTests::Layouts::Base + layout true + end + end + end + end + end + end +end diff --git a/actionpack/test/abstract/views/abstract_controller/testing/me3/formatted.html.erb b/actionpack/test/abstract/views/abstract_controller/testing/me3/formatted.html.erb new file mode 100644 index 0000000000..785bf69191 --- /dev/null +++ b/actionpack/test/abstract/views/abstract_controller/testing/me3/formatted.html.erb @@ -0,0 +1 @@ +Hello from me3/formatted.html.erb
\ No newline at end of file diff --git a/actionpack/test/abstract/views/abstract_controller/testing/me3/index.erb b/actionpack/test/abstract/views/abstract_controller/testing/me3/index.erb new file mode 100644 index 0000000000..f079ad8204 --- /dev/null +++ b/actionpack/test/abstract/views/abstract_controller/testing/me3/index.erb @@ -0,0 +1 @@ +Hello from me3/index.erb
\ No newline at end of file diff --git a/actionpack/test/abstract/views/abstract_controller/testing/me4/index.erb b/actionpack/test/abstract/views/abstract_controller/testing/me4/index.erb new file mode 100644 index 0000000000..89dce12bdc --- /dev/null +++ b/actionpack/test/abstract/views/abstract_controller/testing/me4/index.erb @@ -0,0 +1 @@ +Hello from me4/index.erb
\ No newline at end of file diff --git a/actionpack/test/abstract/views/abstract_controller/testing/me5/index.erb b/actionpack/test/abstract/views/abstract_controller/testing/me5/index.erb new file mode 100644 index 0000000000..84d0b7417e --- /dev/null +++ b/actionpack/test/abstract/views/abstract_controller/testing/me5/index.erb @@ -0,0 +1 @@ +Hello from me5/index.erb
\ No newline at end of file diff --git a/actionpack/test/abstract/views/action_with_ivars.erb b/actionpack/test/abstract/views/action_with_ivars.erb new file mode 100644 index 0000000000..8d8ae22fd7 --- /dev/null +++ b/actionpack/test/abstract/views/action_with_ivars.erb @@ -0,0 +1 @@ +<%= @my_ivar %> from index_with_ivars.erb
\ No newline at end of file diff --git a/actionpack/test/abstract/views/helper_test.erb b/actionpack/test/abstract/views/helper_test.erb new file mode 100644 index 0000000000..8ae45cc195 --- /dev/null +++ b/actionpack/test/abstract/views/helper_test.erb @@ -0,0 +1 @@ +Hello <%= helpery_test %> : <%= included_method %>
\ No newline at end of file diff --git a/actionpack/test/abstract/views/index.erb b/actionpack/test/abstract/views/index.erb new file mode 100644 index 0000000000..cc1a8b8c85 --- /dev/null +++ b/actionpack/test/abstract/views/index.erb @@ -0,0 +1 @@ +Hello from index.erb
\ No newline at end of file diff --git a/actionpack/test/abstract/views/layouts/abstract_controller/testing/me4.erb b/actionpack/test/abstract/views/layouts/abstract_controller/testing/me4.erb new file mode 100644 index 0000000000..172dd56569 --- /dev/null +++ b/actionpack/test/abstract/views/layouts/abstract_controller/testing/me4.erb @@ -0,0 +1 @@ +Me4 Enter : <%= yield %> : Exit
\ No newline at end of file diff --git a/actionpack/test/abstract/views/layouts/application.erb b/actionpack/test/abstract/views/layouts/application.erb new file mode 100644 index 0000000000..27317140ad --- /dev/null +++ b/actionpack/test/abstract/views/layouts/application.erb @@ -0,0 +1 @@ +Application Enter : <%= yield %> : Exit
\ No newline at end of file diff --git a/actionpack/test/abstract/views/naked_render.erb b/actionpack/test/abstract/views/naked_render.erb new file mode 100644 index 0000000000..1b3d03878b --- /dev/null +++ b/actionpack/test/abstract/views/naked_render.erb @@ -0,0 +1 @@ +Hello from naked_render.erb
\ No newline at end of file |