require 'abstract_unit'
class DateHelperTest < ActionView::TestCase
tests ActionView::Helpers::DateHelper
silence_warnings do
Post = Struct.new("Post", :id, :written_on, :updated_at)
Post.class_eval do
def id
123
end
def id_before_type_cast
123
end
def to_param
'123'
end
end
end
def assert_distance_of_time_in_words(from, to=nil)
Fixnum.send :private, :/ # test we avoid Integer#/ (redefined by mathn)
to ||= from
# 0..1 minute with :include_seconds => true
assert_equal "less than 5 seconds", distance_of_time_in_words(from, to + 0.seconds, :include_seconds => true)
assert_equal "less than 5 seconds", distance_of_time_in_words(from, to + 4.seconds, :include_seconds => true)
assert_equal "less than 10 seconds", distance_of_time_in_words(from, to + 5.seconds, :include_seconds => true)
assert_equal "less than 10 seconds", distance_of_time_in_words(from, to + 9.seconds, :include_seconds => true)
assert_equal "less than 20 seconds", distance_of_time_in_words(from, to + 10.seconds, :include_seconds => true)
assert_equal "less than 20 seconds", distance_of_time_in_words(from, to + 19.seconds, :include_seconds => true)
assert_equal "half a minute", distance_of_time_in_words(from, to + 20.seconds, :include_seconds => true)
assert_equal "half a minute", distance_of_time_in_words(from, to + 39.seconds, :include_seconds => true)
assert_equal "less than a minute", distance_of_time_in_words(from, to + 40.seconds, :include_seconds => true)
assert_equal "less than a minute", distance_of_time_in_words(from, to + 59.seconds, :include_seconds => true)
assert_equal "1 minute", distance_of_time_in_words(from, to + 60.seconds, :include_seconds => true)
assert_equal "1 minute", distance_of_time_in_words(from, to + 89.seconds, :include_seconds => true)
# 0..1 minute with :include_seconds => false
assert_equal "less than a minute", distance_of_time_in_words(from, to + 0.seconds, :include_seconds => false)
assert_equal "less than a minute", distance_of_time_in_words(from, to + 4.seconds, :include_seconds => false)
assert_equal "less than a minute", distance_of_time_in_words(from, to + 5.seconds, :include_seconds => false)
assert_equal "less than a minute", distance_of_time_in_words(from, to + 9.seconds, :include_seconds => false)
assert_equal "less than a minute", distance_of_time_in_words(from, to + 10.seconds, :include_seconds => false)
assert_equal "less than a minute", distance_of_time_in_words(from, to + 19.seconds, :include_seconds => false)
assert_equal "less than a minute", distance_of_time_in_words(from, to + 20.seconds, :include_seconds => false)
assert_equal "1 minute", distance_of_time_in_words(from, to + 39.seconds, :include_seconds => false)
assert_equal "1 minute", distance_of_time_in_words(from, to + 40.seconds, :include_seconds => false)
assert_equal "1 minute", distance_of_time_in_words(from, to + 59.seconds, :include_seconds => false)
assert_equal "1 minute", distance_of_time_in_words(from, to + 60.seconds, :include_seconds => false)
assert_equal "1 minute", distance_of_time_in_words(from, to + 89.seconds, :include_seconds => false)
# Note that we are including a 30-second boundary around the interval we
# want to test. For instance, "1 minute" is actually 30s to 1m29s. The
# reason for doing this is simple -- in `distance_of_time_to_words`, when we
# take the distance between our two Time objects in seconds and convert it
# to minutes, we round the number. So 29s gets rounded down to 0m, 30s gets
# rounded up to 1m, and 1m29s gets rounded down to 1m. A similar thing
# happens with the other cases.
# First case 0..1 minute
assert_equal "less than a minute", distance_of_time_in_words(from, to + 0.seconds)
assert_equal "less than a minute", distance_of_time_in_words(from, to + 29.seconds)
assert_equal "1 minute", distance_of_time_in_words(from, to + 30.seconds)
assert_equal "1 minute", distance_of_time_in_words(from, to + 1.minutes + 29.seconds)
# 2 minutes up to 45 minutes
assert_equal "2 minutes", distance_of_time_in_words(from, to + 1.minutes + 30.seconds)
assert_equal "44 minutes", distance_of_time_in_words(from, to + 44.minutes + 29.seconds)
# 45 minutes up to 90 minutes
assert_equal "about 1 hour", distance_of_time_in_words(from, to + 44.minutes + 30.seconds)
assert_equal "about 1 hour", distance_of_time_in_words(from, to + 89.minutes + 29.seconds)
# 90 minutes up to 24 hours
assert_equal "about 2 hours", distance_of_time_in_words(from, to + 89.minutes + 30.seconds)
assert_equal "about 24 hours", distance_of_time_in_words(from, to + 23.hours + 59.minutes + 29.seconds)
# 24 hours up to 42 hours
assert_equal "1 day", distance_of_time_in_words(from, to + 23.hours + 59.minutes + 30.seconds)
assert_equal "1 day", distance_of_time_in_words(from, to + 41.hours + 59.minutes + 29.seconds)
# 42 hours up to 30 days
assert_equal "2 days", distance_of_time_in_words(from, to + 41.hours + 59.minutes + 30.seconds)
assert_equal "3 days", distance_of_time_in_words(from, to + 2.days + 12.hours)
assert_equal "30 days", distance_of_time_in_words(from, to + 29.days + 23.hours + 59.minutes + 29.seconds)
# 30 days up to 60 days
assert_equal "about 1 month", distance_of_time_in_words(from, to + 29.days + 23.hours + 59.minutes + 30.seconds)
assert_equal "about 1 month", distance_of_time_in_words(from, to + 44.days + 23.hours + 59.minutes + 29.seconds)
assert_equal "about 2 months", distance_of_time_in_words(from, to + 44.days + 23.hours + 59.minutes + 30.seconds)
assert_equal "about 2 months", distance_of_time_in_words(from, to + 59.days + 23.hours + 59.minutes + 29.seconds)
# 60 days up to 365 days
assert_equal "2 months", distance_of_time_in_words(from, to + 59.days + 23.hours + 59.minutes + 30.seconds)
assert_equal "12 months", distance_of_time_in_words(from, to + 1.years - 31.seconds)
# >= 365 days
assert_equal "about 1 year", distance_of_time_in_words(from, to + 1.years - 30.seconds)
assert_equal "about 1 year", distance_of_time_in_words(from, to + 1.years + 3.months - 1.day)
assert_equal "over 1 year", distance_of_time_in_words(from, to + 1.years + 6.months)
assert_equal "almost 2 years", distance_of_time_in_words(from, to + 2.years - 3.months + 1.day)
assert_equal "about 2 years", distance_of_time_in_words(from, to + 2.years + 3.months - 1.day)
assert_equal "over 2 years", distance_of_time_in_words(from, to + 2.years + 3.months + 1.day)
assert_equal "over 2 years", distance_of_time_in_words(from, to + 2.years + 9.months - 1.day)
assert_equal "almost 3 years", distance_of_time_in_words(from, to + 2.years + 9.months + 1.day)
assert_equal "almost 5 years", distance_of_time_in_words(from, to + 5.years - 3.months + 1.day)
assert_equal "about 5 years", distance_of_time_in_words(from, to + 5.years + 3.months - 1.day)
assert_equal "over 5 years", distance_of_time_in_words(from, to + 5.years + 3.months + 1.day)
assert_equal "over 5 years", distance_of_time_in_words(from, to + 5.years + 9.months - 1.day)
assert_equal "almost 6 years", distance_of_time_in_words(from, to + 5.years + 9.months + 1.day)
assert_equal "almost 10 years", distance_of_time_in_words(from, to + 10.years - 3.months + 1.day)
assert_equal "about 10 years", distance_of_time_in_words(from, to + 10.years + 3.months - 1.day)
assert_equal "over 10 years", distance_of_time_in_words(from, to + 10.years + 3.months + 1.day)
assert_equal "over 10 years", distance_of_time_in_words(from, to + 10.years + 9.months - 1.day)
assert_equal "almost 11 years", distance_of_time_in_words(from, to + 10.years + 9.months + 1.day)
# test to < from
assert_equal "about 4 hours", distance_of_time_in_words(from + 4.hours, to)
assert_equal "less than 20 seconds", distance_of_time_in_words(from + 19.seconds, to, :include_seconds => true)
assert_equal "less than a minute", distance_of_time_in_words(from + 19.seconds, to, :include_seconds => false)
ensure
Fixnum.send :public, :/
end
def test_distance_in_words
from = Time.utc(2004, 6, 6, 21, 45, 0)
assert_distance_of_time_in_words(from)
end
def test_time_ago_in_words_passes_include_seconds
assert_equal "less than 20 seconds", time_ago_in_words(15.seconds.ago, :include_seconds => true)
assert_equal "less than a minute", time_ago_in_words(15.seconds.ago, :include_seconds => false)
end
def test_distance_in_words_with_time_zones
from = Time.mktime(2004, 6, 6, 21, 45, 0)
assert_distance_of_time_in_words(from.in_time_zone('Alaska'))
assert_distance_of_time_in_words(from.in_time_zone('Hawaii'))
end
def test_distance_in_words_with_different_time_zones
from = Time.mktime(2004, 6, 6, 21, 45, 0)
assert_distance_of_time_in_words(
from.in_time_zone('Alaska'),
from.in_time_zone('Hawaii')
)
end
def test_distance_in_words_with_dates
start_date = Date.new 1975, 1, 31
end_date = Date.new 1977, 1, 31
assert_equal("about 2 years", distance_of_time_in_words(start_date, end_date))
start_date = Date.new 1982, 12, 3
end_date = Date.new 2010, 11, 30
assert_equal("almost 28 years", distance_of_time_in_words(start_date, end_date))
assert_equal("almost 28 years", distance_of_time_in_words(end_date, start_date))
end
def test_distance_in_words_with_integers
assert_equal "1 minute", distance_of_time_in_words(59)
assert_equal "about 1 hour", distance_of_time_in_words(60*60)
assert_equal "1 minute", distance_of_time_in_words(0, 59)
assert_equal "about 1 hour", distance_of_time_in_words(60*60, 0)
assert_equal "about 3 years", distance_of_time_in_words(10**8)
assert_equal "about 3 years", distance_of_time_in_words(0, 10**8)
end
def test_distance_in_words_with_times
assert_equal "1 minute", distance_of_time_in_words(30.seconds)
assert_equal "1 minute", distance_of_time_in_words(59.seconds)
assert_equal "2 minutes", distance_of_time_in_words(119.seconds)
assert_equal "2 minutes", distance_of_time_in_words(1.minute + 59.seconds)
assert_equal "3 minutes", distance_of_time_in_words(2.minute + 30.seconds)
assert_equal "44 minutes", distance_of_time_in_words(44.minutes + 29.seconds)
assert_equal "about 1 hour", distance_of_time_in_words(44.minutes + 30.seconds)
assert_equal "about 1 hour", distance_of_time_in_words(60.minutes)
# include seconds
assert_equal "half a minute", distance_of_time_in_words(39.seconds, 0, :include_seconds => true)
assert_equal "less than a minute", distance_of_time_in_words(40.seconds, 0, :include_seconds => true)
assert_equal "less than a minute", distance_of_time_in_words(59.seconds, 0, :include_seconds => true)
assert_equal "1 minute", distance_of_time_in_words(60.seconds, 0, :include_seconds => true)
end
def test_time_ago_in_words
assert_equal "about 1 year", time_ago_in_words(1.year.ago - 1.day)
end
def test_select_day
expected = %(\n"
assert_dom_equal expected, select_day(Time.mktime(2003, 8, 16))
assert_dom_equal expected, select_day(16)
end
def test_select_day_with_blank
expected = %(\n"
assert_dom_equal expected, select_day(Time.mktime(2003, 8, 16), :include_blank => true)
assert_dom_equal expected, select_day(16, :include_blank => true)
end
def test_select_day_nil_with_blank
expected = %(\n"
assert_dom_equal expected, select_day(nil, :include_blank => true)
end
def test_select_day_with_two_digit_numbers
expected = %(\n"
assert_dom_equal expected, select_day(Time.mktime(2011, 8, 2), :use_two_digit_numbers => true)
assert_dom_equal expected, select_day(2, :use_two_digit_numbers => true)
end
def test_select_day_with_html_options
expected = %(\n"
assert_dom_equal expected, select_day(Time.mktime(2003, 8, 16), {}, :class => 'selector')
assert_dom_equal expected, select_day(16, {}, :class => 'selector')
end
def test_select_day_with_default_prompt
expected = %(\n"
assert_dom_equal expected, select_day(16, :prompt => true)
end
def test_select_day_with_custom_prompt
expected = %(\n"
assert_dom_equal expected, select_day(16, :prompt => 'Choose day')
end
def test_select_month
expected = %(\n"
assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16))
assert_dom_equal expected, select_month(8)
end
def test_select_month_with_two_digit_numbers
expected = %(\n"
assert_dom_equal expected, select_month(Time.mktime(2011, 8, 16), :use_two_digit_numbers => true)
assert_dom_equal expected, select_month(8, :use_two_digit_numbers => true)
end
def test_select_month_with_disabled
expected = %(\n"
assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16), :disabled => true)
assert_dom_equal expected, select_month(8, :disabled => true)
end
def test_select_month_with_field_name_override
expected = %(\n"
assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16), :field_name => 'mois')
assert_dom_equal expected, select_month(8, :field_name => 'mois')
end
def test_select_month_with_blank
expected = %(\n"
assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16), :include_blank => true)
assert_dom_equal expected, select_month(8, :include_blank => true)
end
def test_select_month_nil_with_blank
expected = %(\n"
assert_dom_equal expected, select_month(nil, :include_blank => true)
end
def test_select_month_with_numbers
expected = %(\n"
assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16), :use_month_numbers => true)
assert_dom_equal expected, select_month(8, :use_month_numbers => true)
end
def test_select_month_with_numbers_and_names
expected = %(\n"
assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16), :add_month_numbers => true)
assert_dom_equal expected, select_month(8, :add_month_numbers => true)
end
def test_select_month_with_numbers_and_names_with_abbv
expected = %(\n"
assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16), :add_month_numbers => true, :use_short_month => true)
assert_dom_equal expected, select_month(8, :add_month_numbers => true, :use_short_month => true)
end
def test_select_month_with_abbv
expected = %(\n"
assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16), :use_short_month => true)
assert_dom_equal expected, select_month(8, :use_short_month => true)
end
def test_select_month_with_custom_names
month_names = %w(nil Januar Februar Marts April Maj Juni Juli August September Oktober November December)
expected = %(\n"
assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16), :use_month_names => month_names)
assert_dom_equal expected, select_month(8, :use_month_names => month_names)
end
def test_select_month_with_zero_indexed_custom_names
month_names = %w(Januar Februar Marts April Maj Juni Juli August September Oktober November December)
expected = %(\n"
assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16), :use_month_names => month_names)
assert_dom_equal expected, select_month(8, :use_month_names => month_names)
end
def test_select_month_with_hidden
assert_dom_equal "\n", select_month(8, :use_hidden => true)
end
def test_select_month_with_hidden_and_field_name
assert_dom_equal "\n", select_month(8, :use_hidden => true, :field_name => 'mois')
end
def test_select_month_with_html_options
expected = %(\n"
assert_dom_equal expected, select_month(Time.mktime(2003, 8, 16), {}, :class => 'selector', :accesskey => 'M')
#result = select_month(Time.mktime(2003, 8, 16), {}, :class => 'selector', :accesskey => 'M')
#assert result.include?('