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)
to ||= from
# 0..1 with include_seconds
assert_equal "less than 5 seconds", distance_of_time_in_words(from, to + 0.seconds, true)
assert_equal "less than 5 seconds", distance_of_time_in_words(from, to + 4.seconds, true)
assert_equal "less than 10 seconds", distance_of_time_in_words(from, to + 5.seconds, true)
assert_equal "less than 10 seconds", distance_of_time_in_words(from, to + 9.seconds, true)
assert_equal "less than 20 seconds", distance_of_time_in_words(from, to + 10.seconds, true)
assert_equal "less than 20 seconds", distance_of_time_in_words(from, to + 19.seconds, true)
assert_equal "half a minute", distance_of_time_in_words(from, to + 20.seconds, true)
assert_equal "half a minute", distance_of_time_in_words(from, to + 39.seconds, true)
assert_equal "less than a minute", distance_of_time_in_words(from, to + 40.seconds, true)
assert_equal "less than a minute", distance_of_time_in_words(from, to + 59.seconds, true)
assert_equal "1 minute", distance_of_time_in_words(from, to + 60.seconds, true)
assert_equal "1 minute", distance_of_time_in_words(from, to + 89.seconds, true)
# First case 0..1
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..44
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..89
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..1439
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)
# 1440..2519
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)
# 2520..43199
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)
# 43200..86399
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 + 59.days + 23.hours + 59.minutes + 29.seconds)
# 86400..525599
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)
# > 525599
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, true)
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_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))
end
def test_distance_in_words_with_integers
assert_equal "less than a minute", distance_of_time_in_words(59)
assert_equal "about 1 hour", distance_of_time_in_words(60*60)
assert_equal "less than a minute", distance_of_time_in_words(0, 59)
assert_equal "about 1 hour", distance_of_time_in_words(60*60, 0)
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?('