2005-05-22 07:43:05 +00:00
|
|
|
require File.dirname(__FILE__) + '/../abstract_unit'
|
|
|
|
|
2005-07-04 23:09:58 +00:00
|
|
|
silence_warnings { Customer = Struct.new("Customer", :name) }
|
2005-05-22 07:43:05 +00:00
|
|
|
|
|
|
|
module Fun
|
|
|
|
class GamesController < ActionController::Base
|
|
|
|
def hello_world
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-05-30 09:00:46 +00:00
|
|
|
class NewRenderTestController < ActionController::Base
|
2005-05-22 07:43:05 +00:00
|
|
|
layout :determine_layout
|
|
|
|
|
2005-05-30 09:00:46 +00:00
|
|
|
def self.controller_name; "test"; end
|
|
|
|
def self.controller_path; "test"; end
|
|
|
|
|
2005-05-22 07:43:05 +00:00
|
|
|
def hello_world
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_hello_world
|
2005-05-22 08:58:43 +00:00
|
|
|
render :template => "test/hello_world"
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def render_hello_world_from_variable
|
|
|
|
@person = "david"
|
2005-05-22 08:58:43 +00:00
|
|
|
render :text => "hello #{@person}"
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def render_action_hello_world
|
2005-05-22 08:58:43 +00:00
|
|
|
render :action => "hello_world"
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def render_text_hello_world
|
2005-05-22 08:58:43 +00:00
|
|
|
render :text => "hello world"
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def render_custom_code
|
2005-05-22 08:58:43 +00:00
|
|
|
render :text => "hello world", :status => "404 Moved"
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def render_xml_hello
|
|
|
|
@name = "David"
|
2005-05-22 08:58:43 +00:00
|
|
|
render :template => "test/hello"
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def greeting
|
|
|
|
# let's just rely on the template
|
|
|
|
end
|
|
|
|
|
|
|
|
def layout_test
|
2005-05-22 08:58:43 +00:00
|
|
|
render :action => "hello_world"
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def layout_test_with_different_layout
|
2005-05-22 08:58:43 +00:00
|
|
|
render :action => "hello_world", :layout => "standard"
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def rendering_without_layout
|
2005-05-22 08:58:43 +00:00
|
|
|
render :action => "hello_world", :layout => false
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
2005-08-05 20:51:03 +00:00
|
|
|
|
|
|
|
def layout_overriding_layout
|
|
|
|
render :action => "hello_world", :layout => "standard"
|
|
|
|
end
|
2005-05-22 07:43:05 +00:00
|
|
|
|
2005-05-22 16:59:55 +00:00
|
|
|
def rendering_nothing_on_layout
|
|
|
|
render :nothing => true
|
|
|
|
end
|
|
|
|
|
2005-05-22 07:43:05 +00:00
|
|
|
def builder_layout_test
|
2005-05-22 08:58:43 +00:00
|
|
|
render :action => "hello"
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def partials_list
|
2005-07-05 18:21:26 +00:00
|
|
|
@test_unchanged = 'hello'
|
2005-05-22 07:43:05 +00:00
|
|
|
@customers = [ Customer.new("david"), Customer.new("mary") ]
|
2005-05-22 08:58:43 +00:00
|
|
|
render :action => "list"
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def partial_only
|
2005-05-30 07:20:37 +00:00
|
|
|
render :partial => true
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
2005-05-30 07:51:02 +00:00
|
|
|
def partial_only_with_layout
|
2005-06-01 13:39:58 +00:00
|
|
|
render :partial => "partial_only", :layout => true
|
2005-05-30 07:51:02 +00:00
|
|
|
end
|
2005-07-27 09:11:00 +00:00
|
|
|
|
|
|
|
def partial_with_locals
|
|
|
|
render :partial => "customer", :locals => { :customer => Customer.new("david") }
|
|
|
|
end
|
|
|
|
|
|
|
|
def partial_collection
|
|
|
|
render :partial => "customer", :collection => [ Customer.new("david"), Customer.new("mary") ]
|
|
|
|
end
|
|
|
|
|
|
|
|
def partial_collection_with_locals
|
2005-07-27 12:06:19 +00:00
|
|
|
render :partial => "customer_greeting", :collection => [ Customer.new("david"), Customer.new("mary") ], :locals => { :greeting => "Bonjour" }
|
2005-07-27 09:11:00 +00:00
|
|
|
end
|
2005-05-30 07:51:02 +00:00
|
|
|
|
2005-09-01 21:43:38 +00:00
|
|
|
def empty_partial_collection
|
|
|
|
render :partial => "customer", :collection => []
|
|
|
|
end
|
2005-09-09 07:27:44 +00:00
|
|
|
|
|
|
|
def partial_with_hash_object
|
|
|
|
render :partial => "hash_object", :object => {:first_name => "Sam"}
|
|
|
|
end
|
2005-09-01 21:43:38 +00:00
|
|
|
|
2005-05-22 07:43:05 +00:00
|
|
|
def hello_in_a_string
|
|
|
|
@customers = [ Customer.new("david"), Customer.new("mary") ]
|
2005-05-22 08:58:43 +00:00
|
|
|
render :text => "How's there? #{render_to_string("test/list")}"
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def accessing_params_in_template
|
2005-05-22 08:58:43 +00:00
|
|
|
render :inline => "Hello: <%= params[:name] %>"
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
2005-05-30 09:12:12 +00:00
|
|
|
def accessing_params_in_template_with_layout
|
2005-07-23 09:00:05 +00:00
|
|
|
render :layout => nil, :inline => "Hello: <%= params[:name] %>"
|
2005-05-30 09:12:12 +00:00
|
|
|
end
|
|
|
|
|
2005-06-15 13:20:17 +00:00
|
|
|
def render_with_explicit_template
|
|
|
|
render "test/hello_world"
|
|
|
|
end
|
|
|
|
|
2005-07-09 15:31:06 +00:00
|
|
|
def double_render
|
|
|
|
render :text => "hello"
|
|
|
|
render :text => "world"
|
|
|
|
end
|
|
|
|
|
|
|
|
def double_redirect
|
|
|
|
redirect_to :action => "double_render"
|
|
|
|
redirect_to :action => "double_render"
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_and_redirect
|
|
|
|
render :text => "hello"
|
|
|
|
redirect_to :action => "double_render"
|
|
|
|
end
|
|
|
|
|
2005-07-21 07:14:35 +00:00
|
|
|
def rendering_with_conflicting_local_vars
|
|
|
|
@name = "David"
|
2005-08-26 22:37:36 +00:00
|
|
|
def @template.name() nil end
|
2005-07-21 07:14:35 +00:00
|
|
|
render :action => "potential_conflicts"
|
|
|
|
end
|
|
|
|
|
2005-07-23 08:43:01 +00:00
|
|
|
def action_talk_to_layout
|
|
|
|
# Action template sets variable that's picked up by layout
|
|
|
|
end
|
|
|
|
|
2005-07-31 12:16:21 +00:00
|
|
|
def render_text_with_assigns
|
|
|
|
@hello = "world"
|
|
|
|
render :text => "foo"
|
|
|
|
end
|
|
|
|
|
2005-09-01 21:51:27 +00:00
|
|
|
def yield_content_for
|
|
|
|
render :action => "content_for", :layout => "yield"
|
|
|
|
end
|
|
|
|
|
2005-05-22 07:43:05 +00:00
|
|
|
def rescue_action(e) raise end
|
|
|
|
|
|
|
|
private
|
|
|
|
def determine_layout
|
|
|
|
case action_name
|
2005-06-01 13:39:58 +00:00
|
|
|
when "hello_world", "layout_test", "rendering_without_layout",
|
2005-05-30 07:51:02 +00:00
|
|
|
"rendering_nothing_on_layout", "render_text_hello_world",
|
2005-05-30 09:12:12 +00:00
|
|
|
"partial_only", "partial_only_with_layout",
|
|
|
|
"accessing_params_in_template",
|
2005-06-15 13:20:17 +00:00
|
|
|
"accessing_params_in_template_with_layout",
|
|
|
|
"render_with_explicit_template"
|
2005-05-22 07:43:05 +00:00
|
|
|
"layouts/standard"
|
|
|
|
when "builder_layout_test"
|
|
|
|
"layouts/builder"
|
2005-08-05 20:51:03 +00:00
|
|
|
when "action_talk_to_layout", "layout_overriding_layout"
|
2005-07-23 08:43:01 +00:00
|
|
|
"layouts/talk_from_action"
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-05-30 09:00:46 +00:00
|
|
|
NewRenderTestController.template_root = File.dirname(__FILE__) + "/../fixtures/"
|
2005-05-22 07:43:05 +00:00
|
|
|
Fun::GamesController.template_root = File.dirname(__FILE__) + "/../fixtures/"
|
|
|
|
|
2005-05-30 09:00:46 +00:00
|
|
|
class NewRenderTest < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@controller = NewRenderTestController.new
|
2005-05-22 07:43:05 +00:00
|
|
|
|
2005-05-30 09:00:46 +00:00
|
|
|
# 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".
|
|
|
|
@controller.logger = Logger.new(nil)
|
2005-05-22 07:43:05 +00:00
|
|
|
|
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@response = ActionController::TestResponse.new
|
|
|
|
|
|
|
|
@request.host = "www.nextangle.com"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_simple_show
|
2005-05-22 17:12:29 +00:00
|
|
|
get :hello_world
|
|
|
|
assert_response :success
|
|
|
|
assert_template "test/hello_world"
|
2005-06-01 13:39:58 +00:00
|
|
|
assert_equal "<html>Hello world!</html>", @response.body
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_do_with_render
|
2005-05-22 17:12:29 +00:00
|
|
|
get :render_hello_world
|
|
|
|
assert_template "test/hello_world"
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_do_with_render_from_variable
|
2005-05-22 17:12:29 +00:00
|
|
|
get :render_hello_world_from_variable
|
|
|
|
assert_equal "hello david", @response.body
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_do_with_render_action
|
2005-05-22 17:12:29 +00:00
|
|
|
get :render_action_hello_world
|
|
|
|
assert_template "test/hello_world"
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_do_with_render_text
|
2005-05-22 17:12:29 +00:00
|
|
|
get :render_text_hello_world
|
|
|
|
assert_equal "hello world", @response.body
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_do_with_render_custom_code
|
2005-05-22 17:12:29 +00:00
|
|
|
get :render_custom_code
|
|
|
|
assert_response :missing
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_attempt_to_access_object_method
|
2005-05-22 17:12:29 +00:00
|
|
|
assert_raises(ActionController::UnknownAction, "No action responded to [clone]") { get :clone }
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_private_methods
|
2005-05-22 17:12:29 +00:00
|
|
|
assert_raises(ActionController::UnknownAction, "No action responded to [determine_layout]") { get :determine_layout }
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_access_to_request_in_view
|
2005-07-23 09:00:05 +00:00
|
|
|
view_internals_old_value = ActionController::Base.view_controller_internals
|
|
|
|
|
2005-05-22 07:43:05 +00:00
|
|
|
ActionController::Base.view_controller_internals = false
|
2005-07-23 09:00:05 +00:00
|
|
|
ActionController::Base.protected_variables_cache = nil
|
2005-05-22 07:43:05 +00:00
|
|
|
|
2005-05-22 17:12:29 +00:00
|
|
|
get :hello_world
|
|
|
|
assert_nil(assigns["request"])
|
2005-05-22 07:43:05 +00:00
|
|
|
|
|
|
|
ActionController::Base.view_controller_internals = true
|
2005-07-23 09:00:05 +00:00
|
|
|
ActionController::Base.protected_variables_cache = nil
|
2005-05-22 07:43:05 +00:00
|
|
|
|
2005-05-22 17:12:29 +00:00
|
|
|
get :hello_world
|
|
|
|
assert_kind_of ActionController::AbstractRequest, assigns["request"]
|
2005-07-23 09:00:05 +00:00
|
|
|
|
|
|
|
ActionController::Base.view_controller_internals = view_internals_old_value
|
|
|
|
ActionController::Base.protected_variables_cache = nil
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_xml
|
2005-05-22 17:12:29 +00:00
|
|
|
get :render_xml_hello
|
|
|
|
assert_equal "<html>\n <p>Hello David</p>\n<p>This is grand!</p>\n</html>\n", @response.body
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_xml_with_default
|
2005-05-22 17:12:29 +00:00
|
|
|
get :greeting
|
|
|
|
assert_equal "<p>This is grand!</p>\n", @response.body
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_layout_rendering
|
2005-05-22 17:12:29 +00:00
|
|
|
get :layout_test
|
|
|
|
assert_equal "<html>Hello world!</html>", @response.body
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_layout_test_with_different_layout
|
2005-05-22 17:12:29 +00:00
|
|
|
get :layout_test_with_different_layout
|
|
|
|
assert_equal "<html>Hello world!</html>", @response.body
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_rendering_without_layout
|
2005-05-22 17:12:29 +00:00
|
|
|
get :rendering_without_layout
|
|
|
|
assert_equal "Hello world!", @response.body
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
2005-08-05 20:51:03 +00:00
|
|
|
def test_layout_overriding_layout
|
|
|
|
get :layout_overriding_layout
|
|
|
|
assert_no_match %r{<title>}, @response.body
|
|
|
|
end
|
|
|
|
|
2005-05-22 16:59:55 +00:00
|
|
|
def test_rendering_nothing_on_layout
|
2005-05-22 17:12:29 +00:00
|
|
|
get :rendering_nothing_on_layout
|
2005-07-14 07:19:26 +00:00
|
|
|
assert_equal " ", @response.body
|
2005-05-22 16:59:55 +00:00
|
|
|
end
|
|
|
|
|
2005-05-22 07:43:05 +00:00
|
|
|
def test_render_xml_with_layouts
|
2005-05-22 17:12:29 +00:00
|
|
|
get :builder_layout_test
|
|
|
|
assert_equal "<wrapper>\n<html>\n <p>Hello </p>\n<p>This is grand!</p>\n</html>\n</wrapper>\n", @response.body
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_partial_only
|
2005-05-22 17:12:29 +00:00
|
|
|
get :partial_only
|
|
|
|
assert_equal "only partial", @response.body
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
2005-05-30 07:51:02 +00:00
|
|
|
def test_partial_only_with_layout
|
|
|
|
get :partial_only_with_layout
|
|
|
|
assert_equal "<html>only partial</html>", @response.body
|
|
|
|
end
|
|
|
|
|
2005-05-22 07:43:05 +00:00
|
|
|
def test_render_to_string
|
2005-05-22 17:12:29 +00:00
|
|
|
get :hello_in_a_string
|
2005-07-05 18:21:26 +00:00
|
|
|
assert_equal "How's there? goodbyeHello: davidHello: marygoodbye\n", @response.body
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_nested_rendering
|
2005-05-22 17:12:29 +00:00
|
|
|
get :hello_world
|
2005-05-22 07:43:05 +00:00
|
|
|
assert_equal "Living in a nested world", Fun::GamesController.process(@request, @response).body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_accessing_params_in_template
|
2005-05-22 17:12:29 +00:00
|
|
|
get :accessing_params_in_template, :name => "David"
|
|
|
|
assert_equal "Hello: David", @response.body
|
2005-05-22 07:43:05 +00:00
|
|
|
end
|
2005-05-30 09:12:12 +00:00
|
|
|
|
|
|
|
def test_accessing_params_in_template_with_layout
|
|
|
|
get :accessing_params_in_template_with_layout, :name => "David"
|
|
|
|
assert_equal "<html>Hello: David</html>", @response.body
|
|
|
|
end
|
2005-06-15 13:20:17 +00:00
|
|
|
|
|
|
|
def test_render_with_explicit_template
|
|
|
|
get :render_with_explicit_template
|
|
|
|
assert_response :success
|
|
|
|
end
|
2005-07-09 15:31:06 +00:00
|
|
|
|
|
|
|
def test_double_render
|
|
|
|
assert_raises(ActionController::DoubleRenderError) { get :double_render }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_double_redirect
|
|
|
|
assert_raises(ActionController::DoubleRenderError) { get :double_redirect }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_and_redirect
|
|
|
|
assert_raises(ActionController::DoubleRenderError) { get :render_and_redirect }
|
|
|
|
end
|
2005-07-21 07:14:35 +00:00
|
|
|
|
|
|
|
def test_rendering_with_conflicting_local_vars
|
|
|
|
get :rendering_with_conflicting_local_vars
|
|
|
|
assert_equal("First: David\nSecond: Stephan\nThird: David\nFourth: David\nFifth: ", @response.body)
|
|
|
|
end
|
|
|
|
|
2005-07-23 08:43:01 +00:00
|
|
|
def test_action_talk_to_layout
|
|
|
|
get :action_talk_to_layout
|
|
|
|
assert_equal "<title>Talking to the layout</title>\nAction was here!", @response.body
|
|
|
|
end
|
|
|
|
|
2005-07-24 16:45:39 +00:00
|
|
|
def test_partials_list
|
|
|
|
get :partials_list
|
|
|
|
assert_equal "goodbyeHello: davidHello: marygoodbye\n", @response.body
|
|
|
|
end
|
2005-07-27 09:11:00 +00:00
|
|
|
|
|
|
|
def test_partial_with_locals
|
|
|
|
get :partial_with_locals
|
2005-07-27 12:24:14 +00:00
|
|
|
assert_equal "Hello: david", @response.body
|
2005-07-27 09:11:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_partial_collection
|
|
|
|
get :partial_collection
|
|
|
|
assert_equal "Hello: davidHello: mary", @response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_partial_collection_with_locals
|
|
|
|
get :partial_collection_with_locals
|
2005-07-27 12:06:19 +00:00
|
|
|
assert_equal "Bonjour: davidBonjour: mary", @response.body
|
2005-07-27 09:11:00 +00:00
|
|
|
end
|
2005-07-31 12:16:21 +00:00
|
|
|
|
2005-09-01 21:43:38 +00:00
|
|
|
def test_empty_partial_collection
|
|
|
|
get :empty_partial_collection
|
|
|
|
assert_equal " ", @response.body
|
|
|
|
end
|
|
|
|
|
2005-09-09 07:27:44 +00:00
|
|
|
def test_partial_with_hash_object
|
|
|
|
get :partial_with_hash_object
|
|
|
|
assert_equal "Sam", @response.body
|
|
|
|
end
|
|
|
|
|
2005-07-31 12:16:21 +00:00
|
|
|
def test_render_text_with_assigns
|
|
|
|
get :render_text_with_assigns
|
|
|
|
assert_equal "world", assigns["hello"]
|
|
|
|
end
|
2005-09-01 21:51:27 +00:00
|
|
|
|
|
|
|
def test_yield_content_for
|
|
|
|
get :yield_content_for
|
|
|
|
assert_equal "<title>Putting stuff in the title!</title>\n\nGreat stuff!\n", @response.body
|
|
|
|
end
|
2005-07-31 12:16:21 +00:00
|
|
|
end
|