chronic 0.2.2 → 0.10.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +6 -0
  3. data/.travis.yml +8 -0
  4. data/HISTORY.md +243 -0
  5. data/LICENSE +21 -0
  6. data/README.md +182 -0
  7. data/Rakefile +62 -15
  8. data/chronic.gemspec +23 -0
  9. data/lib/chronic/date.rb +82 -0
  10. data/lib/chronic/grabber.rb +24 -17
  11. data/lib/chronic/handler.rb +97 -0
  12. data/lib/chronic/handlers.rb +490 -312
  13. data/lib/chronic/mini_date.rb +38 -0
  14. data/lib/chronic/numerizer.rb +130 -0
  15. data/lib/chronic/ordinal.rb +33 -24
  16. data/lib/chronic/parser.rb +268 -0
  17. data/lib/chronic/pointer.rb +22 -17
  18. data/lib/chronic/repeater.rb +137 -107
  19. data/lib/chronic/repeaters/repeater_day.rb +51 -44
  20. data/lib/chronic/repeaters/repeater_day_name.rb +50 -43
  21. data/lib/chronic/repeaters/repeater_day_portion.rb +97 -81
  22. data/lib/chronic/repeaters/repeater_fortnight.rb +61 -54
  23. data/lib/chronic/repeaters/repeater_hour.rb +51 -44
  24. data/lib/chronic/repeaters/repeater_minute.rb +51 -44
  25. data/lib/chronic/repeaters/repeater_month.rb +79 -60
  26. data/lib/chronic/repeaters/repeater_month_name.rb +85 -83
  27. data/lib/chronic/repeaters/repeater_season.rb +110 -22
  28. data/lib/chronic/repeaters/repeater_season_name.rb +41 -22
  29. data/lib/chronic/repeaters/repeater_second.rb +41 -34
  30. data/lib/chronic/repeaters/repeater_time.rb +128 -104
  31. data/lib/chronic/repeaters/repeater_week.rb +65 -58
  32. data/lib/chronic/repeaters/repeater_weekday.rb +86 -0
  33. data/lib/chronic/repeaters/repeater_weekend.rb +59 -52
  34. data/lib/chronic/repeaters/repeater_year.rb +72 -52
  35. data/lib/chronic/scalar.rb +53 -46
  36. data/lib/chronic/season.rb +26 -0
  37. data/lib/chronic/separator.rb +174 -43
  38. data/lib/chronic/sign.rb +49 -0
  39. data/lib/chronic/span.rb +31 -0
  40. data/lib/chronic/tag.rb +37 -0
  41. data/lib/chronic/time.rb +40 -0
  42. data/lib/chronic/time_zone.rb +21 -11
  43. data/lib/chronic/token.rb +51 -0
  44. data/lib/chronic.rb +94 -69
  45. data/test/helper.rb +12 -0
  46. data/test/test_chronic.rb +183 -0
  47. data/test/test_daylight_savings.rb +118 -0
  48. data/test/{test_Handler.rb → test_handler.rb} +73 -55
  49. data/test/test_mini_date.rb +32 -0
  50. data/test/test_numerizer.rb +86 -0
  51. data/test/test_parsing.rb +891 -248
  52. data/test/{test_RepeaterDayName.rb → test_repeater_day_name.rb} +16 -17
  53. data/test/test_repeater_day_portion.rb +254 -0
  54. data/test/{test_RepeaterFortnight.rb → test_repeater_fortnight.rb} +17 -18
  55. data/test/{test_RepeaterHour.rb → test_repeater_hour.rb} +23 -20
  56. data/test/test_repeater_minute.rb +34 -0
  57. data/test/{test_RepeaterMonth.rb → test_repeater_month.rb} +21 -18
  58. data/test/{test_RepeaterMonthName.rb → test_repeater_month_name.rb} +18 -19
  59. data/test/test_repeater_season.rb +40 -0
  60. data/test/{test_RepeaterTime.rb → test_repeater_time.rb} +39 -23
  61. data/test/{test_RepeaterWeek.rb → test_repeater_week.rb} +17 -18
  62. data/test/test_repeater_weekday.rb +55 -0
  63. data/test/{test_RepeaterWeekend.rb → test_repeater_weekend.rb} +22 -23
  64. data/test/{test_RepeaterYear.rb → test_repeater_year.rb} +25 -19
  65. data/test/{test_Span.rb → test_span.rb} +7 -8
  66. data/test/{test_Token.rb → test_token.rb} +6 -7
  67. metadata +163 -85
  68. data/History.txt +0 -49
  69. data/Manifest.txt +0 -47
  70. data/README.txt +0 -149
  71. data/lib/chronic/chronic.rb +0 -239
  72. data/lib/numerizer/numerizer.rb +0 -103
  73. data/test/suite.rb +0 -9
  74. data/test/test_Chronic.rb +0 -50
  75. data/test/test_Numerizer.rb +0 -48
  76. data/test/test_Time.rb +0 -50
