From 278186534c0ccf285a20497461f40d2e54aa20a0 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 3 Feb 2009 18:25:37 -0800 Subject: Bump mocha requirement for Ruby 1.9 compat. Remove uses_mocha. --- .../template/active_record_helper_i18n_test.rb | 64 +- actionpack/test/template/asset_tag_helper_test.rb | 26 +- .../test/template/compiled_templates_test.rb | 134 ++- actionpack/test/template/date_helper_i18n_test.rb | 176 ++- actionpack/test/template/date_helper_test.rb | 66 +- .../test/template/form_options_helper_test.rb | 1198 ++++++++++---------- .../test/template/number_helper_i18n_test.rb | 108 +- actionpack/test/template/test_test.rb | 10 +- .../test/template/translation_helper_test.rb | 36 +- 9 files changed, 899 insertions(+), 919 deletions(-) (limited to 'actionpack/test/template') diff --git a/actionpack/test/template/active_record_helper_i18n_test.rb b/actionpack/test/template/active_record_helper_i18n_test.rb index 7e6bf70706..4b6e8ddcca 100644 --- a/actionpack/test/template/active_record_helper_i18n_test.rb +++ b/actionpack/test/template/active_record_helper_i18n_test.rb @@ -4,43 +4,41 @@ class ActiveRecordHelperI18nTest < Test::Unit::TestCase include ActionView::Helpers::ActiveRecordHelper attr_reader :request - uses_mocha 'active_record_helper_i18n_test' do - def setup - @object = stub :errors => stub(:count => 1, :full_messages => ['full_messages']) - @object_name = 'book' - stubs(:content_tag).returns 'content_tag' + def setup + @object = stub :errors => stub(:count => 1, :full_messages => ['full_messages']) + @object_name = 'book' + stubs(:content_tag).returns 'content_tag' - I18n.stubs(:t).with(:'header', :locale => 'en', :scope => [:activerecord, :errors, :template], :count => 1, :model => '').returns "1 error prohibited this from being saved" - I18n.stubs(:t).with(:'body', :locale => 'en', :scope => [:activerecord, :errors, :template]).returns 'There were problems with the following fields:' - end + I18n.stubs(:t).with(:'header', :locale => 'en', :scope => [:activerecord, :errors, :template], :count => 1, :model => '').returns "1 error prohibited this from being saved" + I18n.stubs(:t).with(:'body', :locale => 'en', :scope => [:activerecord, :errors, :template]).returns 'There were problems with the following fields:' + end - def test_error_messages_for_given_a_header_option_it_does_not_translate_header_message - I18n.expects(:translate).with(:'header', :locale => 'en', :scope => [:activerecord, :errors, :template], :count => 1, :model => '').never - error_messages_for(:object => @object, :header_message => 'header message', :locale => 'en') - end + def test_error_messages_for_given_a_header_option_it_does_not_translate_header_message + I18n.expects(:translate).with(:'header', :locale => 'en', :scope => [:activerecord, :errors, :template], :count => 1, :model => '').never + error_messages_for(:object => @object, :header_message => 'header message', :locale => 'en') + end - def test_error_messages_for_given_no_header_option_it_translates_header_message - I18n.expects(:t).with(:'header', :locale => 'en', :scope => [:activerecord, :errors, :template], :count => 1, :model => '').returns 'header message' - I18n.expects(:t).with('', :default => '', :count => 1, :scope => [:activerecord, :models]).once.returns '' - error_messages_for(:object => @object, :locale => 'en') - end + def test_error_messages_for_given_no_header_option_it_translates_header_message + I18n.expects(:t).with(:'header', :locale => 'en', :scope => [:activerecord, :errors, :template], :count => 1, :model => '').returns 'header message' + I18n.expects(:t).with('', :default => '', :count => 1, :scope => [:activerecord, :models]).once.returns '' + error_messages_for(:object => @object, :locale => 'en') + end - def test_error_messages_for_given_a_message_option_it_does_not_translate_message - I18n.expects(:t).with(:'body', :locale => 'en', :scope => [:activerecord, :errors, :template]).never - I18n.expects(:t).with('', :default => '', :count => 1, :scope => [:activerecord, :models]).once.returns '' - error_messages_for(:object => @object, :message => 'message', :locale => 'en') - end + def test_error_messages_for_given_a_message_option_it_does_not_translate_message + I18n.expects(:t).with(:'body', :locale => 'en', :scope => [:activerecord, :errors, :template]).never + I18n.expects(:t).with('', :default => '', :count => 1, :scope => [:activerecord, :models]).once.returns '' + error_messages_for(:object => @object, :message => 'message', :locale => 'en') + end - def test_error_messages_for_given_no_message_option_it_translates_message - I18n.expects(:t).with(:'body', :locale => 'en', :scope => [:activerecord, :errors, :template]).returns 'There were problems with the following fields:' - I18n.expects(:t).with('', :default => '', :count => 1, :scope => [:activerecord, :models]).once.returns '' - error_messages_for(:object => @object, :locale => 'en') - end - - def test_error_messages_for_given_object_name_it_translates_object_name - I18n.expects(:t).with(:header, :locale => 'en', :scope => [:activerecord, :errors, :template], :count => 1, :model => @object_name).returns "1 error prohibited this #{@object_name} from being saved" - I18n.expects(:t).with(@object_name, :default => @object_name, :count => 1, :scope => [:activerecord, :models]).once.returns @object_name - error_messages_for(:object => @object, :locale => 'en', :object_name => @object_name) - end + def test_error_messages_for_given_no_message_option_it_translates_message + I18n.expects(:t).with(:'body', :locale => 'en', :scope => [:activerecord, :errors, :template]).returns 'There were problems with the following fields:' + I18n.expects(:t).with('', :default => '', :count => 1, :scope => [:activerecord, :models]).once.returns '' + error_messages_for(:object => @object, :locale => 'en') + end + + def test_error_messages_for_given_object_name_it_translates_object_name + I18n.expects(:t).with(:header, :locale => 'en', :scope => [:activerecord, :errors, :template], :count => 1, :model => @object_name).returns "1 error prohibited this #{@object_name} from being saved" + I18n.expects(:t).with(@object_name, :default => @object_name, :count => 1, :scope => [:activerecord, :models]).once.returns @object_name + error_messages_for(:object => @object, :locale => 'en', :object_name => @object_name) end end diff --git a/actionpack/test/template/asset_tag_helper_test.rb b/actionpack/test/template/asset_tag_helper_test.rb index 5e2fc20167..32ad87c12d 100644 --- a/actionpack/test/template/asset_tag_helper_test.rb +++ b/actionpack/test/template/asset_tag_helper_test.rb @@ -228,20 +228,18 @@ class AssetTagHelperTest < ActionView::TestCase ImageLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } end - uses_mocha 'test image tag with windows behaviour' do - def test_image_tag_windows_behaviour - old_asset_id, ENV["RAILS_ASSET_ID"] = ENV["RAILS_ASSET_ID"], "1" - # This simulates the behaviour of File#exist? on windows when testing a file ending in "." - # If the file "rails.png" exists, windows will return true when asked if "rails.png." exists (notice trailing ".") - # OS X, linux etc will return false in this case. - File.stubs(:exist?).with('template/../fixtures/public/images/rails.png.').returns(true) - assert_equal 'Rails', image_tag('rails.png') - ensure - if old_asset_id - ENV["RAILS_ASSET_ID"] = old_asset_id - else - ENV.delete("RAILS_ASSET_ID") - end + def test_image_tag_windows_behaviour + old_asset_id, ENV["RAILS_ASSET_ID"] = ENV["RAILS_ASSET_ID"], "1" + # This simulates the behaviour of File#exist? on windows when testing a file ending in "." + # If the file "rails.png" exists, windows will return true when asked if "rails.png." exists (notice trailing ".") + # OS X, linux etc will return false in this case. + File.stubs(:exist?).with('template/../fixtures/public/images/rails.png.').returns(true) + assert_equal 'Rails', image_tag('rails.png') + ensure + if old_asset_id + ENV["RAILS_ASSET_ID"] = old_asset_id + else + ENV.delete("RAILS_ASSET_ID") end end diff --git a/actionpack/test/template/compiled_templates_test.rb b/actionpack/test/template/compiled_templates_test.rb index caea1bd643..a7ed13cf57 100644 --- a/actionpack/test/template/compiled_templates_test.rb +++ b/actionpack/test/template/compiled_templates_test.rb @@ -1,87 +1,85 @@ require 'abstract_unit' require 'controller/fake_models' -uses_mocha 'TestTemplateRecompilation' do - class CompiledTemplatesTest < Test::Unit::TestCase - def setup - @compiled_templates = ActionView::Base::CompiledTemplates - @compiled_templates.instance_methods.each do |m| - @compiled_templates.send(:remove_method, m) if m =~ /^_run_/ - end +class CompiledTemplatesTest < Test::Unit::TestCase + def setup + @compiled_templates = ActionView::Base::CompiledTemplates + @compiled_templates.instance_methods.each do |m| + @compiled_templates.send(:remove_method, m) if m =~ /^_run_/ end + end - def test_template_gets_compiled - assert_equal 0, @compiled_templates.instance_methods.size - assert_equal "Hello world!", render(:file => "test/hello_world.erb") - assert_equal 1, @compiled_templates.instance_methods.size - end + def test_template_gets_compiled + assert_equal 0, @compiled_templates.instance_methods.size + assert_equal "Hello world!", render(:file => "test/hello_world.erb") + assert_equal 1, @compiled_templates.instance_methods.size + end - def test_template_gets_recompiled_when_using_different_keys_in_local_assigns - assert_equal 0, @compiled_templates.instance_methods.size - assert_equal "Hello world!", render(:file => "test/hello_world.erb") - assert_equal "Hello world!", render(:file => "test/hello_world.erb", :locals => {:foo => "bar"}) - assert_equal 2, @compiled_templates.instance_methods.size - end + def test_template_gets_recompiled_when_using_different_keys_in_local_assigns + assert_equal 0, @compiled_templates.instance_methods.size + assert_equal "Hello world!", render(:file => "test/hello_world.erb") + assert_equal "Hello world!", render(:file => "test/hello_world.erb", :locals => {:foo => "bar"}) + assert_equal 2, @compiled_templates.instance_methods.size + end - def test_compiled_template_will_not_be_recompiled_when_rendered_with_identical_local_assigns - assert_equal 0, @compiled_templates.instance_methods.size - assert_equal "Hello world!", render(:file => "test/hello_world.erb") - ActionView::Template.any_instance.expects(:compile!).never - assert_equal "Hello world!", render(:file => "test/hello_world.erb") - end + def test_compiled_template_will_not_be_recompiled_when_rendered_with_identical_local_assigns + assert_equal 0, @compiled_templates.instance_methods.size + assert_equal "Hello world!", render(:file => "test/hello_world.erb") + ActionView::Template.any_instance.expects(:compile!).never + assert_equal "Hello world!", render(:file => "test/hello_world.erb") + end - def test_compiled_template_will_always_be_recompiled_when_template_is_not_cached - ActionView::Template.any_instance.expects(:recompile?).times(3).returns(true) - assert_equal 0, @compiled_templates.instance_methods.size - assert_equal "Hello world!", render(:file => "#{FIXTURE_LOAD_PATH}/test/hello_world.erb") - ActionView::Template.any_instance.expects(:compile!).times(3) - 3.times { assert_equal "Hello world!", render(:file => "#{FIXTURE_LOAD_PATH}/test/hello_world.erb") } - assert_equal 1, @compiled_templates.instance_methods.size - end + def test_compiled_template_will_always_be_recompiled_when_template_is_not_cached + ActionView::Template.any_instance.expects(:recompile?).times(3).returns(true) + assert_equal 0, @compiled_templates.instance_methods.size + assert_equal "Hello world!", render(:file => "#{FIXTURE_LOAD_PATH}/test/hello_world.erb") + ActionView::Template.any_instance.expects(:compile!).times(3) + 3.times { assert_equal "Hello world!", render(:file => "#{FIXTURE_LOAD_PATH}/test/hello_world.erb") } + assert_equal 1, @compiled_templates.instance_methods.size + end - def test_template_changes_are_not_reflected_with_cached_templates - assert_equal "Hello world!", render(:file => "test/hello_world.erb") - modify_template "test/hello_world.erb", "Goodbye world!" do - assert_equal "Hello world!", render(:file => "test/hello_world.erb") - end + def test_template_changes_are_not_reflected_with_cached_templates + assert_equal "Hello world!", render(:file => "test/hello_world.erb") + modify_template "test/hello_world.erb", "Goodbye world!" do assert_equal "Hello world!", render(:file => "test/hello_world.erb") end + assert_equal "Hello world!", render(:file => "test/hello_world.erb") + end - def test_template_changes_are_reflected_with_uncached_templates - assert_equal "Hello world!", render_without_cache(:file => "test/hello_world.erb") - modify_template "test/hello_world.erb", "Goodbye world!" do - assert_equal "Goodbye world!", render_without_cache(:file => "test/hello_world.erb") - end - assert_equal "Hello world!", render_without_cache(:file => "test/hello_world.erb") + def test_template_changes_are_reflected_with_uncached_templates + assert_equal "Hello world!", render_without_cache(:file => "test/hello_world.erb") + modify_template "test/hello_world.erb", "Goodbye world!" do + assert_equal "Goodbye world!", render_without_cache(:file => "test/hello_world.erb") end + assert_equal "Hello world!", render_without_cache(:file => "test/hello_world.erb") + end - private - def render(*args) - render_with_cache(*args) - end + private + def render(*args) + render_with_cache(*args) + end - def render_with_cache(*args) - view_paths = ActionController::Base.view_paths - assert_equal ActionView::Template::EagerPath, view_paths.first.class - ActionView::Base.new(view_paths, {}).render(*args) - end + def render_with_cache(*args) + view_paths = ActionController::Base.view_paths + assert_equal ActionView::Template::EagerPath, view_paths.first.class + ActionView::Base.new(view_paths, {}).render(*args) + end - def render_without_cache(*args) - path = ActionView::Template::Path.new(FIXTURE_LOAD_PATH) - view_paths = ActionView::Base.process_view_paths(path) - assert_equal ActionView::Template::Path, view_paths.first.class - ActionView::Base.new(view_paths, {}).render(*args) - end + def render_without_cache(*args) + path = ActionView::Template::Path.new(FIXTURE_LOAD_PATH) + view_paths = ActionView::Base.process_view_paths(path) + assert_equal ActionView::Template::Path, view_paths.first.class + ActionView::Base.new(view_paths, {}).render(*args) + end - def modify_template(template, content) - filename = "#{FIXTURE_LOAD_PATH}/#{template}" - old_content = File.read(filename) - begin - File.open(filename, "wb+") { |f| f.write(content) } - yield - ensure - File.open(filename, "wb+") { |f| f.write(old_content) } - end + def modify_template(template, content) + filename = "#{FIXTURE_LOAD_PATH}/#{template}" + old_content = File.read(filename) + begin + File.open(filename, "wb+") { |f| f.write(content) } + yield + ensure + File.open(filename, "wb+") { |f| f.write(old_content) } end - end + end end diff --git a/actionpack/test/template/date_helper_i18n_test.rb b/actionpack/test/template/date_helper_i18n_test.rb index fac30da128..bc011f59b8 100644 --- a/actionpack/test/template/date_helper_i18n_test.rb +++ b/actionpack/test/template/date_helper_i18n_test.rb @@ -8,66 +8,64 @@ class DateHelperDistanceOfTimeInWordsI18nTests < Test::Unit::TestCase @from = Time.mktime(2004, 6, 6, 21, 45, 0) end - uses_mocha 'date_helper_distance_of_time_in_words_i18n_test' do - # distance_of_time_in_words - - def test_distance_of_time_in_words_calls_i18n - { # with include_seconds - [2.seconds, true] => [:'less_than_x_seconds', 5], - [9.seconds, true] => [:'less_than_x_seconds', 10], - [19.seconds, true] => [:'less_than_x_seconds', 20], - [30.seconds, true] => [:'half_a_minute', nil], - [59.seconds, true] => [:'less_than_x_minutes', 1], - [60.seconds, true] => [:'x_minutes', 1], - - # without include_seconds - [29.seconds, false] => [:'less_than_x_minutes', 1], - [60.seconds, false] => [:'x_minutes', 1], - [44.minutes, false] => [:'x_minutes', 44], - [61.minutes, false] => [:'about_x_hours', 1], - [24.hours, false] => [:'x_days', 1], - [30.days, false] => [:'about_x_months', 1], - [60.days, false] => [:'x_months', 2], - [1.year, false] => [:'about_x_years', 1], - [3.years, false] => [:'over_x_years', 3] - - }.each do |passed, expected| - assert_distance_of_time_in_words_translates_key passed, expected - end + # distance_of_time_in_words + + def test_distance_of_time_in_words_calls_i18n + { # with include_seconds + [2.seconds, true] => [:'less_than_x_seconds', 5], + [9.seconds, true] => [:'less_than_x_seconds', 10], + [19.seconds, true] => [:'less_than_x_seconds', 20], + [30.seconds, true] => [:'half_a_minute', nil], + [59.seconds, true] => [:'less_than_x_minutes', 1], + [60.seconds, true] => [:'x_minutes', 1], + + # without include_seconds + [29.seconds, false] => [:'less_than_x_minutes', 1], + [60.seconds, false] => [:'x_minutes', 1], + [44.minutes, false] => [:'x_minutes', 44], + [61.minutes, false] => [:'about_x_hours', 1], + [24.hours, false] => [:'x_days', 1], + [30.days, false] => [:'about_x_months', 1], + [60.days, false] => [:'x_months', 2], + [1.year, false] => [:'about_x_years', 1], + [3.years, false] => [:'over_x_years', 3] + + }.each do |passed, expected| + assert_distance_of_time_in_words_translates_key passed, expected end + end - def assert_distance_of_time_in_words_translates_key(passed, expected) - diff, include_seconds = *passed - key, count = *expected - to = @from + diff + def assert_distance_of_time_in_words_translates_key(passed, expected) + diff, include_seconds = *passed + key, count = *expected + to = @from + diff - options = {:locale => 'en', :scope => :'datetime.distance_in_words'} - options[:count] = count if count + options = {:locale => 'en', :scope => :'datetime.distance_in_words'} + options[:count] = count if count - I18n.expects(:t).with(key, options) - distance_of_time_in_words(@from, to, include_seconds, :locale => 'en') - end + I18n.expects(:t).with(key, options) + distance_of_time_in_words(@from, to, include_seconds, :locale => 'en') + end - def test_distance_of_time_pluralizations - { [:'less_than_x_seconds', 1] => 'less than 1 second', - [:'less_than_x_seconds', 2] => 'less than 2 seconds', - [:'less_than_x_minutes', 1] => 'less than a minute', - [:'less_than_x_minutes', 2] => 'less than 2 minutes', - [:'x_minutes', 1] => '1 minute', - [:'x_minutes', 2] => '2 minutes', - [:'about_x_hours', 1] => 'about 1 hour', - [:'about_x_hours', 2] => 'about 2 hours', - [:'x_days', 1] => '1 day', - [:'x_days', 2] => '2 days', - [:'about_x_years', 1] => 'about 1 year', - [:'about_x_years', 2] => 'about 2 years', - [:'over_x_years', 1] => 'over 1 year', - [:'over_x_years', 2] => 'over 2 years' - - }.each do |args, expected| - key, count = *args - assert_equal expected, I18n.t(key, :count => count, :scope => 'datetime.distance_in_words') - end + def test_distance_of_time_pluralizations + { [:'less_than_x_seconds', 1] => 'less than 1 second', + [:'less_than_x_seconds', 2] => 'less than 2 seconds', + [:'less_than_x_minutes', 1] => 'less than a minute', + [:'less_than_x_minutes', 2] => 'less than 2 minutes', + [:'x_minutes', 1] => '1 minute', + [:'x_minutes', 2] => '2 minutes', + [:'about_x_hours', 1] => 'about 1 hour', + [:'about_x_hours', 2] => 'about 2 hours', + [:'x_days', 1] => '1 day', + [:'x_days', 2] => '2 days', + [:'about_x_years', 1] => 'about 1 year', + [:'about_x_years', 2] => 'about 2 years', + [:'over_x_years', 1] => 'over 1 year', + [:'over_x_years', 2] => 'over 2 years' + + }.each do |args, expected| + key, count = *args + assert_equal expected, I18n.t(key, :count => count, :scope => 'datetime.distance_in_words') end end end @@ -76,49 +74,47 @@ class DateHelperSelectTagsI18nTests < Test::Unit::TestCase include ActionView::Helpers::DateHelper attr_reader :request - uses_mocha 'date_helper_select_tags_i18n_tests' do - def setup - @prompt_defaults = {:year => 'Year', :month => 'Month', :day => 'Day', :hour => 'Hour', :minute => 'Minute', :second => 'Seconds'} - - I18n.stubs(:translate).with(:'date.month_names', :locale => 'en').returns Date::MONTHNAMES - end + def setup + @prompt_defaults = {:year => 'Year', :month => 'Month', :day => 'Day', :hour => 'Hour', :minute => 'Minute', :second => 'Seconds'} - # select_month + I18n.stubs(:translate).with(:'date.month_names', :locale => 'en').returns Date::MONTHNAMES + end - def test_select_month_given_use_month_names_option_does_not_translate_monthnames - I18n.expects(:translate).never - select_month(8, :locale => 'en', :use_month_names => Date::MONTHNAMES) - end + # select_month - def test_select_month_translates_monthnames - I18n.expects(:translate).with(:'date.month_names', :locale => 'en').returns Date::MONTHNAMES - select_month(8, :locale => 'en') - end + def test_select_month_given_use_month_names_option_does_not_translate_monthnames + I18n.expects(:translate).never + select_month(8, :locale => 'en', :use_month_names => Date::MONTHNAMES) + end - def test_select_month_given_use_short_month_option_translates_abbr_monthnames - I18n.expects(:translate).with(:'date.abbr_month_names', :locale => 'en').returns Date::ABBR_MONTHNAMES - select_month(8, :locale => 'en', :use_short_month => true) - end + def test_select_month_translates_monthnames + I18n.expects(:translate).with(:'date.month_names', :locale => 'en').returns Date::MONTHNAMES + select_month(8, :locale => 'en') + end - def test_date_or_time_select_translates_prompts - @prompt_defaults.each do |key, prompt| - I18n.expects(:translate).with(('datetime.prompts.' + key.to_s).to_sym, :locale => 'en').returns prompt - end + def test_select_month_given_use_short_month_option_translates_abbr_monthnames + I18n.expects(:translate).with(:'date.abbr_month_names', :locale => 'en').returns Date::ABBR_MONTHNAMES + select_month(8, :locale => 'en', :use_short_month => true) + end - I18n.expects(:translate).with(:'date.order', :locale => 'en').returns [:year, :month, :day] - datetime_select('post', 'updated_at', :locale => 'en', :include_seconds => true, :prompt => true) + def test_date_or_time_select_translates_prompts + @prompt_defaults.each do |key, prompt| + I18n.expects(:translate).with(('datetime.prompts.' + key.to_s).to_sym, :locale => 'en').returns prompt end - # date_or_time_select + I18n.expects(:translate).with(:'date.order', :locale => 'en').returns [:year, :month, :day] + datetime_select('post', 'updated_at', :locale => 'en', :include_seconds => true, :prompt => true) + end - def test_date_or_time_select_given_an_order_options_does_not_translate_order - I18n.expects(:translate).never - datetime_select('post', 'updated_at', :order => [:year, :month, :day], :locale => 'en') - end + # date_or_time_select - def test_date_or_time_select_given_no_order_options_translates_order - I18n.expects(:translate).with(:'date.order', :locale => 'en').returns [:year, :month, :day] - datetime_select('post', 'updated_at', :locale => 'en') - end + def test_date_or_time_select_given_an_order_options_does_not_translate_order + I18n.expects(:translate).never + datetime_select('post', 'updated_at', :order => [:year, :month, :day], :locale => 'en') + end + + def test_date_or_time_select_given_no_order_options_translates_order + I18n.expects(:translate).with(:'date.order', :locale => 'en').returns [:year, :month, :day] + datetime_select('post', 'updated_at', :locale => 'en') end -end \ No newline at end of file +end diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb index 92cdce2e45..59e921f09b 100644 --- a/actionpack/test/template/date_helper_test.rb +++ b/actionpack/test/template/date_helper_test.rb @@ -1726,40 +1726,38 @@ class DateHelperTest < ActionView::TestCase assert_dom_equal expected, datetime_select("post", "updated_at") end - uses_mocha 'TestDatetimeSelectDefaultsToTimeZoneNowWhenConfigTimeZoneIsSet' do - def test_datetime_select_defaults_to_time_zone_now_when_config_time_zone_is_set - time = stub(:year => 2004, :month => 6, :day => 15, :hour => 16, :min => 35, :sec => 0) - time_zone = mock() - time_zone.expects(:now).returns time - Time.zone_default = time_zone - @post = Post.new - - expected = %{\n" - - expected << %{\n" - - expected << %{\n" - - expected << " — " - - expected << %{\n" - expected << " : " - expected << %{\n" - - assert_dom_equal expected, datetime_select("post", "updated_at") - ensure - Time.zone_default = nil - end + def test_datetime_select_defaults_to_time_zone_now_when_config_time_zone_is_set + time = stub(:year => 2004, :month => 6, :day => 15, :hour => 16, :min => 35, :sec => 0) + time_zone = mock() + time_zone.expects(:now).returns time + Time.zone_default = time_zone + @post = Post.new + + expected = %{\n" + + expected << %{\n" + + expected << %{\n" + + expected << " — " + + expected << %{\n" + expected << " : " + expected << %{\n" + + assert_dom_equal expected, datetime_select("post", "updated_at") + ensure + Time.zone_default = nil end def test_datetime_select_with_html_options_within_fields_for diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb index 86b321e6e5..83c27ac042 100644 --- a/actionpack/test/template/form_options_helper_test.rb +++ b/actionpack/test/template/form_options_helper_test.rb @@ -3,42 +3,54 @@ require 'tzinfo' TZInfo::Timezone.cattr_reader :loaded_zones -uses_mocha "FormOptionsHelperTest" do - class FormOptionsHelperTest < ActionView::TestCase - tests ActionView::Helpers::FormOptionsHelper - - silence_warnings do - 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) - Firm = Struct.new('Firm', :time_zone) - Album = Struct.new('Album', :id, :title, :genre) - end +class FormOptionsHelperTest < ActionView::TestCase + tests ActionView::Helpers::FormOptionsHelper + + silence_warnings do + 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) + Firm = Struct.new('Firm', :time_zone) + Album = Struct.new('Album', :id, :title, :genre) + end - def setup - @fake_timezones = %w(A B C D E).inject([]) do |zones, id| - tz = TZInfo::Timezone.loaded_zones[id] = stub(:name => id, :to_s => id) - ActiveSupport::TimeZone.stubs(:[]).with(id).returns(tz) - zones << tz - end - ActiveSupport::TimeZone.stubs(:all).returns(@fake_timezones) + def setup + @fake_timezones = %w(A B C D E).inject([]) do |zones, id| + tz = TZInfo::Timezone.loaded_zones[id] = stub(:name => id, :to_s => id) + ActiveSupport::TimeZone.stubs(:[]).with(id).returns(tz) + zones << tz end + ActiveSupport::TimeZone.stubs(:all).returns(@fake_timezones) + end - def test_collection_options - @posts = [ - Post.new(" went home", "", "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!") - ] + def test_collection_options + @posts = [ + Post.new(" went home", "", "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_dom_equal( + "\n\n", + options_from_collection_for_select(@posts, "author_name", "title") + ) + end - assert_dom_equal( - "\n\n", - options_from_collection_for_select(@posts, "author_name", "title") - ) - end + def test_collection_options_with_preselected_value + @posts = [ + Post.new(" went home", "", "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!") + ] - def test_collection_options_with_preselected_value + assert_dom_equal( + "\n\n", + options_from_collection_for_select(@posts, "author_name", "title", "Babe") + ) + end + + def test_collection_options_with_preselected_value_array @posts = [ Post.new(" went home", "", "To a little house", "shh!"), Post.new("Babe went home", "Babe", "To a little house", "shh!"), @@ -46,683 +58,669 @@ uses_mocha "FormOptionsHelperTest" do ] assert_dom_equal( - "\n\n", - options_from_collection_for_select(@posts, "author_name", "title", "Babe") + "\n\n", + options_from_collection_for_select(@posts, "author_name", "title", [ "Babe", "Cabe" ]) ) - end + end - def test_collection_options_with_preselected_value_array - @posts = [ - Post.new(" went home", "", "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_dom_equal( - "\n\n", - options_from_collection_for_select(@posts, "author_name", "title", [ "Babe", "Cabe" ]) - ) - end + def test_array_options_for_select + assert_dom_equal( + "\n\n", + options_for_select([ "", "USA", "Sweden" ]) + ) + end - def test_array_options_for_select - assert_dom_equal( - "\n\n", - options_for_select([ "", "USA", "Sweden" ]) - ) - end + def test_array_options_for_select_with_selection + assert_dom_equal( + "\n\n", + options_for_select([ "Denmark", "", "Sweden" ], "") + ) + end - def test_array_options_for_select_with_selection + def test_array_options_for_select_with_selection_array assert_dom_equal( - "\n\n", - options_for_select([ "Denmark", "", "Sweden" ], "") + "\n\n", + options_for_select([ "Denmark", "", "Sweden" ], [ "", "Sweden" ]) ) - end - - def test_array_options_for_select_with_selection_array - assert_dom_equal( - "\n\n", - options_for_select([ "Denmark", "", "Sweden" ], [ "", "Sweden" ]) - ) - end - - def test_array_options_for_string_include_in_other_string_bug_fix - assert_dom_equal( - "\n", - options_for_select([ "ruby", "rubyonrails" ], "rubyonrails") - ) - assert_dom_equal( - "\n", - options_for_select([ "ruby", "rubyonrails" ], "ruby") - ) - assert_dom_equal( - %(\n\n), - options_for_select([ "ruby", "rubyonrails", nil ], "ruby") - ) - end + end - def test_hash_options_for_select + def test_array_options_for_string_include_in_other_string_bug_fix assert_dom_equal( - "\n", - options_for_select("$" => "Dollar", "" => "").split("\n").sort.join("\n") + "\n", + options_for_select([ "ruby", "rubyonrails" ], "rubyonrails") ) assert_dom_equal( - "\n", - options_for_select({ "$" => "Dollar", "" => "" }, "Dollar").split("\n").sort.join("\n") + "\n", + options_for_select([ "ruby", "rubyonrails" ], "ruby") ) assert_dom_equal( - "\n", - options_for_select({ "$" => "Dollar", "" => "" }, [ "Dollar", "" ]).split("\n").sort.join("\n") + %(\n\n), + options_for_select([ "ruby", "rubyonrails", nil ], "ruby") ) - end + end - def test_ducktyped_options_for_select - quack = Struct.new(:first, :last) - assert_dom_equal( - "\n", - options_for_select([quack.new("", ""), quack.new("$", "Dollar")]) - ) - assert_dom_equal( - "\n", - options_for_select([quack.new("", ""), quack.new("$", "Dollar")], "Dollar") - ) - assert_dom_equal( - "\n", - options_for_select([quack.new("", ""), quack.new("$", "Dollar")], ["Dollar", ""]) - ) - end + def test_hash_options_for_select + assert_dom_equal( + "\n", + options_for_select("$" => "Dollar", "" => "").split("\n").sort.join("\n") + ) + assert_dom_equal( + "\n", + options_for_select({ "$" => "Dollar", "" => "" }, "Dollar").split("\n").sort.join("\n") + ) + assert_dom_equal( + "\n", + options_for_select({ "$" => "Dollar", "" => "" }, [ "Dollar", "" ]).split("\n").sort.join("\n") + ) + end - def test_option_groups_from_collection_for_select - @continents = [ - Continent.new("", [Country.new("", ""), Country.new("so", "Somalia")] ), - Continent.new("Europe", [Country.new("dk", "Denmark"), Country.new("ie", "Ireland")] ) - ] + def test_ducktyped_options_for_select + quack = Struct.new(:first, :last) + assert_dom_equal( + "\n", + options_for_select([quack.new("", ""), quack.new("$", "Dollar")]) + ) + assert_dom_equal( + "\n", + options_for_select([quack.new("", ""), quack.new("$", "Dollar")], "Dollar") + ) + assert_dom_equal( + "\n", + options_for_select([quack.new("", ""), quack.new("$", "Dollar")], ["Dollar", ""]) + ) + end - assert_dom_equal( - "\n\n", - option_groups_from_collection_for_select(@continents, "countries", "continent_name", "country_id", "country_name", "dk") - ) - end + def test_option_groups_from_collection_for_select + @continents = [ + Continent.new("", [Country.new("", ""), Country.new("so", "Somalia")] ), + Continent.new("Europe", [Country.new("dk", "Denmark"), Country.new("ie", "Ireland")] ) + ] - def test_grouped_options_for_select_with_array - assert_dom_equal( - "\n\n", - grouped_options_for_select([ - ["North America", - [['United States','US'],"Canada"]], - ["Europe", - [["Great Britain","GB"], "Germany"]] - ]) - ) - end + assert_dom_equal( + "\n\n", + option_groups_from_collection_for_select(@continents, "countries", "continent_name", "country_id", "country_name", "dk") + ) + end - def test_grouped_options_for_select_with_selected_and_prompt - assert_dom_equal( - "\n", - grouped_options_for_select([["Hats", ["Baseball Cap","Cowboy Hat"]]], "Cowboy Hat", "Choose a product...") - ) - end + def test_grouped_options_for_select_with_array + assert_dom_equal( + "\n\n", + grouped_options_for_select([ + ["North America", + [['United States','US'],"Canada"]], + ["Europe", + [["Great Britain","GB"], "Germany"]] + ]) + ) + end - def test_optgroups_with_with_options_with_hash - assert_dom_equal( - "\n\n", - grouped_options_for_select({'North America' => ['United States','Canada'], 'Europe' => ['Denmark','Germany']}) - ) - end + def test_grouped_options_for_select_with_selected_and_prompt + assert_dom_equal( + "\n", + grouped_options_for_select([["Hats", ["Baseball Cap","Cowboy Hat"]]], "Cowboy Hat", "Choose a product...") + ) + end - def test_time_zone_options_no_parms - opts = time_zone_options_for_select - assert_dom_equal "\n" + - "\n" + - "\n" + - "\n" + - "", - opts - end + def test_optgroups_with_with_options_with_hash + assert_dom_equal( + "\n\n", + grouped_options_for_select({'North America' => ['United States','Canada'], 'Europe' => ['Denmark','Germany']}) + ) + end - def test_time_zone_options_with_selected - opts = time_zone_options_for_select( "D" ) - assert_dom_equal "\n" + - "\n" + - "\n" + - "\n" + - "", - opts - end + def test_time_zone_options_no_parms + opts = time_zone_options_for_select + assert_dom_equal "\n" + + "\n" + + "\n" + + "\n" + + "", + opts + end - def test_time_zone_options_with_unknown_selected - opts = time_zone_options_for_select( "K" ) - assert_dom_equal "\n" + - "\n" + - "\n" + - "\n" + - "", - opts - end + def test_time_zone_options_with_selected + opts = time_zone_options_for_select( "D" ) + assert_dom_equal "\n" + + "\n" + + "\n" + + "\n" + + "", + opts + end - def test_time_zone_options_with_priority_zones - zones = [ ActiveSupport::TimeZone.new( "B" ), ActiveSupport::TimeZone.new( "E" ) ] - opts = time_zone_options_for_select( nil, zones ) - assert_dom_equal "\n" + - "" + - "\n" + - "\n" + - "\n" + - "", - opts - end + def test_time_zone_options_with_unknown_selected + opts = time_zone_options_for_select( "K" ) + assert_dom_equal "\n" + + "\n" + + "\n" + + "\n" + + "", + opts + end - def test_time_zone_options_with_selected_priority_zones - zones = [ ActiveSupport::TimeZone.new( "B" ), ActiveSupport::TimeZone.new( "E" ) ] - opts = time_zone_options_for_select( "E", zones ) - assert_dom_equal "\n" + - "" + - "\n" + - "\n" + - "\n" + - "", - opts - end + def test_time_zone_options_with_priority_zones + zones = [ ActiveSupport::TimeZone.new( "B" ), ActiveSupport::TimeZone.new( "E" ) ] + opts = time_zone_options_for_select( nil, zones ) + assert_dom_equal "\n" + + "" + + "\n" + + "\n" + + "\n" + + "", + opts + end - def test_time_zone_options_with_unselected_priority_zones - zones = [ ActiveSupport::TimeZone.new( "B" ), ActiveSupport::TimeZone.new( "E" ) ] - opts = time_zone_options_for_select( "C", zones ) - assert_dom_equal "\n" + - "" + - "\n" + - "\n" + - "\n" + - "", - opts - end + def test_time_zone_options_with_selected_priority_zones + zones = [ ActiveSupport::TimeZone.new( "B" ), ActiveSupport::TimeZone.new( "E" ) ] + opts = time_zone_options_for_select( "E", zones ) + assert_dom_equal "\n" + + "" + + "\n" + + "\n" + + "\n" + + "", + opts + end - def test_select - @post = Post.new - @post.category = "" - assert_dom_equal( - "", - select("post", "category", %w( abe hest)) - ) - end + def test_time_zone_options_with_unselected_priority_zones + zones = [ ActiveSupport::TimeZone.new( "B" ), ActiveSupport::TimeZone.new( "E" ) ] + opts = time_zone_options_for_select( "C", zones ) + assert_dom_equal "\n" + + "" + + "\n" + + "\n" + + "\n" + + "", + opts + end - def test_select_under_fields_for - @post = Post.new - @post.category = "" + def test_select + @post = Post.new + @post.category = "" + assert_dom_equal( + "", + select("post", "category", %w( abe hest)) + ) + end - fields_for :post, @post do |f| - concat f.select(:category, %w( abe hest)) - end - - assert_dom_equal( - "", - output_buffer - ) - end + def test_select_under_fields_for + @post = Post.new + @post.category = "" - def test_select_under_fields_for_with_index - @post = Post.new - @post.category = "" + fields_for :post, @post do |f| + concat f.select(:category, %w( abe hest)) + end + + assert_dom_equal( + "", + output_buffer + ) + end - fields_for :post, @post, :index => 108 do |f| - concat f.select(:category, %w( abe hest)) - end + def test_select_under_fields_for_with_index + @post = Post.new + @post.category = "" - assert_dom_equal( - "", - output_buffer - ) + fields_for :post, @post, :index => 108 do |f| + concat f.select(:category, %w( abe hest)) end - def test_select_under_fields_for_with_auto_index - @post = Post.new - @post.category = "" - def @post.to_param; 108; end + assert_dom_equal( + "", + output_buffer + ) + end - fields_for "post[]", @post do |f| - concat f.select(:category, %w( abe hest)) - end + def test_select_under_fields_for_with_auto_index + @post = Post.new + @post.category = "" + def @post.to_param; 108; end - assert_dom_equal( - "", - output_buffer - ) + fields_for "post[]", @post do |f| + concat f.select(:category, %w( abe hest)) end - def test_select_with_blank - @post = Post.new - @post.category = "" - assert_dom_equal( - "", - select("post", "category", %w( abe hest), :include_blank => true) - ) - end + assert_dom_equal( + "", + output_buffer + ) + end - def test_select_with_blank_as_string - @post = Post.new - @post.category = "" - assert_dom_equal( - "", - select("post", "category", %w( abe hest), :include_blank => 'None') - ) - end + def test_select_with_blank + @post = Post.new + @post.category = "" + assert_dom_equal( + "", + select("post", "category", %w( abe hest), :include_blank => true) + ) + end - def test_select_with_default_prompt - @post = Post.new - @post.category = "" - assert_dom_equal( - "", - select("post", "category", %w( abe hest), :prompt => true) - ) - end + def test_select_with_blank_as_string + @post = Post.new + @post.category = "" + assert_dom_equal( + "", + select("post", "category", %w( abe hest), :include_blank => 'None') + ) + end - def test_select_no_prompt_when_select_has_value - @post = Post.new - @post.category = "" - assert_dom_equal( - "", - select("post", "category", %w( abe hest), :prompt => true) - ) - end + def test_select_with_default_prompt + @post = Post.new + @post.category = "" + assert_dom_equal( + "", + select("post", "category", %w( abe hest), :prompt => true) + ) + end - def test_select_with_given_prompt - @post = Post.new - @post.category = "" - assert_dom_equal( - "", - select("post", "category", %w( abe hest), :prompt => 'The prompt') - ) - end + def test_select_no_prompt_when_select_has_value + @post = Post.new + @post.category = "" + assert_dom_equal( + "", + select("post", "category", %w( abe hest), :prompt => true) + ) + end - def test_select_with_prompt_and_blank - @post = Post.new - @post.category = "" - assert_dom_equal( - "", - select("post", "category", %w( abe hest), :prompt => true, :include_blank => true) - ) - end + def test_select_with_given_prompt + @post = Post.new + @post.category = "" + assert_dom_equal( + "", + select("post", "category", %w( abe hest), :prompt => 'The prompt') + ) + end - def test_select_with_selected_value - @post = Post.new - @post.category = "" - assert_dom_equal( - "", - select("post", "category", %w( abe hest ), :selected => 'abe') - ) - end + def test_select_with_prompt_and_blank + @post = Post.new + @post.category = "" + assert_dom_equal( + "", + select("post", "category", %w( abe hest), :prompt => true, :include_blank => true) + ) + end + + def test_select_with_selected_value + @post = Post.new + @post.category = "" + assert_dom_equal( + "", + select("post", "category", %w( abe hest ), :selected => 'abe') + ) + end + + def test_select_with_index_option + @album = Album.new + @album.id = 1 - def test_select_with_index_option - @album = Album.new - @album.id = 1 - - expected = "" + expected = "" - assert_dom_equal( - expected, - select("album[]", "genre", %w[rap rock country], {}, { :index => nil }) - ) - end + assert_dom_equal( + expected, + select("album[]", "genre", %w[rap rock country], {}, { :index => nil }) + ) + end - def test_select_with_selected_nil - @post = Post.new - @post.category = "" - assert_dom_equal( - "", - select("post", "category", %w( abe hest ), :selected => nil) - ) - end + def test_select_with_selected_nil + @post = Post.new + @post.category = "" + assert_dom_equal( + "", + select("post", "category", %w( abe hest ), :selected => nil) + ) + end - def test_collection_select - @posts = [ - Post.new(" went home", "", "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!") - ] + def test_collection_select + @posts = [ + Post.new(" went home", "", "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" + @post = Post.new + @post.author_name = "Babe" - assert_dom_equal( - "", - collection_select("post", "author_name", @posts, "author_name", "author_name") - ) - end + assert_dom_equal( + "", + collection_select("post", "author_name", @posts, "author_name", "author_name") + ) + end - def test_collection_select_under_fields_for - @posts = [ - Post.new(" went home", "", "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!") - ] + def test_collection_select_under_fields_for + @posts = [ + Post.new(" went home", "", "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" + @post = Post.new + @post.author_name = "Babe" - fields_for :post, @post do |f| - concat f.collection_select(:author_name, @posts, :author_name, :author_name) - end - - assert_dom_equal( - "", - output_buffer - ) + fields_for :post, @post do |f| + concat f.collection_select(:author_name, @posts, :author_name, :author_name) end + + assert_dom_equal( + "", + output_buffer + ) + end - def test_collection_select_under_fields_for_with_index - @posts = [ - Post.new(" went home", "", "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" + def test_collection_select_under_fields_for_with_index + @posts = [ + Post.new(" went home", "", "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!") + ] - fields_for :post, @post, :index => 815 do |f| - concat f.collection_select(:author_name, @posts, :author_name, :author_name) - end + @post = Post.new + @post.author_name = "Babe" - assert_dom_equal( - "", - output_buffer - ) + fields_for :post, @post, :index => 815 do |f| + concat f.collection_select(:author_name, @posts, :author_name, :author_name) end - def test_collection_select_under_fields_for_with_auto_index - @posts = [ - Post.new(" went home", "", "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_dom_equal( + "", + output_buffer + ) + end - @post = Post.new - @post.author_name = "Babe" - def @post.to_param; 815; end + def test_collection_select_under_fields_for_with_auto_index + @posts = [ + Post.new(" went home", "", "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!") + ] - fields_for "post[]", @post do |f| - concat f.collection_select(:author_name, @posts, :author_name, :author_name) - end + @post = Post.new + @post.author_name = "Babe" + def @post.to_param; 815; end - assert_dom_equal( - "", - output_buffer - ) + fields_for "post[]", @post do |f| + concat f.collection_select(:author_name, @posts, :author_name, :author_name) end - def test_collection_select_with_blank_and_style - @posts = [ - Post.new(" went home", "", "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_dom_equal( + "", + output_buffer + ) + end - @post = Post.new - @post.author_name = "Babe" + def test_collection_select_with_blank_and_style + @posts = [ + Post.new(" went home", "", "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_dom_equal( - "", - collection_select("post", "author_name", @posts, "author_name", "author_name", { :include_blank => true }, "style" => "width: 200px") - ) - end + @post = Post.new + @post.author_name = "Babe" - def test_collection_select_with_blank_as_string_and_style - @posts = [ - Post.new(" went home", "", "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_dom_equal( + "", + collection_select("post", "author_name", @posts, "author_name", "author_name", { :include_blank => true }, "style" => "width: 200px") + ) + end - @post = Post.new - @post.author_name = "Babe" + def test_collection_select_with_blank_as_string_and_style + @posts = [ + Post.new(" went home", "", "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_dom_equal( - "", - collection_select("post", "author_name", @posts, "author_name", "author_name", { :include_blank => 'No Selection' }, "style" => "width: 200px") - ) - end + @post = Post.new + @post.author_name = "Babe" - def test_collection_select_with_multiple_option_appends_array_brackets - @posts = [ - Post.new(" went home", "", "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_dom_equal( + "", + collection_select("post", "author_name", @posts, "author_name", "author_name", { :include_blank => 'No Selection' }, "style" => "width: 200px") + ) + end - @post = Post.new - @post.author_name = "Babe" + def test_collection_select_with_multiple_option_appends_array_brackets + @posts = [ + Post.new(" went home", "", "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!") + ] - expected = "" + @post = Post.new + @post.author_name = "Babe" - # Should suffix default name with []. - assert_dom_equal expected, collection_select("post", "author_name", @posts, "author_name", "author_name", { :include_blank => true }, :multiple => true) + expected = "" - # Shouldn't suffix custom name with []. - assert_dom_equal expected, collection_select("post", "author_name", @posts, "author_name", "author_name", { :include_blank => true, :name => 'post[author_name][]' }, :multiple => true) - end + # Should suffix default name with []. + assert_dom_equal expected, collection_select("post", "author_name", @posts, "author_name", "author_name", { :include_blank => true }, :multiple => true) - def test_collection_select_with_blank_and_selected - @posts = [ - Post.new(" went home", "", "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!") - ] + # Shouldn't suffix custom name with []. + assert_dom_equal expected, collection_select("post", "author_name", @posts, "author_name", "author_name", { :include_blank => true, :name => 'post[author_name][]' }, :multiple => true) + end - @post = Post.new - @post.author_name = "Babe" + def test_collection_select_with_blank_and_selected + @posts = [ + Post.new(" went home", "", "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_dom_equal( - %{}, - collection_select("post", "author_name", @posts, "author_name", "author_name", {:include_blank => true, :selected => ""}) - ) - end + @post = Post.new + @post.author_name = "Babe" - def test_time_zone_select - @firm = Firm.new("D") - html = time_zone_select( "firm", "time_zone" ) - assert_dom_equal "", - html + assert_dom_equal( + %{}, + collection_select("post", "author_name", @posts, "author_name", "author_name", {:include_blank => true, :selected => ""}) + ) + end + + def test_time_zone_select + @firm = Firm.new("D") + html = time_zone_select( "firm", "time_zone" ) + assert_dom_equal "", + html + end + + def test_time_zone_select_under_fields_for + @firm = Firm.new("D") + + fields_for :firm, @firm do |f| + concat f.time_zone_select(:time_zone) end + + assert_dom_equal( + "", + output_buffer + ) + end - def test_time_zone_select_under_fields_for - @firm = Firm.new("D") + def test_time_zone_select_under_fields_for_with_index + @firm = Firm.new("D") - fields_for :firm, @firm do |f| - concat f.time_zone_select(:time_zone) - end - - assert_dom_equal( - "", - output_buffer - ) + fields_for :firm, @firm, :index => 305 do |f| + concat f.time_zone_select(:time_zone) end - def test_time_zone_select_under_fields_for_with_index - @firm = Firm.new("D") + assert_dom_equal( + "", + output_buffer + ) + end - fields_for :firm, @firm, :index => 305 do |f| - concat f.time_zone_select(:time_zone) - end + def test_time_zone_select_under_fields_for_with_auto_index + @firm = Firm.new("D") + def @firm.to_param; 305; end - assert_dom_equal( - "", - output_buffer - ) + fields_for "firm[]", @firm do |f| + concat f.time_zone_select(:time_zone) end - def test_time_zone_select_under_fields_for_with_auto_index - @firm = Firm.new("D") - def @firm.to_param; 305; end + assert_dom_equal( + "", + output_buffer + ) + end - fields_for "firm[]", @firm do |f| - concat f.time_zone_select(:time_zone) - end + def test_time_zone_select_with_blank + @firm = Firm.new("D") + html = time_zone_select("firm", "time_zone", nil, :include_blank => true) + assert_dom_equal "", + html + end - assert_dom_equal( - "", - output_buffer - ) - end + def test_time_zone_select_with_blank_as_string + @firm = Firm.new("D") + html = time_zone_select("firm", "time_zone", nil, :include_blank => 'No Zone') + assert_dom_equal "", + html + end - def test_time_zone_select_with_blank - @firm = Firm.new("D") - html = time_zone_select("firm", "time_zone", nil, :include_blank => true) - assert_dom_equal "", - html - end + def test_time_zone_select_with_style + @firm = Firm.new("D") + html = time_zone_select("firm", "time_zone", nil, {}, + "style" => "color: red") + assert_dom_equal "", + html + assert_dom_equal html, time_zone_select("firm", "time_zone", nil, {}, + :style => "color: red") + end - def test_time_zone_select_with_blank_as_string - @firm = Firm.new("D") - html = time_zone_select("firm", "time_zone", nil, :include_blank => 'No Zone') - assert_dom_equal "", - html - end + def test_time_zone_select_with_blank_and_style + @firm = Firm.new("D") + html = time_zone_select("firm", "time_zone", nil, + { :include_blank => true }, "style" => "color: red") + assert_dom_equal "", + html + assert_dom_equal html, time_zone_select("firm", "time_zone", nil, + { :include_blank => true }, :style => "color: red") + end - def test_time_zone_select_with_style - @firm = Firm.new("D") - html = time_zone_select("firm", "time_zone", nil, {}, - "style" => "color: red") - assert_dom_equal "", - html - assert_dom_equal html, time_zone_select("firm", "time_zone", nil, {}, - :style => "color: red") - end + def test_time_zone_select_with_blank_as_string_and_style + @firm = Firm.new("D") + html = time_zone_select("firm", "time_zone", nil, + { :include_blank => 'No Zone' }, "style" => "color: red") + assert_dom_equal "", + html + assert_dom_equal html, time_zone_select("firm", "time_zone", nil, + { :include_blank => 'No Zone' }, :style => "color: red") + end - def test_time_zone_select_with_blank_and_style - @firm = Firm.new("D") - html = time_zone_select("firm", "time_zone", nil, - { :include_blank => true }, "style" => "color: red") - assert_dom_equal "", - html - assert_dom_equal html, time_zone_select("firm", "time_zone", nil, - { :include_blank => true }, :style => "color: red") - end + def test_time_zone_select_with_priority_zones + @firm = Firm.new("D") + zones = [ ActiveSupport::TimeZone.new("A"), ActiveSupport::TimeZone.new("D") ] + html = time_zone_select("firm", "time_zone", zones ) + assert_dom_equal "", + html + end - def test_time_zone_select_with_blank_as_string_and_style - @firm = Firm.new("D") - html = time_zone_select("firm", "time_zone", nil, - { :include_blank => 'No Zone' }, "style" => "color: red") - assert_dom_equal "", - html - assert_dom_equal html, time_zone_select("firm", "time_zone", nil, - { :include_blank => 'No Zone' }, :style => "color: red") - end + def test_time_zone_select_with_priority_zones_as_regexp + @firm = Firm.new("D") + @fake_timezones.each_with_index do |tz, i| + tz.stubs(:=~).returns(i.zero? || i == 3) + end + + html = time_zone_select("firm", "time_zone", /A|D/) + assert_dom_equal "", + html + end - def test_time_zone_select_with_priority_zones - @firm = Firm.new("D") - zones = [ ActiveSupport::TimeZone.new("A"), ActiveSupport::TimeZone.new("D") ] - html = time_zone_select("firm", "time_zone", zones ) + def test_time_zone_select_with_default_time_zone_and_nil_value + @firm = Firm.new() + @firm.time_zone = nil + html = time_zone_select( "firm", "time_zone", nil, :default => 'B' ) assert_dom_equal "", html - end - - def test_time_zone_select_with_priority_zones_as_regexp - @firm = Firm.new("D") - @fake_timezones.each_with_index do |tz, i| - tz.stubs(:=~).returns(i.zero? || i == 3) - end + end - html = time_zone_select("firm", "time_zone", /A|D/) + def test_time_zone_select_with_default_time_zone_and_value + @firm = Firm.new('D') + html = time_zone_select( "firm", "time_zone", nil, :default => 'B' ) assert_dom_equal "", html - end - - def test_time_zone_select_with_default_time_zone_and_nil_value - @firm = Firm.new() - @firm.time_zone = nil - html = time_zone_select( "firm", "time_zone", nil, :default => 'B' ) - assert_dom_equal "", - html - end - - def test_time_zone_select_with_default_time_zone_and_value - @firm = Firm.new('D') - html = time_zone_select( "firm", "time_zone", nil, :default => 'B' ) - assert_dom_equal "", - html - end - end + end diff --git a/actionpack/test/template/number_helper_i18n_test.rb b/actionpack/test/template/number_helper_i18n_test.rb index 3fdf991a44..bf5b81292f 100644 --- a/actionpack/test/template/number_helper_i18n_test.rb +++ b/actionpack/test/template/number_helper_i18n_test.rb @@ -5,67 +5,65 @@ class NumberHelperI18nTests < Test::Unit::TestCase attr_reader :request - uses_mocha 'number_helper_i18n_tests' do - def setup - @number_defaults = { :precision => 3, :delimiter => ',', :separator => '.' } - @currency_defaults = { :unit => '$', :format => '%u%n', :precision => 2 } - @human_defaults = { :precision => 1 } - @human_storage_units_format_default = "%n %u" - @human_storage_units_units_byte_other = "Bytes" - @human_storage_units_units_kb_other = "KB" - @percentage_defaults = { :delimiter => '' } - @precision_defaults = { :delimiter => '' } + def setup + @number_defaults = { :precision => 3, :delimiter => ',', :separator => '.' } + @currency_defaults = { :unit => '$', :format => '%u%n', :precision => 2 } + @human_defaults = { :precision => 1 } + @human_storage_units_format_default = "%n %u" + @human_storage_units_units_byte_other = "Bytes" + @human_storage_units_units_kb_other = "KB" + @percentage_defaults = { :delimiter => '' } + @precision_defaults = { :delimiter => '' } - I18n.backend.store_translations 'en', :number => { :format => @number_defaults, - :currency => { :format => @currency_defaults }, :human => @human_defaults } - end + I18n.backend.store_translations 'en', :number => { :format => @number_defaults, + :currency => { :format => @currency_defaults }, :human => @human_defaults } + end - def test_number_to_currency_translates_currency_formats - I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults) - I18n.expects(:translate).with(:'number.currency.format', :locale => 'en', - :raise => true).returns(@currency_defaults) - number_to_currency(1, :locale => 'en') - end + def test_number_to_currency_translates_currency_formats + I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults) + I18n.expects(:translate).with(:'number.currency.format', :locale => 'en', + :raise => true).returns(@currency_defaults) + number_to_currency(1, :locale => 'en') + end - def test_number_with_precision_translates_number_formats - I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults) - I18n.expects(:translate).with(:'number.precision.format', :locale => 'en', - :raise => true).returns(@precision_defaults) - number_with_precision(1, :locale => 'en') - end + def test_number_with_precision_translates_number_formats + I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults) + I18n.expects(:translate).with(:'number.precision.format', :locale => 'en', + :raise => true).returns(@precision_defaults) + number_with_precision(1, :locale => 'en') + end - def test_number_with_delimiter_translates_number_formats - I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults) - number_with_delimiter(1, :locale => 'en') - end + def test_number_with_delimiter_translates_number_formats + I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults) + number_with_delimiter(1, :locale => 'en') + end - def test_number_to_percentage_translates_number_formats - I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults) - I18n.expects(:translate).with(:'number.percentage.format', :locale => 'en', - :raise => true).returns(@percentage_defaults) - number_to_percentage(1, :locale => 'en') - end + def test_number_to_percentage_translates_number_formats + I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults) + I18n.expects(:translate).with(:'number.percentage.format', :locale => 'en', + :raise => true).returns(@percentage_defaults) + number_to_percentage(1, :locale => 'en') + end - def test_number_to_human_size_translates_human_formats - I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults) - I18n.expects(:translate).with(:'number.human.format', :locale => 'en', - :raise => true).returns(@human_defaults) - I18n.expects(:translate).with(:'number.human.storage_units.format', :locale => 'en', - :raise => true).returns(@human_storage_units_format_default) - I18n.expects(:translate).with(:'number.human.storage_units.units.kb', :locale => 'en', :count => 2, - :raise => true).returns(@human_storage_units_units_kb_other) - # 2KB - number_to_human_size(2048, :locale => 'en') + def test_number_to_human_size_translates_human_formats + I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults) + I18n.expects(:translate).with(:'number.human.format', :locale => 'en', + :raise => true).returns(@human_defaults) + I18n.expects(:translate).with(:'number.human.storage_units.format', :locale => 'en', + :raise => true).returns(@human_storage_units_format_default) + I18n.expects(:translate).with(:'number.human.storage_units.units.kb', :locale => 'en', :count => 2, + :raise => true).returns(@human_storage_units_units_kb_other) + # 2KB + number_to_human_size(2048, :locale => 'en') - I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults) - I18n.expects(:translate).with(:'number.human.format', :locale => 'en', - :raise => true).returns(@human_defaults) - I18n.expects(:translate).with(:'number.human.storage_units.format', :locale => 'en', - :raise => true).returns(@human_storage_units_format_default) - I18n.expects(:translate).with(:'number.human.storage_units.units.byte', :locale => 'en', :count => 42, - :raise => true).returns(@human_storage_units_units_byte_other) - # 42 Bytes - number_to_human_size(42, :locale => 'en') - end + I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults) + I18n.expects(:translate).with(:'number.human.format', :locale => 'en', + :raise => true).returns(@human_defaults) + I18n.expects(:translate).with(:'number.human.storage_units.format', :locale => 'en', + :raise => true).returns(@human_storage_units_format_default) + I18n.expects(:translate).with(:'number.human.storage_units.units.byte', :locale => 'en', :count => 42, + :raise => true).returns(@human_storage_units_units_byte_other) + # 42 Bytes + number_to_human_size(42, :locale => 'en') end end diff --git a/actionpack/test/template/test_test.rb b/actionpack/test/template/test_test.rb index 660f51b3be..ccd299f46a 100644 --- a/actionpack/test/template/test_test.rb +++ b/actionpack/test/template/test_test.rb @@ -38,12 +38,10 @@ class PeopleHelperTest < ActionView::TestCase assert_equal "http://test.host/people", homepage_url end - uses_mocha "link_to_person" do - def test_link_to_person - person = mock(:name => "David") - expects(:mocha_mock_path).with(person).returns("/people/1") - assert_equal 'David', link_to_person(person) - end + def test_link_to_person + person = mock(:name => "David") + expects(:mocha_mock_path).with(person).returns("/people/1") + assert_equal 'David', link_to_person(person) end end diff --git a/actionpack/test/template/translation_helper_test.rb b/actionpack/test/template/translation_helper_test.rb index d0d65cb450..6534df6bbd 100644 --- a/actionpack/test/template/translation_helper_test.rb +++ b/actionpack/test/template/translation_helper_test.rb @@ -5,24 +5,22 @@ class TranslationHelperTest < Test::Unit::TestCase include ActionView::Helpers::TranslationHelper attr_reader :request - uses_mocha 'translation_helper_test' do - def setup - end - - def test_delegates_to_i18n_setting_the_raise_option - I18n.expects(:translate).with(:foo, :locale => 'en', :raise => true) - translate :foo, :locale => 'en' - end - - def test_returns_missing_translation_message_wrapped_into_span - expected = 'en, foo' - assert_equal expected, translate(:foo) - end + def setup + end + + def test_delegates_to_i18n_setting_the_raise_option + I18n.expects(:translate).with(:foo, :locale => 'en', :raise => true) + translate :foo, :locale => 'en' + end - def test_delegates_localize_to_i18n - @time = Time.utc(2008, 7, 8, 12, 18, 38) - I18n.expects(:localize).with(@time) - localize @time - end + def test_returns_missing_translation_message_wrapped_into_span + expected = 'en, foo' + assert_equal expected, translate(:foo) + end + + def test_delegates_localize_to_i18n + @time = Time.utc(2008, 7, 8, 12, 18, 38) + I18n.expects(:localize).with(@time) + localize @time end -end \ No newline at end of file +end -- cgit v1.2.3 From b80fa817d422e6c7ab743f4aa5cb4315b48ec7d5 Mon Sep 17 00:00:00 2001 From: chris finne Date: Tue, 3 Feb 2009 21:55:08 -0800 Subject: Fix a syntax error in current_page?() that was prevent matches against URL's with multiple query parameters [#1868 state:committed] Signed-off-by: David Heinemeier Hansson --- actionpack/test/template/url_helper_test.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'actionpack/test/template') diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb index 2f6fa134b5..2950b4b60c 100644 --- a/actionpack/test/template/url_helper_test.rb +++ b/actionpack/test/template/url_helper_test.rb @@ -263,11 +263,23 @@ class UrlHelperTest < ActionView::TestCase assert_equal "Showing", link_to_unless_current("Showing", { :action => "show", :controller => "weblog" }) assert_equal "Showing", link_to_unless_current("Showing", "http://www.example.com/weblog/show") + @controller.request = RequestMock.new("http://www.example.com/weblog/show?order=desc&page=1") + @controller.url = "http://www.example.com/weblog/show?order=desc&page=1" + assert_equal "Showing", link_to_unless_current("Showing", { :action => "show", :controller => "weblog", :order=>'desc', :page=>'1' }) + assert_equal "Showing", link_to_unless_current("Showing", "http://www.example.com/weblog/show?order=desc&page=1") + assert_equal "Showing", link_to_unless_current("Showing", "http://www.example.com/weblog/show?order=desc&page=1") + @controller.request = RequestMock.new("http://www.example.com/weblog/show?order=desc") @controller.url = "http://www.example.com/weblog/show?order=asc" assert_equal "Showing", link_to_unless_current("Showing", { :action => "show", :controller => "weblog" }) assert_equal "Showing", link_to_unless_current("Showing", "http://www.example.com/weblog/show?order=asc") + @controller.request = RequestMock.new("http://www.example.com/weblog/show?order=desc&page=1") + @controller.url = "http://www.example.com/weblog/show?order=desc&page=2" + assert_equal "Showing", link_to_unless_current("Showing", { :action => "show", :controller => "weblog" }) + assert_equal "Showing", link_to_unless_current("Showing", "http://www.example.com/weblog/show?order=desc&page=2") + + @controller.request = RequestMock.new("http://www.example.com/weblog/show") @controller.url = "http://www.example.com/weblog/list" assert_equal "Listing", @@ -319,7 +331,7 @@ class UrlHelperTest < ActionView::TestCase assert_dom_equal "", mail_to("me@domain.com", "My email", :encode => "javascript", :replace_at => "(at)", :replace_dot => "(dot)") assert_dom_equal "", mail_to("me@domain.com", nil, :encode => "javascript", :replace_at => "(at)", :replace_dot => "(dot)") end - + def protect_against_forgery? false end -- cgit v1.2.3 From 6db78e8c02442080d2be93faeeb42be97b09fb53 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 5 Feb 2009 20:37:57 +0100 Subject: Added tests from Andrew Whites fix [#1385 state:committed] --- actionpack/test/template/url_helper_test.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'actionpack/test/template') diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb index 2950b4b60c..e7799fb204 100644 --- a/actionpack/test/template/url_helper_test.rb +++ b/actionpack/test/template/url_helper_test.rb @@ -252,6 +252,27 @@ class UrlHelperTest < ActionView::TestCase assert_equal "Showing", link_to_if(false, "Showing", :action => "show", :controller => "weblog", :id => 1) end + def test_current_page_with_simple_url + @controller.request = RequestMock.new("http://www.example.com/weblog/show") + @controller.url = "http://www.example.com/weblog/show" + assert current_page?({ :action => "show", :controller => "weblog" }) + assert current_page?("http://www.example.com/weblog/show") + end + + def test_current_page_ignoring_params + @controller.request = RequestMock.new("http://www.example.com/weblog/show?order=desc&page=1") + @controller.url = "http://www.example.com/weblog/show?order=desc&page=1" + assert current_page?({ :action => "show", :controller => "weblog" }) + assert current_page?("http://www.example.com/weblog/show") + end + + def test_current_page_with_params_that_match + @controller.request = RequestMock.new("http://www.example.com/weblog/show?order=desc&page=1") + @controller.url = "http://www.example.com/weblog/show?order=desc&page=1" + assert current_page?({ :action => "show", :controller => "weblog", :order => "desc", :page => "1" }) + assert current_page?("http://www.example.com/weblog/show?order=desc&page=1") + end + def test_link_unless_current @controller.request = RequestMock.new("http://www.example.com/weblog/show") @controller.url = "http://www.example.com/weblog/show" -- cgit v1.2.3 From d15d53cf810014b90827015ecd0e601176492fb7 Mon Sep 17 00:00:00 2001 From: Pascal Ehlert Date: Mon, 2 Feb 2009 22:49:28 +0100 Subject: Allowing an object to be passed explicitly to a fields_for with nested_attributes on one-to-one associations Signed-off-by: Michael Koziarski [#1849 state:committed] --- actionpack/test/template/form_helper_test.rb | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'actionpack/test/template') diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 33a542af7e..b7ea2c0176 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -586,6 +586,15 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal expected, output_buffer end + def test_nested_fields_for_with_explicitly_passed_object_on_a_nested_attributes_one_to_one_association + form_for(:post, @post) do |f| + f.fields_for(:author, Author.new(123)) do |af| + assert_not_nil af.object + assert_equal 123, af.object.id + end + end + end + def test_nested_fields_for_with_an_existing_record_on_a_nested_attributes_one_to_one_association @post.author = Author.new(321) -- cgit v1.2.3