tempr 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.3 / 2012-02-19
4
+
5
+ - convert date to time ranges for date <-> time comparisons in `within?/subsume?/intersection_with`
6
+ - fix behavior with exclusive ranges
7
+ - add test cases, DRY up tests
8
+ - add Rakefile, add rake to gemspec
9
+
3
10
  ## 0.1.2 / 2012-02-19
4
11
 
5
12
  - add `within?`, `subsume?`, `intersection_with`, `intersects?`
@@ -0,0 +1,8 @@
1
+ require 'rake'
2
+
3
+ task :default => :test
4
+
5
+ desc 'Run test suite'
6
+ task :test do
7
+ require File.expand_path('test/suite',File.dirname(__FILE__))
8
+ end
@@ -492,13 +492,14 @@ module Tempr
492
492
  # the range intersection of self and other
493
493
  # note: returns nil if no intersection
494
494
  def intersection_with(other)
495
- max_begin = [self.begin,other.begin].max
496
- min_end = [self.end,other.end].min
497
- excl = ( self.end == min_end &&
498
- self.respond_to?(:exclude_end?) && self.exclude_end?
495
+ r1,r2 = matched_range_types(self, other)
496
+ max_begin = [r1.begin,r2.begin].max
497
+ min_end = [r1.end, r2.end ].min
498
+ excl = ( r1.end == min_end &&
499
+ r1.respond_to?(:exclude_end?) && r1.exclude_end?
499
500
  ) ||
500
- ( other.end == min_end &&
501
- other.respond_to?(:exclude_end?) && other.exclude_end?
501
+ ( r2.end == min_end &&
502
+ r2.respond_to?(:exclude_end?) && r2.exclude_end?
502
503
  )
503
504
  unless max_begin > min_end
504
505
  Range.new(max_begin, min_end, excl).extend(DateTimeRange)
@@ -519,7 +520,17 @@ module Tempr
519
520
 
520
521
  private
521
522
 
522
- def range_within_other?(r1,r2)
523
+ def matched_range_types(rng,oth)
524
+ if ( rng.begin.respond_to?(:sec) && rng.end.respond_to?(:sec) ) ||
525
+ ( oth.begin.respond_to?(:sec) && oth.end.respond_to?(:sec) )
526
+ [ time_range(rng), time_range(oth) ]
527
+ else
528
+ [ day_range(rng), day_range(oth) ]
529
+ end
530
+ end
531
+
532
+ def range_within_other?(rng,oth)
533
+ r1, r2 = matched_range_types(rng,oth)
523
534
  if r2.respond_to?(:exclude_end?) && r2.exclude_end? &&
524
535
  !(r1.respond_to?(:exclude_end?) && r1.exclude_end?)
525
536
  r1.begin >= r2.begin && r1.end < r2.end
@@ -3,7 +3,7 @@ module Tempr
3
3
  module Version
4
4
  MAJOR = 0
5
5
  MINOR = 1
6
- TINY = 2
6
+ TINY = 3
7
7
  STRING = "#{MAJOR}.#{MINOR}.#{TINY}"
8
8
  end
9
9
  end
@@ -4,42 +4,49 @@ module RangeIntersectionTests
4
4
 
5
5
  Fixtures = [
6
6
  {
7
- :case => "inclusive ranges, other equals self",
7
+ :case => "inclusive date ranges, other equals self",
8
8
  :subject => (Date.civil(2012,2,13)..
9
9
  Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
10
10
  :other => Date.civil(2012,2,13)..Date.civil(2012,2,17),
11
- :expected => Date.civil(2012,2,13)..Date.civil(2012,2,17)
11
+ :expected => Date.civil(2012,2,13)...Date.civil(2012,2,18)
12
12
  },
13
13
  {
14
- :case => "inclusive ranges, other within self",
14
+ :case => "inclusive date ranges, other within self",
15
15
  :subject => (Date.civil(2012,2,13)..
16
16
  Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
17
17
  :other => Date.civil(2012,2,13)..Date.civil(2012,2,16),
18
- :expected => Date.civil(2012,2,13)..Date.civil(2012,2,16)
18
+ :expected => Date.civil(2012,2,13)...Date.civil(2012,2,17)
19
19
  },
20
20
  {
21
- :case => "inclusive ranges, self within other",
21
+ :case => "inclusive date ranges, self within other",
22
22
  :subject => (Date.civil(2012,2,13)..
23
23
  Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
24
24
  :other => Date.civil(2012,2,13)..Date.civil(2012,2,18),
25
- :expected => Date.civil(2012,2,13)..Date.civil(2012,2,17)
25
+ :expected => Date.civil(2012,2,13)...Date.civil(2012,2,18)
26
26
  },
27
27
  {
28
- :case => "inclusive ranges, self intersects but not within other",
28
+ :case => "inclusive date ranges, self intersects but not within other",
29
29
  :subject => (Date.civil(2012,2,13)..
30
30
  Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
31
31
  :other => Date.civil(2012,2,14)..Date.civil(2012,2,18),
32
- :expected => Date.civil(2012,2,14)..Date.civil(2012,2,17)
32
+ :expected => Date.civil(2012,2,14)...Date.civil(2012,2,18)
33
33
  },
34
34
  {
35
- :case => "inclusive ranges, self does not intersect other",
35
+ :case => "inclusive date ranges, self adjacent to other",
36
36
  :subject => (Date.civil(2012,2,13)..
37
37
  Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
38
38
  :other => Date.civil(2012,2,18)..Date.civil(2012,2,19),
39
+ :expected => Date.civil(2012,2,18)...Date.civil(2012,2,18)
40
+ },
41
+ {
42
+ :case => "inclusive date ranges, self does not intersect other",
43
+ :subject => (Date.civil(2012,2,13)..
44
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
45
+ :other => Date.civil(2012,2,19)..Date.civil(2012,2,21),
39
46
  :expected => nil
40
47
  },
41
48
  {
42
- :case => "exclusive ranges, same endpoint, both exclusive",
49
+ :case => "exclusive date ranges, same endpoint, both exclusive",
43
50
  :subject => (Date.civil(2012,2,13)...
44
51
  Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
45
52
  :other => Date.civil(2012,2,13)...Date.civil(2012,2,17),
@@ -47,7 +54,7 @@ module RangeIntersectionTests
47
54
  :exclusive => true
48
55
  },
49
56
  {
50
- :case => "exclusive ranges, same endpoint, other is non-exclusive, self is exclusive",
57
+ :case => "exclusive date ranges, same endpoint, other is non-exclusive, self is exclusive",
51
58
  :subject => (Date.civil(2012,2,13)...
52
59
  Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
53
60
  :other => Date.civil(2012,2,13)..Date.civil(2012,2,17),
@@ -55,7 +62,7 @@ module RangeIntersectionTests
55
62
  :exclusive => true
56
63
  },
57
64
  {
58
- :case => "exclusive ranges, same endpoint, other is exclusive, self is non-exclusive",
65
+ :case => "exclusive date ranges, same endpoint, other is exclusive, self is non-exclusive",
59
66
  :subject => (Date.civil(2012,2,13)..
60
67
  Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
61
68
  :other => Date.civil(2012,2,13)...Date.civil(2012,2,17),
@@ -63,7 +70,7 @@ module RangeIntersectionTests
63
70
  :exclusive => true
64
71
  },
65
72
  {
66
- :case => "exclusive ranges, different endpoint, both exclusive",
73
+ :case => "exclusive date ranges, different endpoint, both exclusive",
67
74
  :subject => (Date.civil(2012,2,13)...
68
75
  Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
69
76
  :other => Date.civil(2012,2,14)...Date.civil(2012,2,18),
@@ -71,21 +78,215 @@ module RangeIntersectionTests
71
78
  :exclusive => true
72
79
  },
73
80
  {
74
- :case => "exclusive ranges, different endpoint, other is non-exclusive, self is exclusive",
81
+ :case => "exclusive date ranges, different endpoint, other is non-exclusive, self is exclusive",
75
82
  :subject => (Date.civil(2012,2,13)...
76
83
  Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
77
84
  :other => Date.civil(2012,2,12)..Date.civil(2012,2,16),
78
- :expected => Date.civil(2012,2,13)..Date.civil(2012,2,16),
85
+ :expected => Date.civil(2012,2,13)...Date.civil(2012,2,17),
79
86
  :exclusive => true
80
87
  },
81
88
  {
82
- :case => "exclusive ranges, different endpoint, other is exclusive, self is non-exclusive",
89
+ :case => "exclusive date ranges, different endpoint, other is exclusive, self is non-exclusive",
83
90
  :subject => (Date.civil(2012,2,13)..
84
91
  Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
85
92
  :other => Date.civil(2012,2,11)...Date.civil(2012,2,15),
86
93
  :expected => Date.civil(2012,2,13)...Date.civil(2012,2,15),
87
94
  :exclusive => true
88
- }
95
+ },
96
+
97
+ #---------- time range cases
98
+
99
+ {
100
+ :case => "inclusive time ranges, other equals self",
101
+ :subject => (Time.new(2012,2,13,12,11,10)..
102
+ Time.new(2012,2,17,16,15,14)).extend(Tempr::DateTimeRange),
103
+ :other => (Time.new(2012,2,13,12,11,10)..
104
+ Time.new(2012,2,17,16,15,14)),
105
+ :expected => (Time.new(2012,2,13,12,11,10)..
106
+ Time.new(2012,2,17,16,15,14))
107
+ },
108
+ {
109
+ :case => "inclusive time ranges, other within self",
110
+ :subject => (Time.new(2012,2,13,12,11,10)..
111
+ Time.new(2012,2,17,16,15,14)).extend(Tempr::DateTimeRange),
112
+ :other => (Time.new(2012,2,13,12,11,10)..
113
+ Time.new(2012,2,17,16,15,13)),
114
+ :expected => (Time.new(2012,2,13,12,11,10)..
115
+ Time.new(2012,2,17,16,15,13))
116
+ },
117
+ {
118
+ :case => "inclusive time ranges, self within other",
119
+ :subject => (Time.new(2012,2,13,12,11,10)..
120
+ Time.new(2012,2,17,16,15,14)).extend(Tempr::DateTimeRange),
121
+ :other => (Time.new(2012,2,13,12,11,10)..
122
+ Time.new(2012,2,17,16,15,15)),
123
+ :expected => (Time.new(2012,2,13,12,11,10)..
124
+ Time.new(2012,2,17,16,15,14))
125
+ },
126
+ {
127
+ :case => "inclusive time ranges, self intersects but not within other",
128
+ :subject => (Time.new(2012,2,13,12,11,10)..
129
+ Time.new(2012,2,17,16,15,14)).extend(Tempr::DateTimeRange),
130
+ :other => (Time.new(2012,2,13,12,11,11)..
131
+ Time.new(2012,2,17,16,15,15)),
132
+ :expected => (Time.new(2012,2,13,12,11,11)..
133
+ Time.new(2012,2,17,16,15,14))
134
+ },
135
+ {
136
+ :case => "inclusive time ranges, self adjacent to other",
137
+ :subject => (Time.new(2012,2,13,12,11,10)..
138
+ Time.new(2012,2,17,16,15,14)).extend(Tempr::DateTimeRange),
139
+ :other => (Time.new(2012,2,17,16,15,15)..
140
+ Time.new(2012,2,17,16,15,16)),
141
+ :expected => nil
142
+ },
143
+ {
144
+ :case => "inclusive time ranges, self does not intersect other",
145
+ :subject => (Time.new(2012,2,13,12,11,10)..
146
+ Time.new(2012,2,17,16,15,14)).extend(Tempr::DateTimeRange),
147
+ :other => (Time.new(2012,2,17,16,15,15)..
148
+ Time.new(2012,2,17,16,16,0)),
149
+ :expected => nil
150
+ },
151
+ {
152
+ :case => "exclusive time ranges, same endpoint, both exclusive",
153
+ :subject => (Time.new(2012,2,13,12,11,10)...
154
+ Time.new(2012,2,17,16,15,14)).extend(Tempr::DateTimeRange),
155
+ :other => (Time.new(2012,2,13,12,11,10)...
156
+ Time.new(2012,2,17,16,15,14)),
157
+ :expected => (Time.new(2012,2,13,12,11,10)...
158
+ Time.new(2012,2,17,16,15,14)),
159
+ :exclusive => true
160
+ },
161
+ {
162
+ :case => "exclusive time ranges, same endpoint, other is non-exclusive, self is exclusive",
163
+ :subject => (Time.new(2012,2,13,12,11,10)...
164
+ Time.new(2012,2,17,16,15,14)).extend(Tempr::DateTimeRange),
165
+ :other => (Time.new(2012,2,13,12,11,10)..
166
+ Time.new(2012,2,17,16,15,14)),
167
+ :expected => (Time.new(2012,2,13,12,11,10)...
168
+ Time.new(2012,2,17,16,15,14)),
169
+ :exclusive => true
170
+ },
171
+ {
172
+ :case => "exclusive time ranges, same endpoint, other is exclusive, self is non-exclusive",
173
+ :subject => (Time.new(2012,2,13,12,11,10)..
174
+ Time.new(2012,2,17,16,15,14)).extend(Tempr::DateTimeRange),
175
+ :other => (Time.new(2012,2,13,12,11,10)...
176
+ Time.new(2012,2,17,16,15,14)),
177
+ :expected => (Time.new(2012,2,13,12,11,10)...
178
+ Time.new(2012,2,17,16,15,14)),
179
+ :exclusive => true
180
+ },
181
+
182
+ #---------- date/time range cases
183
+
184
+ {
185
+ :case => "date range intersects other time range, other equivalent to self",
186
+ :subject => (Date.civil(2012,2,13)..
187
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
188
+ :other => (Time.new(2012,2,13,0,0,0)...
189
+ Time.new(2012,2,18,0,0,0)),
190
+ :expected => (Time.new(2012,2,13,0,0,0)...
191
+ Time.new(2012,2,18,0,0,0))
192
+ },
193
+ {
194
+ :case => "time range compared to other date range, self equivalent to other",
195
+ :subject => (Time.new(2012,2,13,0,0,0)...
196
+ Time.new(2012,2,18,0,0,0)).extend(Tempr::DateTimeRange),
197
+ :other => (Date.civil(2012,2,13)..
198
+ Date.civil(2012,2,17)),
199
+ :expected => (Time.new(2012,2,13,0,0,0)...
200
+ Time.new(2012,2,18,0,0,0))
201
+ },
202
+ {
203
+ :case => "date range compared to other time range, other within self",
204
+ :subject => (Date.civil(2012,2,13)..
205
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
206
+ :other => (Time.new(2012,2,13,0,0,0)...
207
+ Time.new(2012,2,17,23,59,59)),
208
+ :expected => (Time.new(2012,2,13,0,0,0)...
209
+ Time.new(2012,2,17,23,59,59))
210
+ },
211
+ {
212
+ :case => "time range compared to other date range, other within self",
213
+ :subject => (Time.new(2012,2,13,0,0,0)...
214
+ Time.new(2012,2,18,0,0,0)).extend(Tempr::DateTimeRange),
215
+ :other => (Date.civil(2012,2,13)..
216
+ Date.civil(2012,2,16)),
217
+ :expected => (Time.new(2012,2,13,0,0,0)...
218
+ Time.new(2012,2,17,0,0,0))
219
+ },
220
+ {
221
+ :case => "date range compared to other time range, self within other",
222
+ :subject => (Date.civil(2012,2,13)..
223
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
224
+ :other => (Time.new(2012,2,13,0,0,0)...
225
+ Time.new(2012,2,18,0,0,1)),
226
+ :expected => (Time.new(2012,2,13,0,0,0)...
227
+ Time.new(2012,2,18,0,0,0))
228
+ },
229
+ {
230
+ :case => "time range compared to other date range, self within other",
231
+ :subject => (Time.new(2012,2,13,0,0,0)...
232
+ Time.new(2012,2,18,0,0,0)).extend(Tempr::DateTimeRange),
233
+ :other => (Date.civil(2012,2,13)..
234
+ Date.civil(2012,2,18)),
235
+ :expected => (Time.new(2012,2,13,0,0,0)...
236
+ Time.new(2012,2,18,0,0,0))
237
+ },
238
+ {
239
+ :case => "date range compared to other time range, self intersects but not within other",
240
+ :subject => (Date.civil(2012,2,13)..
241
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
242
+ :other => (Time.new(2012,2,14,0,0,0)...
243
+ Time.new(2012,2,18,0,0,1)),
244
+ :expected => (Time.new(2012,2,14,0,0,0)...
245
+ Time.new(2012,2,18,0,0,0))
246
+ },
247
+ {
248
+ :case => "time range compared to other date range, self intersects but not within other",
249
+ :subject => (Time.new(2012,2,13,0,0,0)...
250
+ Time.new(2012,2,18,0,0,0)).extend(Tempr::DateTimeRange),
251
+ :other => (Date.civil(2012,2,14)..
252
+ Date.civil(2012,2,18)),
253
+ :expected => (Time.new(2012,2,14,0,0,0)...
254
+ Time.new(2012,2,18,0,0,0))
255
+ },
256
+ {
257
+ :case => "date range compared to other time range, self adjacent to other",
258
+ :subject => (Date.civil(2012,2,13)..
259
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
260
+ :other => (Time.new(2012,2,18,0,0,0)...
261
+ Time.new(2012,2,19,0,0,0)),
262
+ :expected => (Time.new(2012,2,18,0,0,0)...
263
+ Time.new(2012,2,18,0,0,0))
264
+ },
265
+ {
266
+ :case => "time range compared to other date range, self adjacent to other",
267
+ :subject => (Time.new(2012,2,13,0,0,0)...
268
+ Time.new(2012,2,18,0,0,0)).extend(Tempr::DateTimeRange),
269
+ :other => (Date.civil(2012,2,18)..
270
+ Date.civil(2012,2,19)),
271
+ :expected => (Time.new(2012,2,18,0,0,0)...
272
+ Time.new(2012,2,18,0,0,0))
273
+ },
274
+ {
275
+ :case => "date range compared to other time range, self does not intersect other",
276
+ :subject => (Date.civil(2012,2,13)..
277
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
278
+ :other => (Time.new(2012,2,18,0,0,1)...
279
+ Time.new(2012,2,19,0,0,0)),
280
+ :expected => nil
281
+ },
282
+ {
283
+ :case => "time range compared to other date range, self does not intersect other",
284
+ :subject => (Time.new(2012,2,13,0,0,0)...
285
+ Time.new(2012,2,18,0,0,0)).extend(Tempr::DateTimeRange),
286
+ :other => (Date.civil(2012,2,19)..
287
+ Date.civil(2012,2,21)),
288
+ :expected => nil
289
+ }
89
290
  ]
90
291
 
91
292
 
@@ -104,8 +305,7 @@ module RangeIntersectionTests
104
305
  if expected.nil?
105
306
  assert_nil actual
106
307
  else
107
- assert_equal expected.begin, actual.begin
108
- assert_equal expected.end, actual.end
308
+ assert_equal expected, actual
109
309
  end
110
310
  end
111
311
 
@@ -2,140 +2,236 @@ require File.expand_path('test_helper', File.dirname(__FILE__))
2
2
 
3
3
  module RangeWithinOtherTests
4
4
 
5
- describe "DateTimeRange#within, non-exclusive date ranges" do
5
+ Fixtures = [
6
+ # ------- date range cases
7
+ {
8
+ :case => "inclusive date ranges, other equals self",
9
+ :subject => (Date.civil(2012,2,13)..
10
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
11
+ :other => Date.civil(2012,2,13)..Date.civil(2012,2,17),
12
+ :expected_within => true,
13
+ :expected_subsume => true
14
+ },
15
+ {
16
+ :case => "inclusive date ranges, other within self",
17
+ :subject => (Date.civil(2012,2,13)..
18
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
19
+ :other => Date.civil(2012,2,13)..Date.civil(2012,2,16),
20
+ :expected_within => false,
21
+ :expected_subsume => true
22
+ },
23
+ {
24
+ :case => "inclusive date ranges, self within other",
25
+ :subject => (Date.civil(2012,2,13)..
26
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
27
+ :other => Date.civil(2012,2,13)..Date.civil(2012,2,18),
28
+ :expected_within => true,
29
+ :expected_subsume => false
30
+ },
31
+ {
32
+ :case => "inclusive date ranges, self intersects but not within other",
33
+ :subject => (Date.civil(2012,2,13)..
34
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
35
+ :other => Date.civil(2012,2,14)..Date.civil(2012,2,18),
36
+ :expected_within => false,
37
+ :expected_subsume => false
38
+ },
39
+ {
40
+ :case => "inclusive date ranges, self does not intersect other",
41
+ :subject => (Date.civil(2012,2,13)..
42
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
43
+ :other => Date.civil(2012,2,18)..Date.civil(2012,2,19),
44
+ :expected_within => false,
45
+ :expected_subsume => false
46
+ },
47
+ {
48
+ :case => "exclusive date ranges, same endpoint, both exclusive",
49
+ :subject => (Date.civil(2012,2,13)...
50
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
51
+ :other => Date.civil(2012,2,13)...Date.civil(2012,2,17),
52
+ :expected_within => true,
53
+ :expected_subsume => true
54
+ },
55
+ {
56
+ :case => "exclusive date ranges, same endpoint, other is non-exclusive, self is exclusive",
57
+ :subject => (Date.civil(2012,2,13)...
58
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
59
+ :other => Date.civil(2012,2,13)..Date.civil(2012,2,17),
60
+ :expected_within => true,
61
+ :expected_subsume => false
62
+ },
63
+ {
64
+ :case => "exclusive date ranges, same endpoint, other is exclusive, self is non-exclusive",
65
+ :subject => (Date.civil(2012,2,13)..
66
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
67
+ :other => Date.civil(2012,2,13)...Date.civil(2012,2,17),
68
+ :expected_within => false,
69
+ :expected_subsume => true
70
+ },
6
71
 
7
- describe "other equals self" do
8
- let(:subject) { (Date.civil(2012,2,13)..
9
- Date.civil(2012,2,17)).extend(Tempr::DateTimeRange)
10
- }
11
- let(:other) { Date.civil(2012,2,13)..Date.civil(2012,2,17) }
12
-
13
- it "within should return true" do
14
- assert_equal true, subject.within?(other)
15
- end
16
-
17
- it "subsume should return true" do
18
- assert_equal true, subject.subsume?(other)
19
- end
20
-
21
- end
72
+ #---------- time range cases
73
+
74
+ {
75
+ :case => "inclusive time ranges, other equals self",
76
+ :subject => (Time.new(2012,2,13,12,11,10)..
77
+ Time.new(2012,2,17,16,15,14)).extend(Tempr::DateTimeRange),
78
+ :other => (Time.new(2012,2,13,12,11,10)..
79
+ Time.new(2012,2,17,16,15,14)),
80
+ :expected_within => true,
81
+ :expected_subsume => true
82
+ },
83
+ {
84
+ :case => "inclusive time ranges, other within self",
85
+ :subject => (Time.new(2012,2,13,12,11,10)..
86
+ Time.new(2012,2,17,16,15,14)).extend(Tempr::DateTimeRange),
87
+ :other => (Time.new(2012,2,13,12,11,10)..
88
+ Time.new(2012,2,17,16,15,13)),
89
+ :expected_within => false,
90
+ :expected_subsume => true
91
+ },
92
+ {
93
+ :case => "inclusive time ranges, self within other",
94
+ :subject => (Time.new(2012,2,13,12,11,10)..
95
+ Time.new(2012,2,17,16,15,14)).extend(Tempr::DateTimeRange),
96
+ :other => (Time.new(2012,2,13,12,11,10)..
97
+ Time.new(2012,2,17,16,15,15)),
98
+ :expected_within => true,
99
+ :expected_subsume => false
100
+ },
101
+ {
102
+ :case => "inclusive time ranges, self intersects but not within other",
103
+ :subject => (Time.new(2012,2,13,12,11,10)..
104
+ Time.new(2012,2,17,16,15,14)).extend(Tempr::DateTimeRange),
105
+ :other => (Time.new(2012,2,13,12,11,11)..
106
+ Time.new(2012,2,17,16,15,15)),
107
+ :expected_within => false,
108
+ :expected_subsume => false
109
+ },
110
+ {
111
+ :case => "inclusive time ranges, self does not intersect other",
112
+ :subject => (Time.new(2012,2,13,12,11,10)..
113
+ Time.new(2012,2,17,16,15,14)).extend(Tempr::DateTimeRange),
114
+ :other => (Time.new(2012,2,17,16,15,15)..
115
+ Time.new(2012,2,17,16,16,0)),
116
+ :expected_within => false,
117
+ :expected_subsume => false
118
+ },
22
119
 
23
- describe "other within self" do
24
- let(:subject) { (Date.civil(2012,2,13)..
25
- Date.civil(2012,2,17)).extend(Tempr::DateTimeRange)
26
- }
27
- let(:other) { Date.civil(2012,2,13)..Date.civil(2012,2,16) }
28
-
29
- it "within should return false" do
30
- assert_equal false, subject.within?(other)
31
- end
32
-
33
- it "subsume should return true" do
34
- assert_equal true, subject.subsume?(other)
35
- end
36
-
37
- end
38
-
39
- describe "self within other" do
40
- let(:subject) { (Date.civil(2012,2,13)..
41
- Date.civil(2012,2,17)).extend(Tempr::DateTimeRange)
42
- }
43
- let(:other) { Date.civil(2012,2,13)..Date.civil(2012,2,18) }
44
-
45
- it "within should return true" do
46
- assert_equal true, subject.within?(other)
47
- end
120
+ #---------- date/time range cases
121
+
122
+ {
123
+ :case => "date range compared to other time range, other equivalent to self",
124
+ :subject => (Date.civil(2012,2,13)..
125
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
126
+ :other => (Time.new(2012,2,13,0,0,0)...
127
+ Time.new(2012,2,18,0,0,0)),
128
+ :expected_within => true,
129
+ :expected_subsume => true
130
+ },
131
+ {
132
+ :case => "time range compared to other date range, self equivalent to other",
133
+ :subject => (Time.new(2012,2,13,0,0,0)...
134
+ Time.new(2012,2,18,0,0,0)).extend(Tempr::DateTimeRange),
135
+ :other => (Date.civil(2012,2,13)..
136
+ Date.civil(2012,2,17)),
137
+ :expected_within => true,
138
+ :expected_subsume => true
139
+ },
140
+ {
141
+ :case => "date range compared to other time range, other within self",
142
+ :subject => (Date.civil(2012,2,13)..
143
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
144
+ :other => (Time.new(2012,2,13,0,0,0)...
145
+ Time.new(2012,2,17,23,59,59)),
146
+ :expected_within => false,
147
+ :expected_subsume => true
148
+ },
149
+ {
150
+ :case => "time range compared to other date range, other within self",
151
+ :subject => (Time.new(2012,2,13,0,0,0)...
152
+ Time.new(2012,2,18,0,0,0)).extend(Tempr::DateTimeRange),
153
+ :other => (Date.civil(2012,2,13)..
154
+ Date.civil(2012,2,16)),
155
+ :expected_within => false,
156
+ :expected_subsume => true
157
+ },
158
+ {
159
+ :case => "date range compared to other time range, self within other",
160
+ :subject => (Date.civil(2012,2,13)..
161
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
162
+ :other => (Time.new(2012,2,13,0,0,0)...
163
+ Time.new(2012,2,18,0,0,1)),
164
+ :expected_within => true,
165
+ :expected_subsume => false
166
+ },
167
+ {
168
+ :case => "time range compared to other date range, self within other",
169
+ :subject => (Time.new(2012,2,13,0,0,0)...
170
+ Time.new(2012,2,18,0,0,0)).extend(Tempr::DateTimeRange),
171
+ :other => (Date.civil(2012,2,13)..
172
+ Date.civil(2012,2,18)),
173
+ :expected_within => true,
174
+ :expected_subsume => false
175
+ },
176
+ {
177
+ :case => "date range compared to other time range, self intersects but not within other",
178
+ :subject => (Date.civil(2012,2,13)..
179
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
180
+ :other => (Time.new(2012,2,14,0,0,0)...
181
+ Time.new(2012,2,18,0,0,1)),
182
+ :expected_within => false,
183
+ :expected_subsume => false
184
+ },
185
+ {
186
+ :case => "time range compared to other date range, self intersects but not within other",
187
+ :subject => (Time.new(2012,2,13,0,0,0)...
188
+ Time.new(2012,2,18,0,0,0)).extend(Tempr::DateTimeRange),
189
+ :other => (Date.civil(2012,2,14)..
190
+ Date.civil(2012,2,18)),
191
+ :expected_within => false,
192
+ :expected_subsume => false
193
+ },
194
+ {
195
+ :case => "date range compared to other time range, self does not intersect other",
196
+ :subject => (Date.civil(2012,2,13)..
197
+ Date.civil(2012,2,17)).extend(Tempr::DateTimeRange),
198
+ :other => (Time.new(2012,2,18,0,0,1)...
199
+ Time.new(2012,2,19,0,0,0)),
200
+ :expected_within => false,
201
+ :expected_subsume => false
202
+ },
203
+ {
204
+ :case => "time range compared to other date range, self does not intersect other",
205
+ :subject => (Time.new(2012,2,13,0,0,0)...
206
+ Time.new(2012,2,18,0,0,0)).extend(Tempr::DateTimeRange),
207
+ :other => (Date.civil(2012,2,19)..
208
+ Date.civil(2012,2,21)),
209
+ :expected_within => false,
210
+ :expected_subsume => false
211
+ }
212
+ ]
213
+
48
214
 
49
- it "subsume should return false" do
50
- assert_equal false, subject.subsume?(other)
51
- end
52
-
53
- end
215
+ describe "DateTimeRange#within and #subsume" do
216
+
217
+ Fixtures.each do |fixture|
54
218
 
55
- describe "self intersects but not within other" do
56
- let(:subject) { (Date.civil(2012,2,13)..
57
- Date.civil(2012,2,17)).extend(Tempr::DateTimeRange)
58
- }
59
- let(:other) { Date.civil(2012,2,14)..Date.civil(2012,2,18) }
219
+ describe fixture[:case] do
220
+ let(:subject) { fixture[:subject] }
221
+ let(:other) { fixture[:other] }
222
+
223
+ it "within should return #{fixture[:expected_within]}" do
224
+ assert_equal fixture[:expected_within], subject.within?(other)
225
+ end
60
226
 
61
- it "within should return false" do
62
- assert_equal false, subject.within?(other)
63
- end
64
-
65
- it "subsume should return false" do
66
- assert_equal false, subject.subsume?(other)
67
- end
68
-
69
- end
70
-
71
- describe "self does not intersect other" do
72
- let(:subject) { (Date.civil(2012,2,13)..
73
- Date.civil(2012,2,17)).extend(Tempr::DateTimeRange)
74
- }
75
- let(:other) { Date.civil(2012,2,18)..Date.civil(2012,2,19) }
76
-
77
- it "within should return false" do
78
- assert_equal false, subject.within?(other)
79
- end
80
-
81
- it "subsume should return false" do
82
- assert_equal false, subject.subsume?(other)
227
+ it "subsume should return #{fixture[:expected_subsume]}" do
228
+ assert_equal fixture[:expected_subsume], subject.subsume?(other)
229
+ end
83
230
  end
84
231
 
85
232
  end
86
233
 
87
234
  end
88
-
89
- describe "DateTimeRange#within, exclusive date ranges" do
90
235
 
91
- describe "same endpoint, both exclusive" do
92
- let(:subject) { (Date.civil(2012,2,13)...
93
- Date.civil(2012,2,17)).extend(Tempr::DateTimeRange)
94
- }
95
- let(:other) { Date.civil(2012,2,13)...Date.civil(2012,2,17) }
96
-
97
- it "within should return true" do
98
- assert_equal true, subject.within?(other)
99
- end
100
-
101
- it "subsume should return true" do
102
- assert_equal true, subject.subsume?(other)
103
- end
104
-
105
- end
106
-
107
- describe "same endpoint, other is non-exclusive, self is exclusive" do
108
- let(:subject) { (Date.civil(2012,2,13)...
109
- Date.civil(2012,2,17)).extend(Tempr::DateTimeRange)
110
- }
111
- let(:other) { Date.civil(2012,2,13)..Date.civil(2012,2,17) }
112
-
113
- it "within should return true" do
114
- assert_equal true, subject.within?(other)
115
- end
116
-
117
- it "subsume should return false" do
118
- assert_equal false, subject.subsume?(other)
119
- end
120
-
121
- end
122
-
123
- describe "same endpoint, other is exclusive, self is non-exclusive" do
124
- let(:subject) { (Date.civil(2012,2,13)..
125
- Date.civil(2012,2,17)).extend(Tempr::DateTimeRange)
126
- }
127
- let(:other) { Date.civil(2012,2,13)...Date.civil(2012,2,17) }
128
-
129
- it "within should return false" do
130
- assert_equal false, subject.within?(other)
131
- end
132
-
133
- it "subsume should return true" do
134
- assert_equal true, subject.subsume?(other)
135
- end
136
-
137
- end
138
-
139
- end
140
236
 
141
237
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tempr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-02-19 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
16
- requirement: &9985020 !ruby/object:Gem::Requirement
16
+ requirement: &13275580 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,18 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *9985020
24
+ version_requirements: *13275580
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &13275120 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *13275120
25
36
  description: ''
26
37
  email:
27
38
  - ericgj72@gmail.com
@@ -33,6 +44,7 @@ files:
33
44
  - CHANGELOG.markdown
34
45
  - Gemfile
35
46
  - README.markdown
47
+ - Rakefile
36
48
  - lib/tempr.rb
37
49
  - lib/tempr/date_time_range.rb
38
50
  - lib/tempr/version.rb