@@ -1,110 +1,128 @@
1
- require 'chronic'
2
- require 'test/unit'
1
+ require 'helper'
2
+
3
+ class TestHandler < TestCase
3
4
 
4
- class TestHandler < Test::Unit::TestCase
5
-
6
5
  def setup
7
6
  # Wed Aug 16 14:00:00 UTC 2006
8
7
  @now = Time.local(2006, 8, 16, 14, 0, 0, 0)
9
8
  end
10
9
 
10
+ def definitions
11
+ @definitions ||= Chronic::Parser.new.definitions
12
+ end
13
+
11
14
  def test_handler_class_1
12
15
  handler = Chronic::Handler.new([:repeater], :handler)
13
-
16
+
14
17
  tokens = [Chronic::Token.new('friday')]
15
18
  tokens[0].tag(Chronic::RepeaterDayName.new(:friday))
16
-
17
- assert handler.match(tokens, Chronic.definitions)
18
-
19
+
20
+ assert handler.match(tokens, definitions)
21
+
19
22
  tokens << Chronic::Token.new('afternoon')
20
23
  tokens[1].tag(Chronic::RepeaterDayPortion.new(:afternoon))
21
-
22
- assert !handler.match(tokens, Chronic.definitions)
24
+
25
+ assert !handler.match(tokens, definitions)
23
26
  end
24
-
27
+
25
28
  def test_handler_class_2
26
29
  handler = Chronic::Handler.new([:repeater, :repeater?], :handler)
27
-
30
+
28
31
  tokens = [Chronic::Token.new('friday')]
29
32
  tokens[0].tag(Chronic::RepeaterDayName.new(:friday))
30
-
31
- assert handler.match(tokens, Chronic.definitions)
32
-
33
+
34
+ assert handler.match(tokens, definitions)
35
+
33
36
  tokens << Chronic::Token.new('afternoon')
34
37
  tokens[1].tag(Chronic::RepeaterDayPortion.new(:afternoon))
35
-
36
- assert handler.match(tokens, Chronic.definitions)
37
-
38
+
39
+ assert handler.match(tokens, definitions)
40
+
38
41
  tokens << Chronic::Token.new('afternoon')
39
42
  tokens[2].tag(Chronic::RepeaterDayPortion.new(:afternoon))
40
-
41
- assert !handler.match(tokens, Chronic.definitions)
43
+
44
+ assert !handler.match(tokens, definitions)
42
45
  end
43
-
46
+
44
47
  def test_handler_class_3
45
48
  handler = Chronic::Handler.new([:repeater, 'time?'], :handler)
46
-
49
+
47
50
  tokens = [Chronic::Token.new('friday')]
48
51
  tokens[0].tag(Chronic::RepeaterDayName.new(:friday))
49
-
50
- assert handler.match(tokens, Chronic.definitions)
51
-
52
+
53
+ assert handler.match(tokens, definitions)
54
+
52
55
  tokens << Chronic::Token.new('afternoon')
53
56
  tokens[1].tag(Chronic::RepeaterDayPortion.new(:afternoon))
54
-
55
- assert !handler.match(tokens, Chronic.definitions)
57
+
58
+ assert !handler.match(tokens, definitions)
56
59
  end
57
-
60
+
58
61
  def test_handler_class_4
59
62
  handler = Chronic::Handler.new([:repeater_month_name, :scalar_day, 'time?'], :handler)
60
-
63
+
61
64
  tokens = [Chronic::Token.new('may')]
62
65
  tokens[0].tag(Chronic::RepeaterMonthName.new(:may))
63
-
64
- assert !handler.match(tokens, Chronic.definitions)
65
-
66
+
67
+ assert !handler.match(tokens, definitions)
68
+
66
69
  tokens << Chronic::Token.new('27')
