require 'abstract_unit' class DateHelperTest < Test::Unit::TestCase include ActionView::Helpers::DateHelper include ActionView::Helpers::FormHelper 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 test_distance_in_words from = Time.mktime(2004, 6, 6, 21, 45, 0) # 0..1 with include_seconds assert_equal "less than 5 seconds", distance_of_time_in_words(from, from + 0.seconds, true) assert_equal "less than 5 seconds", distance_of_time_in_words(from, from + 4.seconds, true) assert_equal "less than 10 seconds", distance_of_time_in_words(from, from + 5.seconds, true) assert_equal "less than 10 seconds", distance_of_time_in_words(from, from + 9.seconds, true) assert_equal "less than 20 seconds", distance_of_time_in_words(from, from + 10.seconds, true) assert_equal "less than 20 seconds", distance_of_time_in_words(from, from + 19.seconds, true) assert_equal "half a minute", distance_of_time_in_words(from, from + 20.seconds, true) assert_equal "half a minute", distance_of_time_in_words(from, from + 39.seconds, true) assert_equal "less than a minute", distance_of_time_in_words(from, from + 40.seconds, true) assert_equal "less than a minute", distance_of_time_in_words(from, from + 59.seconds, true) assert_equal "1 minute", distance_of_time_in_words(from, from + 60.seconds, true) assert_equal "1 minute", distance_of_time_in_words(from, from + 89.seconds, true) # First case 0..1 assert_equal "less than a minute", distance_of_time_in_words(from, from + 0.seconds) assert_equal "less than a minute", distance_of_time_in_words(from, from + 29.seconds) assert_equal "1 minute", distance_of_time_in_words(from, from + 30.seconds) assert_equal "1 minute", distance_of_time_in_words(from, from + 1.minutes + 29.seconds) # 2..44 assert_equal "2 minutes", distance_of_time_in_words(from, from + 1.minutes + 30.seconds) assert_equal "44 minutes", distance_of_time_in_words(from, from + 44.minutes + 29.seconds) # 45..89 assert_equal "about 1 hour", distance_of_time_in_words(from, from + 44.minutes + 30.seconds) assert_equal "about 1 hour", distance_of_time_in_words(from, from + 89.minutes + 29.seconds) # 90..1439 assert_equal "about 2 hours", distance_of_time_in_words(from, from + 89.minutes + 30.seconds) assert_equal "about 24 hours", distance_of_time_in_words(from, from + 23.hours + 59.minutes + 29.seconds) # 1440..2879 assert_equal "1 day", distance_of_time_in_words(from, from + 23.hours + 59.minutes + 30.seconds) assert_equal "1 day", distance_of_time_in_words(from, from + 47.hours + 59.minutes + 29.seconds) # 2880..43199 assert_equal "2 days", distance_of_time_in_words(from, from + 47.hours + 59.minutes + 30.seconds) assert_equal "29 days", distance_of_time_in_words(from, from + 29.days + 23.hours + 59.minutes + 29.seconds) # 43200..86399 assert_equal "about 1 month", distance_of_time_in_words(from, from + 29.days + 23.hours + 59.minutes + 30.seconds) assert_equal "about 1 month", distance_of_time_in_words(from, from + 59.days + 23.hours + 59.minutes + 29.seconds) # 86400..525599 assert_equal "2 months", distance_of_time_in_words(from, from + 59.days + 23.hours + 59.minutes + 30.seconds) assert_equal "12 months", distance_of_time_in_words(from, from + 1.years - 31.seconds) # 525600..1051199 assert_equal "about 1 year", distance_of_time_in_words(from, from + 1.years - 30.seconds) assert_equal "about 1 year", distance_of_time_in_words(from, from + 2.years - 31.seconds) # > 1051199 assert_equal "over 2 years", distance_of_time_in_words(from, from + 2.years + 30.seconds) assert_equal "over 10 years", distance_of_time_in_words(from, from + 10.years) # test to < from assert_equal "about 4 hours", distance_of_time_in_words(from + 4.hours, from) assert_equal "less than 20 seconds", distance_of_time_in_words(from + 19.seconds, from, true) # test 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_distance_in_words_with_dates start_date = Date.new 1975, 1, 31 end_date = Date.new 1977, 1, 31 assert_equal("over 2 years", distance_of_time_in_words(start_date, end_date)) 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_equal expected, select_day(Time.mktime(2003, 8, 16)) assert_equal expected, select_day(16) end def test_select_day_with_blank expected = %(\n" assert_equal expected, select_day(Time.mktime(2003, 8, 16), :include_blank => true) assert_equal expected, select_day(16, :include_blank => true) end def test_select_day_nil_with_blank expected = %(\n" assert_equal expected, select_day(nil, :include_blank => true) end def test_select_month expected = %(\n" assert_equal expected, select_month(Time.mktime(2003, 8, 16)) assert_equal expected, select_month(8) end def test_select_month_with_disabled expected = %(\n" assert_equal expected, select_month(Time.mktime(2003, 8, 16), :disabled => true) assert_equal expected, select_month(8, :disabled => true) end def test_select_month_with_field_name_override expected = %(\n" assert_equal expected, select_month(Time.mktime(2003, 8, 16), :field_name => 'mois') assert_equal expected, select_month(8, :field_name => 'mois') end def test_select_month_with_blank expected = %(\n" assert_equal expected, select_month(Time.mktime(2003, 8, 16), :include_blank => true) assert_equal expected, select_month(8, :include_blank => true) end def test_select_month_nil_with_blank expected = %(\n" assert_equal expected, select_month(nil, :include_blank => true) end def test_select_month_with_numbers expected = %(\n" assert_equal expected, select_month(Time.mktime(2003, 8, 16), :use_month_numbers => true) assert_equal expected, select_month(8, :use_month_numbers => true) end def test_select_month_with_numbers_and_names expected = %(\n" assert_equal expected, select_month(Time.mktime(2003, 8, 16), :add_month_numbers => true) assert_equal expected, select_month(8, :add_month_numbers => true) end def test_select_month_with_numbers_and_names_with_abbv expected = %(\n" assert_equal expected, select_month(Time.mktime(2003, 8, 16), :add_month_numbers => true, :use_short_month => true) assert_equal expected, select_month(8, :add_month_numbers => true, :use_short_month => true) end def test_select_month_with_abbv expected = %(\n" assert_equal expected, select_month(Time.mktime(2003, 8, 16), :use_short_month => true) assert_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_equal expected, select_month(Time.mktime(2003, 8, 16), :use_month_names => month_names) assert_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_equal expected, select_month(Time.mktime(2003, 8, 16), :use_month_names => month_names) assert_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_year expected = %(\n" assert_equal expected, select_year(Time.mktime(2003, 8, 16), :start_year => 2003, :end_year => 2005) assert_equal expected, select_year(2003, :start_year => 2003, :end_year => 2005) end def test_select_year_with_disabled expected = %(\n" assert_equal expected, select_year(Time.mktime(2003, 8, 16), :disabled => true, :start_year => 2003, :end_year => 2005) assert_equal expected, select_year(2003, :disabled => true, :start_year => 2003, :end_year => 2005) end def test_select_year_with_field_name_override expected = %(\n" assert_equal expected, select_year(Time.mktime(2003, 8, 16), :start_year => 2003, :end_year => 2005, :field_name => 'annee') assert_equal expected, select_year(2003, :start_year => 2003, :end_year => 2005, :field_name => 'annee') end def test_select_year_with_type_discarding expected = %(\n" assert_equal expected, select_year( Time.mktime(2003, 8, 16), :prefix => "date_year", :discard_type => true, :start_year => 2003, :end_year => 2005) assert_equal expected, select_year( 2003, :prefix => "date_year", :discard_type => true, :start_year => 2003, :end_year => 2005) end def test_select_year_descending expected = %(\n" assert_equal expected, select_year(Time.mktime(2005, 8, 16), :start_year => 2005, :end_year => 2003) assert_equal expected, select_year(2005, :start_year => 2005, :end_year => 2003) end def test_select_year_with_hidden assert_dom_equal "\n", select_year(2007, :use_hidden => true) end def test_select_year_with_hidden_and_field_name assert_dom_equal "\n", select_year(2007, :use_hidden => true, :field_name => 'anno') end def test_select_hour expected = %(\n" assert_equal expected, select_hour(Time.mktime(2003, 8, 16, 8, 4, 18)) end def test_select_hour_with_disabled expected = %(\n" assert_equal expected, select_hour(Time.mktime(2003, 8, 16, 8, 4, 18), :disabled => true) end def test_select_hour_with_field_name_override expected = %(\n" assert_equal expected, select_hour(Time.mktime(2003, 8, 16, 8, 4, 18), :field_name => 'heure') end def test_select_hour_with_blank expected = %(\n" assert_equal expected, select_hour(Time.mktime(2003, 8, 16, 8, 4, 18), :include_blank => true) end def test_select_hour_nil_with_blank expected = %(\n" assert_equal expected, select_hour(nil, :include_blank => true) end def test_select_minute expected = %(\n" assert_equal expected, select_minute(Time.mktime(2003, 8, 16, 8, 4, 18)) end def test_select_minute_with_disabled expected = %(\n" assert_equal expected, select_minute(Time.mktime(2003, 8, 16, 8, 4, 18), :disabled => true) end def test_select_minute_with_field_name_override expected = %(\n" assert_equal expected, select_minute(Time.mktime(2003, 8, 16, 8, 4, 18), :field_name => 'minuto') end def test_select_minute_with_blank expected = %(\n" assert_equal expected, select_minute(Time.mktime(2003, 8, 16, 8, 4, 18), :include_blank => true) end def test_select_minute_with_blank_and_step expected = %(\n" assert_equal expected, select_minute(Time.mktime(2003, 8, 16, 8, 4, 18), { :include_blank => true , :minute_step => 15 }) end def test_select_minute_nil_with_blank expected = %(\n" assert_equal expected, select_minute(nil, :include_blank => true) end def test_select_minute_nil_with_blank_and_step expected = %(\n" assert_equal expected, select_minute(nil, { :include_blank => true , :minute_step => 15 }) end def test_select_minute_with_hidden assert_dom_equal "\n", select_minute(8, :use_hidden => true) end def test_select_minute_with_hidden_and_field_name assert_dom_equal "\n", select_minute(8, :use_hidden => true, :field_name => 'minuto') end def test_select_second expected = %(\n" assert_equal expected, select_second(Time.mktime(2003, 8, 16, 8, 4, 18)) end def test_select_second_with_disabled expected = %(\n" assert_equal expected, select_second(Time.mktime(2003, 8, 16, 8, 4, 18), :disabled => true) end def test_select_second_with_field_name_override expected = %(\n" assert_equal expected, select_second(Time.mktime(2003, 8, 16, 8, 4, 18), :field_name => 'segundo') end def test_select_second_with_blank expected = %(\n" assert_equal expected, select_second(Time.mktime(2003, 8, 16, 8, 4, 18), :include_blank => true) end def test_select_second_nil_with_blank expected = %(\n" assert_equal expected, select_second(nil, :include_blank => true) end def test_select_date expected = %(\n" expected << %(\n" expected << %(\n" assert_equal expected, select_date(Time.mktime(2003, 8, 16), :start_year => 2003, :end_year => 2005, :prefix => "date[first]") end def test_select_date_with_order expected = %(\n" expected << %(\n" expected << %(\n" assert_equal expected, select_date(Time.mktime(2003, 8, 16), :start_year => 2003, :end_year => 2005, :prefix => "date[first]", :order => [:month, :day, :year]) end def test_select_date_with_incomplete_order expected = %(\n" expected << %(\n" expected << %(\n" assert_equal expected, select_date(Time.mktime(2003, 8, 16), :start_year => 2003, :end_year => 2005, :prefix => "date[first]", :order => [:day]) end def test_select_date_with_disabled expected = %(\n" expected << %(\n" expected << %(\n" assert_equal expected, select_date(Time.mktime(2003, 8, 16), :start_year => 2003, :end_year => 2005, :prefix => "date[first]", :disabled => true) end def test_select_date_with_no_start_year expected = %(\n" expected << %(\n" expected << %(\n" assert_equal expected, select_date( Time.mktime(Date.today.year, 8, 16), :end_year => Date.today.year+1, :prefix => "date[first]" ) end def test_select_date_with_no_end_year expected = %(\n" expected << %(\n" expected << %(\n" assert_equal expected, select_date( Time.mktime(2003, 8, 16), :start_year => 2003, :prefix => "date[first]" ) end def test_select_date_with_no_start_or_end_year expected = %(\n" expected << %(\n" expected << %(\n" assert_equal expected, select_date( Time.mktime(Date.today.year, 8, 16), :prefix => "date[first]" ) end def test_select_date_with_zero_value expected = %(\n" expected << %(\n" expected << %(\n" assert_equal expected, select_date(0, :start_year => 2003, :end_year => 2005, :prefix => "date[first]") end def test_select_date_with_zero_value_and_no_start_year expected = %(\n" expected << %(\n" expected << %(\n" assert_equal expected, select_date(0, :end_year => Date.today.year+1, :prefix => "date[first]") end def test_select_date_with_zero_value_and_no_end_year expected = %(\n" expected << %(\n" expected << %(\n" assert_equal expected, select_date(0, :start_year => 2003, :prefix => "date[first]") end def test_select_date_with_zero_value_and_no_start_and_end_year expected = %(\n" expected << %(\n" expected << %(\n" assert_equal expected, select_date(0, :prefix => "date[first]") end def test_select_date_with_nil_value_and_no_start_and_end_year expected = %(\n" expected << %(\n" expected << %(\n" assert_equal expected, select_date(nil, :prefix => "date[first]") end def test_select_datetime expected = %(\n" expected << %(\n" expected << %(\n" expected << %(\n" expected << %(\n" assert_equal expected, select_datetime(Time.mktime(2003, 8, 16, 8, 4, 18), :start_year => 2003, :end_year => 2005, :prefix => "date[first]") end def test_select_datetime_with_separators expected = %(\n" expected << %(\n" expected << %(\n" expected << " — " expected << %(\n" expected << " : " expected << %(\n" assert_equal expected, select_datetime(Time.mktime(2003, 8, 16, 8, 4, 18), :start_year => 2003, :end_year => 2005, :prefix => "date[first]", :datetime_separator => ' — ', :time_separator => ' : ') end def test_select_datetime_with_nil_value_and_no_start_and_end_year expected = %(\n" expected << %(\n" expected << %(\n" expected << %(\n" expected << %(\n" assert_equal expected, select_datetime(nil, :prefix => "date[first]") end def test_select_time expected = %(\n" expected << %(\n" assert_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18)) assert_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18), :include_seconds => false) end def test_select_time_with_separator expected = %(\n" expected << " : " expected << %(\n" assert_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18), :time_separator => ' : ') assert_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18), :time_separator => ' : ', :include_seconds => false) end def test_select_time_with_seconds expected = %(\n" expected << %(\n" expected << %(\n" assert_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18), :include_seconds => true) end def test_select_time_with_seconds_and_separator expected = %(\n" expected << " : " expected << %(\n" expected << " : " expected << %(\n" assert_equal expected, select_time(Time.mktime(2003, 8, 16, 8, 4, 18), :include_seconds => true, :time_separator => ' : ') end def test_date_select @post = Post.new @post.written_on = Date.new(2004, 6, 15) expected = %{\n" expected << %{\n" expected << %{\n" assert_equal expected, date_select("post", "written_on") end def test_date_select_without_day @post = Post.new @post.written_on = Date.new(2004, 6, 15) expected = "\n" expected << %{\n" expected << %{\n" assert_equal expected, date_select("post", "written_on", :order => [ :month, :year ]) end def test_date_select_within_fields_for @post = Post.new @post.written_on = Date.new(2004, 6, 15) _erbout = '' fields_for :post, @post do |f| _erbout.concat f.date_select(:written_on) end expected = "\n" expected << "\n" expected << "\n" assert_dom_equal(expected, _erbout) end def test_date_select_with_index @post = Post.new @post.written_on = Date.new(2004, 6, 15) id = 456 expected = %{\n" expected << %{\n" expected << %{\n" assert_equal expected, date_select("post", "written_on", :index => id) end def test_date_select_with_auto_index @post = Post.new @post.written_on = Date.new(2004, 6, 15) id = 123 expected = %{\n" expected << %{\n" expected << %{\n" assert_equal expected, date_select("post[]", "written_on") end def test_date_select_with_different_order @post = Post.new @post.written_on = Date.new(2004, 6, 15) expected = %{\n" expected << %{\n" expected << %{\n" assert_equal expected, date_select("post", "written_on", :order => [:day, :month, :year]) end def test_date_select_with_nil @post = Post.new start_year = Time.now.year-5 end_year = Time.now.year+5 expected = %{\n" expected << %{\n" expected << %{\n" assert_equal expected, date_select("post", "written_on") end def test_date_select_with_nil_and_blank @post = Post.new start_year = Time.now.year-5 end_year = Time.now.year+5 expected = %{\n" expected << %{\n" expected << %{\n" assert_equal expected, date_select("post", "written_on", :include_blank => true) end def test_date_select_cant_override_discard_hour @post = Post.new @post.written_on = Date.new(2004, 6, 15) expected = %{\n" expected << %{\n" expected << %{\n" assert_equal expected, date_select("post", "written_on", :discard_hour => false) end def test_time_select @post = Post.new @post.written_on = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n} expected << %{\n} expected << %{\n} expected << %(\n" expected << " : " expected << %(\n" assert_equal expected, time_select("post", "written_on") end def test_time_select_with_seconds @post = Post.new @post.written_on = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n} expected << %{\n} expected << %{\n} expected << %(\n" expected << " : " expected << %(\n" expected << " : " expected << %(\n" assert_equal expected, time_select("post", "written_on", :include_seconds => true) end def test_datetime_select @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 16, 35) expected = %{\n" expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << " : " expected << %{\n" assert_equal expected, datetime_select("post", "updated_at") end def test_datetime_select_within_fields_for @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 16, 35) _erbout = '' fields_for :post, @post do |f| _erbout.concat f.datetime_select(:updated_at) end expected = "\n" expected << "\n" expected << "\n" expected << " — \n" expected << " : \n" assert_dom_equal(expected, _erbout) end def test_date_select_with_zero_value_and_no_start_year expected = %(\n" expected << %(\n" expected << %(\n" assert_equal expected, select_date(0, :end_year => Date.today.year+1, :prefix => "date[first]") end def test_date_select_with_zero_value_and_no_end_year expected = %(\n" expected << %(\n" expected << %(\n" assert_equal expected, select_date(0, :start_year => 2003, :prefix => "date[first]") end def test_date_select_with_zero_value_and_no_start_and_end_year expected = %(\n" expected << %(\n" expected << %(\n" assert_equal expected, select_date(0, :prefix => "date[first]") end def test_date_select_with_nil_value_and_no_start_and_end_year expected = %(\n" expected << %(\n" expected << %(\n" assert_equal expected, select_date(nil, :prefix => "date[first]") end def test_datetime_select_with_nil_value_and_no_start_and_end_year expected = %(\n" expected << %(\n" expected << %(\n" expected << %(\n" expected << %(\n" assert_equal expected, select_datetime(nil, :prefix => "date[first]") end def test_datetime_select_with_options_index @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 16, 35) id = 456 expected = %{\n" expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << " : " expected << %{\n" assert_equal expected, datetime_select("post", "updated_at", :index => id) end def test_datetime_select_with_auto_index @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 16, 35) id = @post.id expected = %{\n" expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << " : " expected << %{\n" assert_equal expected, datetime_select("post[]", "updated_at") end def test_datetime_select_with_seconds @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n" expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << " : " expected << %{\n" expected << " : " expected << %{\n" assert_equal expected, datetime_select("post", "updated_at", :include_seconds => true) end def test_datetime_select_discard_year @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n} expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << " : " expected << %{\n" assert_equal expected, datetime_select("post", "updated_at", :discard_year => true) end def test_datetime_select_discard_month @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n" expected << %{\n} expected << %{\n} expected << " — " expected << %{\n" expected << " : " expected << %{\n" assert_equal expected, datetime_select("post", "updated_at", :discard_month => true) end def test_datetime_select_discard_year_and_month @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n} expected << %{\n} expected << %{\n} expected << %{\n" expected << " : " expected << %{\n" assert_equal expected, datetime_select("post", "updated_at", :discard_year => true, :discard_month => true) end def test_datetime_select_invalid_order @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n" expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << " : " expected << %{\n" assert_equal expected, datetime_select("post", "updated_at", :order => [:minute, :day, :hour, :month, :year, :second]) end def test_datetime_select_discard_with_order @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 15, 16, 35) expected = %{\n} expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << " : " expected << %{\n" assert_equal expected, datetime_select("post", "updated_at", :order => [:day, :month]) end def test_datetime_select_with_default_value_as_time @post = Post.new @post.updated_at = nil expected = %{\n" expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << " : " expected << %{\n" assert_equal expected, datetime_select("post", "updated_at", :default => Time.local(2006, 9, 19, 15, 16, 35)) end def test_include_blank_overrides_default_option @post = Post.new @post.updated_at = nil expected = %{\n" expected << %{\n" expected << %{\n" assert_equal expected, date_select("post", "updated_at", :default => Time.local(2006, 9, 19, 15, 16, 35), :include_blank => true) end def test_datetime_select_with_default_value_as_hash @post = Post.new @post.updated_at = nil expected = %{\n" expected << %{\n" expected << %{\n" expected << " — " expected << %{\n" expected << " : " expected << %{\n" assert_equal expected, datetime_select("post", "updated_at", :default => { :month => 10, :minute => 42, :hour => 9 }) end end