2005-05-22 07:43:05 +00:00
|
|
|
require File.dirname(__FILE__) + '/../abstract_unit'
|
|
|
|
|
|
|
|
Customer = Struct.new("Customer", :name)
|
|
|
|
|
|
|
|
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-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
|
|
|
|
@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-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
|
|
|
|
render :inline => "Hello: <%= params[:name] %>", :layout => nil
|
|
|
|
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",
|
|
|
|
"accessing_params_in_template_with_layout"
|
2005-05-22 07:43:05 +00:00
|
|
|
"layouts/standard"
|
|
|
|
when "builder_layout_test"
|
|
|
|
"layouts/builder"
|
|
|
|
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
|
|
|
|
ActionController::Base.view_controller_internals = false
|
|
|
|
|
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-05-22 17:12:29 +00:00
|
|
|
get :hello_world
|
|
|
|
assert_kind_of ActionController::AbstractRequest, assigns["request"]
|
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-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
|
|
|
|
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_partials_list
|
2005-05-22 17:12:29 +00:00
|
|
|
get :partials_list
|
|
|
|
assert_equal "Hello: davidHello: mary", @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
|
|
|
|
assert_equal "How's there? Hello: davidHello: mary", @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-05-30 07:20:37 +00:00
|
|
|
end
|