67
70
  tokens[1].tag(Chronic::ScalarDay.new(27))
68
-
69
- assert handler.match(tokens, Chronic.definitions)
71
+
72
+ assert handler.match(tokens, definitions)
70
73
  end
71
-
74
+
72
75
  def test_handler_class_5
73
76
  handler = Chronic::Handler.new([:repeater, 'time?'], :handler)
74
-
77
+
75
78
  tokens = [Chronic::Token.new('friday')]
76
79
  tokens[0].tag(Chronic::RepeaterDayName.new(:friday))
77
-
78
- assert handler.match(tokens, Chronic.definitions)
79
-
80
+
81
+ assert handler.match(tokens, definitions)
82
+
80
83
  tokens << Chronic::Token.new('5:00')
81
84
  tokens[1].tag(Chronic::RepeaterTime.new('5:00'))
82
-
83
- assert handler.match(tokens, Chronic.definitions)
84
-
85
+
86
+ assert handler.match(tokens, definitions)
87
+
85
88
  tokens << Chronic::Token.new('pm')
86
89
  tokens[2].tag(Chronic::RepeaterDayPortion.new(:pm))
87
-
88
- assert handler.match(tokens, Chronic.definitions)
90
+
91
+ assert handler.match(tokens, definitions)
89
92
  end
90
-
93
+
91
94
  def test_handler_class_6
92
95
  handler = Chronic::Handler.new([:scalar, :repeater, :pointer], :handler)
93
-
96
+
94
97
  tokens = [Chronic::Token.new('3'),
95
98
  Chronic::Token.new('years'),
96
99
  Chronic::Token.new('past')]
97
-
100
+
98
101
  tokens[0].tag(Chronic::Scalar.new(3))
99
102
  tokens[1].tag(Chronic::RepeaterYear.new(:year))
100
103
  tokens[2].tag(Chronic::Pointer.new(:past))
101
-
102
- assert handler.match(tokens, Chronic.definitions)
104
+
105
+ assert handler.match(tokens, definitions)
103
106
  end
104
-
105
- def test_constantize
106
- handler = Chronic::Handler.new([], :handler)
107
- assert_equal Chronic::RepeaterTime, handler.constantize(:repeater_time)
107
+
108
+ def test_handler_class_7
109
+ handler = Chronic::Handler.new([[:separator_on, :separator_at], :scalar], :handler)
110
+
111
+ tokens = [Chronic::Token.new('at'),
112
+ Chronic::Token.new('14')]
113
+
114
+ tokens[0].tag(Chronic::SeparatorAt.new('at'))
115
+ tokens[1].tag(Chronic::Scalar.new(14))
116
+
117
+ assert handler.match(tokens, definitions)
118
+
119
+ tokens = [Chronic::Token.new('on'),
120
+ Chronic::Token.new('15')]
121
+
122
+ tokens[0].tag(Chronic::SeparatorOn.new('on'))
123
+ tokens[1].tag(Chronic::Scalar.new(15))
124
+
125
+ assert handler.match(tokens, definitions)
108
126
  end
