diff options
Diffstat (limited to 'actionpack/test/template')
4 files changed, 199 insertions, 0 deletions
diff --git a/actionpack/test/template/active_record_helper_i18n_test.rb b/actionpack/test/template/active_record_helper_i18n_test.rb new file mode 100644 index 0000000000..057fb9bd1a --- /dev/null +++ b/actionpack/test/template/active_record_helper_i18n_test.rb @@ -0,0 +1,47 @@ +require 'abstract_unit' + +class ActiveRecordHelperI18nTest < Test::Unit::TestCase + include ActionView::Helpers::ActiveRecordHelper + + attr_reader :request + def setup + @request = mock + @object = stub :errors => stub(:count => 1, :full_messages => ['full_messages']) + stubs(:content_tag).returns 'content_tag' + + I18n.stubs(:t).with(:'header_message', :locale => 'en-US', :scope => [:active_record, :error], :count => 1, :object_name => '').returns "1 error prohibited this from being saved" + I18n.stubs(:t).with(:'message', :locale => 'en-US', :scope => [:active_record, :error]).returns 'There were problems with the following fields:' + end + + def test_error_messages_for_given_a_locale_it_does_not_check_request_for_locale + request.expects(:locale).never + @object.errors.stubs(:count).returns 0 + error_messages_for(:object => @object, :locale => 'en-US') + end + + def test_error_messages_for_given_no_locale_it_checks_request_for_locale + request.expects(:locale).returns 'en-US' + @object.errors.stubs(:count).returns 0 + error_messages_for(:object => @object) + end + + def test_error_messages_for_given_a_header_message_option_it_does_not_translate_header_message + I18n.expects(:translate).with(:'header_message', :locale => 'en-US', :scope => [:active_record, :error], :count => 1, :object_name => '').never + error_messages_for(:object => @object, :header_message => 'header message', :locale => 'en-US') + end + + def test_error_messages_for_given_no_header_message_option_it_translates_header_message + I18n.expects(:t).with(:'header_message', :locale => 'en-US', :scope => [:active_record, :error], :count => 1, :object_name => '').returns 'header message' + error_messages_for(:object => @object, :locale => 'en-US') + end + + def test_error_messages_for_given_a_message_option_it_does_not_translate_message + I18n.expects(:t).with(:'message', :locale => 'en-US', :scope => [:active_record, :error]).never + error_messages_for(:object => @object, :message => 'message', :locale => 'en-US') + end + + def test_error_messages_for_given_no_message_option_it_translates_message + I18n.expects(:t).with(:'message', :locale => 'en-US', :scope => [:active_record, :error]).returns 'There were problems with the following fields:' + error_messages_for(:object => @object, :locale => 'en-US') + end +end
\ No newline at end of file diff --git a/actionpack/test/template/date_helper_i18n_test.rb b/actionpack/test/template/date_helper_i18n_test.rb new file mode 100644 index 0000000000..9b7c03a400 --- /dev/null +++ b/actionpack/test/template/date_helper_i18n_test.rb @@ -0,0 +1,99 @@ +require 'abstract_unit' + +class DateHelperDistanceOfTimeInWordsI18nTests < Test::Unit::TestCase + include ActionView::Helpers::DateHelper + attr_reader :request + + def setup + @request = mock + @from = Time.mktime(2004, 6, 6, 21, 45, 0) + end + + # distance_of_time_in_words + + def test_distance_of_time_in_words_given_a_locale_it_does_not_check_request_for_locale + request.expects(:locale).never + distance_of_time_in_words @from, @from + 1.second, false, :locale => 'en-US' + end + + def test_distance_of_time_in_words_given_no_locale_it_checks_request_for_locale + request.expects(:locale).returns 'en-US' + distance_of_time_in_words @from, @from + 1.second + end + + 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 + + options = {:locale => 'en-US', :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-US') + end +end + +class DateHelperSelectTagsI18nTests < Test::Unit::TestCase + include ActionView::Helpers::DateHelper + attr_reader :request + + def setup + @request = mock + I18n.stubs(:translate).with(:'date.month_names', 'en-US').returns Date::MONTHNAMES + end + + # select_month + + def test_select_month_given_use_month_names_option_does_not_translate_monthnames + I18n.expects(:translate).never + select_month(8, :locale => 'en-US', :use_month_names => Date::MONTHNAMES) + end + + def test_select_month_translates_monthnames + I18n.expects(:translate).with(:'date.month_names', 'en-US').returns Date::MONTHNAMES + select_month(8, :locale => 'en-US') + end + + def test_select_month_given_use_short_month_option_translates_abbr_monthnames + I18n.expects(:translate).with(:'date.abbr_month_names', 'en-US').returns Date::ABBR_MONTHNAMES + select_month(8, :locale => 'en-US', :use_short_month => true) + end + + # date_or_time_select + + 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-US') + end + + def test_date_or_time_select_given_no_order_options_translates_order + I18n.expects(:translate).with(:'date.order', 'en-US').returns [:year, :month, :day] + datetime_select('post', 'updated_at', :locale => 'en-US') + end +end
\ No newline at end of file diff --git a/actionpack/test/template/form_options_helper_i18n_test.rb b/actionpack/test/template/form_options_helper_i18n_test.rb new file mode 100644 index 0000000000..c9fc0768bb --- /dev/null +++ b/actionpack/test/template/form_options_helper_i18n_test.rb @@ -0,0 +1,26 @@ +require 'abstract_unit' + +class FormOptionsHelperI18nTests < Test::Unit::TestCase + include ActionView::Helpers::FormOptionsHelper + attr_reader :request + + def setup + @request = mock + end + + def test_country_options_for_select_given_a_locale_it_does_not_check_request_for_locale + request.expects(:locale).never + country_options_for_select :locale => 'en-US' + end + + def test_country_options_for_select_given_no_locale_it_checks_request_for_locale + request.expects(:locale).returns 'en-US' + country_options_for_select + end + + def test_country_options_for_select_translates_country_names + countries = ActionView::Helpers::FormOptionsHelper::COUNTRIES + I18n.expects(:translate).with(:'countries.names', 'en-US').returns countries + country_options_for_select :locale => 'en-US' + end +end
\ No newline at end of file diff --git a/actionpack/test/template/number_helper_i18n_test.rb b/actionpack/test/template/number_helper_i18n_test.rb new file mode 100644 index 0000000000..47cb035f56 --- /dev/null +++ b/actionpack/test/template/number_helper_i18n_test.rb @@ -0,0 +1,27 @@ +require 'abstract_unit' + +class NumberHelperI18nTests < Test::Unit::TestCase + include ActionView::Helpers::NumberHelper + + attr_reader :request + def setup + @request = mock + @defaults = {:separator => ".", :unit => "$", :format => "%u%n", :delimiter => ",", :precision => 2} + I18n.backend.add_translations 'en-US', :currency => {:format => @defaults} + end + + def test_number_to_currency_given_a_locale_it_does_not_check_request_for_locale + request.expects(:locale).never + number_to_currency(1, :locale => 'en-US') + end + + def test_number_to_currency_given_no_locale_it_checks_request_for_locale + request.expects(:locale).returns 'en-US' + number_to_currency(1) + end + + def test_number_to_currency_translates_currency_formats + I18n.expects(:translate).with(:'currency.format', 'en-US').returns @defaults + number_to_currency(1, :locale => 'en-US') + end +end
\ No newline at end of file |