test/test_parsing.rb in chronic-0.8.0 vs test/test_parsing.rb in chronic-0.9.0

- old
+ new

@@ -7,27 +7,43 @@ def setup @time_2006_08_16_14_00_00 = TIME_2006_08_16_14_00_00 end def test_handle_generic - time = Chronic.parse("2012-08-02T12:00:00+01:00") - assert_equal Time.local(2012, 8, 2, 12), time + time = Chronic.parse("2012-08-02T13:00:00") + assert_equal Time.local(2012, 8, 2, 13), time + time = Chronic.parse("2012-08-02T13:00:00+01:00") + assert_equal Time.utc(2012, 8, 2, 12), time + + time = Chronic.parse("2012-08-02T08:00:00-04:00") + assert_equal Time.utc(2012, 8, 2, 12), time + time = Chronic.parse("2012-08-02T12:00:00Z") assert_equal Time.utc(2012, 8, 2, 12), time + + time = Chronic.parse("2012-01-03 01:00:00.100") + time2 = Time.parse("2012-01-03 01:00:00.100") + assert_equal time, time2 end def test_handle_rmn_sd time = parse_now("aug 3") assert_equal Time.local(2006, 8, 3, 12), time time = parse_now("aug 3", :context => :past) assert_equal Time.local(2006, 8, 3, 12), time + time = parse_now("aug. 3") + assert_equal Time.local(2006, 8, 3, 12), time + time = parse_now("aug 20") assert_equal Time.local(2006, 8, 20, 12), time + time = parse_now("aug-20") + assert_equal Time.local(2006, 8, 20, 12), time + time = parse_now("aug 20", :context => :future) assert_equal Time.local(2006, 8, 20, 12), time time = parse_now("may 27") assert_equal Time.local(2007, 5, 27, 12), time @@ -96,10 +112,16 @@ def test_handle_sd_rmn time = parse_now("22 February") assert_equal Time.local(2007, 2, 22, 12), time + time = parse_now("22 feb") + assert_equal Time.local(2007, 2, 22, 12), time + + time = parse_now("22-feb") + assert_equal Time.local(2007, 2, 22, 12), time + time = parse_now("31 of may at 6:30pm") assert_equal Time.local(2007, 5, 31, 18, 30), time time = parse_now("11 december 8am") assert_equal Time.local(2006, 12, 11, 8), time @@ -180,10 +202,13 @@ time = parse_now("may 27 79 at 4:30", :ambiguous_time_range => :none) assert_equal Time.local(1979, 5, 27, 4, 30), time time = parse_now("may 27 32") assert_equal Time.local(2032, 5, 27, 12, 0, 0), time + + time = parse_now("oct 5 2012 1045pm") + assert_equal Time.local(2012, 10, 5, 22, 45), time end def test_handle_rmn_od_sy time = parse_now("may 1st 01") assert_equal Time.local(2001, 5, 1, 12), time @@ -300,23 +325,33 @@ assert_equal Time.local(2012, 5, 25, 22, 6, 50), time end def test_handle_sm_sd time = parse_now("05/06") - assert_equal Time.local(2006, 5, 6, 12), time + assert_equal Time.local(2007, 5, 6, 12), time time = parse_now("05/06", :endian_precedence => [:little, :medium]) - assert_equal Time.local(2006, 6, 5, 12), time + assert_equal Time.local(2007, 6, 5, 12), time time = parse_now("05/06 6:05:57 PM") - assert_equal Time.local(2006, 5, 6, 18, 05, 57), time + assert_equal Time.local(2007, 5, 6, 18, 05, 57), time time = parse_now("05/06 6:05:57 PM", :endian_precedence => [:little, :medium]) - assert_equal Time.local(2006, 6, 5, 18, 05, 57), time + assert_equal Time.local(2007, 6, 5, 18, 05, 57), time - time = parse_now("13/01") - assert_equal Time.local(2006, 1, 13, 12), time + time = parse_now("13/09") + assert_equal Time.local(2006, 9, 13, 12), time + + # future + time = parse_now("05/06") # future is default context + assert_equal Time.local(2007, 5, 6, 12), time + + time = parse_now("1/13", :context => :future) + assert_equal Time.local(2007, 1, 13, 12), time + + time = parse_now("3/13", :context => :none) + assert_equal Time.local(2006, 3, 13, 12), time end # def test_handle_sm_sy # time = parse_now("05/06") # assert_equal Time.local(2006, 5, 16, 12), time @@ -400,10 +435,13 @@ end def test_handle_sm_rmn_sy time = parse_now('30-Mar-11') assert_equal Time.local(2011, 3, 30, 12), time + + time = parse_now('31-Aug-12') + assert_equal Time.local(2012, 8, 31), time end # end of testing handlers def test_parse_guess_r @@ -832,10 +870,20 @@ time = parse_now("20 seconds from now") assert_equal Time.local(2006, 8, 16, 14, 0, 20), time time = Chronic.parse("2 months ago", :now => Time.parse("2007-03-07 23:30")) assert_equal Time.local(2007, 1, 7, 23, 30), time + + # Two repeaters + time = parse_now("25 minutes and 20 seconds from now") + assert_equal Time.local(2006, 8, 16, 14, 25, 20), time + + time = parse_now("24 hours and 20 minutes from now") + assert_equal Time.local(2006, 8, 17, 14, 20, 0), time + + time = parse_now("24 hours 20 minutes from now") + assert_equal Time.local(2006, 8, 17, 14, 20, 0), time end def test_parse_guess_p_s_r time = parse_now("in 3 hours") assert_equal Time.local(2006, 8, 16, 17), time @@ -921,10 +969,31 @@ assert_equal parse_now("2867532 seconds from now"), parse_now("two million eight hundred and sixty seven thousand five hundred and thirty two seconds from now") assert_equal parse_now("may 10th"), parse_now("may tenth") assert_equal parse_now("second monday in january"), parse_now("2nd monday in january") end + def test_relative_to_an_hour_before + # example prenormalization "10 to 2" becomes "10 minutes past 2" + assert_equal Time.local(2006, 8, 16, 13, 50), parse_now("10 to 2") + assert_equal Time.local(2006, 8, 16, 13, 50), parse_now("10 till 2") + assert_equal Time.local(2006, 8, 16, 13, 50), parse_now("10 prior to 2") + assert_equal Time.local(2006, 8, 16, 13, 50), parse_now("10 before 2") + + # uses the current hour, so 2006-08-16 13:50:00, not 14:50 + assert_equal Time.local(2006, 8, 16, 13, 50), parse_now("10 to") + assert_equal Time.local(2006, 8, 16, 13, 50), parse_now("10 till") + + assert_equal Time.local(2006, 8, 16, 15, 45), parse_now("quarter to 4") + end + + def test_relative_to_an_hour_after + # not nil + assert_equal Time.local(2006, 8, 16, 14, 10), parse_now("10 after 2") + assert_equal Time.local(2006, 8, 16, 14, 10), parse_now("10 past 2") + assert_equal Time.local(2006, 8, 16, 14, 30), parse_now("half past 2") + end + def test_parse_only_complete_pointers assert_equal parse_now("eat pasty buns today at 2pm"), @time_2006_08_16_14_00_00 assert_equal parse_now("futuristically speaking today at 2pm"), @time_2006_08_16_14_00_00 assert_equal parse_now("meeting today at 2pm"), @time_2006_08_16_14_00_00 end @@ -937,20 +1006,10 @@ def test_a_p assert_equal Time.local(2006, 8, 16, 0, 15), parse_now("8/16/2006 at 12:15a") assert_equal Time.local(2006, 8, 16, 18, 30), parse_now("8/16/2006 at 6:30p") end - def test_argument_validation - assert_raises(ArgumentError) do - time = Chronic.parse("may 27", :foo => :bar) - end - - assert_raises(ArgumentError) do - time = Chronic.parse("may 27", :context => :bar) - end - end - def test_seasons t = parse_now("this spring", :guess => false) assert_equal Time.local(2007, 3, 20), t.begin assert_equal Time.local(2007, 6, 20), t.end @@ -1017,10 +1076,42 @@ time = parse_now("Thursday December 31") assert_equal Time.local(2006, 12, 31, 12), time end + def test_handle_rdn_rmn_sd_rt + time = parse_now("Thu Aug 10 4pm") + assert_equal Time.local(2006, 8, 10, 16), time + + time = parse_now("Thu Aug 10 at 4pm") + assert_equal Time.local(2006, 8, 10, 16), time + end + + def test_handle_rdn_rmn_od_rt + time = parse_now("Thu Aug 10th at 4pm") + assert_equal Time.local(2006, 8, 10, 16), time + end + + def test_handle_rdn_od_rt + time = parse_now("Thu 17th at 4pm") + assert_equal Time.local(2006, 8, 17, 16), time + + time = parse_now("Thu 16th at 4pm") + assert_equal Time.local(2006, 8, 16, 16), time + + time = parse_now("Thu 1st at 4pm") + assert_equal Time.local(2006, 9, 1, 16), time + + time = parse_now("Thu 1st at 4pm", :context => :past) + assert_equal Time.local(2006, 8, 1, 16), time + end + + def test_handle_rdn_od + time = parse_now("Thu 17th") + assert_equal Time.local(2006, 8, 17, 12), time + end + def test_handle_rdn_rmn_sd_sy time = parse_now("Thu Aug 10 2006") assert_equal Time.local(2006, 8, 10, 12), time time = parse_now("Thursday July 31 2006") @@ -1045,7 +1136,10 @@ end private def parse_now(string, options={}) Chronic.parse(string, {:now => TIME_2006_08_16_14_00_00 }.merge(options)) + end + def pre_normalize(s) + Chronic::Parser.new.pre_normalize s end end