109
-
110
- end
127
+
128
+ end
@@ -0,0 +1,32 @@
1
+ require 'helper'
2
+
3
+ class TestMiniDate < TestCase
4
+ def test_valid_month
5
+ assert_raises(ArgumentError){ Chronic::MiniDate.new(0,12) }
6
+ assert_raises(ArgumentError){ Chronic::MiniDate.new(13,1) }
7
+ end
8
+
9
+ def test_is_between
10
+ m=Chronic::MiniDate.new(3,2)
11
+ assert m.is_between?(Chronic::MiniDate.new(2,4), Chronic::MiniDate.new(4,7))
12
+ assert !m.is_between?(Chronic::MiniDate.new(1,5), Chronic::MiniDate.new(2,7))
13
+
14
+ #There was a hang if date tested is in december and outside the testing range
15
+ m=Chronic::MiniDate.new(12,24)
16
+ assert !m.is_between?(Chronic::MiniDate.new(10,1), Chronic::MiniDate.new(12,21))
17
+ end
18
+
19
+ def test_is_between_short_range
20
+ m=Chronic::MiniDate.new(5,10)
21
+ assert m.is_between?(Chronic::MiniDate.new(5,3), Chronic::MiniDate.new(5,12))
22
+ assert !m.is_between?(Chronic::MiniDate.new(5,11), Chronic::MiniDate.new(5,15))
23
+ end
24
+
25
+ def test_is_between_wrapping_range
26
+ m=Chronic::MiniDate.new(1,1)
27
+ assert m.is_between?(Chronic::MiniDate.new(11,11), Chronic::MiniDate.new(2,2))
28
+ m=Chronic::MiniDate.new(12,12)
29
+ assert m.is_between?(Chronic::MiniDate.new(11,11), Chronic::MiniDate.new(1,5))
30
+ end
31
+
32
+ end
@@ -0,0 +1,86 @@
1
+ require 'helper'
2
+
3
+ class ParseNumbersTest < TestCase
4
+
5
+ def test_straight_parsing
6
+ strings = {
7
+ 'one' => 1,
8
+ 'five' => 5,
9
+ 'ten' => 10,
10
+ 'eleven' => 11,
11
+ 'twelve' => 12,
12
+ 'thirteen' => 13,
13
+ 'fourteen' => 14,
14
+ 'fifteen' => 15,
15
+ 'sixteen' => 16,
16
+ 'seventeen' => 17,
17
+ 'eighteen' => 18,
18
+ 'nineteen' => 19,
19
+ 'twenty' => 20,
20
+ 'twenty seven' => 27,
21
+ 'thirty-one' => 31,
22
+ 'thirty-seven' => 37,
23
+ 'thirty seven' => 37,
24
+ 'fifty nine' => 59,
25
+ 'forty two' => 42,
26
+ 'fourty two' => 42,
27
+ # 'a hundred' => 100,
28
+ 'one hundred' => 100,
29
+ 'one hundred and fifty' => 150,
30
+ # 'one fifty' => 150,
31
+ 'two-hundred' => 200,
32
+ '5 hundred' => 500,
33
+ 'nine hundred and ninety nine' => 999,
34
+ 'one thousand' => 1000,
35
+ 'twelve hundred' => 1200,
36
+ 'one thousand two hundred' => 1_200,
37
+ 'seventeen thousand' => 17_000,
38
+ 'twentyone-thousand-four-hundred-and-seventy-three' => 21_473,
39
+ 'seventy four thousand and two' => 74_002,
40
+ 'ninety nine thousand nine hundred ninety nine' => 99_999,
41
+ '100 thousand' => 100_000,
42
+ 'two hundred fifty thousand' => 250_000,
43
+ 'one million' => 1_000_000,
44
+ 'one million two hundred fifty thousand and seven' => 1_250_007,
45
+ 'one billion' => 1_000_000_000,
46
+ 'one billion and one' => 1_000_000_001}
47
+
48
+ strings.each do |key, val|
49
+ assert_equal val, Chronic::Numerizer.numerize(key).to_i
50
+ end
51
+ end
52
+
53
+ def test_ordinal_strings
54
+ {
55
+ 'first' => '1st',
56
+ 'second' => 'second',
57
+ 'second day' => '2nd day',
58
+ 'second of may' => '2nd of may',
59
+ 'fifth' => '5th',
60
+ 'twelfth' => '12th',
61
+ 'twentieth' => '20th',
62
+ 'thirtieth' => '30th',
63
+ 'fourtieth' => '40th',
64
+ 'fiftieth' => '50th',
65
+ 'sixtieth' => '60th',
66
+ 'seventieth' => '70th',
67
+ 'eightieth' => '80th',
68
+ 'ninetieth' => '90th',
69
+ 'hundredth' => '100th',
70
+ 'thousandth' => '1000th',
71
+ 'millionth' => '1000000th',
72
+ 'billionth' => '1000000000th',
73
+ 'trillionth' => '1000000000000th',
74
+ 'twenty third' => '23rd',
75
+ 'first day month two' => '1st day month 2'
76
+ }.each do |key, val|
77
+ # Use pre_normalize here instead of Numerizer directly because
78
+ # pre_normalize deals with parsing 'second' appropriately
79
+ assert_equal val, Chronic::Parser.new.pre_normalize(key)
80
+ end
81
+ end
82
+
83
+ def test_edges
84
+ assert_equal "27 Oct 2006 7:30am", Chronic::Numerizer.numerize("27 Oct 2006 7:30am")
85
+ end
86
+ end