2017-07-23 11:36:41 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:50:17 -04:00
|
|
|
require "abstract_unit"
|
2008-06-19 10:25:27 -04:00
|
|
|
|
2012-01-05 20:01:45 -05:00
|
|
|
class DateHelperDistanceOfTimeInWordsI18nTests < ActiveSupport::TestCase
|
2008-06-19 10:25:27 -04:00
|
|
|
include ActionView::Helpers::DateHelper
|
|
|
|
attr_reader :request
|
2008-08-07 14:13:47 -04:00
|
|
|
|
2008-06-19 10:25:27 -04:00
|
|
|
def setup
|
2010-09-22 13:50:08 -04:00
|
|
|
@from = Time.utc(2004, 6, 6, 21, 45, 0)
|
2008-06-19 10:25:27 -04:00
|
|
|
end
|
2008-08-07 14:13:47 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
# distance_of_time_in_words
|
|
|
|
|
|
|
|
def test_distance_of_time_in_words_calls_i18n
|
|
|
|
{ # with include_seconds
|
2016-08-06 13:36:34 -04:00
|
|
|
[2.seconds, { include_seconds: true }] => [:'less_than_x_seconds', 5],
|
|
|
|
[9.seconds, { include_seconds: true }] => [:'less_than_x_seconds', 10],
|
|
|
|
[19.seconds, { include_seconds: true }] => [:'less_than_x_seconds', 20],
|
|
|
|
[30.seconds, { include_seconds: true }] => [:'half_a_minute', nil],
|
|
|
|
[59.seconds, { include_seconds: true }] => [:'less_than_x_minutes', 1],
|
|
|
|
[60.seconds, { include_seconds: true }] => [:'x_minutes', 1],
|
2009-02-03 21:25:37 -05:00
|
|
|
|
|
|
|
# without include_seconds
|
2016-08-06 13:36:34 -04:00
|
|
|
[29.seconds, { include_seconds: false }] => [:'less_than_x_minutes', 1],
|
|
|
|
[60.seconds, { include_seconds: false }] => [:'x_minutes', 1],
|
|
|
|
[44.minutes, { include_seconds: false }] => [:'x_minutes', 44],
|
|
|
|
[61.minutes, { include_seconds: false }] => [:'about_x_hours', 1],
|
|
|
|
[24.hours, { include_seconds: false }] => [:'x_days', 1],
|
|
|
|
[30.days, { include_seconds: false }] => [:'about_x_months', 1],
|
|
|
|
[60.days, { include_seconds: false }] => [:'x_months', 2],
|
|
|
|
[1.year, { include_seconds: false }] => [:'about_x_years', 1],
|
|
|
|
[3.years + 6.months, { include_seconds: false }] => [:'over_x_years', 3],
|
|
|
|
[3.years + 10.months, { include_seconds: false }] => [:'almost_x_years', 4]
|
2009-02-03 21:25:37 -05:00
|
|
|
|
|
|
|
}.each do |passed, expected|
|
|
|
|
assert_distance_of_time_in_words_translates_key passed, expected
|
2008-06-19 10:25:27 -04:00
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-19 10:25:27 -04:00
|
|
|
|
2012-10-19 15:43:42 -04:00
|
|
|
def test_distance_of_time_in_words_calls_i18n_with_custom_scope
|
|
|
|
{
|
|
|
|
[30.days, { scope: :'datetime.distance_in_words_ago' }] => [:'about_x_months', 1],
|
|
|
|
[60.days, { scope: :'datetime.distance_in_words_ago' }] => [:'x_months', 2],
|
|
|
|
}.each do |passed, expected|
|
|
|
|
assert_distance_of_time_in_words_translates_key(passed, expected, scope: :'datetime.distance_in_words_ago')
|
|
|
|
end
|
2012-04-30 03:11:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_time_ago_in_words_passes_locale
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_called_with(I18n, :t, [:less_than_x_minutes, scope: :'datetime.distance_in_words', count: 1, locale: "ru"]) do
|
|
|
|
time_ago_in_words(15.seconds.ago, locale: "ru")
|
2015-08-22 00:03:53 -04:00
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-08-10 15:25:24 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_distance_of_time_pluralizations
|
2016-08-06 12:50:17 -04:00
|
|
|
{ [:'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"
|
2009-02-03 21:25:37 -05:00
|
|
|
|
|
|
|
}.each do |args, expected|
|
|
|
|
key, count = *args
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_equal expected, I18n.t(key, count: count, scope: "datetime.distance_in_words")
|
2008-08-10 15:25:24 -04:00
|
|
|
end
|
2008-06-19 10:25:27 -04:00
|
|
|
end
|
2012-10-19 15:43:42 -04:00
|
|
|
|
|
|
|
def assert_distance_of_time_in_words_translates_key(passed, expected, expected_options = {})
|
|
|
|
diff, passed_options = *passed
|
|
|
|
key, count = *expected
|
|
|
|
to = @from + diff
|
|
|
|
|
2016-08-06 12:50:17 -04:00
|
|
|
options = { locale: "en", scope: :'datetime.distance_in_words' }.merge!(expected_options)
|
2012-10-19 15:43:42 -04:00
|
|
|
options[:count] = count if count
|
|
|
|
|
2015-08-22 00:03:53 -04:00
|
|
|
assert_called_with(I18n, :t, [key, options]) do
|
2016-08-06 12:50:17 -04:00
|
|
|
distance_of_time_in_words(@from, to, passed_options.merge(locale: "en"))
|
2015-08-22 00:03:53 -04:00
|
|
|
end
|
2012-10-19 15:43:42 -04:00
|
|
|
end
|
2008-06-19 10:25:27 -04:00
|
|
|
end
|
2008-08-07 14:13:47 -04:00
|
|
|
|
2012-01-05 20:01:45 -05:00
|
|
|
class DateHelperSelectTagsI18nTests < ActiveSupport::TestCase
|
2008-06-19 10:25:27 -04:00
|
|
|
include ActionView::Helpers::DateHelper
|
|
|
|
attr_reader :request
|
2008-08-07 14:13:47 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
# select_month
|
2008-06-27 09:00:55 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_select_month_given_use_month_names_option_does_not_translate_monthnames
|
2015-08-22 00:03:53 -04:00
|
|
|
assert_not_called(I18n, :translate) do
|
2016-08-06 13:36:34 -04:00
|
|
|
select_month(8, locale: "en", use_month_names: Date::MONTHNAMES)
|
2015-08-22 00:03:53 -04:00
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-27 09:00:55 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_select_month_translates_monthnames
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_called_with(I18n, :translate, [:'date.month_names', locale: "en"], returns: Date::MONTHNAMES) do
|
|
|
|
select_month(8, locale: "en")
|
2015-08-22 00:03:53 -04:00
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-27 09:00:55 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_select_month_given_use_short_month_option_translates_abbr_monthnames
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_called_with(I18n, :translate, [:'date.abbr_month_names', locale: "en"], returns: Date::ABBR_MONTHNAMES) do
|
|
|
|
select_month(8, locale: "en", use_short_month: true)
|
2015-08-22 00:03:53 -04:00
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-12-22 06:41:47 -05:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_date_or_time_select_translates_prompts
|
2016-08-16 03:30:11 -04:00
|
|
|
prompt_defaults = { year: "Year", month: "Month", day: "Day", hour: "Hour", minute: "Minute", second: "Seconds" }
|
|
|
|
defaults = { [:'date.order', locale: "en", default: []] => %w(year month day) }
|
2015-08-22 00:03:53 -04:00
|
|
|
|
|
|
|
prompt_defaults.each do |key, prompt|
|
2016-08-06 13:36:34 -04:00
|
|
|
defaults[[("datetime.prompts." + key.to_s).to_sym, locale: "en"]] = prompt
|
2015-08-22 00:03:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
prompts_check = -> (prompt, x) do
|
|
|
|
@prompt_called ||= 0
|
|
|
|
|
|
|
|
return_value = defaults[[prompt, x]]
|
|
|
|
@prompt_called += 1 if return_value.present?
|
|
|
|
|
|
|
|
return_value
|
2008-12-22 06:41:47 -05:00
|
|
|
end
|
|
|
|
|
2015-08-22 00:03:53 -04:00
|
|
|
I18n.stub(:translate, prompts_check) do
|
2016-08-06 13:36:34 -04:00
|
|
|
datetime_select("post", "updated_at", locale: "en", include_seconds: true, prompt: true, use_month_names: Date::MONTHNAMES)
|
2015-08-22 00:03:53 -04:00
|
|
|
end
|
|
|
|
assert_equal defaults.count, @prompt_called
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
2008-06-27 09:00:55 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
# date_or_time_select
|
2008-06-27 09:00:55 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_date_or_time_select_given_an_order_options_does_not_translate_order
|
2015-08-22 00:03:53 -04:00
|
|
|
assert_not_called(I18n, :translate) do
|
2016-08-06 13:36:34 -04:00
|
|
|
datetime_select("post", "updated_at", order: [:year, :month, :day], locale: "en", use_month_names: Date::MONTHNAMES)
|
2015-08-22 00:03:53 -04:00
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_date_or_time_select_given_no_order_options_translates_order
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_called_with(I18n, :translate, [ [:'date.order', locale: "en", default: []], [:"date.month_names", { locale: "en" }] ], returns: %w(year month day)) do
|
2016-08-06 13:36:34 -04:00
|
|
|
datetime_select("post", "updated_at", locale: "en")
|
2015-08-22 00:03:53 -04:00
|
|
|
end
|
2008-06-19 10:25:27 -04:00
|
|
|
end
|
2012-01-22 16:47:36 -05:00
|
|
|
|
|
|
|
def test_date_or_time_select_given_invalid_order
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_called_with(I18n, :translate, [:'date.order', locale: "en", default: []], returns: %w(invalid month day)) do
|
2015-08-22 00:03:53 -04:00
|
|
|
assert_raise StandardError do
|
2016-08-06 13:36:34 -04:00
|
|
|
datetime_select("post", "updated_at", locale: "en")
|
2015-08-22 00:03:53 -04:00
|
|
|
end
|
2012-01-22 16:47:36 -05:00
|
|
|
end
|
|
|
|
end
|
2013-01-17 10:21:26 -05:00
|
|
|
|
|
|
|
def test_date_or_time_select_given_symbol_keys
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_called_with(I18n, :translate, [ [:'date.order', locale: "en", default: []], [:"date.month_names", { locale: "en" }] ], returns: [:year, :month, :day]) do
|
2016-08-06 13:36:34 -04:00
|
|
|
datetime_select("post", "updated_at", locale: "en")
|
2015-08-22 00:03:53 -04:00
|
|
|
end
|
2013-01-17 10:21:26 -05:00
|
|
|
end
|
2009-02-03 21:25:37 -05:00
|
|
|
end
|