1 require 'abstract_unit'
3 class DateHelperDistanceOfTimeInWordsI18nTests
< Test
::Unit::TestCase
4 include ActionView
::Helpers::DateHelper
8 @from = Time
.mktime(2004, 6, 6, 21, 45, 0)
11 # distance_of_time_in_words
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],
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]
33 }.each
do |passed
, expected
|
34 assert_distance_of_time_in_words_translates_key passed
, expected
38 def assert_distance_of_time_in_words_translates_key(passed
, expected
)
39 diff
, include_seconds
= *passed
40 key
, count
= *expected
43 options
= {:locale => 'en', :scope => :'datetime.distance_in_words'}
44 options
[:count] = count
if count
46 I18n
.expects(:t).with(key
, options
)
47 distance_of_time_in_words(@from, to
, include_seconds
, :locale => 'en')
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'
66 }.each
do |args
, expected
|
68 assert_equal expected
, I18n
.t(key
, :count => count
, :scope => 'datetime.distance_in_words')
73 class DateHelperSelectTagsI18nTests
< Test
::Unit::TestCase
74 include ActionView
::Helpers::DateHelper
78 @prompt_defaults = {:year => 'Year', :month => 'Month', :day => 'Day', :hour => 'Hour', :minute => 'Minute', :second => 'Seconds'}
80 I18n
.stubs(:translate).with(:'date.month_names', :locale => 'en').returns Date
::MONTHNAMES
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)
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')
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)
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
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)
109 # date_or_time_select
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')
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')