texp 0.0.3

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.
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'date'
5
+ require 'texp'
6
+
7
+ ######################################################################
8
+ # TODO: This test is incomplete.
9
+ #
10
+ class ToHashTest < Test::Unit::TestCase
11
+ def test_to_hash
12
+ assert_hash '2008-02-14,2i', 'type' => 'i', 'i1' => '2008-02-14', 'i2' => '2'
13
+ assert_hash '[0,2]w', 'type' => 'w', 'w1' => ['0', '2']
14
+ assert_hash '[1,15]d', 'type' => 'd', 'd1' => ['1', '15']
15
+ assert_hash '[1,2,-1]k', 'type' => 'k', 'k1' => ['1', '2', '-1']
16
+ assert_hash '[1,3]m', 'type' => 'm', 'm1' => ['1', '3']
17
+ assert_hash '[2008,2010]y', 'type' => 'y', 'y1' => ['2008', '2010']
18
+ assert_hash 'e', 'type' => 'e'
19
+ end
20
+
21
+ def assert_hash(texp, hash)
22
+ assert_equal hash, TExp.parse(texp).to_hash, "should match for '#{texp}'"
23
+ # assert_equal texp, TExp.from_params('1' => hash).to_s
24
+ end
25
+ end
26
+
@@ -0,0 +1,65 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'date'
5
+ require 'texp'
6
+ require 'flexmock/test_unit'
7
+
8
+ ######################################################################
9
+ class InspectTest < Test::Unit::TestCase
10
+ def test_inspect
11
+ assert_inspect "e", "every day"
12
+
13
+ assert_inspect "0w", "the day of the week is Sunday"
14
+ assert_inspect "[0,1]w", "the day of the week is Sunday or Monday"
15
+ assert_inspect "[0,1,4,2,3,6,5]w",
16
+ "the day of the week is Sunday, Monday, Tuesday, Wednesday, Thursday, Friday or Saturday"
17
+
18
+ assert_inspect "1d", "the day of the month is the 1st"
19
+ assert_inspect "2d", "the day of the month is the 2nd"
20
+ assert_inspect "3d", "the day of the month is the 3rd"
21
+ assert_inspect "4d", "the day of the month is the 4th"
22
+ assert_inspect "5d", "the day of the month is the 5th"
23
+ assert_inspect "10d", "the day of the month is the 10th"
24
+ assert_inspect "11d", "the day of the month is the 11th"
25
+ assert_inspect "12d", "the day of the month is the 12th"
26
+ assert_inspect "13d", "the day of the month is the 13th"
27
+ assert_inspect "14d", "the day of the month is the 14th"
28
+ assert_inspect "15d", "the day of the month is the 15th"
29
+ assert_inspect "16d", "the day of the month is the 16th"
30
+ assert_inspect "17d", "the day of the month is the 17th"
31
+ assert_inspect "18d", "the day of the month is the 18th"
32
+ assert_inspect "19d", "the day of the month is the 19th"
33
+ assert_inspect "20d", "the day of the month is the 20th"
34
+ assert_inspect "[1,10,15]d", "the day of the month is the 1st, 10th or 15th"
35
+
36
+ assert_inspect "2008-02-15,3i", "every 3rd day starting on February 15, 2008"
37
+ assert_inspect "2008-02-15,1i", "every day starting on February 15, 2008"
38
+
39
+ assert_inspect "1k", "it is the 1st week of the month"
40
+ assert_inspect "[1,3]k", "it is the 1st or 3rd week of the month"
41
+ assert_inspect "[1,2,-1]k", "it is the 1st, 2nd or last week of the month"
42
+ assert_inspect "[-5,-4,-3,-2,-1]k",
43
+ "it is the 5th from the last, 4th from the last, 3rd from the last, next to the last or last week of the month"
44
+
45
+ assert_inspect "2008y", "the year is 2008"
46
+
47
+ assert_inspect "1m", "the month is January"
48
+ assert_inspect "[1,2,4]m", "the month is January, February or April"
49
+
50
+ assert_inspect "[1d1m]a",
51
+ "the day of the month is the 1st and the month is January"
52
+ assert_inspect "[1d1m]o",
53
+ "the day of the month is the 1st or the month is January"
54
+
55
+ assert_inspect "[1d1m]on",
56
+ "it is not the case that the day of the month is the 1st or the month is January"
57
+
58
+ assert_inspect "3w2,1s",
59
+ "the day of the week is Wednesday, or up to 2 days prior, or up to 1 day after"
60
+ end
61
+
62
+ def assert_inspect(texp, string)
63
+ assert_equal string, TExp.parse(texp).inspect
64
+ end
65
+ end
@@ -0,0 +1,51 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'date'
5
+ require 'texp'
6
+
7
+ class LogicTest < Test::Unit::TestCase
8
+
9
+ DATE = Date.parse("Feb 14, 2008")
10
+ LEFT = TExp::DayInterval.new(DATE, 2)
11
+ RIGHT = TExp::DayInterval.new(DATE, 3)
12
+ TT = DATE
13
+ TF = DATE+2
14
+ FT = DATE+3
15
+ FF = DATE+1
16
+
17
+ def test_constants
18
+ assert LEFT.include?(TT)
19
+ assert LEFT.include?(TF)
20
+ assert ! LEFT.include?(FT)
21
+ assert ! LEFT.include?(FF)
22
+ assert RIGHT.include?(TT)
23
+ assert RIGHT.include?(FT)
24
+ assert ! RIGHT.include?(TF)
25
+ assert ! RIGHT.include?(FF)
26
+ end
27
+
28
+ def test_and
29
+ te = TExp::And.new(LEFT, RIGHT, LEFT, RIGHT)
30
+ assert te.include?(TT)
31
+ assert ! te.include?(FT)
32
+ assert ! te.include?(TF)
33
+ assert ! te.include?(FF)
34
+ end
35
+
36
+ def test_or
37
+ te = TExp::Or.new(LEFT, RIGHT, LEFT, RIGHT)
38
+ assert te.include?(TT)
39
+ assert te.include?(FT)
40
+ assert te.include?(TF)
41
+ assert ! te.include?(FF)
42
+ end
43
+
44
+ def test_not
45
+ te = TExp::Not.new(LEFT)
46
+ assert ! te.include?(TT)
47
+ assert te.include?(FF)
48
+ end
49
+
50
+ end
51
+
File without changes
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'date'
5
+ require 'texp'
6
+
7
+ class MonthTest < Test::Unit::TestCase
8
+
9
+ def setup
10
+ @date = Date.parse("Feb 14, 2008")
11
+ end
12
+
13
+ def test_initial_conditions
14
+ te = TExp::Month.new(2)
15
+ assert te.include?(@date)
16
+ assert te.include?(@date + 1)
17
+ assert ! te.include?(@date + 30)
18
+ end
19
+ end
20
+
@@ -0,0 +1,233 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'date'
5
+ require 'texp'
6
+ require 'flexmock/test_unit'
7
+
8
+ ######################################################################
9
+ class TExpParseLexicalTest < Test::Unit::TestCase
10
+ def setup
11
+ @tokens = []
12
+ flexmock(TExp).should_receive(:compile).with(any).
13
+ and_return { |tok| @tokens << tok }
14
+ end
15
+
16
+ def test_letters
17
+ assert_lex "abc", 'a', 'b', 'c'
18
+ end
19
+
20
+ def test_numbers
21
+ assert_lex "1,23,4", '1', '23', '4'
22
+ end
23
+
24
+ def test_positive_numbers
25
+ assert_lex "+1,23,+4", '+1', '23', '+4'
26
+ end
27
+
28
+ def test_negative_numbers
29
+ assert_lex "-1,23,-4", '-1', '23', '-4'
30
+ end
31
+
32
+ def test_punctuation
33
+ assert_lex "[@]()|&", '[', '@', ']', '(', ')', '|', '&'
34
+ end
35
+
36
+ def test_dates
37
+ assert_lex "2008-02-14", "2008-02-14"
38
+ end
39
+
40
+ def test_bad_dates
41
+ assert_lex "2008-2-14", "2008", "-2", "-14"
42
+ end
43
+
44
+ def test_extension_tokens
45
+ assert_lex "<hi>", "<hi>"
46
+ end
47
+
48
+ EXPECTED = [
49
+ '[', '[', '2008', 'y', '2', 'm', '14', 'd', ']', 'a', '12', 'm',
50
+ '[', '1', '15', ']', 'd', '2008-02-14', '3', 'i', ']', 'o'
51
+ ]
52
+
53
+ def test_mixed
54
+ assert_lex "[[2008y2m14d]a12m[1,15]d2008-02-14,3i]o", *EXPECTED
55
+ end
56
+
57
+ def test_mixed_with_spaces
58
+ assert_lex "[[2008y 2m 14d]a 12m [1,15]d 2008-02-14,3i ]o", *EXPECTED
59
+ end
60
+
61
+ private
62
+
63
+ def assert_lex(string, *tokens)
64
+ TExp.parse(string)
65
+ assert_equal tokens, @tokens
66
+ end
67
+ end
68
+
69
+
70
+ ######################################################################
71
+ class ParseTest < Test::Unit::TestCase
72
+ def setup
73
+ @date = Date.parse("Feb 14, 2008")
74
+ end
75
+
76
+ def test_bad_parse_string
77
+ assert_raise TExp::ParseError do
78
+ TExp.parse("(1,2)d")
79
+ end
80
+ end
81
+
82
+ def test_unbalanced_list
83
+ assert_raise TExp::ParseError do
84
+ TExp.parse("[1")
85
+ end
86
+ end
87
+
88
+ def test_unbalanced_list2
89
+ assert_raise TExp::ParseError do
90
+ TExp.parse("1]")
91
+ end
92
+ end
93
+
94
+ def test_parse_date
95
+ assert_equal Date.parse("Feb 14, 2008"), TExp.parse("2008-02-14")
96
+ end
97
+
98
+ def test_parse_interval
99
+ te = TExp.parse("2008-02-14,2i")
100
+ assert te.include?(@date)
101
+ assert ! te.include?(@date+1)
102
+ assert te.include?(@date+2)
103
+ end
104
+
105
+ def test_parse_day_of_month
106
+ te = TExp.parse("14d")
107
+ te.include?(@date)
108
+ end
109
+
110
+ def test_parse_list
111
+ te = TExp.parse("[1,2,3]")
112
+ assert_equal [1,2,3], te
113
+ end
114
+
115
+ def test_parse_day_of_month_with_multiple_args
116
+ te = TExp.parse("[13,14]d")
117
+ assert te.include?(@date)
118
+ assert te.include?(@date-1)
119
+ assert ! te.include?(@date+2)
120
+ end
121
+
122
+ def test_parse_day_of_week_with_single_arg
123
+ te = TExp.parse("4w")
124
+ assert te.include?(@date)
125
+ assert ! te.include?(@date+2)
126
+ end
127
+
128
+ def test_parse_day_of_week_with_multiple_args
129
+ te = TExp.parse("[3,4]w")
130
+ assert te.include?(@date)
131
+ assert te.include?(@date-1)
132
+ assert ! te.include?(@date+2)
133
+ end
134
+
135
+ def test_parse_month_with_single_arg
136
+ te = TExp.parse("2m")
137
+ assert te.include?(@date)
138
+ assert ! te.include?(@date + 30)
139
+ end
140
+
141
+ def test_parse_month_with_multiple_args
142
+ te = TExp.parse("[2,3]m")
143
+ assert te.include?(@date)
144
+ assert te.include?(@date + 30)
145
+ assert ! te.include?(@date + 60)
146
+ end
147
+
148
+ def test_parse_year_with_single_arg
149
+ te = TExp.parse("2008y")
150
+ assert te.include?(@date)
151
+ assert ! te.include?(@date + 365)
152
+ end
153
+
154
+ def test_parse_year_with_multiple_args
155
+ te = TExp.parse("[2007,2008]y")
156
+ assert te.include?(@date)
157
+ assert te.include?(@date - 365)
158
+ assert ! te.include?(@date + 365)
159
+ end
160
+
161
+ def test_parse_window
162
+ te = TExp.parse("3w2,1s")
163
+ assert ! te.include?(Date.parse("Mar 2, 2008"))
164
+ assert te.include?(Date.parse("Mar 3, 2008"))
165
+ assert te.include?(Date.parse("Mar 4, 2008"))
166
+ assert te.include?(Date.parse("Mar 5, 2008"))
167
+ assert te.include?(Date.parse("Mar 6, 2008"))
168
+ assert ! te.include?(Date.parse("Mar 7, 2008"))
169
+ end
170
+
171
+ def test_parse_not
172
+ te = TExp.parse("14dn")
173
+ assert ! te.include?(@date)
174
+ assert te.include?(@date + 1)
175
+ end
176
+
177
+ def test_parse_and
178
+ TExp.parse("14d").include?(@date)
179
+ TExp.parse("2m").include?(@date)
180
+ te = TExp.parse("[14d 2m] a")
181
+ assert te.include?(@date)
182
+ assert ! te.include?(@date + 1)
183
+ assert ! te.include?(@date + 365)
184
+ end
185
+
186
+ def test_parse_or
187
+ te = TExp.parse("[14d 15d] o")
188
+ assert te.include?(@date)
189
+ assert te.include?(@date + 1)
190
+ assert ! te.include?(@date + 2)
191
+ end
192
+
193
+ def test_parse_every_day
194
+ te = TExp.parse('e')
195
+ assert te.include?(Date.today)
196
+ end
197
+ end
198
+
199
+ ######################################################################
200
+ class ParseReverseTest < Test::Unit::TestCase
201
+ def setup
202
+ @date = Date.parse("Feb 14, 2008")
203
+ end
204
+
205
+ def test_round_trip
206
+ assert_round_trip("2008-02-14,2i")
207
+ assert_round_trip("14d")
208
+ assert_round_trip("[13,14]d")
209
+ assert_round_trip("[3,4]w")
210
+ assert_round_trip("2m")
211
+ assert_round_trip("[2,3]m")
212
+ assert_round_trip("2008y")
213
+ assert_round_trip("[2007,2008]y")
214
+ assert_round_trip("14dn")
215
+ assert_round_trip("[14d2m]a")
216
+ assert_round_trip("[14d15d]o")
217
+ assert_round_trip("14do")
218
+ assert_round_trip("[]o")
219
+ assert_round_trip("e")
220
+ assert_round_trip("1k")
221
+ assert_round_trip("[1,2]k")
222
+ assert_round_trip("[1,2,-1]k")
223
+ assert_round_trip("3w2,1s")
224
+ end
225
+
226
+ private
227
+
228
+ def assert_round_trip(string)
229
+ te = TExp.parse(string)
230
+ assert_equal string, te.to_s
231
+ end
232
+ end
233
+
@@ -0,0 +1,58 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'date'
4
+ require 'test/unit'
5
+ require 'texp'
6
+
7
+ class WeekTest < Test::Unit::TestCase
8
+
9
+ def test_week_include_with_one_week
10
+ te = TExp::Week.new([1])
11
+ assert te.include?(Date.parse("Feb 1, 2008"))
12
+ assert te.include?(Date.parse("Feb 2, 2008"))
13
+ assert te.include?(Date.parse("Feb 3, 2008"))
14
+ assert te.include?(Date.parse("Feb 5, 2008"))
15
+ assert te.include?(Date.parse("Feb 6, 2008"))
16
+ assert te.include?(Date.parse("Feb 7, 2008"))
17
+ assert ! te.include?(Date.parse("Feb 8, 2008"))
18
+ end
19
+
20
+ def test_week_include_with_several_weeks
21
+ te = TExp::Week.new([2,4])
22
+ assert ! te.include?(Date.parse("Feb 1, 2008"))
23
+ assert ! te.include?(Date.parse("Feb 7, 2008"))
24
+ assert te.include?(Date.parse("Feb 8, 2008"))
25
+ assert te.include?(Date.parse("Feb 14, 2008"))
26
+ assert ! te.include?(Date.parse("Feb 15, 2008"))
27
+ assert ! te.include?(Date.parse("Feb 21, 2008"))
28
+ assert te.include?(Date.parse("Feb 22, 2008"))
29
+ assert te.include?(Date.parse("Feb 28, 2008"))
30
+ end
31
+
32
+ def test_week_with_last_week_of_month
33
+ te = TExp::Week.new([-1])
34
+ assert ! te.include?(Date.parse("Feb 1, 2008"))
35
+ assert ! te.include?(Date.parse("Feb 22, 2008"))
36
+ assert te.include?(Date.parse("Feb 23, 2008"))
37
+ assert te.include?(Date.parse("Feb 29, 2008"))
38
+ end
39
+
40
+ # Test some private methods
41
+
42
+ def test_week_from_back
43
+ te = TExp::Week.new([1])
44
+ assert_equal -1, te.send(:week_from_back, Date.parse("Mar 31, 2008"))
45
+ assert_equal -1, te.send(:week_from_back, Date.parse("Mar 25, 2008"))
46
+ assert_equal -2, te.send(:week_from_back, Date.parse("Mar 24, 2008"))
47
+ end
48
+
49
+ def test_last_day_of_month
50
+ te = TExp::Week.new([1])
51
+ assert_equal 31, te.send(:last_day_of_month, Date.parse("Jan 21, 2008"))
52
+ assert_equal 29, te.send(:last_day_of_month, Date.parse("Feb 21, 2008"))
53
+ assert_equal 28, te.send(:last_day_of_month, Date.parse("Feb 21, 2007"))
54
+ assert_equal 30, te.send(:last_day_of_month, Date.parse("Nov 1, 2007"))
55
+ assert_equal 31, te.send(:last_day_of_month, Date.parse("Dec 1, 2007"))
56
+ end
57
+ end
58
+