diff options
Diffstat (limited to 'actionview/test/actionpack')
7 files changed, 293 insertions, 65 deletions
diff --git a/actionview/test/actionpack/abstract/abstract_controller_test.rb b/actionview/test/actionpack/abstract/abstract_controller_test.rb index 40d3b17131..490932fef0 100644 --- a/actionview/test/actionpack/abstract/abstract_controller_test.rb +++ b/actionview/test/actionpack/abstract/abstract_controller_test.rb @@ -150,6 +150,38 @@ module AbstractController end end + class OverridingLocalPrefixes < AbstractController::Base + include AbstractController::Rendering + include ActionView::Rendering + append_view_path File.expand_path(File.join(File.dirname(__FILE__), "views")) + + def index + render + end + + def self.local_prefixes + # this would usually return "abstract_controller/testing/overriding_local_prefixes" + super + ["abstract_controller/testing/me3"] + end + + class Inheriting < self + end + end + + class OverridingLocalPrefixesTest < ActiveSupport::TestCase + test "overriding .local_prefixes adds prefix" do + @controller = OverridingLocalPrefixes.new + @controller.process(:index) + assert_equal "Hello from me3/index.erb", @controller.response_body + end + + test ".local_prefixes is inherited" do + @controller = OverridingLocalPrefixes::Inheriting.new + @controller.process(:index) + assert_equal "Hello from me3/index.erb", @controller.response_body + end + end + # Test rendering with layouts # ==== # self._layout is used when defined diff --git a/actionview/test/actionpack/abstract/helper_test.rb b/actionview/test/actionpack/abstract/helper_test.rb index 4e05245584..7d346e917d 100644 --- a/actionview/test/actionpack/abstract/helper_test.rb +++ b/actionview/test/actionpack/abstract/helper_test.rb @@ -52,7 +52,7 @@ module AbstractController class AbstractInvalidHelpers < AbstractHelpers include ActionController::Helpers - path = File.join(File.expand_path('../../../fixtures', __FILE__), "helpers_missing") + path = File.expand_path('../../../fixtures/helpers_missing', __FILE__) $:.unshift(path) self.helpers_path = path end @@ -78,9 +78,9 @@ module AbstractController end def test_declare_missing_helper - AbstractHelpers.helper :missing - flunk "should have raised an exception" - rescue LoadError => e + e = assert_raise AbstractController::Helpers::MissingHelperError do + AbstractHelpers.helper :missing + end assert_equal "helpers/missing_helper.rb", e.path end diff --git a/actionview/test/actionpack/abstract/layouts_test.rb b/actionview/test/actionpack/abstract/layouts_test.rb index c79cb50fd7..a6786d9b6b 100644 --- a/actionview/test/actionpack/abstract/layouts_test.rb +++ b/actionview/test/actionpack/abstract/layouts_test.rb @@ -214,19 +214,19 @@ module AbstractControllerTests assert_equal "With String Hello string!", controller.response_body end - test "when layout is overwriten by :default in render, render default layout" do + test "when layout is overwritten by :default in render, render default layout" do controller = WithString.new controller.process(:overwrite_default) assert_equal "With String Hello string!", controller.response_body end - test "when layout is overwriten by string in render, render new layout" do + test "when layout is overwritten by string in render, render new layout" do controller = WithString.new controller.process(:overwrite_string) assert_equal "Overwrite Hello string!", controller.response_body end - test "when layout is overwriten by false in render, render no layout" do + test "when layout is overwritten by false in render, render no layout" do controller = WithString.new controller.process(:overwrite_false) assert_equal "Hello string!", controller.response_body @@ -264,7 +264,7 @@ module AbstractControllerTests assert_equal "Overwrite Hello proc!", controller.response_body end - test "when layout is specified as a proc and the proc retuns nil, don't use a layout" do + test "when layout is specified as a proc and the proc returns nil, don't use a layout" do controller = WithProcReturningNil.new controller.process(:index) assert_equal "Hello nil!", controller.response_body diff --git a/actionview/test/actionpack/abstract/render_test.rb b/actionview/test/actionpack/abstract/render_test.rb index f9d8c916d9..d09f91c1e2 100644 --- a/actionview/test/actionpack/abstract/render_test.rb +++ b/actionview/test/actionpack/abstract/render_test.rb @@ -60,42 +60,42 @@ module AbstractController end def test_render_template - @controller.process(:template) + assert_equal "With Template", @controller.process(:template) assert_equal "With Template", @controller.response_body end def test_render_file - @controller.process(:file) + assert_equal "With File", @controller.process(:file) assert_equal "With File", @controller.response_body end def test_render_inline - @controller.process(:inline) + assert_equal "With Inline", @controller.process(:inline) assert_equal "With Inline", @controller.response_body end def test_render_text - @controller.process(:text) + assert_equal "With Text", @controller.process(:text) assert_equal "With Text", @controller.response_body end def test_render_default - @controller.process(:default) + assert_equal "With Default", @controller.process(:default) assert_equal "With Default", @controller.response_body end def test_render_string - @controller.process(:string) + assert_equal "With String", @controller.process(:string) assert_equal "With String", @controller.response_body end def test_render_symbol - @controller.process(:symbol) + assert_equal "With Symbol", @controller.process(:symbol) assert_equal "With Symbol", @controller.response_body end def test_render_string_with_path - @controller.process(:string_with_path) + assert_equal "With String With Path", @controller.process(:string_with_path) assert_equal "With String With Path", @controller.response_body end end diff --git a/actionview/test/actionpack/controller/layout_test.rb b/actionview/test/actionpack/controller/layout_test.rb index 5dd23c4b31..7b8a83e2fe 100644 --- a/actionview/test/actionpack/controller/layout_test.rb +++ b/actionview/test/actionpack/controller/layout_test.rb @@ -1,14 +1,10 @@ require 'abstract_unit' -require 'rbconfig' 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 -ActionView::Template::register_template_handler :mab, - lambda { |template| template.source.inspect } - ActionController::Base.view_paths = [ File.dirname(__FILE__) + '/../../fixtures/actionpack/layout_tests/' ] class LayoutTest < ActionController::Base @@ -17,6 +13,15 @@ class LayoutTest < ActionController::Base self.view_paths = ActionController::Base.view_paths.dup end +module TemplateHandlerHelper + def with_template_handler(*extensions, handler) + ActionView::Template.register_template_handler(*extensions, handler) + yield + ensure + ActionView::Template.unregister_template_handler(*extensions) + end +end + # Restore view_paths to previous value ActionController::Base.view_paths = old_load_paths @@ -39,6 +44,8 @@ class MultipleExtensions < LayoutTest end class LayoutAutoDiscoveryTest < ActionController::TestCase + include TemplateHandlerHelper + def setup super @request.host = "www.nextangle.com" @@ -57,10 +64,12 @@ class LayoutAutoDiscoveryTest < ActionController::TestCase end def test_third_party_template_library_auto_discovers_layout - @controller = ThirdPartyTemplateLibraryController.new - get :hello - assert_response :success - assert_equal 'layouts/third_party_template_library.mab', @response.body + with_template_handler :mab, lambda { |template| template.source.inspect } do + @controller = ThirdPartyTemplateLibraryController.new + get :hello + assert_response :success + assert_equal 'layouts/third_party_template_library.mab', @response.body + end end def test_namespaced_controllers_auto_detect_layouts1 @@ -135,6 +144,7 @@ end class LayoutSetInResponseTest < ActionController::TestCase include ActionView::Template::Handlers + include TemplateHandlerHelper def test_layout_set_when_using_default_layout @controller = DefaultLayoutController.new @@ -191,9 +201,11 @@ class LayoutSetInResponseTest < ActionController::TestCase end def test_layout_set_when_using_render - @controller = SetsLayoutInRenderController.new - get :hello - assert_template :layout => "layouts/third_party_template_library" + with_template_handler :mab, lambda { |template| template.source.inspect } do + @controller = SetsLayoutInRenderController.new + get :hello + assert_template :layout => "layouts/third_party_template_library" + end end def test_layout_is_not_set_when_none_rendered @@ -215,12 +227,6 @@ class LayoutSetInResponseTest < ActionController::TestCase end end -class RenderWithTemplateOptionController < LayoutTest - def hello - render :template => 'alt/hello' - end -end - class SetsNonExistentLayoutFile < LayoutTest layout "nofile" end diff --git a/actionview/test/actionpack/controller/render_test.rb b/actionview/test/actionpack/controller/render_test.rb index 964dccbffb..fe4cf3688a 100644 --- a/actionview/test/actionpack/controller/render_test.rb +++ b/actionview/test/actionpack/controller/render_test.rb @@ -44,8 +44,6 @@ module Quiz end end - class Store < Question; end - # Controller class QuestionsController < ApplicationController def new @@ -93,17 +91,17 @@ class TestController < ApplicationController # :ported: def render_hello_world - render :template => "test/hello_world" + render "test/hello_world" end def render_hello_world_with_last_modified_set response.last_modified = Date.new(2008, 10, 10).to_time - render :template => "test/hello_world" + render "test/hello_world" end # :ported: compatibility def render_hello_world_with_forward_slash - render :template => "/test/hello_world" + render "/test/hello_world" end # :ported: @@ -113,7 +111,7 @@ class TestController < ApplicationController # :deprecated: def render_template_in_top_directory_with_slash - render :template => '/shared' + render '/shared' end # :ported: @@ -162,13 +160,6 @@ class TestController < ApplicationController end # :ported: - def render_file_as_string_with_instance_variables - @secret = 'in the sauce' - path = File.expand_path(File.join(File.dirname(__FILE__), '../../fixtures/test/render_file_with_ivar')) - render path - end - - # :ported: def render_file_not_using_full_path @secret = 'in the sauce' render :file => 'test/render_file_with_ivar' @@ -196,7 +187,7 @@ class TestController < ApplicationController def render_file_as_string_with_locals path = File.expand_path(File.join(File.dirname(__FILE__), '../../fixtures/test/render_file_with_locals')) - render path, :locals => {:secret => 'in the sauce'} + render file: path, :locals => {:secret => 'in the sauce'} end def accessing_request_in_template @@ -462,6 +453,10 @@ class TestController < ApplicationController render :text => "foo" end + def render_with_assigns_option + render inline: '<%= @hello %>', assigns: { hello: "world" } + end + def yield_content_for render :action => "content_for", :layout => "yield" end @@ -538,6 +533,14 @@ class TestController < ApplicationController 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') ] + 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 + end + def partial_collection_with_counter render :partial => "customer_counter", :collection => [ Customer.new("david"), Customer.new("mary") ] end @@ -693,9 +696,9 @@ class RenderTest < ActionController::TestCase end def test_line_offset - get :render_line_offset - flunk "the action should have raised an exception" - rescue StandardError => exc + exc = assert_raises ActionView::Template::Error do + get :render_line_offset + end line = exc.backtrace.first assert(line =~ %r{:(\d+):}) assert_equal "1", $1, @@ -722,6 +725,11 @@ 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 @@ -783,12 +791,6 @@ class RenderTest < ActionController::TestCase end # :ported: - def test_render_file_as_string_with_instance_variables - get :render_file_as_string_with_instance_variables - assert_equal "The secret is in the sauce\n", @response.body - end - - # :ported: def test_render_file_not_using_full_path get :render_file_not_using_full_path assert_equal "The secret is in the sauce\n", @response.body @@ -836,7 +838,7 @@ class RenderTest < ActionController::TestCase def test_render_text_with_nil get :render_text_with_nil assert_response 200 - assert_equal ' ', @response.body + assert_equal '', @response.body end # :ported: @@ -955,23 +957,23 @@ class RenderTest < ActionController::TestCase end def test_accessing_params_in_template - get :accessing_params_in_template, :name => "David" + get :accessing_params_in_template, params: { name: "David" } assert_equal "Hello: David", @response.body end def test_accessing_local_assigns_in_inline_template - get :accessing_local_assigns_in_inline_template, :local_name => "Local David" + get :accessing_local_assigns_in_inline_template, params: { local_name: "Local David" } assert_equal "Goodbye, Local David", @response.body assert_equal "text/html", @response.content_type end def test_should_implicitly_render_html_template_from_xhr_request - xhr :get, :render_implicit_html_template_from_xhr_request + get :render_implicit_html_template_from_xhr_request, xhr: true assert_equal "XHR!\nHello HTML!", @response.body end def test_should_implicitly_render_js_template_without_layout - get :render_implicit_js_template_without_layout, :format => :js + get :render_implicit_js_template_without_layout, format: :js, xhr: true assert_no_match %r{<html>}, @response.body end @@ -992,7 +994,7 @@ class RenderTest < ActionController::TestCase end def test_should_render_formatted_html_erb_template_with_faulty_accepts_header - @request.accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, appliction/x-shockwave-flash, */*" + @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 end @@ -1024,7 +1026,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 @@ -1044,7 +1046,7 @@ class RenderTest < ActionController::TestCase end def test_accessing_params_in_template_with_layout - get :accessing_params_in_template_with_layout, :name => "David" + get :accessing_params_in_template_with_layout, params: { name: "David" } assert_equal "<html>Hello: David</html>", @response.body end @@ -1104,6 +1106,11 @@ class RenderTest < ActionController::TestCase assert_equal "world", assigns["hello"] end + def test_render_text_with_assigns_option + get :render_with_assigns_option + assert_equal 'world', response.body + end + # :ported: def test_template_with_locals get :render_with_explicit_template_with_locals @@ -1234,6 +1241,16 @@ class RenderTest < ActionController::TestCase assert_equal "david david davidmary mary mary", @response.body end + def test_partial_collection_with_iteration + get :partial_collection_with_iteration + assert_equal "3-0: david-first3-1: mary3-2: christine-last", @response.body + end + + def test_partial_collection_with_as_and_iteration + get :partial_collection_with_as_and_iteration + assert_equal "3-0: david-first3-1: mary3-2: christine-last", @response.body + end + def test_partial_collection_with_counter get :partial_collection_with_counter assert_equal "david0mary1", @response.body @@ -1334,4 +1351,3 @@ class RenderTest < ActionController::TestCase assert_equal "Before (Anthony)\nInside from partial (Anthony)\nAfter\nBefore (David)\nInside from partial (David)\nAfter\nBefore (Ramm)\nInside from partial (Ramm)\nAfter", @response.body end end - diff --git a/actionview/test/actionpack/controller/view_paths_test.rb b/actionview/test/actionpack/controller/view_paths_test.rb new file mode 100644 index 0000000000..7fba9ff8ff --- /dev/null +++ b/actionview/test/actionpack/controller/view_paths_test.rb @@ -0,0 +1,174 @@ +require 'abstract_unit' + +class ViewLoadPathsTest < ActionController::TestCase + class TestController < ActionController::Base + def self.controller_path() "test" end + + 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 + + private + def add_view_path + prepend_view_path "#{FIXTURE_LOAD_PATH}/override" + end + end + + module Test + class SubController < ActionController::Base + layout 'test/sub' + def hello_world; render(:template => 'test/hello_world'); end + end + end + + def setup + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + @controller = TestController.new + @paths = TestController.view_paths + end + + def teardown + TestController.view_paths = @paths + end + + def expand(array) + array.map {|x| File.expand_path(x.to_s)} + end + + def assert_paths(*paths) + controller = paths.first.is_a?(Class) ? paths.shift : @controller + assert_equal expand(paths), controller.view_paths.map(&:to_s) + end + + def test_template_load_path_was_set_correctly + assert_paths FIXTURE_LOAD_PATH + end + + def test_controller_appends_view_path_correctly + @controller.append_view_path 'foo' + assert_paths(FIXTURE_LOAD_PATH, "foo") + + @controller.append_view_path(%w(bar baz)) + assert_paths(FIXTURE_LOAD_PATH, "foo", "bar", "baz") + + @controller.append_view_path(FIXTURE_LOAD_PATH) + assert_paths(FIXTURE_LOAD_PATH, "foo", "bar", "baz", FIXTURE_LOAD_PATH) + end + + def test_controller_prepends_view_path_correctly + @controller.prepend_view_path 'baz' + assert_paths("baz", FIXTURE_LOAD_PATH) + + @controller.prepend_view_path(%w(foo bar)) + assert_paths "foo", "bar", "baz", FIXTURE_LOAD_PATH + + @controller.prepend_view_path(FIXTURE_LOAD_PATH) + assert_paths FIXTURE_LOAD_PATH, "foo", "bar", "baz", FIXTURE_LOAD_PATH + end + + def test_template_appends_view_path_correctly + @controller.instance_variable_set :@template, ActionView::Base.new(TestController.view_paths, {}, @controller) + class_view_paths = TestController.view_paths + + @controller.append_view_path 'foo' + assert_paths FIXTURE_LOAD_PATH, "foo" + + @controller.append_view_path(%w(bar baz)) + assert_paths FIXTURE_LOAD_PATH, "foo", "bar", "baz" + assert_paths TestController, *class_view_paths + end + + def test_template_prepends_view_path_correctly + @controller.instance_variable_set :@template, ActionView::Base.new(TestController.view_paths, {}, @controller) + class_view_paths = TestController.view_paths + + @controller.prepend_view_path 'baz' + assert_paths "baz", FIXTURE_LOAD_PATH + + @controller.prepend_view_path(%w(foo bar)) + assert_paths "foo", "bar", "baz", FIXTURE_LOAD_PATH + assert_paths TestController, *class_view_paths + end + + def test_view_paths + get :hello_world + assert_response :success + assert_equal "Hello world!", @response.body + end + + def test_view_paths_override + TestController.prepend_view_path "#{FIXTURE_LOAD_PATH}/override" + get :hello_world + assert_response :success + assert_equal "Hello overridden world!", @response.body + end + + def test_view_paths_override_for_layouts_in_controllers_with_a_module + @controller = Test::SubController.new + Test::SubController.view_paths = [ "#{FIXTURE_LOAD_PATH}/override", FIXTURE_LOAD_PATH, "#{FIXTURE_LOAD_PATH}/override2" ] + get :hello_world + assert_response :success + assert_equal "layout: Hello overridden world!", @response.body + end + + def test_view_paths_override_at_request_time + get :hello_world_at_request_time + assert_response :success + assert_equal "Hello overridden world!", @response.body + end + + def test_decorate_view_paths_with_custom_resolver + decorator_class = Class.new(ActionView::PathResolver) do + def initialize(path_set) + @path_set = path_set + end + + def find_all(*args) + @path_set.find_all(*args).collect do |template| + ::ActionView::Template.new( + "Decorated body", + template.identifier, + template.handler, + { + :virtual_path => template.virtual_path, + :format => template.formats + } + ) + end + end + end + + decorator = decorator_class.new(TestController.view_paths) + TestController.view_paths = ActionView::PathSet.new.push(decorator) + + get :hello_world + assert_response :success + assert_equal "Decorated body", @response.body + end + + def test_inheritance + original_load_paths = ActionController::Base.view_paths + + self.class.class_eval %{ + class A < ActionController::Base; end + class B < A; end + class C < ActionController::Base; end + } + + 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_paths C, "c/path" + end + + def test_lookup_context_accessor + assert_equal ["test"], TestController.new.lookup_context.prefixes + end +end |