Barry Warsaw | 4d895fa | 2002-09-23 22:46:49 +0000 | [diff] [blame] | 1 | """PyUnit testing against strptime""" |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 3 | import unittest |
| 4 | import time |
| 5 | import locale |
| 6 | import re |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 7 | from test import test_support |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 8 | |
| 9 | import _strptime |
| 10 | |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 11 | class LocaleTime_Tests(unittest.TestCase): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 12 | """Tests for _strptime.LocaleTime.""" |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 13 | |
| 14 | def setUp(self): |
| 15 | """Create time tuple based on current time.""" |
| 16 | self.time_tuple = time.localtime() |
| 17 | self.LT_ins = _strptime.LocaleTime() |
| 18 | |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 19 | def compare_against_time(self, testing, directive, tuple_position, |
| 20 | error_msg): |
Tim Peters | 469cdad | 2002-08-08 20:19:19 +0000 | [diff] [blame] | 21 | """Helper method that tests testing against directive based on the |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 22 | tuple_position of time_tuple. Uses error_msg as error message. |
| 23 | |
| 24 | """ |
| 25 | strftime_output = time.strftime(directive, self.time_tuple) |
| 26 | comparison = testing[self.time_tuple[tuple_position]] |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 27 | self.failUnless(strftime_output in testing, "%s: not found in tuple" % |
| 28 | error_msg) |
| 29 | self.failUnless(comparison == strftime_output, |
| 30 | "%s: position within tuple incorrect; %s != %s" % |
| 31 | (error_msg, comparison, strftime_output)) |
Tim Peters | 469cdad | 2002-08-08 20:19:19 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 33 | def test_weekday(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 34 | # Make sure that full and abbreviated weekday names are correct in |
| 35 | # both string and position with tuple |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 36 | self.compare_against_time(self.LT_ins.f_weekday, '%A', 6, |
| 37 | "Testing of full weekday name failed") |
| 38 | self.compare_against_time(self.LT_ins.a_weekday, '%a', 6, |
| 39 | "Testing of abbreviated weekday name failed") |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 40 | |
| 41 | def test_month(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 42 | # Test full and abbreviated month names; both string and position |
| 43 | # within the tuple |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 44 | self.compare_against_time(self.LT_ins.f_month, '%B', 1, |
| 45 | "Testing against full month name failed") |
| 46 | self.compare_against_time(self.LT_ins.a_month, '%b', 1, |
| 47 | "Testing against abbreviated month name failed") |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 48 | |
| 49 | def test_am_pm(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 50 | # Make sure AM/PM representation done properly |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 51 | strftime_output = time.strftime("%p", self.time_tuple) |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 52 | self.failUnless(strftime_output in self.LT_ins.am_pm, |
| 53 | "AM/PM representation not in tuple") |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 54 | if self.time_tuple[3] < 12: position = 0 |
| 55 | else: position = 1 |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 56 | self.failUnless(strftime_output == self.LT_ins.am_pm[position], |
| 57 | "AM/PM representation in the wrong position within the tuple") |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 58 | |
| 59 | def test_timezone(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 60 | # Make sure timezone is correct |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 61 | if time.strftime("%Z", self.time_tuple): |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 62 | self.compare_against_time(self.LT_ins.timezone, '%Z', 8, |
| 63 | "Testing against timezone failed") |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 64 | |
| 65 | def test_date_time(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 66 | # Check that LC_date_time, LC_date, and LC_time are correct |
Barry Warsaw | 4d895fa | 2002-09-23 22:46:49 +0000 | [diff] [blame] | 67 | # the magic date is used so as to not have issues with %c when day of |
| 68 | # the month is a single digit and has a leading space. This is not an |
| 69 | # issue since strptime still parses it correctly. The problem is |
| 70 | # testing these directives for correctness by comparing strftime |
| 71 | # output. |
| 72 | magic_date = (1999, 3, 17, 22, 44, 55, 2, 76, 0) |
| 73 | strftime_output = time.strftime("%c", magic_date) |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 74 | self.failUnless(strftime_output == time.strftime(self.LT_ins.LC_date_time, |
| 75 | magic_date), |
| 76 | "LC_date_time incorrect") |
Barry Warsaw | 4d895fa | 2002-09-23 22:46:49 +0000 | [diff] [blame] | 77 | strftime_output = time.strftime("%x", magic_date) |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 78 | self.failUnless(strftime_output == time.strftime(self.LT_ins.LC_date, |
| 79 | magic_date), |
| 80 | "LC_date incorrect") |
Barry Warsaw | 4d895fa | 2002-09-23 22:46:49 +0000 | [diff] [blame] | 81 | strftime_output = time.strftime("%X", magic_date) |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 82 | self.failUnless(strftime_output == time.strftime(self.LT_ins.LC_time, |
| 83 | magic_date), |
| 84 | "LC_time incorrect") |
Barry Warsaw | 4d895fa | 2002-09-23 22:46:49 +0000 | [diff] [blame] | 85 | LT = _strptime.LocaleTime(am_pm=('','')) |
| 86 | self.failUnless(LT.LC_time, "LocaleTime's LC directives cannot handle " |
| 87 | "empty strings") |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 88 | |
| 89 | def test_lang(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 90 | # Make sure lang is set |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 91 | self.failUnless(self.LT_ins.lang in (locale.getdefaultlocale()[0], |
| 92 | locale.getlocale(locale.LC_TIME), |
| 93 | ''), |
| 94 | "Setting of lang failed") |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 95 | |
| 96 | def test_by_hand_input(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 97 | # Test passed-in initialization value checks |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 98 | self.failUnless(_strptime.LocaleTime(f_weekday=range(7)), |
| 99 | "Argument size check for f_weekday failed") |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 100 | self.assertRaises(TypeError, _strptime.LocaleTime, f_weekday=range(8)) |
| 101 | self.assertRaises(TypeError, _strptime.LocaleTime, f_weekday=range(6)) |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 102 | self.failUnless(_strptime.LocaleTime(a_weekday=range(7)), |
| 103 | "Argument size check for a_weekday failed") |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 104 | self.assertRaises(TypeError, _strptime.LocaleTime, a_weekday=range(8)) |
| 105 | self.assertRaises(TypeError, _strptime.LocaleTime, a_weekday=range(6)) |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 106 | self.failUnless(_strptime.LocaleTime(f_month=range(12)), |
| 107 | "Argument size check for f_month failed") |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 108 | self.assertRaises(TypeError, _strptime.LocaleTime, f_month=range(11)) |
| 109 | self.assertRaises(TypeError, _strptime.LocaleTime, f_month=range(13)) |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 110 | self.failUnless(len(_strptime.LocaleTime(f_month=range(12)).f_month) == 13, |
| 111 | "dummy value for f_month not added") |
| 112 | self.failUnless(_strptime.LocaleTime(a_month=range(12)), |
| 113 | "Argument size check for a_month failed") |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 114 | self.assertRaises(TypeError, _strptime.LocaleTime, a_month=range(11)) |
| 115 | self.assertRaises(TypeError, _strptime.LocaleTime, a_month=range(13)) |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 116 | self.failUnless(len(_strptime.LocaleTime(a_month=range(12)).a_month) == 13, |
| 117 | "dummy value for a_month not added") |
| 118 | self.failUnless(_strptime.LocaleTime(am_pm=range(2)), |
| 119 | "Argument size check for am_pm failed") |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 120 | self.assertRaises(TypeError, _strptime.LocaleTime, am_pm=range(1)) |
| 121 | self.assertRaises(TypeError, _strptime.LocaleTime, am_pm=range(3)) |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 122 | self.failUnless(_strptime.LocaleTime(timezone=range(2)), |
| 123 | "Argument size check for timezone failed") |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 124 | self.assertRaises(TypeError, _strptime.LocaleTime, timezone=range(1)) |
| 125 | self.assertRaises(TypeError, _strptime.LocaleTime, timezone=range(3)) |
| 126 | |
| 127 | class TimeRETests(unittest.TestCase): |
| 128 | """Tests for TimeRE.""" |
| 129 | |
| 130 | def setUp(self): |
| 131 | """Construct generic TimeRE object.""" |
| 132 | self.time_re = _strptime.TimeRE() |
| 133 | self.locale_time = _strptime.LocaleTime() |
| 134 | |
| 135 | def test_getitem(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 136 | # Make sure that __getitem__ works properly |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 137 | self.failUnless(self.time_re['m'], |
| 138 | "Fetching 'm' directive (built-in) failed") |
| 139 | self.failUnless(self.time_re['b'], |
| 140 | "Fetching 'b' directive (built w/ __tupleToRE) failed") |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 141 | for name in self.locale_time.a_month: |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 142 | self.failUnless(self.time_re['b'].find(name) != -1, |
| 143 | "Not all abbreviated month names in regex") |
| 144 | self.failUnless(self.time_re['c'], |
| 145 | "Fetching 'c' directive (built w/ format) failed") |
| 146 | self.failUnless(self.time_re['c'].find('%') == -1, |
| 147 | "Conversion of 'c' directive failed; '%' found") |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 148 | self.assertRaises(KeyError, self.time_re.__getitem__, '1') |
| 149 | |
| 150 | def test_pattern(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 151 | # Test TimeRE.pattern |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 152 | pattern_string = self.time_re.pattern(r"%a %A %d") |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 153 | self.failUnless(pattern_string.find(self.locale_time.a_weekday[2]) != -1, |
| 154 | "did not find abbreviated weekday in pattern string '%s'" % |
| 155 | pattern_string) |
| 156 | self.failUnless(pattern_string.find(self.locale_time.f_weekday[4]) != -1, |
| 157 | "did not find full weekday in pattern string '%s'" % |
| 158 | pattern_string) |
| 159 | self.failUnless(pattern_string.find(self.time_re['d']) != -1, |
| 160 | "did not find 'd' directive pattern string '%s'" % |
| 161 | pattern_string) |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 162 | |
| 163 | def test_compile(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 164 | # Check that compiled regex is correct |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 165 | found = self.time_re.compile(r"%A").match(self.locale_time.f_weekday[6]) |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 166 | self.failUnless(found and found.group('A') == self.locale_time.f_weekday[6], |
| 167 | "re object for '%A' failed") |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 168 | compiled = self.time_re.compile(r"%a %b") |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 169 | found = compiled.match("%s %s" % (self.locale_time.a_weekday[4], |
| 170 | self.locale_time.a_month[4])) |
Tim Peters | 469cdad | 2002-08-08 20:19:19 +0000 | [diff] [blame] | 171 | self.failUnless(found, |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 172 | "Match failed with '%s' regex and '%s' string" % |
| 173 | (compiled.pattern, "%s %s" % (self.locale_time.a_weekday[4], |
| 174 | self.locale_time.a_month[4]))) |
| 175 | self.failUnless(found.group('a') == self.locale_time.a_weekday[4] and |
| 176 | found.group('b') == self.locale_time.a_month[4], |
| 177 | "re object couldn't find the abbreviated weekday month in " |
| 178 | "'%s' using '%s'; group 'a' = '%s', group 'b' = %s'" % |
| 179 | (found.string, found.re.pattern, found.group('a'), |
| 180 | found.group('b'))) |
| 181 | for directive in ('a','A','b','B','c','d','H','I','j','m','M','p','S', |
| 182 | 'U','w','W','x','X','y','Y','Z','%'): |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 183 | compiled = self.time_re.compile("%%%s"% directive) |
| 184 | found = compiled.match(time.strftime("%%%s" % directive)) |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 185 | self.failUnless(found, "Matching failed on '%s' using '%s' regex" % |
| 186 | (time.strftime("%%%s" % directive), |
| 187 | compiled.pattern)) |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 188 | |
| 189 | class StrptimeTests(unittest.TestCase): |
| 190 | """Tests for _strptime.strptime.""" |
| 191 | |
| 192 | def setUp(self): |
| 193 | """Create testing time tuple.""" |
| 194 | self.time_tuple = time.gmtime() |
| 195 | |
| 196 | def test_TypeError(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 197 | # Make sure ValueError is raised when match fails |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 198 | self.assertRaises(ValueError, _strptime.strptime, data_string="%d", |
| 199 | format="%A") |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 200 | |
| 201 | def test_returning_RE(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 202 | # Make sure that an re can be returned |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 203 | strp_output = _strptime.strptime(False, "%Y") |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 204 | self.failUnless(isinstance(strp_output, type(re.compile(''))), |
| 205 | "re object not returned correctly") |
| 206 | self.failUnless(_strptime.strptime("1999", strp_output), |
| 207 | "Use of re object failed") |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 208 | bad_locale_time = _strptime.LocaleTime(lang="gibberish") |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 209 | self.assertRaises(TypeError, _strptime.strptime, data_string='1999', |
| 210 | format=strp_output, locale_time=bad_locale_time) |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 211 | |
| 212 | def helper(self, directive, position): |
| 213 | """Helper fxn in testing.""" |
| 214 | strf_output = time.strftime("%%%s" % directive, self.time_tuple) |
| 215 | strp_output = _strptime.strptime(strf_output, "%%%s" % directive) |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 216 | self.failUnless(strp_output[position] == self.time_tuple[position], |
| 217 | "testing of '%s' directive failed; '%s' -> %s != %s" % |
| 218 | (directive, strf_output, strp_output[position], |
| 219 | self.time_tuple[position])) |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 220 | |
| 221 | def test_year(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 222 | # Test that the year is handled properly |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 223 | for directive in ('y', 'Y'): |
| 224 | self.helper(directive, 0) |
| 225 | |
| 226 | def test_month(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 227 | # Test for month directives |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 228 | for directive in ('B', 'b', 'm'): |
| 229 | self.helper(directive, 1) |
| 230 | |
| 231 | def test_day(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 232 | # Test for day directives |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 233 | self.helper('d', 2) |
| 234 | |
| 235 | def test_hour(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 236 | # Test hour directives |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 237 | self.helper('H', 3) |
| 238 | strf_output = time.strftime("%I %p", self.time_tuple) |
| 239 | strp_output = _strptime.strptime(strf_output, "%I %p") |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 240 | self.failUnless(strp_output[3] == self.time_tuple[3], |
| 241 | "testing of '%%I %%p' directive failed; '%s' -> %s != %s" % |
| 242 | (strf_output, strp_output[3], self.time_tuple[3])) |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 243 | |
| 244 | def test_minute(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 245 | # Test minute directives |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 246 | self.helper('M', 4) |
| 247 | |
| 248 | def test_second(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 249 | # Test second directives |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 250 | self.helper('S', 5) |
| 251 | |
| 252 | def test_weekday(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 253 | # Test weekday directives |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 254 | for directive in ('A', 'a', 'w'): |
| 255 | self.helper(directive,6) |
| 256 | |
| 257 | def test_julian(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 258 | # Test julian directives |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 259 | self.helper('j', 7) |
| 260 | |
| 261 | def test_timezone(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 262 | # Test timezone directives. |
| 263 | # When gmtime() is used with %Z, entire result of strftime() is empty. |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 264 | # Check for equal timezone names deals with bad locale info when this |
| 265 | # occurs; first found in FreeBSD 4.4 -current |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 266 | time_tuple = time.localtime() |
| 267 | strf_output = time.strftime("%Z") #UTC does not have a timezone |
| 268 | strp_output = _strptime.strptime(strf_output, "%Z") |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 269 | locale_time = _strptime.LocaleTime() |
| 270 | if locale_time.timezone[0] != locale_time.timezone[1]: |
| 271 | self.failUnless(strp_output[8] == time_tuple[8], |
| 272 | "timezone check failed; '%s' -> %s != %s" % |
| 273 | (strf_output, strp_output[8], time_tuple[8])) |
| 274 | else: |
| 275 | self.failUnless(strp_output[8] == -1, |
| 276 | "LocaleTime().timezone has duplicate values but " |
| 277 | "timzone value not set to -1") |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 278 | |
| 279 | def test_date_time(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 280 | # Test %c directive |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 281 | for position in range(6): |
| 282 | self.helper('c', position) |
| 283 | |
| 284 | def test_date(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 285 | # Test %x directive |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 286 | for position in range(0,3): |
| 287 | self.helper('x', position) |
| 288 | |
| 289 | def test_time(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 290 | # Test %X directive |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 291 | for position in range(3,6): |
| 292 | self.helper('X', position) |
| 293 | |
| 294 | def test_percent(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 295 | # Make sure % signs are handled properly |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 296 | strf_output = time.strftime("%m %% %Y", self.time_tuple) |
| 297 | strp_output = _strptime.strptime(strf_output, "%m %% %Y") |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 298 | self.failUnless(strp_output[0] == self.time_tuple[0] and |
| 299 | strp_output[1] == self.time_tuple[1], |
| 300 | "handling of percent sign failed") |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 301 | |
Barry Warsaw | 4d895fa | 2002-09-23 22:46:49 +0000 | [diff] [blame] | 302 | def test_caseinsensitive(self): |
| 303 | # Should handle names case-insensitively. |
| 304 | strf_output = time.strftime("%B", self.time_tuple) |
| 305 | self.failUnless(_strptime.strptime(strf_output.upper(), "%B"), |
| 306 | "strptime does not handle ALL-CAPS names properly") |
| 307 | self.failUnless(_strptime.strptime(strf_output.lower(), "%B"), |
| 308 | "strptime does not handle lowercase names properly") |
| 309 | self.failUnless(_strptime.strptime(strf_output.capitalize(), "%B"), |
| 310 | "strptime does not handle capword names properly") |
| 311 | |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 312 | class FxnTests(unittest.TestCase): |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 313 | """Test functions that fill in info by validating result and are triggered |
| 314 | properly.""" |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 315 | |
| 316 | def setUp(self): |
| 317 | """Create an initial time tuple.""" |
| 318 | self.time_tuple = time.gmtime() |
| 319 | |
| 320 | def test_julianday_result(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 321 | # Test julianday |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 322 | result = _strptime.julianday(self.time_tuple[0], self.time_tuple[1], |
| 323 | self.time_tuple[2]) |
| 324 | self.failUnless(result == self.time_tuple[7], |
| 325 | "julianday failed; %s != %s" % |
| 326 | (result, self.time_tuple[7])) |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 327 | |
| 328 | def test_julianday_trigger(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 329 | # Make sure julianday is called |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 330 | strf_output = time.strftime("%Y-%m-%d", self.time_tuple) |
| 331 | strp_output = _strptime.strptime(strf_output, "%Y-%m-%d") |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 332 | self.failUnless(strp_output[7] == self.time_tuple[7], |
| 333 | "strptime did not trigger julianday(); %s != %s" % |
| 334 | (strp_output[7], self.time_tuple[7])) |
Tim Peters | 469cdad | 2002-08-08 20:19:19 +0000 | [diff] [blame] | 335 | |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 336 | def test_gregorian_result(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 337 | # Test gregorian |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 338 | result = _strptime.gregorian(self.time_tuple[7], self.time_tuple[0]) |
| 339 | comparison = [self.time_tuple[0], self.time_tuple[1], self.time_tuple[2]] |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 340 | self.failUnless(result == comparison, |
| 341 | "gregorian() failed; %s != %s" % (result, comparison)) |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 342 | |
| 343 | def test_gregorian_trigger(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 344 | # Test that gregorian() is triggered |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 345 | strf_output = time.strftime("%j %Y", self.time_tuple) |
| 346 | strp_output = _strptime.strptime(strf_output, "%j %Y") |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 347 | self.failUnless(strp_output[1] == self.time_tuple[1] and |
| 348 | strp_output[2] == self.time_tuple[2], |
| 349 | "gregorian() not triggered; month -- %s != %s, " |
| 350 | "day -- %s != %s" % |
| 351 | (strp_output[1], self.time_tuple[1], strp_output[2], |
| 352 | self.time_tuple[2])) |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 353 | |
| 354 | def test_dayofweek_result(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 355 | # Test dayofweek |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 356 | result = _strptime.dayofweek(self.time_tuple[0], self.time_tuple[1], |
| 357 | self.time_tuple[2]) |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 358 | comparison = self.time_tuple[6] |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 359 | self.failUnless(result == comparison, |
| 360 | "dayofweek() failed; %s != %s" % (result, comparison)) |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 361 | |
| 362 | def test_dayofweek_trigger(self): |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 363 | # Make sure dayofweek() gets triggered |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 364 | strf_output = time.strftime("%Y-%m-%d", self.time_tuple) |
| 365 | strp_output = _strptime.strptime(strf_output, "%Y-%m-%d") |
Martin v. Löwis | e16e01f | 2002-11-27 08:30:25 +0000 | [diff] [blame] | 366 | self.failUnless(strp_output[6] == self.time_tuple[6], |
| 367 | "triggering of dayofweek() failed; %s != %s" % |
| 368 | (strp_output[6], self.time_tuple[6])) |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 369 | |
| 370 | |
Barry Warsaw | 375e0ee | 2002-08-29 15:25:04 +0000 | [diff] [blame] | 371 | class Strptime12AMPMTests(unittest.TestCase): |
| 372 | """Test a _strptime regression in '%I %p' at 12 noon (12 PM)""" |
| 373 | |
| 374 | def test_twelve_noon_midnight(self): |
| 375 | eq = self.assertEqual |
| 376 | eq(time.strptime('12 PM', '%I %p')[3], 12) |
| 377 | eq(time.strptime('12 AM', '%I %p')[3], 0) |
| 378 | eq(_strptime.strptime('12 PM', '%I %p')[3], 12) |
| 379 | eq(_strptime.strptime('12 AM', '%I %p')[3], 0) |
| 380 | |
| 381 | |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 382 | def test_main(): |
| 383 | suite = unittest.TestSuite() |
| 384 | suite.addTest(unittest.makeSuite(LocaleTime_Tests)) |
| 385 | suite.addTest(unittest.makeSuite(TimeRETests)) |
| 386 | suite.addTest(unittest.makeSuite(StrptimeTests)) |
| 387 | suite.addTest(unittest.makeSuite(FxnTests)) |
Barry Warsaw | 375e0ee | 2002-08-29 15:25:04 +0000 | [diff] [blame] | 388 | suite.addTest(unittest.makeSuite(Strptime12AMPMTests)) |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 389 | test_support.run_suite(suite) |
Guido van Rossum | 00efe7e | 2002-07-19 17:04:46 +0000 | [diff] [blame] | 390 | |
| 391 | |
| 392 | if __name__ == '__main__': |
Guido van Rossum | a5ce2e8 | 2002-08-22 19:57:50 +0000 | [diff] [blame] | 393 | test_main() |