bc011f59b8b56cab560d29b31e2ed4ed2875afef
[feedcatcher.git] / vendor / rails / actionpack / test / template / date_helper_i18n_test.rb
1 require 'abstract_unit'
2
3 class DateHelperDistanceOfTimeInWordsI18nTests < Test::Unit::TestCase
4 include ActionView::Helpers::DateHelper
5 attr_reader :request
6
7 def setup
8 @from = Time.mktime(2004, 6, 6, 21, 45, 0)
9 end
10
11 # distance_of_time_in_words
12
13 def test_distance_of_time_in_words_calls_i18n
14 { # with include_seconds
15 [2.seconds, true] => [:'less_than_x_seconds', 5],
16 [9.seconds, true] => [:'less_than_x_seconds', 10],
17 [19.seconds, true] => [:'less_than_x_seconds', 20],
18 [30.seconds, true] => [:'half_a_minute', nil],
19 [59.seconds, true] => [:'less_than_x_minutes', 1],
20 [60.seconds, true] => [:'x_minutes', 1],
21
22 # without include_seconds
23 [29.seconds, false] => [:'less_than_x_minutes', 1],
24 [60.seconds, false] => [:'x_minutes', 1],
25 [44.minutes, false] => [:'x_minutes', 44],
26 [61.minutes, false] => [:'about_x_hours', 1],
27 [24.hours, false] => [:'x_days', 1],
28 [30.days, false] => [:'about_x_months', 1],
29 [60.days, false] => [:'x_months', 2],
30 [1.year, false] => [:'about_x_years', 1],
31 [3.years, false] => [:'over_x_years', 3]
32
33 }.each do |passed, expected|
34 assert_distance_of_time_in_words_translates_key passed, expected
35 end
36 end
37
38 def assert_distance_of_time_in_words_translates_key(passed, expected)
39 diff, include_seconds = *passed
40 key, count = *expected
41 to = @from + diff
42
43 options = {:locale => 'en', :scope => :'datetime.distance_in_words'}
44 options[:count] = count if count
45
46 I18n.expects(:t).with(key, options)
47 distance_of_time_in_words(@from, to, include_seconds, :locale => 'en')
48 end
49
50 def test_distance_of_time_pluralizations
51 { [:'less_than_x_seconds', 1] => 'less than 1 second',
52 [:'less_than_x_seconds', 2] => 'less than 2 seconds',
53 [:'less_than_x_minutes', 1] => 'less than a minute',
54 [:'less_than_x_minutes', 2] => 'less than 2 minutes',
55 [:'x_minutes', 1] => '1 minute',
56 [:'x_minutes', 2] => '2 minutes',
57 [:'about_x_hours', 1] => 'about 1 hour',
58 [:'about_x_hours', 2] => 'about 2 hours',
59 [:'x_days', 1] => '1 day',
60 [:'x_days', 2] => '2 days',
61 [:'about_x_years', 1] => 'about 1 year',
62 [:'about_x_years', 2] => 'about 2 years',
63 [:'over_x_years', 1] => 'over 1 year',
64 [:'over_x_years', 2] => 'over 2 years'
65
66 }.each do |args, expected|
67 key, count = *args
68 assert_equal expected, I18n.t(key, :count => count, :scope => 'datetime.distance_in_words')
69 end
70 end
71 end
72
73 class DateHelperSelectTagsI18nTests < Test::Unit::TestCase
74 include ActionView::Helpers::DateHelper
75 attr_reader :request
76
77 def setup
78 @prompt_defaults = {:year => 'Year', :month => 'Month', :day => 'Day', :hour => 'Hour', :minute => 'Minute', :second => 'Seconds'}
79
80 I18n.stubs(:translate).with(:'date.month_names', :locale => 'en').returns Date::MONTHNAMES
81 end
82
83 # select_month
84
85 def test_select_month_given_use_month_names_option_does_not_translate_monthnames
86 I18n.expects(:translate).never
87 select_month(8, :locale => 'en', :use_month_names => Date::MONTHNAMES)
88 end
89
90 def test_select_month_translates_monthnames
91 I18n.expects(:translate).with(:'date.month_names', :locale => 'en').returns Date::MONTHNAMES
92 select_month(8, :locale => 'en')
93 end
94
95 def test_select_month_given_use_short_month_option_translates_abbr_monthnames
96 I18n.expects(:translate).with(:'date.abbr_month_names', :locale => 'en').returns Date::ABBR_MONTHNAMES
97 select_month(8, :locale => 'en', :use_short_month => true)
98 end
99
100 def test_date_or_time_select_translates_prompts
101 @prompt_defaults.each do |key, prompt|
102 I18n.expects(:translate).with(('datetime.prompts.' + key.to_s).to_sym, :locale => 'en').returns prompt
103 end
104
105 I18n.expects(:translate).with(:'date.order', :locale => 'en').returns [:year, :month, :day]
106 datetime_select('post', 'updated_at', :locale => 'en', :include_seconds => true, :prompt => true)
107 end
108
109 # date_or_time_select
110
111 def test_date_or_time_select_given_an_order_options_does_not_translate_order
112 I18n.expects(:translate).never
113 datetime_select('post', 'updated_at', :order => [:year, :month, :day], :locale => 'en')
114 end
115
116 def test_date_or_time_select_given_no_order_options_translates_order
117 I18n.expects(:translate).with(:'date.order', :locale => 'en').returns [:year, :month, :day]
118 datetime_select('post', 'updated_at', :locale => 'en')
119 end
120 end