chronic 0.1.5 → 0.6.5
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.
- data/.gemtest +0 -0
- data/.gitignore +5 -0
- data/.yardopts +3 -0
- data/HISTORY.md +180 -0
- data/LICENSE +21 -0
- data/README.md +176 -0
- data/Rakefile +39 -32
- data/chronic.gemspec +17 -0
- data/lib/chronic/chronic.rb +294 -208
- data/lib/chronic/grabber.rb +22 -17
- data/lib/chronic/handler.rb +90 -0
- data/lib/chronic/handlers.rb +365 -278
- data/lib/chronic/mini_date.rb +38 -0
- data/lib/chronic/numerizer.rb +121 -0
- data/lib/chronic/ordinal.rb +23 -19
- data/lib/chronic/pointer.rb +20 -17
- data/lib/chronic/repeater.rb +126 -105
- data/lib/chronic/repeaters/repeater_day.rb +49 -43
- data/lib/chronic/repeaters/repeater_day_name.rb +48 -42
- data/lib/chronic/repeaters/repeater_day_portion.rb +81 -80
- data/lib/chronic/repeaters/repeater_fortnight.rb +59 -53
- data/lib/chronic/repeaters/repeater_hour.rb +49 -43
- data/lib/chronic/repeaters/repeater_minute.rb +55 -39
- data/lib/chronic/repeaters/repeater_month.rb +77 -55
- data/lib/chronic/repeaters/repeater_month_name.rb +83 -82
- data/lib/chronic/repeaters/repeater_season.rb +107 -21
- data/lib/chronic/repeaters/repeater_season_name.rb +41 -22
- data/lib/chronic/repeaters/repeater_second.rb +39 -33
- data/lib/chronic/repeaters/repeater_time.rb +117 -103
- data/lib/chronic/repeaters/repeater_week.rb +63 -57
- data/lib/chronic/repeaters/repeater_weekday.rb +85 -0
- data/lib/chronic/repeaters/repeater_weekend.rb +64 -9
- data/lib/chronic/repeaters/repeater_year.rb +70 -51
- data/lib/chronic/scalar.rb +64 -29
- data/lib/chronic/season.rb +37 -0
- data/lib/chronic/separator.rb +50 -38
- data/lib/chronic/span.rb +31 -0
- data/lib/chronic/tag.rb +42 -0
- data/lib/chronic/time_zone.rb +30 -0
- data/lib/chronic/token.rb +45 -0
- data/lib/chronic.rb +86 -22
- data/test/helper.rb +6 -0
- data/test/test_Chronic.rb +118 -20
- data/test/test_DaylightSavings.rb +118 -0
- data/test/test_Handler.rb +36 -42
- data/test/test_MiniDate.rb +32 -0
- data/test/test_Numerizer.rb +72 -0
- data/test/test_RepeaterDayName.rb +15 -16
- data/test/test_RepeaterFortnight.rb +16 -17
- data/test/test_RepeaterHour.rb +22 -19
- data/test/test_RepeaterMinute.rb +34 -0
- data/test/test_RepeaterMonth.rb +20 -17
- data/test/test_RepeaterMonthName.rb +17 -18
- data/test/test_RepeaterSeason.rb +40 -0
- data/test/test_RepeaterTime.rb +20 -22
- data/test/test_RepeaterWeek.rb +16 -17
- data/test/test_RepeaterWeekday.rb +55 -0
- data/test/test_RepeaterWeekend.rb +74 -0
- data/test/test_RepeaterYear.rb +24 -18
- data/test/test_Span.rb +5 -6
- data/test/test_Token.rb +5 -6
- data/test/test_parsing.rb +743 -338
- metadata +81 -54
- data/History.txt +0 -29
- data/Manifest.txt +0 -43
- data/README.txt +0 -149
- data/test/parse_numbers.rb +0 -50
- data/test/suite.rb +0 -9
data/test/test_Chronic.rb
CHANGED
|
@@ -1,50 +1,148 @@
|
|
|
1
|
-
require '
|
|
2
|
-
require 'test/unit'
|
|
1
|
+
require 'helper'
|
|
3
2
|
|
|
4
3
|
class TestChronic < Test::Unit::TestCase
|
|
5
|
-
|
|
4
|
+
|
|
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 test_pre_normalize_numerized_string
|
|
11
|
+
string = 'two and a half years'
|
|
12
|
+
assert_equal Chronic::Numerizer.numerize(string), Chronic.pre_normalize(string)
|
|
13
|
+
end
|
|
14
|
+
|
|
11
15
|
def test_post_normalize_am_pm_aliases
|
|
12
16
|
# affect wanted patterns
|
|
13
|
-
|
|
17
|
+
|
|
14
18
|
tokens = [Chronic::Token.new("5:00"), Chronic::Token.new("morning")]
|
|
15
19
|
tokens[0].tag(Chronic::RepeaterTime.new("5:00"))
|
|
16
20
|
tokens[1].tag(Chronic::RepeaterDayPortion.new(:morning))
|
|
17
|
-
|
|
21
|
+
|
|
18
22
|
assert_equal :morning, tokens[1].tags[0].type
|
|
19
|
-
|
|
20
|
-
tokens = Chronic.dealias_and_disambiguate_times(tokens, {})
|
|
21
|
-
|
|
23
|
+
|
|
24
|
+
tokens = Chronic::Handlers.dealias_and_disambiguate_times(tokens, {})
|
|
25
|
+
|
|
22
26
|
assert_equal :am, tokens[1].tags[0].type
|
|
23
27
|
assert_equal 2, tokens.size
|
|
24
|
-
|
|
28
|
+
|
|
25
29
|
# don't affect unwanted patterns
|
|
26
|
-
|
|
30
|
+
|
|
27
31
|
tokens = [Chronic::Token.new("friday"), Chronic::Token.new("morning")]
|
|
28
32
|
tokens[0].tag(Chronic::RepeaterDayName.new(:friday))
|
|
29
33
|
tokens[1].tag(Chronic::RepeaterDayPortion.new(:morning))
|
|
30
|
-
|
|
34
|
+
|
|
31
35
|
assert_equal :morning, tokens[1].tags[0].type
|
|
32
|
-
|
|
33
|
-
tokens = Chronic.dealias_and_disambiguate_times(tokens, {})
|
|
34
|
-
|
|
36
|
+
|
|
37
|
+
tokens = Chronic::Handlers.dealias_and_disambiguate_times(tokens, {})
|
|
38
|
+
|
|
35
39
|
assert_equal :morning, tokens[1].tags[0].type
|
|
36
40
|
assert_equal 2, tokens.size
|
|
37
41
|
end
|
|
38
|
-
|
|
42
|
+
|
|
39
43
|
def test_guess
|
|
40
44
|
span = Chronic::Span.new(Time.local(2006, 8, 16, 0), Time.local(2006, 8, 17, 0))
|
|
41
45
|
assert_equal Time.local(2006, 8, 16, 12), Chronic.guess(span)
|
|
42
|
-
|
|
46
|
+
|
|
43
47
|
span = Chronic::Span.new(Time.local(2006, 8, 16, 0), Time.local(2006, 8, 17, 0, 0, 1))
|
|
44
48
|
assert_equal Time.local(2006, 8, 16, 12), Chronic.guess(span)
|
|
45
|
-
|
|
49
|
+
|
|
46
50
|
span = Chronic::Span.new(Time.local(2006, 11), Time.local(2006, 12))
|
|
47
51
|
assert_equal Time.local(2006, 11, 16), Chronic.guess(span)
|
|
48
52
|
end
|
|
49
|
-
|
|
50
|
-
|
|
53
|
+
|
|
54
|
+
def test_now
|
|
55
|
+
Chronic.parse('now', :now => Time.local(2006, 01))
|
|
56
|
+
assert_equal Time.local(2006, 01), Chronic.now
|
|
57
|
+
|
|
58
|
+
Chronic.parse('now', :now => Time.local(2007, 01))
|
|
59
|
+
assert_equal Time.local(2007, 01), Chronic.now
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def test_endian_definitions
|
|
63
|
+
# middle, little
|
|
64
|
+
endians = [
|
|
65
|
+
Chronic::Handler.new([:scalar_month, :separator_slash_or_dash, :scalar_day, :separator_slash_or_dash, :scalar_year, :separator_at?, 'time?'], :handle_sm_sd_sy),
|
|
66
|
+
Chronic::Handler.new([:scalar_day, :separator_slash_or_dash, :scalar_month, :separator_slash_or_dash, :scalar_year, :separator_at?, 'time?'], :handle_sd_sm_sy)
|
|
67
|
+
]
|
|
68
|
+
|
|
69
|
+
assert_equal endians, Chronic.definitions[:endian]
|
|
70
|
+
|
|
71
|
+
defs = Chronic.definitions(:endian_precedence => :little)
|
|
72
|
+
assert_equal endians.reverse, defs[:endian]
|
|
73
|
+
|
|
74
|
+
defs = Chronic.definitions(:endian_precedence => [:little, :middle])
|
|
75
|
+
assert_equal endians.reverse, defs[:endian]
|
|
76
|
+
|
|
77
|
+
assert_raises(ArgumentError) do
|
|
78
|
+
Chronic.definitions(:endian_precedence => :invalid)
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def test_passing_options
|
|
83
|
+
assert_raises(ArgumentError) do
|
|
84
|
+
Chronic.parse('now', :invalid => :option)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
assert_raises(ArgumentError) do
|
|
88
|
+
Chronic.parse('now', :context => :invalid_context)
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def test_debug
|
|
93
|
+
require 'stringio'
|
|
94
|
+
$stdout = StringIO.new
|
|
95
|
+
Chronic.debug = true
|
|
96
|
+
|
|
97
|
+
Chronic.parse 'now'
|
|
98
|
+
assert $stdout.string.include?('this(grabber-this)')
|
|
99
|
+
ensure
|
|
100
|
+
$stdout = STDOUT
|
|
101
|
+
Chronic.debug = false
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# Chronic.construct
|
|
105
|
+
|
|
106
|
+
def test_normal
|
|
107
|
+
assert_equal Time.local(2006, 1, 2, 0, 0, 0), Chronic.construct(2006, 1, 2, 0, 0, 0)
|
|
108
|
+
assert_equal Time.local(2006, 1, 2, 3, 0, 0), Chronic.construct(2006, 1, 2, 3, 0, 0)
|
|
109
|
+
assert_equal Time.local(2006, 1, 2, 3, 4, 0), Chronic.construct(2006, 1, 2, 3, 4, 0)
|
|
110
|
+
assert_equal Time.local(2006, 1, 2, 3, 4, 5), Chronic.construct(2006, 1, 2, 3, 4, 5)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def test_second_overflow
|
|
114
|
+
assert_equal Time.local(2006, 1, 1, 0, 1, 30), Chronic.construct(2006, 1, 1, 0, 0, 90)
|
|
115
|
+
assert_equal Time.local(2006, 1, 1, 0, 5, 0), Chronic.construct(2006, 1, 1, 0, 0, 300)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def test_minute_overflow
|
|
119
|
+
assert_equal Time.local(2006, 1, 1, 1, 30), Chronic.construct(2006, 1, 1, 0, 90)
|
|
120
|
+
assert_equal Time.local(2006, 1, 1, 5), Chronic.construct(2006, 1, 1, 0, 300)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def test_hour_overflow
|
|
124
|
+
assert_equal Time.local(2006, 1, 2, 12), Chronic.construct(2006, 1, 1, 36)
|
|
125
|
+
assert_equal Time.local(2006, 1, 7), Chronic.construct(2006, 1, 1, 144)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def test_day_overflow
|
|
129
|
+
assert_equal Time.local(2006, 2, 1), Chronic.construct(2006, 1, 32)
|
|
130
|
+
assert_equal Time.local(2006, 3, 5), Chronic.construct(2006, 2, 33)
|
|
131
|
+
assert_equal Time.local(2004, 3, 4), Chronic.construct(2004, 2, 33)
|
|
132
|
+
assert_equal Time.local(2000, 3, 4), Chronic.construct(2000, 2, 33)
|
|
133
|
+
|
|
134
|
+
assert_nothing_raised do
|
|
135
|
+
Chronic.construct(2006, 1, 56)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
assert_raise(RuntimeError) do
|
|
139
|
+
Chronic.construct(2006, 1, 57)
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def test_month_overflow
|
|
144
|
+
assert_equal Time.local(2006, 1), Chronic.construct(2005, 13)
|
|
145
|
+
assert_equal Time.local(2005, 12), Chronic.construct(2000, 72)
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
end
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
require 'helper'
|
|
2
|
+
|
|
3
|
+
class TestDaylightSavings < Test::Unit::TestCase
|
|
4
|
+
|
|
5
|
+
def setup
|
|
6
|
+
@begin_daylight_savings = Time.local(2008, 3, 9, 5, 0, 0, 0)
|
|
7
|
+
@end_daylight_savings = Time.local(2008, 11, 2, 5, 0, 0, 0)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def test_begin_past
|
|
11
|
+
# ambiguous - resolve to last night
|
|
12
|
+
t = Chronic::RepeaterTime.new('900')
|
|
13
|
+
t.start = @begin_daylight_savings
|
|
14
|
+
assert_equal Time.local(2008, 3, 8, 21), t.next(:past).begin
|
|
15
|
+
|
|
16
|
+
# ambiguous - resolve to this afternoon
|
|
17
|
+
t = Chronic::RepeaterTime.new('900')
|
|
18
|
+
t.start = Time.local(2008, 3, 9, 22, 0, 0, 0)
|
|
19
|
+
assert_equal Time.local(2008, 3, 9, 21), t.next(:past).begin
|
|
20
|
+
|
|
21
|
+
# ambiguous - resolve to this morning
|
|
22
|
+
t = Chronic::RepeaterTime.new('400')
|
|
23
|
+
t.start = @begin_daylight_savings
|
|
24
|
+
assert_equal Time.local(2008, 3, 9, 4), t.next(:past).begin
|
|
25
|
+
|
|
26
|
+
# unambiguous - resolve to today
|
|
27
|
+
t = Chronic::RepeaterTime.new('0400')
|
|
28
|
+
t.start = @begin_daylight_savings
|
|
29
|
+
assert_equal Time.local(2008, 3, 9, 4), t.next(:past).begin
|
|
30
|
+
|
|
31
|
+
# unambiguous - resolve to yesterday
|
|
32
|
+
t = Chronic::RepeaterTime.new('1300')
|
|
33
|
+
t.start = @begin_daylight_savings
|
|
34
|
+
assert_equal Time.local(2008, 3, 8, 13), t.next(:past).begin
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def test_begin_future
|
|
38
|
+
# ambiguous - resolve to this morning
|
|
39
|
+
t = Chronic::RepeaterTime.new('900')
|
|
40
|
+
t.start = @begin_daylight_savings
|
|
41
|
+
assert_equal Time.local(2008, 3, 9, 9), t.next(:future).begin
|
|
42
|
+
|
|
43
|
+
# ambiguous - resolve to this afternoon
|
|
44
|
+
t = Chronic::RepeaterTime.new('900')
|
|
45
|
+
t.start = Time.local(2008, 3, 9, 13, 0, 0, 0)
|
|
46
|
+
assert_equal Time.local(2008, 3, 9, 21), t.next(:future).begin
|
|
47
|
+
|
|
48
|
+
# ambiguous - resolve to tomorrow
|
|
49
|
+
t = Chronic::RepeaterTime.new('900')
|
|
50
|
+
t.start = Time.local(2008, 3, 9, 22, 0, 0, 0)
|
|
51
|
+
assert_equal Time.local(2008, 3, 10, 9), t.next(:future).begin
|
|
52
|
+
|
|
53
|
+
# unambiguous - resolve to today
|
|
54
|
+
t = Chronic::RepeaterTime.new('0900')
|
|
55
|
+
t.start = @begin_daylight_savings
|
|
56
|
+
assert_equal Time.local(2008, 3, 9, 9), t.next(:future).begin
|
|
57
|
+
|
|
58
|
+
# unambiguous - resolve to tomorrow
|
|
59
|
+
t = Chronic::RepeaterTime.new('0400')
|
|
60
|
+
t.start = @begin_daylight_savings
|
|
61
|
+
assert_equal Time.local(2008, 3, 10, 4), t.next(:future).begin
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def test_end_past
|
|
65
|
+
# ambiguous - resolve to last night
|
|
66
|
+
t = Chronic::RepeaterTime.new('900')
|
|
67
|
+
t.start = @end_daylight_savings
|
|
68
|
+
assert_equal Time.local(2008, 11, 1, 21), t.next(:past).begin
|
|
69
|
+
|
|
70
|
+
# ambiguous - resolve to this afternoon
|
|
71
|
+
t = Chronic::RepeaterTime.new('900')
|
|
72
|
+
t.start = Time.local(2008, 11, 2, 22, 0, 0, 0)
|
|
73
|
+
assert_equal Time.local(2008, 11, 2, 21), t.next(:past).begin
|
|
74
|
+
|
|
75
|
+
# ambiguous - resolve to this morning
|
|
76
|
+
t = Chronic::RepeaterTime.new('400')
|
|
77
|
+
t.start = @end_daylight_savings
|
|
78
|
+
assert_equal Time.local(2008, 11, 2, 4), t.next(:past).begin
|
|
79
|
+
|
|
80
|
+
# unambiguous - resolve to today
|
|
81
|
+
t = Chronic::RepeaterTime.new('0400')
|
|
82
|
+
t.start = @end_daylight_savings
|
|
83
|
+
assert_equal Time.local(2008, 11, 2, 4), t.next(:past).begin
|
|
84
|
+
|
|
85
|
+
# unambiguous - resolve to yesterday
|
|
86
|
+
t = Chronic::RepeaterTime.new('1300')
|
|
87
|
+
t.start = @end_daylight_savings
|
|
88
|
+
assert_equal Time.local(2008, 11, 1, 13), t.next(:past).begin
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def test_end_future
|
|
92
|
+
# ambiguous - resolve to this morning
|
|
93
|
+
t = Chronic::RepeaterTime.new('900')
|
|
94
|
+
t.start = @end_daylight_savings
|
|
95
|
+
assert_equal Time.local(2008, 11, 2, 9), t.next(:future).begin
|
|
96
|
+
|
|
97
|
+
# ambiguous - resolve to this afternoon
|
|
98
|
+
t = Chronic::RepeaterTime.new('900')
|
|
99
|
+
t.start = Time.local(2008, 11, 2, 13, 0, 0, 0)
|
|
100
|
+
assert_equal Time.local(2008, 11, 2, 21), t.next(:future).begin
|
|
101
|
+
|
|
102
|
+
# ambiguous - resolve to tomorrow
|
|
103
|
+
t = Chronic::RepeaterTime.new('900')
|
|
104
|
+
t.start = Time.local(2008, 11, 2, 22, 0, 0, 0)
|
|
105
|
+
assert_equal Time.local(2008, 11, 3, 9), t.next(:future).begin
|
|
106
|
+
|
|
107
|
+
# unambiguous - resolve to today
|
|
108
|
+
t = Chronic::RepeaterTime.new('0900')
|
|
109
|
+
t.start = @end_daylight_savings
|
|
110
|
+
assert_equal Time.local(2008, 11, 2, 9), t.next(:future).begin
|
|
111
|
+
|
|
112
|
+
# unambiguous - resolve to tomorrow
|
|
113
|
+
t = Chronic::RepeaterTime.new('0400')
|
|
114
|
+
t.start = @end_daylight_savings
|
|
115
|
+
assert_equal Time.local(2008, 11, 3, 4), t.next(:future).begin
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
end
|
data/test/test_Handler.rb
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
require '
|
|
2
|
-
require 'test/unit'
|
|
1
|
+
require 'helper'
|
|
3
2
|
|
|
4
3
|
class TestHandler < Test::Unit::TestCase
|
|
5
|
-
|
|
4
|
+
|
|
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)
|
|
@@ -10,101 +9,96 @@ class TestHandler < Test::Unit::TestCase
|
|
|
10
9
|
|
|
11
10
|
def test_handler_class_1
|
|
12
11
|
handler = Chronic::Handler.new([:repeater], :handler)
|
|
13
|
-
|
|
12
|
+
|
|
14
13
|
tokens = [Chronic::Token.new('friday')]
|
|
15
14
|
tokens[0].tag(Chronic::RepeaterDayName.new(:friday))
|
|
16
|
-
|
|
15
|
+
|
|
17
16
|
assert handler.match(tokens, Chronic.definitions)
|
|
18
|
-
|
|
17
|
+
|
|
19
18
|
tokens << Chronic::Token.new('afternoon')
|
|
20
19
|
tokens[1].tag(Chronic::RepeaterDayPortion.new(:afternoon))
|
|
21
|
-
|
|
20
|
+
|
|
22
21
|
assert !handler.match(tokens, Chronic.definitions)
|
|
23
22
|
end
|
|
24
|
-
|
|
23
|
+
|
|
25
24
|
def test_handler_class_2
|
|
26
25
|
handler = Chronic::Handler.new([:repeater, :repeater?], :handler)
|
|
27
|
-
|
|
26
|
+
|
|
28
27
|
tokens = [Chronic::Token.new('friday')]
|
|
29
28
|
tokens[0].tag(Chronic::RepeaterDayName.new(:friday))
|
|
30
|
-
|
|
29
|
+
|
|
31
30
|
assert handler.match(tokens, Chronic.definitions)
|
|
32
|
-
|
|
31
|
+
|
|
33
32
|
tokens << Chronic::Token.new('afternoon')
|
|
34
33
|
tokens[1].tag(Chronic::RepeaterDayPortion.new(:afternoon))
|
|
35
|
-
|
|
34
|
+
|
|
36
35
|
assert handler.match(tokens, Chronic.definitions)
|
|
37
|
-
|
|
36
|
+
|
|
38
37
|
tokens << Chronic::Token.new('afternoon')
|
|
39
38
|
tokens[2].tag(Chronic::RepeaterDayPortion.new(:afternoon))
|
|
40
|
-
|
|
39
|
+
|
|
41
40
|
assert !handler.match(tokens, Chronic.definitions)
|
|
42
41
|
end
|
|
43
|
-
|
|
42
|
+
|
|
44
43
|
def test_handler_class_3
|
|
45
44
|
handler = Chronic::Handler.new([:repeater, 'time?'], :handler)
|
|
46
|
-
|
|
45
|
+
|
|
47
46
|
tokens = [Chronic::Token.new('friday')]
|
|
48
47
|
tokens[0].tag(Chronic::RepeaterDayName.new(:friday))
|
|
49
|
-
|
|
48
|
+
|
|
50
49
|
assert handler.match(tokens, Chronic.definitions)
|
|
51
|
-
|
|
50
|
+
|
|
52
51
|
tokens << Chronic::Token.new('afternoon')
|
|
53
52
|
tokens[1].tag(Chronic::RepeaterDayPortion.new(:afternoon))
|
|
54
|
-
|
|
53
|
+
|
|
55
54
|
assert !handler.match(tokens, Chronic.definitions)
|
|
56
55
|
end
|
|
57
|
-
|
|
56
|
+
|
|
58
57
|
def test_handler_class_4
|
|
59
58
|
handler = Chronic::Handler.new([:repeater_month_name, :scalar_day, 'time?'], :handler)
|
|
60
|
-
|
|
59
|
+
|
|
61
60
|
tokens = [Chronic::Token.new('may')]
|
|
62
61
|
tokens[0].tag(Chronic::RepeaterMonthName.new(:may))
|
|
63
|
-
|
|
62
|
+
|
|
64
63
|
assert !handler.match(tokens, Chronic.definitions)
|
|
65
|
-
|
|
64
|
+
|
|
66
65
|
tokens << Chronic::Token.new('27')
|
|
67
66
|
tokens[1].tag(Chronic::ScalarDay.new(27))
|
|
68
|
-
|
|
67
|
+
|
|
69
68
|
assert handler.match(tokens, Chronic.definitions)
|
|
70
69
|
end
|
|
71
|
-
|
|
70
|
+
|
|
72
71
|
def test_handler_class_5
|
|
73
72
|
handler = Chronic::Handler.new([:repeater, 'time?'], :handler)
|
|
74
|
-
|
|
73
|
+
|
|
75
74
|
tokens = [Chronic::Token.new('friday')]
|
|
76
75
|
tokens[0].tag(Chronic::RepeaterDayName.new(:friday))
|
|
77
|
-
|
|
76
|
+
|
|
78
77
|
assert handler.match(tokens, Chronic.definitions)
|
|
79
|
-
|
|
78
|
+
|
|
80
79
|
tokens << Chronic::Token.new('5:00')
|
|
81
80
|
tokens[1].tag(Chronic::RepeaterTime.new('5:00'))
|
|
82
|
-
|
|
81
|
+
|
|
83
82
|
assert handler.match(tokens, Chronic.definitions)
|
|
84
|
-
|
|
83
|
+
|
|
85
84
|
tokens << Chronic::Token.new('pm')
|
|
86
85
|
tokens[2].tag(Chronic::RepeaterDayPortion.new(:pm))
|
|
87
|
-
|
|
86
|
+
|
|
88
87
|
assert handler.match(tokens, Chronic.definitions)
|
|
89
88
|
end
|
|
90
|
-
|
|
89
|
+
|
|
91
90
|
def test_handler_class_6
|
|
92
91
|
handler = Chronic::Handler.new([:scalar, :repeater, :pointer], :handler)
|
|
93
|
-
|
|
92
|
+
|
|
94
93
|
tokens = [Chronic::Token.new('3'),
|
|
95
94
|
Chronic::Token.new('years'),
|
|
96
95
|
Chronic::Token.new('past')]
|
|
97
|
-
|
|
96
|
+
|
|
98
97
|
tokens[0].tag(Chronic::Scalar.new(3))
|
|
99
98
|
tokens[1].tag(Chronic::RepeaterYear.new(:year))
|
|
100
99
|
tokens[2].tag(Chronic::Pointer.new(:past))
|
|
101
|
-
|
|
100
|
+
|
|
102
101
|
assert handler.match(tokens, Chronic.definitions)
|
|
103
102
|
end
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
handler = Chronic::Handler.new([], :handler)
|
|
107
|
-
assert_equal Chronic::RepeaterTime, handler.constantize(:repeater_time)
|
|
108
|
-
end
|
|
109
|
-
|
|
110
|
-
end
|
|
103
|
+
|
|
104
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
require 'helper'
|
|
2
|
+
|
|
3
|
+
class TestMiniDate < Test::Unit::TestCase
|
|
4
|
+
def test_valid_month
|
|
5
|
+
assert_raise(ArgumentError){ Chronic::MiniDate.new(0,12) }
|
|
6
|
+
assert_raise(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,72 @@
|
|
|
1
|
+
require 'helper'
|
|
2
|
+
|
|
3
|
+
class ParseNumbersTest < Test::Unit::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
|
+
'twenty third' => '23rd',
|
|
61
|
+
'first day month two' => '1st day month 2'
|
|
62
|
+
}.each do |key, val|
|
|
63
|
+
# Use pre_normalize here instead of Numerizer directly because
|
|
64
|
+
# pre_normalize deals with parsing 'second' appropriately
|
|
65
|
+
assert_equal val, Chronic.pre_normalize(key)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def test_edges
|
|
70
|
+
assert_equal "27 Oct 2006 7:30am", Chronic::Numerizer.numerize("27 Oct 2006 7:30am")
|
|
71
|
+
end
|
|
72
|
+
end
|
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
require '
|
|
2
|
-
require 'test/unit'
|
|
1
|
+
require 'helper'
|
|
3
2
|
|
|
4
3
|
class TestRepeaterDayName < Test::Unit::TestCase
|
|
5
|
-
|
|
4
|
+
|
|
6
5
|
def setup
|
|
7
6
|
@now = Time.local(2006, 8, 16, 14, 0, 0, 0)
|
|
8
7
|
end
|
|
9
|
-
|
|
8
|
+
|
|
10
9
|
def test_match
|
|
11
10
|
token = Chronic::Token.new('saturday')
|
|
12
11
|
repeater = Chronic::Repeater.scan_for_day_names(token)
|
|
13
12
|
assert_equal Chronic::RepeaterDayName, repeater.class
|
|
14
13
|
assert_equal :saturday, repeater.type
|
|
15
|
-
|
|
14
|
+
|
|
16
15
|
token = Chronic::Token.new('sunday')
|
|
17
16
|
repeater = Chronic::Repeater.scan_for_day_names(token)
|
|
18
17
|
assert_equal Chronic::RepeaterDayName, repeater.class
|
|
@@ -22,31 +21,31 @@ class TestRepeaterDayName < Test::Unit::TestCase
|
|
|
22
21
|
def test_next_future
|
|
23
22
|
mondays = Chronic::RepeaterDayName.new(:monday)
|
|
24
23
|
mondays.start = @now
|
|
25
|
-
|
|
24
|
+
|
|
26
25
|
span = mondays.next(:future)
|
|
27
|
-
|
|
26
|
+
|
|
28
27
|
assert_equal Time.local(2006, 8, 21), span.begin
|
|
29
|
-
assert_equal Time.local(2006, 8, 22), span.end
|
|
28
|
+
assert_equal Time.local(2006, 8, 22), span.end
|
|
30
29
|
|
|
31
30
|
span = mondays.next(:future)
|
|
32
|
-
|
|
31
|
+
|
|
33
32
|
assert_equal Time.local(2006, 8, 28), span.begin
|
|
34
33
|
assert_equal Time.local(2006, 8, 29), span.end
|
|
35
34
|
end
|
|
36
|
-
|
|
35
|
+
|
|
37
36
|
def test_next_past
|
|
38
37
|
mondays = Chronic::RepeaterDayName.new(:monday)
|
|
39
38
|
mondays.start = @now
|
|
40
|
-
|
|
39
|
+
|
|
41
40
|
span = mondays.next(:past)
|
|
42
|
-
|
|
41
|
+
|
|
43
42
|
assert_equal Time.local(2006, 8, 14), span.begin
|
|
44
|
-
assert_equal Time.local(2006, 8, 15), span.end
|
|
43
|
+
assert_equal Time.local(2006, 8, 15), span.end
|
|
45
44
|
|
|
46
45
|
span = mondays.next(:past)
|
|
47
|
-
|
|
46
|
+
|
|
48
47
|
assert_equal Time.local(2006, 8, 7), span.begin
|
|
49
48
|
assert_equal Time.local(2006, 8, 8), span.end
|
|
50
49
|
end
|
|
51
|
-
|
|
52
|
-
end
|
|
50
|
+
|
|
51
|
+
end
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
require '
|
|
2
|
-
require 'test/unit'
|
|
1
|
+
require 'helper'
|
|
3
2
|
|
|
4
3
|
class TestRepeaterFortnight < Test::Unit::TestCase
|
|
5
|
-
|
|
4
|
+
|
|
6
5
|
def setup
|
|
7
6
|
@now = Time.local(2006, 8, 16, 14, 0, 0, 0)
|
|
8
7
|
end
|
|
@@ -10,54 +9,54 @@ class TestRepeaterFortnight < Test::Unit::TestCase
|
|
|
10
9
|
def test_next_future
|
|
11
10
|
fortnights = Chronic::RepeaterFortnight.new(:fortnight)
|
|
12
11
|
fortnights.start = @now
|
|
13
|
-
|
|
12
|
+
|
|
14
13
|
next_fortnight = fortnights.next(:future)
|
|
15
14
|
assert_equal Time.local(2006, 8, 20), next_fortnight.begin
|
|
16
15
|
assert_equal Time.local(2006, 9, 3), next_fortnight.end
|
|
17
|
-
|
|
16
|
+
|
|
18
17
|
next_next_fortnight = fortnights.next(:future)
|
|
19
18
|
assert_equal Time.local(2006, 9, 3), next_next_fortnight.begin
|
|
20
19
|
assert_equal Time.local(2006, 9, 17), next_next_fortnight.end
|
|
21
20
|
end
|
|
22
|
-
|
|
21
|
+
|
|
23
22
|
def test_next_past
|
|
24
23
|
fortnights = Chronic::RepeaterFortnight.new(:fortnight)
|
|
25
24
|
fortnights.start = @now
|
|
26
|
-
|
|
25
|
+
|
|
27
26
|
last_fortnight = fortnights.next(:past)
|
|
28
27
|
assert_equal Time.local(2006, 7, 30), last_fortnight.begin
|
|
29
28
|
assert_equal Time.local(2006, 8, 13), last_fortnight.end
|
|
30
|
-
|
|
29
|
+
|
|
31
30
|
last_last_fortnight = fortnights.next(:past)
|
|
32
31
|
assert_equal Time.local(2006, 7, 16), last_last_fortnight.begin
|
|
33
32
|
assert_equal Time.local(2006, 7, 30), last_last_fortnight.end
|
|
34
33
|
end
|
|
35
|
-
|
|
34
|
+
|
|
36
35
|
def test_this_future
|
|
37
36
|
fortnights = Chronic::RepeaterFortnight.new(:fortnight)
|
|
38
37
|
fortnights.start = @now
|
|
39
|
-
|
|
38
|
+
|
|
40
39
|
this_fortnight = fortnights.this(:future)
|
|
41
40
|
assert_equal Time.local(2006, 8, 16, 15), this_fortnight.begin
|
|
42
41
|
assert_equal Time.local(2006, 8, 27), this_fortnight.end
|
|
43
42
|
end
|
|
44
|
-
|
|
43
|
+
|
|
45
44
|
def test_this_past
|
|
46
45
|
fortnights = Chronic::RepeaterFortnight.new(:fortnight)
|
|
47
46
|
fortnights.start = @now
|
|
48
|
-
|
|
47
|
+
|
|
49
48
|
this_fortnight = fortnights.this(:past)
|
|
50
49
|
assert_equal Time.local(2006, 8, 13, 0), this_fortnight.begin
|
|
51
50
|
assert_equal Time.local(2006, 8, 16, 14), this_fortnight.end
|
|
52
51
|
end
|
|
53
|
-
|
|
52
|
+
|
|
54
53
|
def test_offset
|
|
55
54
|
span = Chronic::Span.new(@now, @now + 1)
|
|
56
|
-
|
|
55
|
+
|
|
57
56
|
offset_span = Chronic::RepeaterWeek.new(:week).offset(span, 3, :future)
|
|
58
|
-
|
|
57
|
+
|
|
59
58
|
assert_equal Time.local(2006, 9, 6, 14), offset_span.begin
|
|
60
59
|
assert_equal Time.local(2006, 9, 6, 14, 0, 1), offset_span.end
|
|
61
60
|
end
|
|
62
|
-
|
|
63
|
-
end
|
|
61
|
+
|
|
62
|
+
end
|