@@ -82,6 +82,12 @@ def test_eq(self):
8282 )
8383 assert UndateInterval (Undate (2022 , 5 )) == UndateInterval (Undate (2022 , 5 ))
8484
85+ def test_eq_type_check (self ):
86+ # doesn't currently support comparison with anything else
87+ interval = UndateInterval (Undate (900 ))
88+ # returns NotImplemented if comparison with this type is not supported
89+ assert interval .__eq__ ("foo" ) == NotImplemented
90+
8591 def test_not_eq (self ):
8692 assert UndateInterval (Undate (2022 ), Undate (2023 )) != UndateInterval (
8793 Undate (2022 ), Undate (2024 )
@@ -143,3 +149,85 @@ def test_duration(self):
143149 # one year set and the other not currently raises not implemented error
144150 with pytest .raises (NotImplementedError ):
145151 UndateInterval (Undate (2000 ), Undate (month = 10 )).duration ()
152+
153+ def test_intersection (self ):
154+ century11th = UndateInterval (Undate (1001 ), Undate (1100 ))
155+ century20th = UndateInterval (Undate (1901 ), Undate (2000 ))
156+ # no intersection
157+ assert century11th .intersection (century20th ) is None
158+ # should work in either direction
159+ assert century20th .intersection (century11th ) is None
160+
161+ decade1990s = UndateInterval (Undate (1990 ), Undate (1999 ))
162+ # intersection of an interval completely contained in another
163+ # returns an interval equivalent to the smaller one
164+ assert century20th .intersection (decade1990s ) == decade1990s
165+ assert decade1990s .intersection (century20th ) == decade1990s
166+
167+ # partial overlap
168+ nineties_oughts = UndateInterval (Undate (1990 ), Undate (2009 ))
169+ assert century20th .intersection (nineties_oughts ) == UndateInterval (
170+ Undate (1990 ), Undate (2000 )
171+ )
172+
173+ # intersections between half open intervals
174+ after_c11th = UndateInterval (Undate (1001 ), None )
175+ assert after_c11th .intersection (century20th ) == century20th
176+ assert after_c11th .intersection (decade1990s ) == decade1990s
177+
178+ before_20th = UndateInterval (None , Undate (1901 ))
179+ assert before_20th .intersection (decade1990s ) is None
180+ assert before_20th .intersection (century11th ) == century11th
181+ assert before_20th .intersection (after_c11th ) == UndateInterval (
182+ Undate (1001 ), Undate (1901 )
183+ )
184+
185+ def test_contains (self ):
186+ century11th = UndateInterval (Undate (1001 ), Undate (1100 ))
187+ century20th = UndateInterval (Undate (1901 ), Undate (2000 ))
188+ decade1990s = UndateInterval (Undate (1990 ), Undate (1999 ))
189+ # an interval DOES contain itself
190+ for interval in [century11th , century20th , decade1990s ]:
191+ assert interval in interval
192+
193+ # checking if an interval is within another interval
194+ assert decade1990s in century20th
195+ assert decade1990s not in century11th
196+ assert century11th not in decade1990s
197+ assert century20th not in decade1990s
198+ # a specific date can be contained by an interval
199+ y2k = Undate (2000 )
200+ assert y2k in century20th
201+ assert y2k not in century11th
202+ # partially known date should work too
203+ april_someyear = Undate ("198X" , 4 )
204+ assert april_someyear in century20th
205+ assert april_someyear not in century11th
206+ # conversion from datetime.date also works
207+ assert datetime .date (1922 , 5 , 1 ) in century20th
208+ # unsupported types result in a type error
209+ with pytest .raises (TypeError ):
210+ assert "nineteen-eighty-four" in century20th
211+
212+ # contains check with half-open intervals
213+ after_c11th = UndateInterval (Undate (1001 ), None )
214+ before_20th = UndateInterval (None , Undate (1901 ))
215+ # neither of them contains the other
216+ assert after_c11th not in before_20th
217+ assert before_20th not in after_c11th
218+ # nor are they contained by a smaller range
219+ assert after_c11th not in decade1990s
220+ assert before_20th not in decade1990s
221+
222+ # all of our previous test dates are in the 1900s,
223+ # so they are after the 11th century and not before the 20th
224+ for period in [decade1990s , y2k , april_someyear ]:
225+ assert period in after_c11th
226+ assert period not in before_20th
227+
228+ # fully open interval - is this even meaningful?
229+ whenever = UndateInterval (None , None )
230+ assert decade1990s in whenever
231+ # NOTE: an interval contains itself or an equivalent interval,
232+ # but that may not make sense for open intervals...
233+ assert whenever in whenever
0 commit comments