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
|
|
|
|
|
|
|
|
|
|
|
|
class TestController < ActionController::Base
|
|
|
|
layout :determine_layout
|
|
|
|
|
|
|
|
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
|
|
|
|
render_partial
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def rescue_action(e) raise end
|
|
|
|
|
|
|
|
private
|
|
|
|
def determine_layout
|
|
|
|
case action_name
|
2005-05-22 16:59:55 +00:00
|
|
|
when "layout_test", "rendering_without_layout", "rendering_nothing_on_layout"
|
2005-05-22 07:43:05 +00:00
|
|
|
"layouts/standard"
|
|
|
|
when "builder_layout_test"
|
|
|
|
"layouts/builder"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
TestController.template_root = File.dirname(__FILE__) + "/../fixtures/"
|
|
|
|
Fun::GamesController.template_root = File.dirname(__FILE__) + "/../fixtures/"
|
|
|
|
|
|
|
|
class TestLayoutController < ActionController::Base
|
|
|
|
layout "layouts/standard"
|
|
|
|
|
|
|
|
def hello_world
|
|
|
|
end
|
|
|
|
|
|
|
|
def hello_world_outside_layout
|
|
|
|
end
|
|
|
|
|
|
|
|
def rescue_action(e)
|
|
|
|
raise unless ActionController::MissingTemplate === e
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class RenderTest < Test::Unit::TestCase
|
|
|
|
def setup
|
2005-05-22 17:12:29 +00:00
|
|
|
@controller = TestController.new
|
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-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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
private
|
|
|
|
def process_request
|
|
|
|
TestController.process(@request, @response)
|
|
|
|
end
|
|
|
|
end
|