From e0447023db7152d3ecdf693bd9aa36c7daa02653 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Wed, 18 Mar 2009 15:58:47 -0700 Subject: Implemented basic template rendering in AC::Base2: * Created several macros for writing simpler specs * Finished making Rack::Test work right * Implemented render_to_string * Status Codes * render :text => nil --- actionpack/test/new_base/base_test.rb | 88 +-------------------- actionpack/test/new_base/render_test.rb | 124 +++++++++++++++++++++++++++++ actionpack/test/new_base/test_helper.rb | 133 ++++++++++++++++++++++++++++++++ 3 files changed, 258 insertions(+), 87 deletions(-) create mode 100644 actionpack/test/new_base/render_test.rb create mode 100644 actionpack/test/new_base/test_helper.rb (limited to 'actionpack/test/new_base') diff --git a/actionpack/test/new_base/base_test.rb b/actionpack/test/new_base/base_test.rb index 9609c11753..4f46cb6492 100644 --- a/actionpack/test/new_base/base_test.rb +++ b/actionpack/test/new_base/base_test.rb @@ -1,82 +1,4 @@ -$:.unshift(File.dirname(__FILE__) + '/../../lib') -$:.unshift(File.dirname(__FILE__) + '/../../../activesupport/lib') - -require 'test/unit' -require 'active_support' -require 'active_support/test_case' -require 'action_controller' -require 'action_view/base' - -begin - require 'ruby-debug' - Debugger.settings[:autoeval] = true - Debugger.start -rescue LoadError - # Debugging disabled. `gem install ruby-debug` to enable. -end - -require 'action_controller/abstract' -require 'action_controller/new_base' -require 'pp' # require 'pp' early to prevent hidden_methods from not picking up the pretty-print methods until too late - -require 'rubygems' -require 'rack/test' - -module ActionController - class Base2 < AbstractBase - include AbstractController::Callbacks - include AbstractController::Renderer - include AbstractController::Helpers - include AbstractController::Layouts - include AbstractController::Logger - - include ActionController::HideActions - include ActionController::UrlFor - include ActionController::Renderer - - CORE_METHODS = self.public_instance_methods - end -end - -# Temporary base class -class Rack::TestCase < ActiveSupport::TestCase - - include Rack::Test::Methods - - setup do - ActionController::Base.session_options[:key] = "abc" - ActionController::Base.session_options[:secret] = ("*" * 30) - ActionController::Routing.use_controllers! %w(happy_path/simple_dispatch) - end - - def self.get(url) - setup do |test| - test.get url - end - end - - def app - @app ||= ActionController::Dispatcher.new - end - - def assert_body(body) - assert_equal [body], last_response.body - end - - def assert_status(code) - assert_equal code, last_response.status - end - - def assert_content_type(type) - assert_equal type, last_response.headers["Content-Type"] - end - - def assert_header(name, value) - assert_equal value, last_response.headers[name] - end - -end - +require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper") # Tests the controller dispatching happy path module HappyPath @@ -99,14 +21,6 @@ module HappyPath end end - class SimpleRouteCase < Rack::TestCase - setup do - ActionController::Routing::Routes.draw do |map| - map.connect ':controller/:action' - end - end - end - class TestSimpleDispatch < SimpleRouteCase get "/happy_path/simple_dispatch/index" diff --git a/actionpack/test/new_base/render_test.rb b/actionpack/test/new_base/render_test.rb new file mode 100644 index 0000000000..2f43bc1fc6 --- /dev/null +++ b/actionpack/test/new_base/render_test.rb @@ -0,0 +1,124 @@ +require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper") + +module HappyPath + + class RenderTextController < ActionController::Base2 + def render_hello_world_from_variable + @person = "david" + render :text => "hello #{@person}" + end + + def render_custom_code + render :text => "hello world", :status => 404 + end + + def render_with_custom_code_as_string + render :text => "hello world", :status => "404 Not Found" + end + + def render_text_with_nil + render :text => nil + end + + def render_text_with_nil_and_status + render :text => nil, :status => 403 + end + + def render_text_with_false + render :text => false + end + end + + class TestSimpleTextRender < SimpleRouteCase + describe "Rendering text from a action with default options" + + get "/happy_path/render_text/render_hello_world_from_variable" + assert_body "hello david" + assert_status 200 + end + + class TestTextRenderWithStatus < SimpleRouteCase + describe "Rendering text, while also providing a custom status code" + + get "/happy_path/render_text/render_custom_code" + assert_body "hello world" + assert_status 404 + end + + class TestTextRenderWithNil < SimpleRouteCase + describe "Rendering text with nil returns a single space character" + + get "/happy_path/render_text/render_text_with_nil" + assert_body " " + assert_status 200 + end + + class TestTextRenderWithNilAndStatus < SimpleRouteCase + describe "Rendering text with nil and custom status code returns a single space character with the status" + + get "/happy_path/render_text/render_text_with_nil_and_status" + assert_body " " + assert_status 403 + end + + class TestTextRenderWithFalse < SimpleRouteCase + describe "Rendering text with false returns the string 'false'" + + get "/happy_path/render_text/render_text_with_false" + assert_body "false" + assert_status 200 + end + + class RenderTemplateController < ActionController::Base2 + + def render_hello_world + render :template => "test/basic" + end + + def render_hello_world_with_forward_slash + render :template => "/test/basic" + end + + def render_template_in_top_directory + render :template => 'shared' + end + + def render_template_in_top_directory_with_slash + render :template => '/shared' + end + end + + class TestTemplateRender < SimpleRouteCase + describe "rendering a normal template with full path" + + get "/happy_path/render_template/render_hello_world" + assert_body "Hello from basic.html.erb" + assert_status 200 + end + + class TestTemplateRenderWithForwardSlash < SimpleRouteCase + describe "rendering a normal template with full path starting with a leading slash" + + get "/happy_path/render_template/render_hello_world_with_forward_slash" + assert_body "Hello from basic.html.erb" + assert_status 200 + end + + class TestTemplateRenderInTopDirectory < SimpleRouteCase + describe "rendering a template not in a subdirectory" + + get "/happy_path/render_template/render_template_in_top_directory" + assert_body "Elastica" + assert_status 200 + end + + class TestTemplateRenderInTopDirectoryWithSlash < SimpleRouteCase + describe "rendering a template not in a subdirectory with a leading slash" + + get "/happy_path/render_template/render_template_in_top_directory_with_slash" + assert_body "Elastica" + assert_status 200 + end + + # TODO: Other language craziness +end \ No newline at end of file diff --git a/actionpack/test/new_base/test_helper.rb b/actionpack/test/new_base/test_helper.rb new file mode 100644 index 0000000000..7a1adc3755 --- /dev/null +++ b/actionpack/test/new_base/test_helper.rb @@ -0,0 +1,133 @@ +$:.unshift(File.dirname(__FILE__) + '/../../lib') +$:.unshift(File.dirname(__FILE__) + '/../../../activesupport/lib') + +require 'test/unit' +require 'active_support' +require 'active_support/test_case' +require 'action_controller' +require 'action_view/base' + +begin + require 'ruby-debug' + Debugger.settings[:autoeval] = true + Debugger.start +rescue LoadError + # Debugging disabled. `gem install ruby-debug` to enable. +end + +require 'action_controller/abstract' +require 'action_controller/new_base' +require 'pp' # require 'pp' early to prevent hidden_methods from not picking up the pretty-print methods until too late + +require 'rubygems' +require 'rack/test' + +module ActionController + class Base2 < AbstractBase + include AbstractController::Callbacks + include AbstractController::Renderer + include AbstractController::Helpers + include AbstractController::Layouts + include AbstractController::Logger + + include ActionController::HideActions + include ActionController::UrlFor + include ActionController::Renderer + + def self.inherited(klass) + @subclasses ||= [] + @subclasses << klass.to_s + end + + def self.subclasses + @subclasses + end + + append_view_path File.join(File.dirname(__FILE__), '..', 'fixtures') + + CORE_METHODS = self.public_instance_methods + end +end + +# Temporary base class +class Rack::TestCase < ActiveSupport::TestCase + include Rack::Test::Methods + + setup do + ActionController::Base.session_options[:key] = "abc" + ActionController::Base.session_options[:secret] = ("*" * 30) + + controllers = ActionController::Base2.subclasses.map do |k| + k.underscore.sub(/_controller$/, '') + end + + ActionController::Routing.use_controllers!(controllers) + end + + def self.describe(text) + class_eval <<-RUBY_EVAL + def self.name + "#{text}" + end + RUBY_EVAL + end + + def app + @app ||= ActionController::Dispatcher.new + end + + def self.get(url) + setup do |test| + test.get url + end + end + + def assert_body(body) + assert_equal [body], last_response.body + end + + def self.assert_body(body) + test "body is set to '#{body}'" do + assert_body body + end + end + + def assert_status(code) + assert_equal code, last_response.status + end + + def self.assert_status(code) + test "status code is set to #{code}" do + assert_status code + end + end + + def assert_content_type(type) + assert_equal type, last_response.headers["Content-Type"] + end + + def self.assert_content_type(type) + test "content type is set to #{type}" do + assert_content_type(type) + end + end + + def assert_header(name, value) + assert_equal value, last_response.headers[name] + end + + def self.assert_header(name, value) + test "'#{name}' header is set to #{value.inspect}" do + assert_header(name, value) + end + end + +end + +class SimpleRouteCase < Rack::TestCase + setup do + ActionController::Routing::Routes.draw do |map| + map.connect ':controller/:action/:id' + end + end +end \ No newline at end of file -- cgit v1.2.3