Bump version number to 2.4.2 to pick up the latest minor bug fixes.
[python/dscho.git] / Lib / test / test_strptime.py
blob314f29dd3fdd1616b2e5c65fa3ff879fde88d866
1 """PyUnit testing against strptime"""
3 import unittest
4 import time
5 import locale
6 import re
7 from test import test_support
9 import _strptime
11 __version__ = (1,0,5)
13 class LocaleTime_Tests(unittest.TestCase):
14 """Tests for _strptime.LocaleTime."""
16 def setUp(self):
17 """Create time tuple based on current time."""
18 self.time_tuple = time.localtime()
19 self.LT_ins = _strptime.LocaleTime()
21 def compare_against_time(self, testing, directive, tuple_position, error_msg):
22 """Helper method that tests testing against directive based on the
23 tuple_position of time_tuple. Uses error_msg as error message.
25 """
26 strftime_output = time.strftime(directive, self.time_tuple)
27 comparison = testing[self.time_tuple[tuple_position]]
28 self.failUnless(strftime_output in testing, "%s: not found in tuple" % error_msg)
29 self.failUnless(comparison == strftime_output, "%s: position within tuple incorrect; %s != %s" % (error_msg, comparison, strftime_output))
31 def test_weekday(self):
32 # Make sure that full and abbreviated weekday names are correct in
33 # both string and position with tuple
34 self.compare_against_time(self.LT_ins.f_weekday, '%A', 6, "Testing of full weekday name failed")
35 self.compare_against_time(self.LT_ins.a_weekday, '%a', 6, "Testing of abbreviated weekday name failed")
37 def test_month(self):
38 # Test full and abbreviated month names; both string and position
39 # within the tuple
40 self.compare_against_time(self.LT_ins.f_month, '%B', 1, "Testing against full month name failed")
41 self.compare_against_time(self.LT_ins.a_month, '%b', 1, "Testing against abbreviated month name failed")
43 def test_am_pm(self):
44 # Make sure AM/PM representation done properly
45 strftime_output = time.strftime("%p", self.time_tuple)
46 self.failUnless(strftime_output in self.LT_ins.am_pm, "AM/PM representation not in tuple")
47 if self.time_tuple[3] < 12: position = 0
48 else: position = 1
49 self.failUnless(strftime_output == self.LT_ins.am_pm[position], "AM/PM representation in the wrong position within the tuple")
51 def test_timezone(self):
52 # Make sure timezone is correct
53 if time.strftime("%Z", self.time_tuple):
54 self.compare_against_time(self.LT_ins.timezone, '%Z', 8, "Testing against timezone failed")
56 def test_date_time(self):
57 # Check that LC_date_time, LC_date, and LC_time are correct
58 # the magic date is used so as to not have issues with %c when day of
59 # the month is a single digit and has a leading space. This is not an
60 # issue since strptime still parses it correctly. The problem is
61 # testing these directives for correctness by comparing strftime
62 # output.
63 magic_date = (1999, 3, 17, 22, 44, 55, 2, 76, 0)
64 strftime_output = time.strftime("%c", magic_date)
65 self.failUnless(strftime_output == time.strftime(self.LT_ins.LC_date_time, magic_date), "LC_date_time incorrect")
66 strftime_output = time.strftime("%x", magic_date)
67 self.failUnless(strftime_output == time.strftime(self.LT_ins.LC_date, magic_date), "LC_date incorrect")
68 strftime_output = time.strftime("%X", magic_date)
69 self.failUnless(strftime_output == time.strftime(self.LT_ins.LC_time, magic_date), "LC_time incorrect")
70 LT = _strptime.LocaleTime(am_pm=('',''))
71 self.failUnless(LT.LC_time, "LocaleTime's LC directives cannot handle "
72 "empty strings")
74 def test_lang(self):
75 # Make sure lang is set
76 self.failUnless(self.LT_ins.lang in (locale.getdefaultlocale()[0], locale.getlocale(locale.LC_TIME)), "Setting of lang failed")
78 def test_by_hand_input(self):
79 # Test passed-in initialization value checks
80 self.failUnless(_strptime.LocaleTime(f_weekday=range(7)), "Argument size check for f_weekday failed")
81 self.assertRaises(TypeError, _strptime.LocaleTime, f_weekday=range(8))
82 self.assertRaises(TypeError, _strptime.LocaleTime, f_weekday=range(6))
83 self.failUnless(_strptime.LocaleTime(a_weekday=range(7)), "Argument size check for a_weekday failed")
84 self.assertRaises(TypeError, _strptime.LocaleTime, a_weekday=range(8))
85 self.assertRaises(TypeError, _strptime.LocaleTime, a_weekday=range(6))
86 self.failUnless(_strptime.LocaleTime(f_month=range(12)), "Argument size check for f_month failed")
87 self.assertRaises(TypeError, _strptime.LocaleTime, f_month=range(11))
88 self.assertRaises(TypeError, _strptime.LocaleTime, f_month=range(13))
89 self.failUnless(len(_strptime.LocaleTime(f_month=range(12)).f_month) == 13, "dummy value for f_month not added")
90 self.failUnless(_strptime.LocaleTime(a_month=range(12)), "Argument size check for a_month failed")
91 self.assertRaises(TypeError, _strptime.LocaleTime, a_month=range(11))
92 self.assertRaises(TypeError, _strptime.LocaleTime, a_month=range(13))
93 self.failUnless(len(_strptime.LocaleTime(a_month=range(12)).a_month) == 13, "dummy value for a_month not added")
94 self.failUnless(_strptime.LocaleTime(am_pm=range(2)), "Argument size check for am_pm failed")
95 self.assertRaises(TypeError, _strptime.LocaleTime, am_pm=range(1))
96 self.assertRaises(TypeError, _strptime.LocaleTime, am_pm=range(3))
97 self.failUnless(_strptime.LocaleTime(timezone=range(2)), "Argument size check for timezone failed")
98 self.assertRaises(TypeError, _strptime.LocaleTime, timezone=range(1))
99 self.assertRaises(TypeError, _strptime.LocaleTime, timezone=range(3))
101 class TimeRETests(unittest.TestCase):
102 """Tests for TimeRE."""
104 def setUp(self):
105 """Construct generic TimeRE object."""
106 self.time_re = _strptime.TimeRE()
107 self.locale_time = _strptime.LocaleTime()
109 def test_getitem(self):
110 # Make sure that __getitem__ works properly
111 self.failUnless(self.time_re['m'], "Fetching 'm' directive (built-in) failed")
112 self.failUnless(self.time_re['b'], "Fetching 'b' directive (built w/ __tupleToRE) failed")
113 for name in self.locale_time.a_month:
114 self.failUnless(self.time_re['b'].find(name) != -1, "Not all abbreviated month names in regex")
115 self.failUnless(self.time_re['c'], "Fetching 'c' directive (built w/ format) failed")
116 self.failUnless(self.time_re['c'].find('%') == -1, "Conversion of 'c' directive failed; '%' found")
117 self.assertRaises(KeyError, self.time_re.__getitem__, '1')
119 def test_pattern(self):
120 # Test TimeRE.pattern
121 pattern_string = self.time_re.pattern(r"%a %A %d")
122 self.failUnless(pattern_string.find(self.locale_time.a_weekday[2]) != -1, "did not find abbreviated weekday in pattern string '%s'" % pattern_string)
123 self.failUnless(pattern_string.find(self.locale_time.f_weekday[4]) != -1, "did not find full weekday in pattern string '%s'" % pattern_string)
124 self.failUnless(pattern_string.find(self.time_re['d']) != -1, "did not find 'd' directive pattern string '%s'" % pattern_string)
126 def test_compile(self):
127 # Check that compiled regex is correct
128 found = self.time_re.compile(r"%A").match(self.locale_time.f_weekday[6])
129 self.failUnless(found and found.group('A') == self.locale_time.f_weekday[6], "re object for '%A' failed")
130 compiled = self.time_re.compile(r"%a %b")
131 found = compiled.match("%s %s" % (self.locale_time.a_weekday[4], self.locale_time.a_month[4]))
132 self.failUnless(found,
133 "Match failed with '%s' regex and '%s' string" % (compiled.pattern, "%s %s" % (self.locale_time.a_weekday[4], self.locale_time.a_month[4])))
134 self.failUnless(found.group('a') == self.locale_time.a_weekday[4] and found.group('b') == self.locale_time.a_month[4],
135 "re object couldn't find the abbreviated weekday month in '%s' using '%s'; group 'a' = '%s', group 'b' = %s'" % (found.string, found.re.pattern, found.group('a'), found.group('b')))
136 for directive in ('a','A','b','B','c','d','H','I','j','m','M','p','S','U','w','W','x','X','y','Y','Z','%'):
137 compiled = self.time_re.compile("%%%s"% directive)
138 found = compiled.match(time.strftime("%%%s" % directive))
139 self.failUnless(found, "Matching failed on '%s' using '%s' regex" % (time.strftime("%%%s" % directive), compiled.pattern))
141 class StrptimeTests(unittest.TestCase):
142 """Tests for _strptime.strptime."""
144 def setUp(self):
145 """Create testing time tuple."""
146 self.time_tuple = time.gmtime()
148 def test_TypeError(self):
149 # Make sure ValueError is raised when match fails
150 self.assertRaises(ValueError,_strptime.strptime, data_string="%d", format="%A")
152 def test_returning_RE(self):
153 # Make sure that an re can be returned
154 strp_output = _strptime.strptime(False, "%Y")
155 self.failUnless(isinstance(strp_output, type(re.compile(''))), "re object not returned correctly")
156 self.failUnless(_strptime.strptime("1999", strp_output), "Use or re object failed")
157 bad_locale_time = _strptime.LocaleTime(lang="gibberish")
158 self.assertRaises(TypeError, _strptime.strptime, data_string='1999', format=strp_output, locale_time=bad_locale_time)
160 def helper(self, directive, position):
161 """Helper fxn in testing."""
162 strf_output = time.strftime("%%%s" % directive, self.time_tuple)
163 strp_output = _strptime.strptime(strf_output, "%%%s" % directive)
164 self.failUnless(strp_output[position] == self.time_tuple[position], "testing of '%s' directive failed; '%s' -> %s != %s" % (directive, strf_output, strp_output[position], self.time_tuple[position]))
166 def test_year(self):
167 # Test that the year is handled properly
168 for directive in ('y', 'Y'):
169 self.helper(directive, 0)
171 def test_month(self):
172 # Test for month directives
173 for directive in ('B', 'b', 'm'):
174 self.helper(directive, 1)
176 def test_day(self):
177 # Test for day directives
178 self.helper('d', 2)
180 def test_hour(self):
181 # Test hour directives
182 self.helper('H', 3)
183 strf_output = time.strftime("%I %p", self.time_tuple)
184 strp_output = _strptime.strptime(strf_output, "%I %p")
185 self.failUnless(strp_output[3] == self.time_tuple[3], "testing of '%%I %%p' directive failed; '%s' -> %s != %s" % (strf_output, strp_output[3], self.time_tuple[3]))
187 def test_minute(self):
188 # Test minute directives
189 self.helper('M', 4)
191 def test_second(self):
192 # Test second directives
193 self.helper('S', 5)
195 def test_weekday(self):
196 # Test weekday directives
197 for directive in ('A', 'a', 'w'):
198 self.helper(directive,6)
200 def test_julian(self):
201 # Test julian directives
202 self.helper('j', 7)
204 def test_timezone(self):
205 # Test timezone directives.
206 # When gmtime() is used with %Z, entire result of strftime() is empty.
207 time_tuple = time.localtime()
208 strf_output = time.strftime("%Z") #UTC does not have a timezone
209 strp_output = _strptime.strptime(strf_output, "%Z")
210 self.failUnless(strp_output[8] == time_tuple[8], "timezone check failed; '%s' -> %s != %s" % (strf_output, strp_output[8], time_tuple[8]))
212 def test_date_time(self):
213 # Test %c directive
214 for position in range(6):
215 self.helper('c', position)
217 def test_date(self):
218 # Test %x directive
219 for position in range(0,3):
220 self.helper('x', position)
222 def test_time(self):
223 # Test %X directive
224 for position in range(3,6):
225 self.helper('X', position)
227 def test_percent(self):
228 # Make sure % signs are handled properly
229 strf_output = time.strftime("%m %% %Y", self.time_tuple)
230 strp_output = _strptime.strptime(strf_output, "%m %% %Y")
231 self.failUnless(strp_output[0] == self.time_tuple[0] and strp_output[1] == self.time_tuple[1], "handling of percent sign failed")
233 def test_caseinsensitive(self):
234 # Should handle names case-insensitively.
235 strf_output = time.strftime("%B", self.time_tuple)
236 self.failUnless(_strptime.strptime(strf_output.upper(), "%B"),
237 "strptime does not handle ALL-CAPS names properly")
238 self.failUnless(_strptime.strptime(strf_output.lower(), "%B"),
239 "strptime does not handle lowercase names properly")
240 self.failUnless(_strptime.strptime(strf_output.capitalize(), "%B"),
241 "strptime does not handle capword names properly")
243 class FxnTests(unittest.TestCase):
244 """Test functions that fill in info by validating result and are triggered properly."""
246 def setUp(self):
247 """Create an initial time tuple."""
248 self.time_tuple = time.gmtime()
250 def test_julianday_result(self):
251 # Test julianday
252 result = _strptime.julianday(self.time_tuple[0], self.time_tuple[1], self.time_tuple[2])
253 self.failUnless(result == self.time_tuple[7], "julianday failed; %s != %s" % (result, self.time_tuple[7]))
255 def test_julianday_trigger(self):
256 # Make sure julianday is called
257 strf_output = time.strftime("%Y-%m-%d", self.time_tuple)
258 strp_output = _strptime.strptime(strf_output, "%Y-%m-%d")
259 self.failUnless(strp_output[7] == self.time_tuple[7], "strptime did not trigger julianday(); %s != %s" % (strp_output[7], self.time_tuple[7]))
261 def test_gregorian_result(self):
262 # Test gregorian
263 result = _strptime.gregorian(self.time_tuple[7], self.time_tuple[0])
264 comparison = [self.time_tuple[0], self.time_tuple[1], self.time_tuple[2]]
265 self.failUnless(result == comparison, "gregorian() failed; %s != %s" % (result, comparison))
267 def test_gregorian_trigger(self):
268 # Test that gregorian() is triggered
269 strf_output = time.strftime("%j %Y", self.time_tuple)
270 strp_output = _strptime.strptime(strf_output, "%j %Y")
271 self.failUnless(strp_output[1] == self.time_tuple[1] and strp_output[2] == self.time_tuple[2], "gregorian() not triggered; month -- %s != %s, day -- %s != %s" % (strp_output[1], self.time_tuple[1], strp_output[2], self.time_tuple[2]))
273 def test_dayofweek_result(self):
274 # Test dayofweek
275 result = _strptime.dayofweek(self.time_tuple[0], self.time_tuple[1], self.time_tuple[2])
276 comparison = self.time_tuple[6]
277 self.failUnless(result == comparison, "dayofweek() failed; %s != %s" % (result, comparison))
279 def test_dayofweek_trigger(self):
280 # Make sure dayofweek() gets triggered
281 strf_output = time.strftime("%Y-%m-%d", self.time_tuple)
282 strp_output = _strptime.strptime(strf_output, "%Y-%m-%d")
283 self.failUnless(strp_output[6] == self.time_tuple[6], "triggering of dayofweek() failed; %s != %s" % (strp_output[6], self.time_tuple[6]))
286 class Strptime12AMPMTests(unittest.TestCase):
287 """Test a _strptime regression in '%I %p' at 12 noon (12 PM)"""
289 def test_twelve_noon_midnight(self):
290 eq = self.assertEqual
291 eq(time.strptime('12 PM', '%I %p')[3], 12)
292 eq(time.strptime('12 AM', '%I %p')[3], 0)
293 eq(_strptime.strptime('12 PM', '%I %p')[3], 12)
294 eq(_strptime.strptime('12 AM', '%I %p')[3], 0)
297 def test_main():
298 suite = unittest.TestSuite()
299 suite.addTest(unittest.makeSuite(LocaleTime_Tests))
300 suite.addTest(unittest.makeSuite(TimeRETests))
301 suite.addTest(unittest.makeSuite(StrptimeTests))
302 suite.addTest(unittest.makeSuite(FxnTests))
303 suite.addTest(unittest.makeSuite(Strptime12AMPMTests))
304 test_support.run_suite(suite)
307 if __name__ == '__main__':
308 test_main()