diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2004-11-24 01:04:44 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2004-11-24 01:04:44 +0000 |
commit | db045dbbf60b53dbe013ef25554fd013baf88134 (patch) | |
tree | 257830e3c76458c8ff3d1329de83f32b23926028 /actionpack/test | |
download | rails-db045dbbf60b53dbe013ef25554fd013baf88134.tar.gz rails-db045dbbf60b53dbe013ef25554fd013baf88134.tar.bz2 rails-db045dbbf60b53dbe013ef25554fd013baf88134.zip |
Initial
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/test')
30 files changed, 2303 insertions, 0 deletions
diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb new file mode 100644 index 0000000000..0fcb5e852d --- /dev/null +++ b/actionpack/test/abstract_unit.rb @@ -0,0 +1,9 @@ +$:.unshift(File.dirname(__FILE__) + '/../lib') + +require 'test/unit' +require 'action_controller' + +require 'action_controller/test_process' + +ActionController::Base.logger = nil +ActionController::Base.ignore_missing_templates = true
\ No newline at end of file diff --git a/actionpack/test/controller/action_pack_assertions_test.rb b/actionpack/test/controller/action_pack_assertions_test.rb new file mode 100644 index 0000000000..6d727be5a2 --- /dev/null +++ b/actionpack/test/controller/action_pack_assertions_test.rb @@ -0,0 +1,323 @@ +require File.dirname(__FILE__) + '/../abstract_unit' + +# a controller class to facilitate the tests +class ActionPackAssertionsController < ActionController::Base + + # this does absolutely nothing + def nothing() render_text ""; end + + # a standard template + def hello_world() render "test/hello_world"; end + + # a standard template + def hello_xml_world() render "test/hello_xml_world"; end + + # a redirect to an internal location + def redirect_internal() redirect_to "nothing"; end + + # a redirect to an external location + def redirect_external() redirect_to_url "http://www.rubyonrails.org"; end + + # a 404 + def response404() render_text "", "404 AWOL"; end + + # a 500 + def response500() render_text "", "500 Sorry"; end + + # a fictional 599 + def response599() render_text "", "599 Whoah!"; end + + # putting stuff in the flash + def flash_me + flash['hello'] = 'my name is inigo montoya...' + render_text "Inconceivable!" + end + + # we have a flash, but nothing is in it + def flash_me_naked + flash.clear + render_text "wow!" + end + + # assign some template instance variables + def assign_this + @howdy = "ho" + render_text "Mr. Henke" + end + + def render_based_on_parameters + render_text "Mr. #{@params["name"]}" + end + + # puts something in the session + def session_stuffing + session['xmas'] = 'turkey' + render_text "ho ho ho" + end + + # 911 + def rescue_action(e) raise; end + +end + +# --------------------------------------------------------------------------- + + +# tell the controller where to find its templates but start from parent +# directory of test_request_response to simulate the behaviour of a +# production environment +ActionPackAssertionsController.template_root = File.dirname(__FILE__) + "/../fixtures/" + + +# a test case to exercise the new capabilities TestRequest & TestResponse +class ActionPackAssertionsControllerTest < Test::Unit::TestCase + # let's get this party started + def setup + @controller = ActionPackAssertionsController.new + @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new + end + + # -- assertion-based testing ------------------------------------------------ + + # test the session assertion to make sure something is there. + def test_assert_session_has + process :session_stuffing + assert_session_has 'xmas' + assert_session_has_no 'halloween' + end + + # test the assertion of goodies in the template + def test_assert_template_has + process :assign_this + assert_template_has 'howdy' + end + + # test the assertion for goodies that shouldn't exist in the template + def test_assert_template_has_no + process :nothing + assert_template_has_no 'maple syrup' + assert_template_has_no 'howdy' + end + + # test the redirection assertions + def test_assert_redirect + process :redirect_internal + assert_redirect + end + + # test the redirect url string + def test_assert_redirect_url + process :redirect_external + assert_redirect_url 'http://www.rubyonrails.org' + end + + # test the redirection pattern matching on a string + def test_assert_redirect_url_match_string + process :redirect_external + assert_redirect_url_match 'rails.org' + end + + # test the redirection pattern matching on a pattern + def test_assert_redirect_url_match_pattern + process :redirect_external + assert_redirect_url_match /ruby/ + end + + # test the flash-based assertions with something is in the flash + def test_flash_assertions_full + process :flash_me + assert @response.has_flash_with_contents? + assert_flash_exists + assert ActionController::TestResponse.assertion_target.has_flash_with_contents? + assert_flash_not_empty + assert_flash_has 'hello' + assert_flash_has_no 'stds' + end + + # test the flash-based assertions with no flash at all + def test_flash_assertions_negative + process :nothing + assert_flash_not_exists + assert_flash_empty + assert_flash_has_no 'hello' + assert_flash_has_no 'qwerty' + end + + # test the assert_rendered_file + def test_assert_rendered_file + process :hello_world + assert_rendered_file 'test/hello_world' + assert_rendered_file 'hello_world' + assert_rendered_file + end + + # test the assert_success assertion + def test_assert_success + process :nothing + assert_success + end + + # -- standard request/reponse object testing -------------------------------- + + # ensure our session is working properly + def test_session_objects + process :session_stuffing + assert @response.has_session_object?('xmas') + assert_session_equal 'turkey', 'xmas' + assert !@response.has_session_object?('easter') + end + + # make sure that the template objects exist + def test_template_objects_alive + process :assign_this + assert !@response.has_template_object?('hi') + assert @response.has_template_object?('howdy') + end + + # make sure we don't have template objects when we shouldn't + def test_template_object_missing + process :nothing + assert_nil @response.template_objects['howdy'] + end + + def test_assigned_equal + process :assign_this + assert_assigned_equal "ho", :howdy + end + + # check the empty flashing + def test_flash_me_naked + process :flash_me_naked + assert @response.has_flash? + assert !@response.has_flash_with_contents? + end + + # check if we have flash objects + def test_flash_haves + process :flash_me + assert @response.has_flash? + assert @response.has_flash_with_contents? + assert @response.has_flash_object?('hello') + end + + # ensure we don't have flash objects + def test_flash_have_nots + process :nothing + assert !@response.has_flash? + assert !@response.has_flash_with_contents? + assert_nil @response.flash['hello'] + end + + # examine that the flash objects are what we expect + def test_flash_equals + process :flash_me + assert_flash_equal 'my name is inigo montoya...', 'hello' + end + + + # check if we were rendered by a file-based template? + def test_rendered_action + process :nothing + assert !@response.rendered_with_file? + + process :hello_world + assert @response.rendered_with_file? + assert 'hello_world', @response.rendered_file + end + + # check the redirection location + def test_redirection_location + process :redirect_internal + assert_equal 'nothing', @response.redirect_url + + process :redirect_external + assert_equal 'http://www.rubyonrails.org', @response.redirect_url + + process :nothing + assert_nil @response.redirect_url + end + + + # check server errors + def test_server_error_response_code + process :response500 + assert @response.server_error? + + process :response599 + assert @response.server_error? + + process :response404 + assert !@response.server_error? + end + + # check a 404 response code + def test_missing_response_code + process :response404 + assert @response.missing? + end + + # check to see if our redirection matches a pattern + def test_redirect_url_match + process :redirect_external + assert @response.redirect? + assert @response.redirect_url_match?("rubyonrails") + assert @response.redirect_url_match?(/rubyonrails/) + assert !@response.redirect_url_match?("phpoffrails") + assert !@response.redirect_url_match?(/perloffrails/) + end + + # check for a redirection + def test_redirection + process :redirect_internal + assert @response.redirect? + + process :redirect_external + assert @response.redirect? + + process :nothing + assert !@response.redirect? + end + + # check a successful response code + def test_successful_response_code + process :nothing + assert @response.success? + end + + # a basic check to make sure we have a TestResponse object + def test_has_response + process :nothing + assert_kind_of ActionController::TestResponse, @response + end + + def test_render_based_on_parameters + process :render_based_on_parameters, "name" => "David" + assert_equal "Mr. David", @response.body + end + + def test_simple_one_element_xpath_match + process :hello_xml_world + assert_template_xpath_match('//title', "Hello World") + end + + def test_array_of_elements_in_xpath_match + process :hello_xml_world + assert_template_xpath_match('//p', %w( abes monks wiseguys )) + end +end + +class ActionPackHeaderTest < Test::Unit::TestCase + def setup + @controller = ActionPackAssertionsController.new + @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new + end + def test_rendering_xml_sets_content_type + process :hello_xml_world + assert_equal('text/xml', @controller.headers['Content-Type']) + end + def test_rendering_xml_respects_content_type + @response.headers['Content-Type'] = 'application/pdf' + process :hello_xml_world + assert_equal('application/pdf', @controller.headers['Content-Type']) + end +end diff --git a/actionpack/test/controller/active_record_assertions_test.rb b/actionpack/test/controller/active_record_assertions_test.rb new file mode 100644 index 0000000000..53106aaee7 --- /dev/null +++ b/actionpack/test/controller/active_record_assertions_test.rb @@ -0,0 +1,119 @@ +path_to_ar = File.dirname(__FILE__) + '/../../../activerecord' + +if Object.const_defined?("ActiveRecord") || File.exist?(path_to_ar) +# This test is very different than the others. It requires ActiveRecord to +# run. There's a bunch of stuff we are assuming here: +# +# 1. activerecord exists as a sibling directory to actionpack +# (i.e., actionpack/../activerecord) +# 2. you've created the appropriate database to run the active_record unit tests +# 3. you set the appropriate database connection below + +driver_to_use = 'native_sqlite' + +$: << path_to_ar + '/lib/' +$: << path_to_ar + '/test/' +require 'active_record' unless Object.const_defined?("ActiveRecord") +require "connections/#{driver_to_use}/connection" +require 'fixtures/company' + +# ----------------------------------------------------------------------------- + +# add some validation rules to trip up the assertions +class Company + def validate + errors.add_on_empty('name') + errors.add('rating', 'rating should not be 2') if rating == 2 + errors.add_to_base('oh oh') if rating == 3 + end +end + +# ----------------------------------------------------------------------------- + +require File.dirname(__FILE__) + '/../abstract_unit' + +# a controller class to handle the AR assertions +class ActiveRecordAssertionsController < ActionController::Base + # fail with 1 bad column + def nasty_columns_1 + @company = Company.new + @company.name = "B" + @company.rating = 2 + render_text "snicker...." + end + + # fail with 2 bad column + def nasty_columns_2 + @company = Company.new + @company.name = "" + @company.rating = 2 + render_text "double snicker...." + end + + # this will pass validation + def good_company + @company = Company.new + @company.name = "A" + @company.rating = 69 + render_text "Goodness Gracious!" + end + + # this will fail validation + def bad_company + @company = Company.new + render_text "Who's Bad?" + end + + # the safety dance...... + def rescue_action(e) raise; end +end + +# ----------------------------------------------------------------------------- + +ActiveRecordAssertionsController.template_root = File.dirname(__FILE__) + "/../fixtures/" + +# The test case to try the AR assertions +class ActiveRecordAssertionsControllerTest < Test::Unit::TestCase + # set it up + def setup + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + @controller = ActiveRecordAssertionsController.new + end + + # test for 1 bad apple column + def test_some_invalid_columns + process :nasty_columns_1 + assert_success + assert_invalid_record 'company' + assert_invalid_column_on_record 'company', 'rating' + assert_valid_column_on_record 'company', 'name' + assert_valid_column_on_record 'company', ['name','id'] + end + + # test for 2 bad apples columns + def test_all_invalid_columns + process :nasty_columns_2 + assert_success + assert_invalid_record 'company' + assert_invalid_column_on_record 'company', 'rating' + assert_invalid_column_on_record 'company', 'name' + assert_invalid_column_on_record 'company', ['name','rating'] + end + + # ensure we have no problems with an ActiveRecord + def test_valid_record + process :good_company + assert_success + assert_valid_record 'company' + end + + # ensure we have problems with an ActiveRecord + def test_invalid_record + process :bad_company + assert_success + assert_invalid_record 'company' + end +end + +end
\ No newline at end of file diff --git a/actionpack/test/controller/cgi_test.rb b/actionpack/test/controller/cgi_test.rb new file mode 100755 index 0000000000..46e24ab403 --- /dev/null +++ b/actionpack/test/controller/cgi_test.rb @@ -0,0 +1,142 @@ +$:.unshift(File.dirname(__FILE__) + '/../../lib') + +require 'test/unit' +require 'action_controller/cgi_ext/cgi_methods' +require 'stringio' + +class MockUploadedFile < StringIO + def content_type + "img/jpeg" + end + + def original_filename + "my_file.doc" + end +end + +class CGITest < Test::Unit::TestCase + def setup + @query_string = "action=create_customer&full_name=David%20Heinemeier%20Hansson&customerId=1" + @query_string_with_nil = "action=create_customer&full_name=" + @query_string_with_array = "action=create_customer&selected[]=1&selected[]=2&selected[]=3" + @query_string_with_amps = "action=create_customer&name=Don%27t+%26+Does" + @query_string_with_multiple_of_same_name = + "action=update_order&full_name=Lau%20Taarnskov&products=4&products=2&products=3" + end + + def test_query_string + assert_equal( + { "action" => "create_customer", "full_name" => "David Heinemeier Hansson", "customerId" => "1"}, + CGIMethods.parse_query_parameters(@query_string) + ) + end + + def test_query_string_with_nil + assert_equal( + { "action" => "create_customer", "full_name" => nil}, + CGIMethods.parse_query_parameters(@query_string_with_nil) + ) + end + + def test_query_string_with_array + assert_equal( + { "action" => "create_customer", "selected" => ["1", "2", "3"]}, + CGIMethods.parse_query_parameters(@query_string_with_array) + ) + end + + def test_query_string_with_amps + assert_equal( + { "action" => "create_customer", "name" => "Don't & Does"}, + CGIMethods.parse_query_parameters(@query_string_with_amps) + ) + end + + def test_parse_params + input = { + "customers[boston][first][name]" => [ "David" ], + "customers[boston][first][url]" => [ "http://David" ], + "customers[boston][second][name]" => [ "Allan" ], + "customers[boston][second][url]" => [ "http://Allan" ], + "something_else" => [ "blah" ], + "something_nil" => [ nil ], + "something_empty" => [ "" ], + "products[first]" => [ "Apple Computer" ], + "products[second]" => [ "Pc" ] + } + + expected_output = { + "customers" => { + "boston" => { + "first" => { + "name" => "David", + "url" => "http://David" + }, + "second" => { + "name" => "Allan", + "url" => "http://Allan" + } + } + }, + "something_else" => "blah", + "something_empty" => "", + "something_nil" => "", + "products" => { + "first" => "Apple Computer", + "second" => "Pc" + } + } + + assert_equal expected_output, CGIMethods.parse_request_parameters(input) + end + + def test_parse_params_from_multipart_upload + mock_file = MockUploadedFile.new + + input = { + "something" => [ StringIO.new("") ], + "products[string]" => [ StringIO.new("Apple Computer") ], + "products[file]" => [ mock_file ] + } + + expected_output = { + "something" => "", + "products" => { + "string" => "Apple Computer", + "file" => mock_file + } + } + + assert_equal expected_output, CGIMethods.parse_request_parameters(input) + end + + def test_parse_params_with_file + input = { + "customers[boston][first][name]" => [ "David" ], + "something_else" => [ "blah" ], + "logo" => [ File.new(File.dirname(__FILE__) + "/cgi_test.rb").path ] + } + + expected_output = { + "customers" => { + "boston" => { + "first" => { + "name" => "David" + } + } + }, + "something_else" => "blah", + "logo" => File.new(File.dirname(__FILE__) + "/cgi_test.rb").path, + } + + assert_equal expected_output, CGIMethods.parse_request_parameters(input) + end + + def test_parse_params_with_array + input = { "selected[]" => [ "1", "2", "3" ] } + + expected_output = { "selected" => [ "1", "2", "3" ] } + + assert_equal expected_output, CGIMethods.parse_request_parameters(input) + end +end diff --git a/actionpack/test/controller/cookie_test.rb b/actionpack/test/controller/cookie_test.rb new file mode 100644 index 0000000000..d3099bcd99 --- /dev/null +++ b/actionpack/test/controller/cookie_test.rb @@ -0,0 +1,38 @@ +require File.dirname(__FILE__) + '/../abstract_unit' + +class CookieTest < Test::Unit::TestCase + class TestController < ActionController::Base + def authenticate + cookie "name" => "user_name", "value" => "david" + render_text "hello world" + end + + def access_frozen_cookies + @cookies["wont"] = "work" + end + + def rescue_action(e) raise end + end + + def setup + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + + @request.host = "www.nextangle.com" + end + + def test_setting_cookie + @request.action = "authenticate" + assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "david") ], process_request.headers["cookie"] + end + + def test_setting_cookie + @request.action = "access_frozen_cookies" + assert_raises(TypeError) { process_request } + end + + private + def process_request + TestController.process(@request, @response) + end +end
\ No newline at end of file diff --git a/actionpack/test/controller/filters_test.rb b/actionpack/test/controller/filters_test.rb new file mode 100644 index 0000000000..f4d7a689b5 --- /dev/null +++ b/actionpack/test/controller/filters_test.rb @@ -0,0 +1,159 @@ +require File.dirname(__FILE__) + '/../abstract_unit' + +class FilterTest < Test::Unit::TestCase + class TestController < ActionController::Base + before_filter :ensure_login + + def show + render_text "ran action" + end + + private + def ensure_login + @ran_filter ||= [] + @ran_filter << "ensure_login" + end + end + + class PrependingController < TestController + prepend_before_filter :wonderful_life + + private + def wonderful_life + @ran_filter ||= [] + @ran_filter << "wonderful_life" + end + end + + class ProcController < PrependingController + before_filter(proc { |c| c.assigns["ran_proc_filter"] = true }) + end + + class ImplicitProcController < PrependingController + before_filter { |c| c.assigns["ran_proc_filter"] = true } + end + + class AuditFilter + def self.filter(controller) + controller.assigns["was_audited"] = true + end + end + + class AroundFilter + def before(controller) + @execution_log = "before" + controller.class.execution_log << " before aroundfilter " if controller.respond_to? :execution_log + controller.assigns["before_ran"] = true + end + + def after(controller) + controller.assigns["execution_log"] = @execution_log + " and after" + controller.assigns["after_ran"] = true + controller.class.execution_log << " after aroundfilter " if controller.respond_to? :execution_log + end + end + + class AppendedAroundFilter + def before(controller) + controller.class.execution_log << " before appended aroundfilter " + end + + def after(controller) + controller.class.execution_log << " after appended aroundfilter " + end + end + + class AuditController < ActionController::Base + before_filter(AuditFilter) + + def show + render_text "hello" + end + end + + class BadFilterController < ActionController::Base + before_filter 2 + + def show() "show" end + + protected + def rescue_action(e) raise(e) end + end + + class AroundFilterController < PrependingController + around_filter AroundFilter.new + end + + class MixedFilterController < PrependingController + cattr_accessor :execution_log + def initialize + @@execution_log = "" + end + + before_filter { |c| c.class.execution_log << " before procfilter " } + prepend_around_filter AroundFilter.new + + after_filter { |c| c.class.execution_log << " after procfilter " } + append_around_filter AppendedAroundFilter.new + end + + + def test_added_filter_to_inheritance_graph + assert_equal [ :fire_flash, :ensure_login ], TestController.before_filters + end + + def test_base_class_in_isolation + assert_equal [ :fire_flash ], ActionController::Base.before_filters + end + + def test_prepending_filter + assert_equal [ :wonderful_life, :fire_flash, :ensure_login ], PrependingController.before_filters + end + + def test_running_filters + assert_equal %w( wonderful_life ensure_login ), test_process(PrependingController).template.assigns["ran_filter"] + end + + def test_running_filters_with_proc + assert test_process(ProcController).template.assigns["ran_proc_filter"] + end + + def test_running_filters_with_implicit_proc + assert test_process(ImplicitProcController).template.assigns["ran_proc_filter"] + end + + def test_running_filters_with_class + assert test_process(AuditController).template.assigns["was_audited"] + end + + def test_bad_filter + assert_raises(ActionController::ActionControllerError) { + test_process(BadFilterController) + } + end + + def test_around_filter + controller = test_process(AroundFilterController) + assert controller.template.assigns["before_ran"] + assert controller.template.assigns["after_ran"] + end + + def test_having_properties_in_around_filter + controller = test_process(AroundFilterController) + assert_equal "before and after", controller.template.assigns["execution_log"] + end + + def test_prepending_and_appending_around_filter + controller = test_process(MixedFilterController) + assert_equal " before aroundfilter before procfilter before appended aroundfilter " + + " after appended aroundfilter after aroundfilter after procfilter ", + MixedFilterController.execution_log + end + + private + def test_process(controller) + request = ActionController::TestRequest.new + request.action = "show" + controller.process(request, ActionController::TestResponse.new) + end +end
\ No newline at end of file diff --git a/actionpack/test/controller/flash_test.rb b/actionpack/test/controller/flash_test.rb new file mode 100644 index 0000000000..033477fe39 --- /dev/null +++ b/actionpack/test/controller/flash_test.rb @@ -0,0 +1,69 @@ +require File.dirname(__FILE__) + '/../abstract_unit' + +class FlashTest < Test::Unit::TestCase + class TestController < ActionController::Base + def set_flash + flash["that"] = "hello" + render_text "hello" + end + + def use_flash + @flashy = flash["that"] + render_text "hello" + end + + def use_flash_and_keep_it + @flashy = flash["that"] + keep_flash + render_text "hello" + end + + def rescue_action(e) + raise unless ActionController::MissingTemplate === e + end + end + + def setup + initialize_request_and_response + end + + def test_flash + @request.action = "set_flash" + response = process_request + + @request.action = "use_flash" + first_response = process_request + assert_equal "hello", first_response.template.assigns["flash"]["that"] + assert_equal "hello", first_response.template.assigns["flashy"] + + second_response = process_request + assert_nil second_response.template.assigns["flash"]["that"], "On second flash" + end + + def test_keep_flash + @request.action = "set_flash" + response = process_request + + @request.action = "use_flash_and_keep_it" + first_response = process_request + assert_equal "hello", first_response.template.assigns["flash"]["that"] + assert_equal "hello", first_response.template.assigns["flashy"] + + @request.action = "use_flash" + second_response = process_request + assert_equal "hello", second_response.template.assigns["flash"]["that"], "On second flash" + + third_response = process_request + assert_nil third_response.template.assigns["flash"]["that"], "On third flash" + end + + private + def initialize_request_and_response + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + end + + def process_request + TestController.process(@request, @response) + end +end
\ No newline at end of file diff --git a/actionpack/test/controller/helper_test.rb b/actionpack/test/controller/helper_test.rb new file mode 100644 index 0000000000..9d1da53241 --- /dev/null +++ b/actionpack/test/controller/helper_test.rb @@ -0,0 +1,110 @@ +require File.dirname(__FILE__) + '/../abstract_unit' + +class HelperTest < Test::Unit::TestCase + HELPER_PATHS = %w(/../fixtures/helpers) + + class TestController < ActionController::Base + attr_accessor :delegate_attr + def delegate_method() end + def rescue_action(e) raise end + end + + module LocalAbcHelper + def a() end + def b() end + def c() end + end + + + def setup + # Increment symbol counter. + @symbol = (@@counter ||= 'A0').succ!.dup + + # Generate new controller class. + controller_class_name = "Helper#{@symbol}Controller" + eval("class #{controller_class_name} < TestController; end") + @controller_class = self.class.const_get(controller_class_name) + + # Generate new template class and assign to controller. + template_class_name = "Test#{@symbol}View" + eval("class #{template_class_name} < ActionView::Base; end") + @template_class = self.class.const_get(template_class_name) + @controller_class.template_class = @template_class + + # Add helper paths to LOAD_PATH. + HELPER_PATHS.each { |path| + $LOAD_PATH.unshift(File.dirname(__FILE__) + path) + } + + # Set default test helper. + self.test_helper = LocalAbcHelper + end + + def teardown + # Reset template class. + #ActionController::Base.template_class = ActionView::Base + + # Remove helper paths from LOAD_PATH. + HELPER_PATHS.each { |path| + $LOAD_PATH.delete(File.dirname(__FILE__) + path) + } + end + + + def test_deprecated_helper + assert_equal helper_methods, missing_methods + assert_nothing_raised { @controller_class.helper TestHelper } + assert_equal [], missing_methods + end + + def test_declare_helper + require 'abc_helper' + self.test_helper = AbcHelper + assert_equal helper_methods, missing_methods + assert_nothing_raised { @controller_class.helper :abc } + assert_equal [], missing_methods + end + + def test_declare_missing_helper + assert_equal helper_methods, missing_methods + assert_raise(LoadError) { @controller_class.helper :missing } + end + + def test_helper_block + assert_nothing_raised { + @controller_class.helper { def block_helper_method; end } + } + assert template_methods.include?('block_helper_method') + end + + def test_helper_block_include + assert_equal helper_methods, missing_methods + assert_nothing_raised { + @controller_class.helper { include TestHelper } + } + assert [], missing_methods + end + + def test_helper_method + assert_nothing_raised { @controller_class.helper_method :delegate_method } + assert template_methods.include?('delegate_method') + end + + def test_helper_attr + assert_nothing_raised { @controller_class.helper_attr :delegate_attr } + assert template_methods.include?('delegate_attr') + assert template_methods.include?('delegate_attr=') + end + + + private + def helper_methods; TestHelper.instance_methods end + def template_methods; @template_class.instance_methods end + def missing_methods; helper_methods - template_methods end + + def test_helper=(helper_module) + old_verbose, $VERBOSE = $VERBOSE, nil + self.class.const_set('TestHelper', helper_module) + $VERBOSE = old_verbose + end +end
\ No newline at end of file diff --git a/actionpack/test/controller/layout_test.rb b/actionpack/test/controller/layout_test.rb new file mode 100644 index 0000000000..f652453ebd --- /dev/null +++ b/actionpack/test/controller/layout_test.rb @@ -0,0 +1,49 @@ +require File.dirname(__FILE__) + '/../abstract_unit' + +class TestLayoutController < ActionController::Base + layout "layouts/standard" + + def hello_world + end + + def hello_world_outside_layout + end + + def rescue_action(e) raise end +end + +class ChildWithoutTestLayoutController < TestLayoutController + layout nil + + def hello_world + end +end + +class ChildWithOtherTestLayoutController < TestLayoutController + layout nil + + def hello_world + end +end + +class RenderTest < Test::Unit::TestCase + def setup + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + + @request.host = "www.nextangle.com" + end + + def test_layout_rendering + @request.action = "hello_world" + response = process_request + assert_equal "200 OK", response.headers["Status"] + assert_equal "layouts/standard", response.template.template_name + end + + + private + def process_request + TestLayoutController.process(@request, @response) + end +end
\ No newline at end of file diff --git a/actionpack/test/controller/redirect_test.rb b/actionpack/test/controller/redirect_test.rb new file mode 100755 index 0000000000..6302016a53 --- /dev/null +++ b/actionpack/test/controller/redirect_test.rb @@ -0,0 +1,44 @@ +require File.dirname(__FILE__) + '/../abstract_unit' + +class RedirectTest < Test::Unit::TestCase + class RedirectController < ActionController::Base + def simple_redirect + redirect_to :action => "hello_world" + end + + def method_redirect + redirect_to :dashbord_url, 1, "hello" + end + + def rescue_errors(e) raise e end + + protected + def dashbord_url(id, message) + url_for :action => "dashboard", :params => { "id" => id, "message" => message } + end + end + + def setup + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + end + + def test_simple_redirect + @request.path = "/redirect/simple_redirect" + @request.action = "simple_redirect" + response = process_request + assert_equal "http://test.host/redirect/hello_world", response.headers["location"] + end + + def test_redirect_with_method_reference_and_parameters + @request.path = "/redirect/method_redirect" + @request.action = "method_redirect" + response = process_request + assert_equal "http://test.host/redirect/dashboard?message=hello&id=1", response.headers["location"] + end + + private + def process_request + RedirectController.process(@request, @response) + end +end
\ No newline at end of file diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb new file mode 100644 index 0000000000..ce778e1d7d --- /dev/null +++ b/actionpack/test/controller/render_test.rb @@ -0,0 +1,178 @@ +require File.dirname(__FILE__) + '/../abstract_unit' + +Customer = Struct.new("Customer", :name) + +class RenderTest < Test::Unit::TestCase + class TestController < ActionController::Base + layout :determine_layout + + def hello_world + end + + def render_hello_world + render "test/hello_world" + end + + def render_hello_world_from_variable + @person = "david" + render_text "hello #{@person}" + end + + def render_action_hello_world + render_action "hello_world" + end + + def render_text_hello_world + render_text "hello world" + end + + def render_custom_code + render_text "hello world", "404 Moved" + end + + def render_xml_hello + @name = "David" + render "test/hello" + end + + def greeting + # let's just rely on the template + end + + def layout_test + render_action "hello_world" + end + + def builder_layout_test + render_action "hello" + end + + def partials_list + @customers = [ Customer.new("david"), Customer.new("mary") ] + render_action "list" + end + + def modgreet + end + + def rescue_action(e) raise end + + private + def determine_layout + case action_name + when "layout_test": "layouts/standard" + when "builder_layout_test": "layouts/builder" + end + end + end + + TestController.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 + + def setup + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + + @request.host = "www.nextangle.com" + end + + def test_simple_show + @request.action = "hello_world" + response = process_request + assert_equal "200 OK", response.headers["Status"] + assert_equal "test/hello_world", response.template.first_render + end + + def test_do_with_render + @request.action = "render_hello_world" + assert_equal "test/hello_world", process_request.template.first_render + end + + def test_do_with_render_from_variable + @request.action = "render_hello_world_from_variable" + assert_equal "hello david", process_request.body + end + + def test_do_with_render_action + @request.action = "render_action_hello_world" + assert_equal "test/hello_world", process_request.template.first_render + end + + def test_do_with_render_text + @request.action = "render_text_hello_world" + assert_equal "hello world", process_request.body + end + + def test_do_with_render_custom_code + @request.action = "render_custom_code" + assert_equal "404 Moved", process_request.headers["Status"] + end + + def test_attempt_to_access_object_method + @request.action = "clone" + assert_raises(ActionController::UnknownAction, "No action responded to [clone]") { process_request } + end + + def test_access_to_request_in_view + ActionController::Base.view_controller_internals = false + + @request.action = "hello_world" + response = process_request + assert_nil response.template.assigns["request"] + + ActionController::Base.view_controller_internals = true + + @request.action = "hello_world" + response = process_request + assert_kind_of ActionController::AbstractRequest, response.template.assigns["request"] + end + + def test_render_xml + @request.action = "render_xml_hello" + assert_equal "<html>\n <p>Hello David</p>\n<p>This is grand!</p>\n</html>\n", process_request.body + end + + def test_render_xml_with_default + @request.action = "greeting" + assert_equal "<p>This is grand!</p>\n", process_request.body + end + + def test_layout_rendering + @request.action = "layout_test" + assert_equal "<html>Hello world!</html>", process_request.body + end + + def test_render_xml_with_layouts + @request.action = "builder_layout_test" + assert_equal "<wrapper>\n<html>\n <p>Hello </p>\n<p>This is grand!</p>\n</html>\n</wrapper>\n", process_request.body + end + + def test_partials_list + @request.action = "partials_list" + assert_equal "Hello: davidHello: mary", process_request.body + end + + def test_module_rendering + @request.action = "modgreet" + @request.parameters["module"] = "scope" + assert_equal "<p>Beautiful modules!</p>", process_request.body + end + + private + def process_request + TestController.process(@request, @response) + end +end
\ No newline at end of file diff --git a/actionpack/test/controller/send_file_test.rb b/actionpack/test/controller/send_file_test.rb new file mode 100644 index 0000000000..57205a01c5 --- /dev/null +++ b/actionpack/test/controller/send_file_test.rb @@ -0,0 +1,68 @@ +require File.join(File.dirname(__FILE__), '..', 'abstract_unit') + + +module TestFileUtils + def file_name() File.basename(__FILE__) end + def file_path() File.expand_path(__FILE__) end + def file_data() File.open(file_path, 'rb') { |f| f.read } end +end + + +class SendFileController < ActionController::Base + include TestFileUtils + + attr_writer :options + def options() @options ||= {} end + + def file() send_file(file_path, options) end + def data() send_data(file_data, options) end + + def rescue_action(e) raise end +end + + +class SendFileTest < Test::Unit::TestCase + include TestFileUtils + + def setup + @controller = SendFileController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + end + + def test_file_nostream + @controller.options = { :stream => false } + response = nil + assert_nothing_raised { response = process('file') } + assert_not_nil response + assert_kind_of String, response.body + assert_equal file_data, response.body + end + + def test_file_stream + response = nil + assert_nothing_raised { response = process('file') } + assert_not_nil response + assert_kind_of Proc, response.body + + old_stdout = $stdout + begin + require 'stringio' + $stdout = StringIO.new + $stdout.binmode + assert_nothing_raised { response.body.call } + assert_equal file_data, $stdout.string + ensure + $stdout = old_stdout + end + end + + def test_data + response = nil + assert_nothing_raised { response = process('data') } + assert_not_nil response + + assert_kind_of String, response.body + assert_equal file_data, response.body + end +end diff --git a/actionpack/test/controller/url_test.rb b/actionpack/test/controller/url_test.rb new file mode 100644 index 0000000000..bf6a7aab75 --- /dev/null +++ b/actionpack/test/controller/url_test.rb @@ -0,0 +1,368 @@ +require File.dirname(__FILE__) + '/../abstract_unit' +require 'action_controller/url_rewriter' + +MockRequest = Struct.new("MockRequest", :protocol, :host, :port, :path, :parameters) +class MockRequest + def host_with_port + if (protocol == "http://" && port == 80) || (protocol == "https://" && port == 443) + host + else + host + ":#{port}" + end + end +end + +class UrlTest < Test::Unit::TestCase + def setup + @library_url = ActionController::UrlRewriter.new(MockRequest.new( + "http://", + "www.singlefile.com", + 80, + "/library/books/ISBN/0743536703/show", + { "type" => "ISBN", "code" => "0743536703" } + ), "books", "show") + + @library_url_on_index = ActionController::UrlRewriter.new(MockRequest.new( + "http://", + "www.singlefile.com", + 80, + "/library/books/ISBN/0743536703/", + { "type" => "ISBN", "code" => "0743536703" } + ), "books", "index") + + @clean_urls = [ + ActionController::UrlRewriter.new(MockRequest.new( + "http://", "www.singlefile.com", 80, "/identity/", {} + ), "identity", "index"), + ActionController::UrlRewriter.new(MockRequest.new( + "http://", "www.singlefile.com", 80, "/identity", {} + ), "identity", "index") + ] + + @clean_url_with_id = ActionController::UrlRewriter.new(MockRequest.new( + "http://", "www.singlefile.com", 80, "/identity/show/5", { "id" => "5" } + ), "identity", "show") + + @clean_url_with_id_as_char = ActionController::UrlRewriter.new(MockRequest.new( + "http://", "www.singlefile.com", 80, "/teachers/show/t", { "id" => "t" } + ), "teachers", "show") + end + + def test_clean_action + assert_equal "http://www.singlefile.com/library/books/ISBN/0743536703/edit", @library_url.rewrite(:action => "edit") + end + + def test_clean_action_with_only_path + assert_equal "/library/books/ISBN/0743536703/edit", @library_url.rewrite(:action => "edit", :only_path => true) + end + + def test_action_from_index + assert_equal "http://www.singlefile.com/library/books/ISBN/0743536703/edit", @library_url_on_index.rewrite(:action => "edit") + end + + def test_action_from_index_on_clean + @clean_urls.each do |url| + assert_equal "http://www.singlefile.com/identity/edit", url.rewrite(:action => "edit") + end + end + + def test_action_without_prefix + assert_equal "http://www.singlefile.com/library/books/", @library_url.rewrite(:action => "index", :action_prefix => "") + end + + def test_action_with_prefix + assert_equal( + "http://www.singlefile.com/library/books/XTC/123/show", + @library_url.rewrite(:action => "show", :action_prefix => "XTC/123") + ) + end + + def test_action_prefix_alone + assert_equal( + "http://www.singlefile.com/library/books/XTC/123/", + @library_url.rewrite(:action_prefix => "XTC/123") + ) + end + + def test_action_with_suffix + assert_equal( + "http://www.singlefile.com/library/books/show/XTC/123", + @library_url.rewrite(:action => "show", :action_prefix => "", :action_suffix => "XTC/123") + ) + end + + def test_clean_controller + assert_equal "http://www.singlefile.com/library/settings/", @library_url.rewrite(:controller => "settings") + end + + def test_clean_controller_prefix + assert_equal "http://www.singlefile.com/shop/", @library_url.rewrite(:controller_prefix => "shop") + end + + def test_clean_controller_with_module + assert_equal "http://www.singlefile.com/shop/purchases/", @library_url.rewrite(:module => "shop", :controller => "purchases") + end + + def test_controller_and_action + assert_equal "http://www.singlefile.com/library/settings/show", @library_url.rewrite(:controller => "settings", :action => "show") + end + + def test_controller_and_action_and_anchor + assert_equal( + "http://www.singlefile.com/library/settings/show#5", + @library_url.rewrite(:controller => "settings", :action => "show", :anchor => "5") + ) + end + + def test_controller_and_action_and_empty_overwrite_params_and_anchor + assert_equal( + "http://www.singlefile.com/library/settings/show?code=0743536703&type=ISBN#5", + @library_url.rewrite(:controller => "settings", :action => "show", :overwrite_params => {}, :anchor => "5") + ) + end + + def test_controller_and_action_and_overwrite_params_and_anchor + assert_equal( + "http://www.singlefile.com/library/settings/show?code=0000001&type=ISBN#5", + @library_url.rewrite(:controller => "settings", :action => "show", :overwrite_params => {"code"=>"0000001"}, :anchor => "5") + ) + end + + def test_controller_and_action_and_overwrite_params_with_nil_value_and_anchor + assert_equal( + "http://www.singlefile.com/library/settings/show?type=ISBN#5", + @library_url.rewrite(:controller => "settings", :action => "show", :overwrite_params => {"code" => nil}, :anchor => "5") + ) + end + + def test_controller_and_action_params_and_overwrite_params_and_anchor + assert_equal( + "http://www.singlefile.com/library/settings/show?code=0000001&version=5.0#5", + @library_url.rewrite(:controller => "settings", :action => "show", :params=>{"version" => "5.0"}, :overwrite_params => {"code"=>"0000001"}, :anchor => "5") + ) + end + + def test_controller_and_action_and_params_anchor + assert_equal( + "http://www.singlefile.com/library/settings/show?update=1#5", + @library_url.rewrite(:controller => "settings", :action => "show", :params => { "update" => "1"}, :anchor => "5") + ) + end + + def test_controller_and_index_action + assert_equal "http://www.singlefile.com/library/settings/", @library_url.rewrite(:controller => "settings", :action => "index") + end + + def test_controller_and_action_with_same_name_as_controller + @clean_urls.each do |url| + assert_equal "http://www.singlefile.com/anything/identity", url.rewrite(:controller => "anything", :action => "identity") + end + end + + def test_controller_and_index_action_without_controller_prefix + assert_equal( + "http://www.singlefile.com/settings/", + @library_url.rewrite(:controller => "settings", :action => "index", :controller_prefix => "") + ) + end + + def test_controller_and_index_action_with_controller_prefix + assert_equal( + "http://www.singlefile.com/fantastic/settings/show", + @library_url.rewrite(:controller => "settings", :action => "show", :controller_prefix => "fantastic") + ) + end + + def test_path_parameters + assert_equal "http://www.singlefile.com/library/books/EXBC/0743536703/show", @library_url.rewrite(:path_params => {"type" => "EXBC"}) + end + + def test_parameters + assert_equal( + "http://www.singlefile.com/library/books/ISBN/0743536703/show?delete=1&name=David", + @library_url.rewrite(:params => {"delete" => "1", "name" => "David"}) + ) + end + + def test_parameters_with_id + @clean_urls.each do |url| + assert_equal( + "http://www.singlefile.com/identity/show?name=David&id=5", + url.rewrite( + :action => "show", + :params => { "id" => "5", "name" => "David" } + ) + ) + end + end + + def test_action_with_id + assert_equal( + "http://www.singlefile.com/identity/show/7", + @clean_url_with_id.rewrite( + :action => "show", + :id => 7 + ) + ) + @clean_urls.each do |url| + assert_equal( + "http://www.singlefile.com/identity/index/7", + url.rewrite(:id => 7) + ) + end + end + + def test_parameters_with_id_and_away + assert_equal( + "http://www.singlefile.com/identity/show/25?name=David", + @clean_url_with_id.rewrite( + :path_params => { "id" => "25" }, + :params => { "name" => "David" } + ) + ) + end + + def test_parameters_with_index_and_id + @clean_urls.each do |url| + assert_equal( + "http://www.singlefile.com/identity/index/25?name=David", + url.rewrite( + :path_params => { "id" => "25" }, + :params => { "name" => "David" } + ) + ) + end + end + + def test_action_going_away_from_id + assert_equal( + "http://www.singlefile.com/identity/list", + @clean_url_with_id.rewrite( + :action => "list" + ) + ) + end + + def test_parameters_with_direct_id_and_away + assert_equal( + "http://www.singlefile.com/identity/show/25?name=David", + @clean_url_with_id.rewrite( + :id => "25", + :params => { "name" => "David" } + ) + ) + end + + def test_parameters_with_direct_id_and_away + assert_equal( + "http://www.singlefile.com/store/open/25?name=David", + @clean_url_with_id.rewrite( + :controller => "store", + :action => "open", + :id => "25", + :params => { "name" => "David" } + ) + ) + end + + def test_parameters_to_id + @clean_urls.each do |url| + %w(show index).each do |action| + assert_equal( + "http://www.singlefile.com/identity/#{action}/25?name=David", + url.rewrite( + :action => action, + :path_params => { "id" => "25" }, + :params => { "name" => "David" } + ) + ) + end + end + end + + def test_parameters_from_id + assert_equal( + "http://www.singlefile.com/identity/", + @clean_url_with_id.rewrite( + :action => "index" + ) + ) + end + + def test_id_as_char_and_part_of_controller + assert_equal( + "http://www.singlefile.com/teachers/skill/5", + @clean_url_with_id_as_char.rewrite( + :action => "skill", + :id => 5 + ) + ) + end + + def test_from_clean_to_library + @clean_urls.each do |url| + assert_equal( + "http://www.singlefile.com/library/books/ISBN/0743536703/show?delete=1&name=David", + url.rewrite( + :controller_prefix => "library", + :controller => "books", + :action_prefix => "ISBN/0743536703", + :action => "show", + :params => { "delete" => "1", "name" => "David" } + ) + ) + end + end + + def test_from_library_to_clean + assert_equal( + "http://www.singlefile.com/identity/", + @library_url.rewrite( + :controller => "identity", :controller_prefix => "" + ) + ) + end + + def test_from_another_port + @library_url = ActionController::UrlRewriter.new(MockRequest.new( + "http://", + "www.singlefile.com", + 8080, + "/library/books/ISBN/0743536703/show", + { "type" => "ISBN", "code" => "0743536703" } + ), "books", "show") + + assert_equal( + "http://www.singlefile.com:8080/identity/", + @library_url.rewrite( + :controller => "identity", :controller_prefix => "" + ) + ) + end + + def test_basecamp + basecamp_url = ActionController::UrlRewriter.new(MockRequest.new( + "http://", + "projects.basecamp", + 80, + "/clients/disarray/1/msg/transcripts/", + {"category_name"=>"transcripts", "client_name"=>"disarray", "action"=>"index", "controller"=>"msg", "project_name"=>"1"} + ), "msg", "index") + + assert_equal( + "http://projects.basecamp/clients/disarray/1/msg/transcripts/1/comments", + basecamp_url.rewrite(:action_prefix => "transcripts/1", :action => "comments") + ) + end + + def test_on_explicit_index_page # My index page is very modest, thank you... + url = ActionController::UrlRewriter.new( + MockRequest.new( + "http://", "example.com", 80, "/controller/index", + {"controller"=>"controller", "action"=>"index"} + ), "controller", "index" + ) + assert_equal("http://example.com/controller/foo", url.rewrite(:action => 'foo')) + end + +end diff --git a/actionpack/test/fixtures/helpers/abc_helper.rb b/actionpack/test/fixtures/helpers/abc_helper.rb new file mode 100644 index 0000000000..7104ff3730 --- /dev/null +++ b/actionpack/test/fixtures/helpers/abc_helper.rb @@ -0,0 +1,5 @@ +module AbcHelper + def bare_a() end + def bare_b() end + def bare_c() end +end diff --git a/actionpack/test/fixtures/layouts/builder.rxml b/actionpack/test/fixtures/layouts/builder.rxml new file mode 100644 index 0000000000..729af4b8bc --- /dev/null +++ b/actionpack/test/fixtures/layouts/builder.rxml @@ -0,0 +1,3 @@ +xml.wrapper do + xml << @content_for_layout +end
\ No newline at end of file diff --git a/actionpack/test/fixtures/layouts/standard.rhtml b/actionpack/test/fixtures/layouts/standard.rhtml new file mode 100644 index 0000000000..fcb28ec755 --- /dev/null +++ b/actionpack/test/fixtures/layouts/standard.rhtml @@ -0,0 +1 @@ +<html><%= @content_for_layout %></html>
\ No newline at end of file diff --git a/actionpack/test/fixtures/scope/test/modgreet.rhtml b/actionpack/test/fixtures/scope/test/modgreet.rhtml new file mode 100644 index 0000000000..8947726e89 --- /dev/null +++ b/actionpack/test/fixtures/scope/test/modgreet.rhtml @@ -0,0 +1 @@ +<p>Beautiful modules!</p>
\ No newline at end of file diff --git a/actionpack/test/fixtures/test/_customer.rhtml b/actionpack/test/fixtures/test/_customer.rhtml new file mode 100644 index 0000000000..872d8c44e6 --- /dev/null +++ b/actionpack/test/fixtures/test/_customer.rhtml @@ -0,0 +1 @@ +Hello: <%= customer.name %>
\ No newline at end of file diff --git a/actionpack/test/fixtures/test/greeting.rhtml b/actionpack/test/fixtures/test/greeting.rhtml new file mode 100644 index 0000000000..62fb0293f0 --- /dev/null +++ b/actionpack/test/fixtures/test/greeting.rhtml @@ -0,0 +1 @@ +<p>This is grand!</p> diff --git a/actionpack/test/fixtures/test/hello.rxml b/actionpack/test/fixtures/test/hello.rxml new file mode 100644 index 0000000000..82a4a310d3 --- /dev/null +++ b/actionpack/test/fixtures/test/hello.rxml @@ -0,0 +1,4 @@ +xml.html do + xml.p "Hello #{@name}" + xml << render_file("test/greeting") +end
\ No newline at end of file diff --git a/actionpack/test/fixtures/test/hello_world.rhtml b/actionpack/test/fixtures/test/hello_world.rhtml new file mode 100644 index 0000000000..6769dd60bd --- /dev/null +++ b/actionpack/test/fixtures/test/hello_world.rhtml @@ -0,0 +1 @@ +Hello world!
\ No newline at end of file diff --git a/actionpack/test/fixtures/test/hello_xml_world.rxml b/actionpack/test/fixtures/test/hello_xml_world.rxml new file mode 100644 index 0000000000..02b14fe87c --- /dev/null +++ b/actionpack/test/fixtures/test/hello_xml_world.rxml @@ -0,0 +1,11 @@ +xml.html do + xml.head do + xml.title "Hello World" + end + + xml.body do + xml.p "abes" + xml.p "monks" + xml.p "wiseguys" + end +end
\ No newline at end of file diff --git a/actionpack/test/fixtures/test/list.rhtml b/actionpack/test/fixtures/test/list.rhtml new file mode 100644 index 0000000000..39e2cad966 --- /dev/null +++ b/actionpack/test/fixtures/test/list.rhtml @@ -0,0 +1 @@ +<%= render_collection_of_partials "customer", @customers %>
\ No newline at end of file diff --git a/actionpack/test/template/active_record_helper_test.rb b/actionpack/test/template/active_record_helper_test.rb new file mode 100644 index 0000000000..4b32f4dd48 --- /dev/null +++ b/actionpack/test/template/active_record_helper_test.rb @@ -0,0 +1,76 @@ +require 'test/unit' +require File.dirname(__FILE__) + '/../../lib/action_view/helpers/date_helper' +require File.dirname(__FILE__) + '/../../lib/action_view/helpers/form_helper' +# require File.dirname(__FILE__) + '/../../lib/action_view/helpers/active_record_helper' + +class ActiveRecordHelperTest < Test::Unit::TestCase + include ActionView::Helpers::FormHelper + include ActionView::Helpers::ActiveRecordHelper + + Post = Struct.new("Post", :title, :author_name, :body, :secret, :written_on) + Column = Struct.new("Column", :type, :name, :human_name) + + def setup + @post = Post.new + def @post.errors() Class.new{ def on(field) field == "author_name" || field == "body" end }.new end + def @post.new_record?() true end + + def @post.column_for_attribute(attr_name) + Post.content_columns.select { |column| column.name == attr_name }.first + end + + def Post.content_columns() [ Column.new(:string, "title", "Title"), Column.new(:text, "body", "Body") ] end + + @post.title = "Hello World" + @post.author_name = "" + @post.body = "Back to the hill and over it again!" + @post.secret = 1 + @post.written_on = Date.new(2004, 6, 15) + end + + def test_generic_input_tag + assert_equal( + '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />', input("post", "title") + ) + end + + def test_text_area_with_errors + assert_equal( + "<div class=\"fieldWithErrors\"><textarea cols=\"40\" id=\"post_body\" name=\"post[body]\" rows=\"20\" wrap=\"virtual\">Back to the hill and over it again!</textarea></div>", + text_area("post", "body") + ) + end + + def test_text_field_with_errors + assert_equal( + '<div class="fieldWithErrors"><input id="post_author_name" name="post[author_name]" size="30" type="text" value="" /></div>', + text_field("post", "author_name") + ) + end + + def test_form_with_string + assert_equal( + "<form action='create' method='POST'><p><label for=\"post_title\">Title</label><br /><input id=\"post_title\" name=\"post[title]\" size=\"30\" type=\"text\" value=\"Hello World\" /></p>\n<p><label for=\"post_body\">Body</label><br /><div class=\"fieldWithErrors\"><textarea cols=\"40\" id=\"post_body\" name=\"post[body]\" rows=\"20\" wrap=\"virtual\">Back to the hill and over it again!</textarea></div></p><input type='submit' value='Create' /></form>", + form("post") + ) + end + + def test_form_with_date + def Post.content_columns() [ Column.new(:date, "written_on", "Written on") ] end + + assert_equal( + "<form action='create' method='POST'><p><label for=\"post_written_on\">Written on</label><br /><select name='post[written_on(1i)]'>\n<option>1999</option>\n<option>2000</option>\n<option>2001</option>\n<option>2002</option>\n<option>2003</option>\n<option selected=\"selected\">2004</option>\n<option>2005</option>\n<option>2006</option>\n<option>2007</option>\n<option>2008</option>\n<option>2009</option>\n</select>\n<select name='post[written_on(2i)]'>\n<option value='1'>January</option>\n<option value='2'>February</option>\n<option value='3'>March</option>\n<option value='4'>April</option>\n<option value='5'>May</option>\n<option value='6' selected=\"selected\">June</option>\n<option value='7'>July</option>\n<option value='8'>August</option>\n<option value='9'>September</option>\n<option value='10'>October</option>\n<option value='11'>November</option>\n<option value='12'>December</option>\n</select>\n<select name='post[written_on(3i)]'>\n<option>1</option>\n<option>2</option>\n<option>3</option>\n<option>4</option>\n<option>5</option>\n<option>6</option>\n<option>7</option>\n<option>8</option>\n<option>9</option>\n<option>10</option>\n<option>11</option>\n<option>12</option>\n<option>13</option>\n<option>14</option>\n<option selected=\"selected\">15</option>\n<option>16</option>\n<option>17</option>\n<option>18</option>\n<option>19</option>\n<option>20</option>\n<option>21</option>\n<option>22</option>\n<option>23</option>\n<option>24</option>\n<option>25</option>\n<option>26</option>\n<option>27</option>\n<option>28</option>\n<option>29</option>\n<option>30</option>\n<option>31</option>\n</select>\n</p><input type='submit' value='Create' /></form>", + form("post") + ) + end + + def test_form_with_datetime + def Post.content_columns() [ Column.new(:datetime, "written_on", "Written on") ] end + @post.written_on = Time.gm(2004, 6, 15, 16, 30) + + assert_equal( + "<form action='create' method='POST'><p><label for=\"post_written_on\">Written on</label><br /><select name='post[written_on(1i)]'>\n<option>1999</option>\n<option>2000</option>\n<option>2001</option>\n<option>2002</option>\n<option>2003</option>\n<option selected=\"selected\">2004</option>\n<option>2005</option>\n<option>2006</option>\n<option>2007</option>\n<option>2008</option>\n<option>2009</option>\n</select>\n<select name='post[written_on(2i)]'>\n<option value='1'>January</option>\n<option value='2'>February</option>\n<option value='3'>March</option>\n<option value='4'>April</option>\n<option value='5'>May</option>\n<option value='6' selected=\"selected\">June</option>\n<option value='7'>July</option>\n<option value='8'>August</option>\n<option value='9'>September</option>\n<option value='10'>October</option>\n<option value='11'>November</option>\n<option value='12'>December</option>\n</select>\n<select name='post[written_on(3i)]'>\n<option>1</option>\n<option>2</option>\n<option>3</option>\n<option>4</option>\n<option>5</option>\n<option>6</option>\n<option>7</option>\n<option>8</option>\n<option>9</option>\n<option>10</option>\n<option>11</option>\n<option>12</option>\n<option>13</option>\n<option>14</option>\n<option selected=\"selected\">15</option>\n<option>16</option>\n<option>17</option>\n<option>18</option>\n<option>19</option>\n<option>20</option>\n<option>21</option>\n<option>22</option>\n<option>23</option>\n<option>24</option>\n<option>25</option>\n<option>26</option>\n<option>27</option>\n<option>28</option>\n<option>29</option>\n<option>30</option>\n<option>31</option>\n</select>\n — <select name='post[written_on(4i)]'>\n<option>00</option>\n<option>01</option>\n<option>02</option>\n<option>03</option>\n<option>04</option>\n<option>05</option>\n<option>06</option>\n<option>07</option>\n<option>08</option>\n<option>09</option>\n<option>10</option>\n<option>11</option>\n<option>12</option>\n<option>13</option>\n<option>14</option>\n<option>15</option>\n<option selected=\"selected\">16</option>\n<option>17</option>\n<option>18</option>\n<option>19</option>\n<option>20</option>\n<option>21</option>\n<option>22</option>\n<option>23</option>\n</select>\n : <select name='post[written_on(5i)]'>\n<option>00</option>\n<option>01</option>\n<option>02</option>\n<option>03</option>\n<option>04</option>\n<option>05</option>\n<option>06</option>\n<option>07</option>\n<option>08</option>\n<option>09</option>\n<option>10</option>\n<option>11</option>\n<option>12</option>\n<option>13</option>\n<option>14</option>\n<option>15</option>\n<option>16</option>\n<option>17</option>\n<option>18</option>\n<option>19</option>\n<option>20</option>\n<option>21</option>\n<option>22</option>\n<option>23</option>\n<option>24</option>\n<option>25</option>\n<option>26</option>\n<option>27</option>\n<option>28</option>\n<option>29</option>\n<option selected=\"selected\">30</option>\n<option>31</option>\n<option>32</option>\n<option>33</option>\n<option>34</option>\n<option>35</option>\n<option>36</option>\n<option>37</option>\n<option>38</option>\n<option>39</option>\n<option>40</option>\n<option>41</option>\n<option>42</option>\n<option>43</option>\n<option>44</option>\n<option>45</option>\n<option>46</option>\n<option>47</option>\n<option>48</option>\n<option>49</option>\n<option>50</option>\n<option>51</option>\n<option>52</option>\n<option>53</option>\n<option>54</option>\n<option>55</option>\n<option>56</option>\n<option>57</option>\n<option>58</option>\n<option>59</option>\n</select>\n</p><input type='submit' value='Create' /></form>", + form("post") + ) + end +end
\ No newline at end of file diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb new file mode 100755 index 0000000000..a8ad37918d --- /dev/null +++ b/actionpack/test/template/date_helper_test.rb @@ -0,0 +1,104 @@ +require 'test/unit' +require File.dirname(__FILE__) + '/../../lib/action_view/helpers/date_helper' + +class DateHelperTest < Test::Unit::TestCase + include ActionView::Helpers::DateHelper + + def test_distance_in_words + from = Time.mktime(2004, 3, 6, 21, 41, 18) + + assert_equal "less than a minute", distance_of_time_in_words(from, Time.mktime(2004, 3, 6, 21, 41, 25)) + assert_equal "5 minutes", distance_of_time_in_words(from, Time.mktime(2004, 3, 6, 21, 46, 25)) + assert_equal "about 1 hour", distance_of_time_in_words(from, Time.mktime(2004, 3, 6, 22, 47, 25)) + assert_equal "about 3 hours", distance_of_time_in_words(from, Time.mktime(2004, 3, 7, 0, 41)) + assert_equal "about 4 hours", distance_of_time_in_words(from, Time.mktime(2004, 3, 7, 1, 20)) + assert_equal "2 days", distance_of_time_in_words(from, Time.mktime(2004, 3, 9, 15, 40)) + end + + def test_select_day + expected = "<select name='date[day]'>\n" + expected << +"<option>1</option>\n<option>2</option>\n<option>3</option>\n<option>4</option>\n<option>5</option>\n<option>6</option>\n<option>7</option>\n<option>8</option>\n<option>9</option>\n<option>10</option>\n<option>11</option>\n<option>12</option>\n<option>13</option>\n<option>14</option>\n<option>15</option>\n<option selected=\"selected\">16</option>\n<option>17</option>\n<option>18</option>\n<option>19</option>\n<option>20</option>\n<option>21</option>\n<option>22</option>\n<option>23</option>\n<option>24</option>\n<option>25</option>\n<option>26</option>\n<option>27</option>\n<option>28</option>\n<option>29</option>\n<option>30</option>\n<option>31</option>\n" + expected << "</select>\n" + + assert_equal expected, select_day(Time.mktime(2003, 8, 16)) + assert_equal expected, select_day(16) + end + + def test_select_day_with_blank + expected = "<select name='date[day]'>\n" + expected << +"<option></option>\n<option>1</option>\n<option>2</option>\n<option>3</option>\n<option>4</option>\n<option>5</option>\n<option>6</option>\n<option>7</option>\n<option>8</option>\n<option>9</option>\n<option>10</option>\n<option>11</option>\n<option>12</option>\n<option>13</option>\n<option>14</option>\n<option>15</option>\n<option selected=\"selected\">16</option>\n<option>17</option>\n<option>18</option>\n<option>19</option>\n<option>20</option>\n<option>21</option>\n<option>22</option>\n<option>23</option>\n<option>24</option>\n<option>25</option>\n<option>26</option>\n<option>27</option>\n<option>28</option>\n<option>29</option>\n<option>30</option>\n<option>31</option>\n" + expected << "</select>\n" + + assert_equal expected, select_day(Time.mktime(2003, 8, 16), :include_blank => true) + assert_equal expected, select_day(16, :include_blank => true) + end + + def test_select_month + expected = "<select name='date[month]'>\n" + expected << "<option value='1'>January</option>\n<option value='2'>February</option>\n<option value='3'>March</option>\n<option value='4'>April</option>\n<option value='5'>May</option>\n<option value='6'>June</option>\n<option value='7'>July</option>\n<option value='8' selected=\"selected\">August</option>\n<option value='9'>September</option>\n<option value='10'>October</option>\n<option value='11'>November</option>\n<option value='12'>December</option>\n" + expected << "</select>\n" + + assert_equal expected, select_month(Time.mktime(2003, 8, 16)) + assert_equal expected, select_month(8) + end + + def test_select_month_with_numbers + expected = "<select name='date[month]'>\n" + expected << "<option value='1'>1</option>\n<option value='2'>2</option>\n<option value='3'>3</option>\n<option value='4'>4</option>\n<option value='5'>5</option>\n<option value='6'>6</option>\n<option value='7'>7</option>\n<option value='8' selected=\"selected\">8</option>\n<option value='9'>9</option>\n<option value='10'>10</option>\n<option value='11'>11</option>\n<option value='12'>12</option>\n" + expected << "</select>\n" + + assert_equal expected, select_month(Time.mktime(2003, 8, 16), :use_month_numbers => true) + assert_equal expected, select_month(8, :use_month_numbers => true) + end + + def test_select_month_with_numbers_and_names + expected = "<select name='date[month]'>\n" + expected << "<option value='1'>1 - January</option>\n<option value='2'>2 - February</option>\n<option value='3'>3 - March</option>\n<option value='4'>4 - April</option>\n<option value='5'>5 - May</option>\n<option value='6'>6 - June</option>\n<option value='7'>7 - July</option>\n<option value='8' selected=\"selected\">8 - August</option>\n<option value='9'>9 - September</option>\n<option value='10'>10 - October</option>\n<option value='11'>11 - November</option>\n<option value='12'>12 - December</option>\n" + expected << "</select>\n" + + assert_equal expected, select_month(Time.mktime(2003, 8, 16), :add_month_numbers => true) + assert_equal expected, select_month(8, :add_month_numbers => true) + end + + def test_select_year + expected = "<select name='date[year]'>\n" + expected << "<option selected=\"selected\">2003</option>\n<option>2004</option>\n<option>2005</option>\n" + expected << "</select>\n" + + assert_equal expected, select_year(Time.mktime(2003, 8, 16), :start_year => 2003, :end_year => 2005) + assert_equal expected, select_year(2003, :start_year => 2003, :end_year => 2005) + end + + def test_select_year_with_type_discarding + expected = "<select name='date_year'>\n" + expected << "<option selected=\"selected\">2003</option>\n<option>2004</option>\n<option>2005</option>\n" + expected << "</select>\n" + + assert_equal expected, select_year( + Time.mktime(2003, 8, 16), :prefix => "date_year", :discard_type => true, :start_year => 2003, :end_year => 2005) + assert_equal expected, select_year( + 2003, :prefix => "date_year", :discard_type => true, :start_year => 2003, :end_year => 2005) + end + + + def test_select_date + expected = "<select name='date[first][year]'>\n" + expected << "<option selected=\"selected\">2003</option>\n<option>2004</option>\n<option>2005</option>\n" + expected << "</select>\n" + + expected << "<select name='date[first][month]'>\n" + expected << "<option value='1'>January</option>\n<option value='2'>February</option>\n<option value='3'>March</option>\n<option value='4'>April</option>\n<option value='5'>May</option>\n<option value='6'>June</option>\n<option value='7'>July</option>\n<option value='8' selected=\"selected\">August</option>\n<option value='9'>September</option>\n<option value='10'>October</option>\n<option value='11'>November</option>\n<option value='12'>December</option>\n" + expected << "</select>\n" + + expected << "<select name='date[first][day]'>\n" + expected << +"<option>1</option>\n<option>2</option>\n<option>3</option>\n<option>4</option>\n<option>5</option>\n<option>6</option>\n<option>7</option>\n<option>8</option>\n<option>9</option>\n<option>10</option>\n<option>11</option>\n<option>12</option>\n<option>13</option>\n<option>14</option>\n<option>15</option>\n<option selected=\"selected\">16</option>\n<option>17</option>\n<option>18</option>\n<option>19</option>\n<option>20</option>\n<option>21</option>\n<option>22</option>\n<option>23</option>\n<option>24</option>\n<option>25</option>\n<option>26</option>\n<option>27</option>\n<option>28</option>\n<option>29</option>\n<option>30</option>\n<option>31</option>\n" + expected << "</select>\n" + + assert_equal expected, select_date( + Time.mktime(2003, 8, 16), :start_year => 2003, :end_year => 2005, :prefix => "date[first]" + ) + end +end diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb new file mode 100644 index 0000000000..8f3d5ebb94 --- /dev/null +++ b/actionpack/test/template/form_helper_test.rb @@ -0,0 +1,124 @@ +require 'test/unit' +require File.dirname(__FILE__) + '/../../lib/action_view/helpers/form_helper' + +class FormHelperTest < Test::Unit::TestCase + include ActionView::Helpers::FormHelper + + old_verbose, $VERBOSE = $VERBOSE, nil + Post = Struct.new("Post", :title, :author_name, :body, :secret, :written_on) + $VERBOSE = old_verbose + + def setup + @post = Post.new + def @post.errors() Class.new{ def on(field) field == "author_name" end }.new end + + @post.title = "Hello World" + @post.author_name = "" + @post.body = "Back to the hill and over it again!" + @post.secret = 1 + @post.written_on = Date.new(2004, 6, 15) + end + + def test_text_field + assert_equal( + '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title") + ) + assert_equal( + '<input id="post_title" name="post[title]" size="30" type="password" value="Hello World" />', password_field("post", "title") + ) + assert_equal( + '<input id="person_name" name="person[name]" size="30" type="password" value="" />', password_field("person", "name") + ) + end + + def test_text_field_with_escapes + @post.title = "<b>Hello World</b>" + assert_equal( + '<input id="post_title" name="post[title]" size="30" type="text" value="<b>Hello World</b>" />', text_field("post", "title") + ) + end + + def test_text_field_with_options + assert_equal( + '<input id="post_title" name="post[title]" size="35" type="text" value="Hello World" />', + text_field("post", "title", "size" => "35") + ) + end + + def test_text_field_assuming_size + assert_equal( + '<input id="post_title" maxlength="35" name="post[title]" size="35" type="text" value="Hello World" />', + text_field("post", "title", "maxlength" => 35) + ) + end + + def test_check_box + assert_equal( + '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />', + check_box("post", "secret") + ) + + @post.secret = 0 + assert_equal( + '<input id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />', + check_box("post", "secret") + ) + + @post.secret = true + assert_equal( + '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />', + check_box("post", "secret") + ) + end + + def test_text_area + assert_equal( + '<textarea cols="40" id="post_body" name="post[body]" rows="20" wrap="virtual">Back to the hill and over it again!</textarea>', + text_area("post", "body") + ) + end + + def test_text_area_with_escapes + @post.body = "Back to <i>the</i> hill and over it again!" + assert_equal( + '<textarea cols="40" id="post_body" name="post[body]" rows="20" wrap="virtual">Back to <i>the</i> hill and over it again!</textarea>', + text_area("post", "body") + ) + end + + def test_date_selects + assert_equal( + '<textarea cols="40" id="post_body" name="post[body]" rows="20" wrap="virtual">Back to the hill and over it again!</textarea>', + text_area("post", "body") + ) + end + + + def test_explicit_name + assert_equal( + '<input id="post_title" name="dont guess" size="30" type="text" value="Hello World" />', text_field("post", "title", "name" => "dont guess") + ) + assert_equal( + '<textarea cols="40" id="post_body" name="really!" rows="20" wrap="virtual">Back to the hill and over it again!</textarea>', + text_area("post", "body", "name" => "really!") + ) + assert_equal( + '<input checked="checked" id="post_secret" name="i mean it" type="checkbox" value="1" /><input name="i mean it" type="hidden" value="0" />', + check_box("post", "secret", "name" => "i mean it") + ) + end + + def test_explicit_id + assert_equal( + '<input id="dont guess" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title", "id" => "dont guess") + ) + assert_equal( + '<textarea cols="40" id="really!" name="post[body]" rows="20" wrap="virtual">Back to the hill and over it again!</textarea>', + text_area("post", "body", "id" => "really!") + ) + assert_equal( + '<input checked="checked" id="i mean it" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />', + check_box("post", "secret", "id" => "i mean it") + ) + end +end diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb new file mode 100644 index 0000000000..fa0a37aa36 --- /dev/null +++ b/actionpack/test/template/form_options_helper_test.rb @@ -0,0 +1,165 @@ +require 'test/unit' +require File.dirname(__FILE__) + '/../../lib/action_view/helpers/form_options_helper' + +class FormOptionsHelperTest < Test::Unit::TestCase + include ActionView::Helpers::FormOptionsHelper + + old_verbose, $VERBOSE = $VERBOSE, nil + Post = Struct.new('Post', :title, :author_name, :body, :secret, :written_on, :category, :origin) + Continent = Struct.new('Continent', :continent_name, :countries) + Country = Struct.new('Country', :country_id, :country_name) + $VERBOSE = old_verbose + + def test_collection_options + @posts = [ + Post.new("<Abe> went home", "<Abe>", "To a little house", "shh!"), + Post.new("Babe went home", "Babe", "To a little house", "shh!"), + Post.new("Cabe went home", "Cabe", "To a little house", "shh!") + ] + + assert_equal( + "<option value=\"<Abe>\"><Abe> went home</option>\n<option value=\"Babe\">Babe went home</option>\n<option value=\"Cabe\">Cabe went home</option>", + options_from_collection_for_select(@posts, "author_name", "title") + ) + end + + + def test_collection_options_with_preselected_value + @posts = [ + Post.new("<Abe> went home", "<Abe>", "To a little house", "shh!"), + Post.new("Babe went home", "Babe", "To a little house", "shh!"), + Post.new("Cabe went home", "Cabe", "To a little house", "shh!") + ] + + assert_equal( + "<option value=\"<Abe>\"><Abe> went home</option>\n<option value=\"Babe\" selected=\"selected\">Babe went home</option>\n<option value=\"Cabe\">Cabe went home</option>", + options_from_collection_for_select(@posts, "author_name", "title", "Babe") + ) + end + + def test_collection_options_with_preselected_value_array + @posts = [ + Post.new("<Abe> went home", "<Abe>", "To a little house", "shh!"), + Post.new("Babe went home", "Babe", "To a little house", "shh!"), + Post.new("Cabe went home", "Cabe", "To a little house", "shh!") + ] + + assert_equal( + "<option value=\"<Abe>\"><Abe> went home</option>\n<option value=\"Babe\" selected=\"selected\">Babe went home</option>\n<option value=\"Cabe\" selected=\"selected\">Cabe went home</option>", + options_from_collection_for_select(@posts, "author_name", "title", [ "Babe", "Cabe" ]) + ) + end + + def test_array_options_for_select + assert_equal( + "<option><Denmark></option>\n<option>USA</option>\n<option>Sweden</option>", + options_for_select([ "<Denmark>", "USA", "Sweden" ]) + ) + end + + def test_array_options_for_select_with_selection + assert_equal( + "<option>Denmark</option>\n<option selected=\"selected\"><USA></option>\n<option>Sweden</option>", + options_for_select([ "Denmark", "<USA>", "Sweden" ], "<USA>") + ) + end + + def test_array_options_for_select_with_selection_array + assert_equal( + "<option>Denmark</option>\n<option selected=\"selected\"><USA></option>\n<option selected=\"selected\">Sweden</option>", + options_for_select([ "Denmark", "<USA>", "Sweden" ], [ "<USA>", "Sweden" ]) + ) + end + + def test_hash_options_for_select + assert_equal( + "<option value=\"<Kroner>\"><DKR></option>\n<option value=\"Dollar\">$</option>", + options_for_select({ "$" => "Dollar", "<DKR>" => "<Kroner>" }) + ) + end + + def test_hash_options_for_select_with_selection + assert_equal( + "<option value=\"<Kroner>\"><DKR></option>\n<option value=\"Dollar\" selected=\"selected\">$</option>", + options_for_select({ "$" => "Dollar", "<DKR>" => "<Kroner>" }, "Dollar") + ) + end + + def test_hash_options_for_select_with_selection + assert_equal( + "<option value=\"<Kroner>\" selected=\"selected\"><DKR></option>\n<option value=\"Dollar\" selected=\"selected\">$</option>", + options_for_select({ "$" => "Dollar", "<DKR>" => "<Kroner>" }, [ "Dollar", "<Kroner>" ]) + ) + end + + def test_html_option_groups_from_collection + @continents = [ + Continent.new("<Africa>", [Country.new("<sa>", "<South Africa>"), Country.new("so", "Somalia")] ), + Continent.new("Europe", [Country.new("dk", "Denmark"), Country.new("ie", "Ireland")] ) + ] + + assert_equal( + "<optgroup label=\"<Africa>\"><option value=\"<sa>\"><South Africa></option>\n<option value=\"so\">Somalia</option></optgroup><optgroup label=\"Europe\"><option value=\"dk\" selected=\"selected\">Denmark</option>\n<option value=\"ie\">Ireland</option></optgroup>", + option_groups_from_collection_for_select(@continents, "countries", "continent_name", "country_id", "country_name", "dk") + ) + end + + def test_select + @post = Post.new + @post.category = "<mus>" + assert_equal( + "<select id=\"post_category\" name=\"post[category]\"><option>abe</option>\n<option selected=\"selected\"><mus></option>\n<option>hest</option></select>", + select("post", "category", %w( abe <mus> hest)) + ) + end + + def test_select_with_blank + @post = Post.new + @post.category = "<mus>" + assert_equal( + "<select id=\"post_category\" name=\"post[category]\"><option></option>\n<option>abe</option>\n<option selected=\"selected\"><mus></option>\n<option>hest</option></select>", + select("post", "category", %w( abe <mus> hest), :include_blank => true) + ) + end + + def test_collection_select + @posts = [ + Post.new("<Abe> went home", "<Abe>", "To a little house", "shh!"), + Post.new("Babe went home", "Babe", "To a little house", "shh!"), + Post.new("Cabe went home", "Cabe", "To a little house", "shh!") + ] + + @post = Post.new + @post.author_name = "Babe" + + assert_equal( + "<select id=\"post_author_name\" name=\"post[author_name]\"><option value=\"<Abe>\"><Abe></option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>", + collection_select("post", "author_name", @posts, "author_name", "author_name") + ) + end + + def test_collection_select_with_blank_and_style + @posts = [ + Post.new("<Abe> went home", "<Abe>", "To a little house", "shh!"), + Post.new("Babe went home", "Babe", "To a little house", "shh!"), + Post.new("Cabe went home", "Cabe", "To a little house", "shh!") + ] + + @post = Post.new + @post.author_name = "Babe" + + assert_equal( + "<select id=\"post_author_name\" name=\"post[author_name]\" style=\"width: 200px\"><option></option>\n<option value=\"<Abe>\"><Abe></option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>", + collection_select("post", "author_name", @posts, "author_name", "author_name", { :include_blank => true }, "style" => "width: 200px") + ) + end + + def test_country_select + @post = Post.new + @post.origin = "Denmark" + assert_equal( + "<select id=\"post_origin\" name=\"post[origin]\"><option>Albania</option>\n<option>Algeria</option>\n<option>American Samoa</option>\n<option>Andorra</option>\n<option>Angola</option>\n<option>Anguilla</option>\n<option>Antarctica</option>\n<option>Antigua And Barbuda</option>\n<option>Argentina</option>\n<option>Armenia</option>\n<option>Aruba</option>\n<option>Australia</option>\n<option>Austria</option>\n<option>Azerbaijan</option>\n<option>Bahamas</option>\n<option>Bahrain</option>\n<option>Bangladesh</option>\n<option>Barbados</option>\n<option>Belarus</option>\n<option>Belgium</option>\n<option>Belize</option>\n<option>Benin</option>\n<option>Bermuda</option>\n<option>Bhutan</option>\n<option>Bolivia</option>\n<option>Bosnia and Herzegowina</option>\n<option>Botswana</option>\n<option>Bouvet Island</option>\n<option>Brazil</option>\n<option>British Indian Ocean Territory</option>\n<option>Brunei Darussalam</option>\n<option>Bulgaria</option>\n<option>Burkina Faso</option>\n<option>Burma</option>\n<option>Burundi</option>\n<option>Cambodia</option>\n<option>Cameroon</option>\n<option>Canada</option>\n<option>Cape Verde</option>\n<option>Cayman Islands</option>\n<option>Central African Republic</option>\n<option>Chad</option>\n<option>Chile</option>\n<option>China</option>\n<option>Christmas Island</option>\n<option>Cocos (Keeling) Islands</option>\n<option>Colombia</option>\n<option>Comoros</option>\n<option>Congo</option>\n<option>Congo, the Democratic Republic of the</option>\n<option>Cook Islands</option>\n<option>Costa Rica</option>\n<option>Cote d'Ivoire</option>\n<option>Croatia</option>\n<option>Cyprus</option>\n<option>Czech Republic</option>\n<option selected=\"selected\">Denmark</option>\n<option>Djibouti</option>\n<option>Dominica</option>\n<option>Dominican Republic</option>\n<option>East Timor</option>\n<option>Ecuador</option>\n<option>Egypt</option>\n<option>El Salvador</option>\n<option>England</option>\n<option>Equatorial Guinea</option>\n<option>Eritrea</option>\n<option>Espana</option>\n<option>Estonia</option>\n<option>Ethiopia</option>\n<option>Falkland Islands</option>\n<option>Faroe Islands</option>\n<option>Fiji</option>\n<option>Finland</option>\n<option>France</option>\n<option>French Guiana</option>\n<option>French Polynesia</option>\n<option>French Southern Territories</option>\n<option>Gabon</option>\n<option>Gambia</option>\n<option>Georgia</option>\n<option>Germany</option>\n<option>Ghana</option>\n<option>Gibraltar</option>\n<option>Great Britain</option>\n<option>Greece</option>\n<option>Greenland</option>\n<option>Grenada</option>\n<option>Guadeloupe</option>\n<option>Guam</option>\n<option>Guatemala</option>\n<option>Guinea</option>\n<option>Guinea-Bissau</option>\n<option>Guyana</option>\n<option>Haiti</option>\n<option>Heard and Mc Donald Islands</option>\n<option>Honduras</option>\n<option>Hong Kong</option>\n<option>Hungary</option>\n<option>Iceland</option>\n<option>India</option>\n<option>Indonesia</option>\n<option>Ireland</option>\n<option>Israel</option>\n<option>Italy</option>\n<option>Jamaica</option>\n<option>Japan</option>\n<option>Jordan</option>\n<option>Kazakhstan</option>\n<option>Kenya</option>\n<option>Kiribati</option>\n<option>Korea, Republic of</option>\n<option>Korea (South)</option>\n<option>Kuwait</option>\n<option>Kyrgyzstan</option>\n<option>Lao People's Democratic Republic</option>\n<option>Latvia</option>\n<option>Lebanon</option>\n<option>Lesotho</option>\n<option>Liberia</option>\n<option>Liechtenstein</option>\n<option>Lithuania</option>\n<option>Luxembourg</option>\n<option>Macau</option>\n<option>Macedonia</option>\n<option>Madagascar</option>\n<option>Malawi</option>\n<option>Malaysia</option>\n<option>Maldives</option>\n<option>Mali</option>\n<option>Malta</option>\n<option>Marshall Islands</option>\n<option>Martinique</option>\n<option>Mauritania</option>\n<option>Mauritius</option>\n<option>Mayotte</option>\n<option>Mexico</option>\n<option>Micronesia, Federated States of</option>\n<option>Moldova, Republic of</option>\n<option>Monaco</option>\n<option>Mongolia</option>\n<option>Montserrat</option>\n<option>Morocco</option>\n<option>Mozambique</option>\n<option>Myanmar</option>\n<option>Namibia</option>\n<option>Nauru</option>\n<option>Nepal</option>\n<option>Netherlands</option>\n<option>Netherlands Antilles</option>\n<option>New Caledonia</option>\n<option>New Zealand</option>\n<option>Nicaragua</option>\n<option>Niger</option>\n<option>Nigeria</option>\n<option>Niue</option>\n<option>Norfolk Island</option>\n<option>Northern Ireland</option>\n<option>Northern Mariana Islands</option>\n<option>Norway</option>\n<option>Oman</option>\n<option>Pakistan</option>\n<option>Palau</option>\n<option>Panama</option>\n<option>Papua New Guinea</option>\n<option>Paraguay</option>\n<option>Peru</option>\n<option>Philippines</option>\n<option>Pitcairn</option>\n<option>Poland</option>\n<option>Portugal</option>\n<option>Puerto Rico</option>\n<option>Qatar</option>\n<option>Reunion</option>\n<option>Romania</option>\n<option>Russia</option>\n<option>Rwanda</option>\n<option>Saint Kitts and Nevis</option>\n<option>Saint Lucia</option>\n<option>Saint Vincent and the Grenadines</option>\n<option>Samoa (Independent)</option>\n<option>San Marino</option>\n<option>Sao Tome and Principe</option>\n<option>Saudi Arabia</option>\n<option>Scotland</option>\n<option>Senegal</option>\n<option>Seychelles</option>\n<option>Sierra Leone</option>\n<option>Singapore</option>\n<option>Slovakia</option>\n<option>Slovenia</option>\n<option>Solomon Islands</option>\n<option>Somalia</option>\n<option>South Africa</option>\n<option>South Georgia and the South Sandwich Islands</option>\n<option>South Korea</option>\n<option>Spain</option>\n<option>Sri Lanka</option>\n<option>St. Helena</option>\n<option>St. Pierre and Miquelon</option>\n<option>Suriname</option>\n<option>Svalbard and Jan Mayen Islands</option>\n<option>Swaziland</option>\n<option>Sweden</option>\n<option>Switzerland</option>\n<option>Taiwan</option>\n<option>Tajikistan</option>\n<option>Tanzania</option>\n<option>Thailand</option>\n<option>Togo</option>\n<option>Tokelau</option>\n<option>Tonga</option>\n<option>Trinidad</option>\n<option>Trinidad and Tobago</option>\n<option>Tunisia</option>\n<option>Turkey</option>\n<option>Turkmenistan</option>\n<option>Turks and Caicos Islands</option>\n<option>Tuvalu</option>\n<option>Uganda</option>\n<option>Ukraine</option>\n<option>United Arab Emirates</option>\n<option>United Kingdom</option>\n<option>United States</option>\n<option>United States Minor Outlying Islands</option>\n<option>Uruguay</option>\n<option>Uzbekistan</option>\n<option>Vanuatu</option>\n<option>Vatican City State (Holy See)</option>\n<option>Venezuela</option>\n<option>Viet Nam</option>\n<option>Virgin Islands (British)</option>\n<option>Virgin Islands (U.S.)</option>\n<option>Wales</option>\n<option>Wallis and Futuna Islands</option>\n<option>Western Sahara</option>\n<option>Yemen</option>\n<option>Zambia</option>\n<option>Zimbabwe</option></select>", + country_select("post", "origin") + ) + end +end diff --git a/actionpack/test/template/tag_helper_test.rb b/actionpack/test/template/tag_helper_test.rb new file mode 100644 index 0000000000..c3289af50c --- /dev/null +++ b/actionpack/test/template/tag_helper_test.rb @@ -0,0 +1,18 @@ +require 'test/unit' +require File.dirname(__FILE__) + '/../../lib/action_view/helpers/tag_helper' +require File.dirname(__FILE__) + '/../../lib/action_view/helpers/url_helper' + +class TagHelperTest < Test::Unit::TestCase + include ActionView::Helpers::TagHelper + include ActionView::Helpers::UrlHelper + + def test_tag + assert_equal "<p class=\"show\" />", tag("p", "class" => "show") + end + + def test_content_tag + assert_equal "<a href=\"create\">Create</a>", content_tag("a", "Create", "href" => "create") + end + + # FIXME: Test form tag +end
\ No newline at end of file diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb new file mode 100644 index 0000000000..347420a72b --- /dev/null +++ b/actionpack/test/template/text_helper_test.rb @@ -0,0 +1,62 @@ +require 'test/unit' +require File.dirname(__FILE__) + '/../../lib/action_view/helpers/text_helper' + +class TextHelperTest < Test::Unit::TestCase + include ActionView::Helpers::TextHelper + + def test_truncate + assert_equal "Hello World!", truncate("Hello World!", 12) + assert_equal "Hello Worl...", truncate("Hello World!!", 12) + end + + def test_strip_links + assert_equal "on my mind", strip_links("<a href='almost'>on my mind</a>") + end + + def test_highlighter + assert_equal( + "This is a <strong class=\"highlight\">beautiful</strong> morning", + highlight("This is a beautiful morning", "beautiful") + ) + + assert_equal( + "This is a <strong class=\"highlight\">beautiful</strong> morning, but also a <strong class=\"highlight\">beautiful</strong> day", + highlight("This is a beautiful morning, but also a beautiful day", "beautiful") + ) + + assert_equal( + "This is a <b>beautiful</b> morning, but also a <b>beautiful</b> day", + highlight("This is a beautiful morning, but also a beautiful day", "beautiful", '<b>\1</b>') + ) + end + + def test_highlighter_with_regexp + assert_equal( + "This is a <strong class=\"highlight\">beautiful!</strong> morning", + highlight("This is a beautiful! morning", "beautiful!") + ) + + assert_equal( + "This is a <strong class=\"highlight\">beautiful! morning</strong>", + highlight("This is a beautiful! morning", "beautiful! morning") + ) + + assert_equal( + "This is a <strong class=\"highlight\">beautiful? morning</strong>", + highlight("This is a beautiful? morning", "beautiful? morning") + ) + end + + def test_excerpt + assert_equal("...is a beautiful morni...", excerpt("This is a beautiful morning", "beautiful", 5)) + assert_equal("This is a...", excerpt("This is a beautiful morning", "this", 5)) + assert_equal("...iful morning", excerpt("This is a beautiful morning", "morning", 5)) + assert_equal("...iful morning", excerpt("This is a beautiful morning", "morning", 5)) + assert_nil excerpt("This is a beautiful morning", "day") + end + + def test_pluralization + assert_equal("1 count", pluralize(1, "count")) + assert_equal("2 counts", pluralize(2, "count")) + end +end
\ No newline at end of file diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb new file mode 100644 index 0000000000..198b26b113 --- /dev/null +++ b/actionpack/test/template/url_helper_test.rb @@ -0,0 +1,49 @@ +require 'test/unit' +require File.dirname(__FILE__) + '/../../lib/action_view/helpers/url_helper' +require File.dirname(__FILE__) + '/../../lib/action_view/helpers/tag_helper' + +class UrlHelperTest < Test::Unit::TestCase + include ActionView::Helpers::UrlHelper + include ActionView::Helpers::TagHelper + + def setup + @controller = Class.new do + def url_for(options, *parameters_for_method_reference) + "http://www.world.com" + end + end + @controller = @controller.new + end + + # todo: missing test cases + def test_link_tag_with_straight_url + assert_equal "<a href=\"http://www.world.com\">Hello</a>", link_to("Hello", "http://www.world.com") + end + + def test_link_tag_with_javascript_confirm + assert_equal( + "<a href=\"http://www.world.com\" onclick=\"return confirm('Are you sure?');\">Hello</a>", + link_to("Hello", "http://www.world.com", :confirm => "Are you sure?") + ) + end + + def test_link_unless_current + @params = { "controller" => "weblog", "action" => "show"} + assert_equal "Showing", link_to_unless_current("Showing", :action => "show", :controller => "weblog") + assert "<a href=\"http://www.world.com\">Listing</a>", link_to_unless_current("Listing", :action => "list", :controller => "weblog") + end + + def test_mail_to + assert_equal "<a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>", mail_to("david@loudthinking.com") + assert_equal "<a href=\"mailto:david@loudthinking.com\">David Heinemeier Hansson</a>", mail_to("david@loudthinking.com", "David Heinemeier Hansson") + assert_equal( + "<a class=\"admin\" href=\"mailto:david@loudthinking.com\">David Heinemeier Hansson</a>", + mail_to("david@loudthinking.com", "David Heinemeier Hansson", "class" => "admin") + ) + end + + def test_link_with_nil_html_options + assert "<a href=\"http://www.world.com\">Hello</a>", + link_to("Hello", {:action => 'myaction'}, nil) + end +end
\ No newline at end of file |