Changeset 391 for python/trunk/Lib/test/test_strptime.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/test/test_strptime.py
r2 r391 14 14 """Test _getlang""" 15 15 def test_basic(self): 16 self. failUnlessEqual(_strptime._getlang(), locale.getlocale(locale.LC_TIME))16 self.assertEqual(_strptime._getlang(), locale.getlocale(locale.LC_TIME)) 17 17 18 18 class LocaleTime_Tests(unittest.TestCase): … … 37 37 strftime_output = time.strftime(directive, self.time_tuple).lower() 38 38 comparison = testing[self.time_tuple[tuple_position]] 39 self. failUnless(strftime_output in testing, "%s: not found in tuple" %40 41 self. failUnless(comparison ==strftime_output,42 "%s: position within tuple incorrect; %s != %s" %43 (error_msg, comparison, strftime_output))39 self.assertIn(strftime_output, testing, 40 "%s: not found in tuple" % error_msg) 41 self.assertEqual(comparison, strftime_output, 42 "%s: position within tuple incorrect; %s != %s" % 43 (error_msg, comparison, strftime_output)) 44 44 45 45 def test_weekday(self): … … 62 62 # Make sure AM/PM representation done properly 63 63 strftime_output = time.strftime("%p", self.time_tuple).lower() 64 self. failUnless(strftime_output inself.LT_ins.am_pm,65 64 self.assertIn(strftime_output, self.LT_ins.am_pm, 65 "AM/PM representation not in tuple") 66 66 if self.time_tuple[3] < 12: position = 0 67 67 else: position = 1 68 self. failUnless(strftime_output == self.LT_ins.am_pm[position],69 "AM/PM representation in the wrong position within the tuple")68 self.assertEqual(self.LT_ins.am_pm[position], strftime_output, 69 "AM/PM representation in the wrong position within the tuple") 70 70 71 71 def test_timezone(self): … … 73 73 timezone = time.strftime("%Z", self.time_tuple).lower() 74 74 if timezone: 75 self. failUnless(timezone in self.LT_ins.timezone[0] or \75 self.assertTrue(timezone in self.LT_ins.timezone[0] or 76 76 timezone in self.LT_ins.timezone[1], 77 77 "timezone %s not found in %s" % … … 87 87 magic_date = (1999, 3, 17, 22, 44, 55, 2, 76, 0) 88 88 strftime_output = time.strftime("%c", magic_date) 89 self.failUnless(strftime_output == time.strftime(self.LT_ins.LC_date_time, 90 magic_date), 91 "LC_date_time incorrect") 89 self.assertEqual(time.strftime(self.LT_ins.LC_date_time, magic_date), 90 strftime_output, "LC_date_time incorrect") 92 91 strftime_output = time.strftime("%x", magic_date) 93 self.failUnless(strftime_output == time.strftime(self.LT_ins.LC_date, 94 magic_date), 95 "LC_date incorrect") 92 self.assertEqual(time.strftime(self.LT_ins.LC_date, magic_date), 93 strftime_output, "LC_date incorrect") 96 94 strftime_output = time.strftime("%X", magic_date) 97 self.failUnless(strftime_output == time.strftime(self.LT_ins.LC_time, 98 magic_date), 99 "LC_time incorrect") 95 self.assertEqual(time.strftime(self.LT_ins.LC_time, magic_date), 96 strftime_output, "LC_time incorrect") 100 97 LT = _strptime.LocaleTime() 101 98 LT.am_pm = ('', '') 102 self. failUnless(LT.LC_time, "LocaleTime's LC directives cannot handle "99 self.assertTrue(LT.LC_time, "LocaleTime's LC directives cannot handle " 103 100 "empty strings") 104 101 … … 106 103 # Make sure lang is set to what _getlang() returns 107 104 # Assuming locale has not changed between now and when self.LT_ins was created 108 self. failUnlessEqual(self.LT_ins.lang, _strptime._getlang())105 self.assertEqual(self.LT_ins.lang, _strptime._getlang()) 109 106 110 107 … … 120 117 # Test TimeRE.pattern 121 118 pattern_string = self.time_re.pattern(r"%a %A %d") 122 self. failUnless(pattern_string.find(self.locale_time.a_weekday[2]) != -1,119 self.assertTrue(pattern_string.find(self.locale_time.a_weekday[2]) != -1, 123 120 "did not find abbreviated weekday in pattern string '%s'" % 124 121 pattern_string) 125 self. failUnless(pattern_string.find(self.locale_time.f_weekday[4]) != -1,122 self.assertTrue(pattern_string.find(self.locale_time.f_weekday[4]) != -1, 126 123 "did not find full weekday in pattern string '%s'" % 127 124 pattern_string) 128 self. failUnless(pattern_string.find(self.time_re['d']) != -1,125 self.assertTrue(pattern_string.find(self.time_re['d']) != -1, 129 126 "did not find 'd' directive pattern string '%s'" % 130 127 pattern_string) … … 134 131 # regex syntax is escaped. 135 132 pattern_string = self.time_re.pattern("\d+") 136 self. failUnless(r"\\d\+" inpattern_string,137 138 133 self.assertIn(r"\\d\+", pattern_string, 134 "%s does not have re characters escaped properly" % 135 pattern_string) 139 136 140 137 def test_compile(self): 141 138 # Check that compiled regex is correct 142 139 found = self.time_re.compile(r"%A").match(self.locale_time.f_weekday[6]) 143 self. failUnless(found and found.group('A') == self.locale_time.f_weekday[6],140 self.assertTrue(found and found.group('A') == self.locale_time.f_weekday[6], 144 141 "re object for '%A' failed") 145 142 compiled = self.time_re.compile(r"%a %b") 146 143 found = compiled.match("%s %s" % (self.locale_time.a_weekday[4], 147 144 self.locale_time.a_month[4])) 148 self. failUnless(found,145 self.assertTrue(found, 149 146 "Match failed with '%s' regex and '%s' string" % 150 147 (compiled.pattern, "%s %s" % (self.locale_time.a_weekday[4], 151 148 self.locale_time.a_month[4]))) 152 self. failUnless(found.group('a') == self.locale_time.a_weekday[4] and149 self.assertTrue(found.group('a') == self.locale_time.a_weekday[4] and 153 150 found.group('b') == self.locale_time.a_month[4], 154 151 "re object couldn't find the abbreviated weekday month in " … … 160 157 compiled = self.time_re.compile("%" + directive) 161 158 found = compiled.match(time.strftime("%" + directive)) 162 self. failUnless(found, "Matching failed on '%s' using '%s' regex" %159 self.assertTrue(found, "Matching failed on '%s' using '%s' regex" % 163 160 (time.strftime("%" + directive), 164 161 compiled.pattern)) … … 169 166 test_locale = _strptime.LocaleTime() 170 167 test_locale.timezone = (frozenset(), frozenset()) 171 self. failUnless(_strptime.TimeRE(test_locale).pattern("%Z") =='',172 "with timezone == ('',''), TimeRE().pattern('%Z') != ''")168 self.assertEqual(_strptime.TimeRE(test_locale).pattern("%Z"), '', 169 "with timezone == ('',''), TimeRE().pattern('%Z') != ''") 173 170 174 171 def test_matching_with_escapes(self): … … 176 173 compiled_re = self.time_re.compile("\w+ %m") 177 174 found = compiled_re.match("\w+ 10") 178 self. failUnless(found, "Escaping failed of format '\w+ 10'")175 self.assertTrue(found, "Escaping failed of format '\w+ 10'") 179 176 180 177 def test_locale_data_w_regex_metacharacters(self): … … 187 184 frozenset("Tokyo (daylight time)")) 188 185 time_re = _strptime.TimeRE(locale_time) 189 self. failUnless(time_re.compile("%Z").match("Tokyo (standard time)"),186 self.assertTrue(time_re.compile("%Z").match("Tokyo (standard time)"), 190 187 "locale data that contains regex metacharacters is not" 191 188 " properly escaped") … … 196 193 # "steal" characters from each other. 197 194 pattern = self.time_re.pattern('%j %H') 198 self. failUnless(notre.match(pattern, "180"))199 self. failUnless(re.match(pattern, "18 0"))195 self.assertFalse(re.match(pattern, "180")) 196 self.assertTrue(re.match(pattern, "18 0")) 200 197 201 198 … … 230 227 strf_output = time.strftime("%" + directive, self.time_tuple) 231 228 strp_output = _strptime._strptime_time(strf_output, "%" + directive) 232 self. failUnless(strp_output[position] == self.time_tuple[position],229 self.assertTrue(strp_output[position] == self.time_tuple[position], 233 230 "testing of '%s' directive failed; '%s' -> %s != %s" % 234 231 (directive, strf_output, strp_output[position], … … 244 241 strp_output = _strptime._strptime_time(bound, '%y') 245 242 expected_result = century + int(bound) 246 self. failUnless(strp_output[0] == expected_result,243 self.assertTrue(strp_output[0] == expected_result, 247 244 "'y' test failed; passed in '%s' " 248 245 "and returned '%s'" % (bound, strp_output[0])) … … 262 259 strf_output = time.strftime("%I %p", self.time_tuple) 263 260 strp_output = _strptime._strptime_time(strf_output, "%I %p") 264 self. failUnless(strp_output[3] == self.time_tuple[3],261 self.assertTrue(strp_output[3] == self.time_tuple[3], 265 262 "testing of '%%I %%p' directive failed; '%s' -> %s != %s" % 266 263 (strf_output, strp_output[3], self.time_tuple[3])) … … 296 293 # occurs; first found in FreeBSD 4.4. 297 294 strp_output = _strptime._strptime_time("UTC", "%Z") 298 self. failUnlessEqual(strp_output.tm_isdst, 0)295 self.assertEqual(strp_output.tm_isdst, 0) 299 296 strp_output = _strptime._strptime_time("GMT", "%Z") 300 self.failUnlessEqual(strp_output.tm_isdst, 0) 301 if sys.platform == "mac": 302 # Timezones don't really work on MacOS9 303 return 297 self.assertEqual(strp_output.tm_isdst, 0) 304 298 time_tuple = time.localtime() 305 299 strf_output = time.strftime("%Z") #UTC does not have a timezone … … 307 301 locale_time = _strptime.LocaleTime() 308 302 if time.tzname[0] != time.tzname[1] or not time.daylight: 309 self. failUnless(strp_output[8] == time_tuple[8],303 self.assertTrue(strp_output[8] == time_tuple[8], 310 304 "timezone check failed; '%s' -> %s != %s" % 311 305 (strf_output, strp_output[8], time_tuple[8])) 312 306 else: 313 self. failUnless(strp_output[8] == -1,307 self.assertTrue(strp_output[8] == -1, 314 308 "LocaleTime().timezone has duplicate values and " 315 309 "time.daylight but timezone value not set to -1") … … 318 312 # Explicitly test possibility of bad timezone; 319 313 # when time.tzname[0] == time.tzname[1] and time.daylight 320 if sys.platform == "mac":321 return #MacOS9 has severely broken timezone support.322 314 tz_name = time.tzname[0] 323 315 if tz_name.upper() in ("UTC", "GMT"): … … 329 321 time.daylight = 1 330 322 tz_value = _strptime._strptime_time(tz_name, "%Z")[8] 331 self. failUnlessEqual(tz_value, -1,323 self.assertEqual(tz_value, -1, 332 324 "%s lead to a timezone value of %s instead of -1 when " 333 325 "time.daylight set to %s and passing in %s" % … … 356 348 strf_output = time.strftime("%m %% %Y", self.time_tuple) 357 349 strp_output = _strptime._strptime_time(strf_output, "%m %% %Y") 358 self. failUnless(strp_output[0] == self.time_tuple[0] and350 self.assertTrue(strp_output[0] == self.time_tuple[0] and 359 351 strp_output[1] == self.time_tuple[1], 360 352 "handling of percent sign failed") … … 363 355 # Should handle names case-insensitively. 364 356 strf_output = time.strftime("%B", self.time_tuple) 365 self. failUnless(_strptime._strptime_time(strf_output.upper(), "%B"),357 self.assertTrue(_strptime._strptime_time(strf_output.upper(), "%B"), 366 358 "strptime does not handle ALL-CAPS names properly") 367 self. failUnless(_strptime._strptime_time(strf_output.lower(), "%B"),359 self.assertTrue(_strptime._strptime_time(strf_output.lower(), "%B"), 368 360 "strptime does not handle lowercase names properly") 369 self. failUnless(_strptime._strptime_time(strf_output.capitalize(), "%B"),361 self.assertTrue(_strptime._strptime_time(strf_output.capitalize(), "%B"), 370 362 "strptime does not handle capword names properly") 371 363 … … 374 366 defaults = (1900, 1, 1, 0, 0, 0, 0, 1, -1) 375 367 strp_output = _strptime._strptime_time('1', '%m') 376 self. failUnless(strp_output == defaults,368 self.assertTrue(strp_output == defaults, 377 369 "Default values for strptime() are incorrect;" 378 370 " %s != %s" % (strp_output, defaults)) … … 385 377 # Test instigated by bug #796149 . 386 378 need_escaping = ".^$*+?{}\[]|)(" 387 self.failUnless(_strptime._strptime_time(need_escaping, need_escaping)) 379 self.assertTrue(_strptime._strptime_time(need_escaping, need_escaping)) 380 381 def test_feb29_on_leap_year_without_year(self): 382 time.strptime("Feb 29", "%b %d") 383 384 def test_mar1_comes_after_feb29_even_when_omitting_the_year(self): 385 self.assertLess( 386 time.strptime("Feb 29", "%b %d"), 387 time.strptime("Mar 1", "%b %d")) 388 388 389 389 class Strptime12AMPMTests(unittest.TestCase): … … 418 418 result = _strptime._strptime_time(time.strftime(format_string, self.time_tuple), 419 419 format_string) 420 self. failUnless(result.tm_yday == self.time_tuple.tm_yday,420 self.assertTrue(result.tm_yday == self.time_tuple.tm_yday, 421 421 "Calculation of tm_yday failed; %s != %s" % 422 422 (result.tm_yday, self.time_tuple.tm_yday)) … … 427 427 result = _strptime._strptime_time(time.strftime(format_string, self.time_tuple), 428 428 format_string) 429 self. failUnless(result.tm_year == self.time_tuple.tm_year and429 self.assertTrue(result.tm_year == self.time_tuple.tm_year and 430 430 result.tm_mon == self.time_tuple.tm_mon and 431 431 result.tm_mday == self.time_tuple.tm_mday, … … 441 441 result = _strptime._strptime_time(time.strftime(format_string, self.time_tuple), 442 442 format_string) 443 self. failUnless(result.tm_wday == self.time_tuple.tm_wday,443 self.assertTrue(result.tm_wday == self.time_tuple.tm_wday, 444 444 "Calculation of day of the week failed;" 445 445 "%s != %s" % (result.tm_wday, self.time_tuple.tm_wday)) … … 454 454 strp_input = dt_date.strftime(format_string) 455 455 strp_output = _strptime._strptime_time(strp_input, format_string) 456 self. failUnless(strp_output[:3] == ymd_tuple,456 self.assertTrue(strp_output[:3] == ymd_tuple, 457 457 "%s(%s) test failed w/ '%s': %s != %s (%s != %s)" % 458 458 (test_reason, directive, strp_input, … … 495 495 _strptime._strptime_time("2005", "%Y") 496 496 _strptime._TimeRE_cache.locale_time.lang = "Ni" 497 original_time_re = id(_strptime._TimeRE_cache)497 original_time_re = _strptime._TimeRE_cache 498 498 _strptime._strptime_time("10", "%d") 499 self. failIfEqual(original_time_re, id(_strptime._TimeRE_cache))500 self. failUnlessEqual(len(_strptime._regex_cache), 1)499 self.assertIsNot(original_time_re, _strptime._TimeRE_cache) 500 self.assertEqual(len(_strptime._regex_cache), 1) 501 501 502 502 def test_regex_cleanup(self): … … 511 511 bogus_key += 1 512 512 _strptime._strptime_time("10", "%d") 513 self. failUnlessEqual(len(_strptime._regex_cache), 1)513 self.assertEqual(len(_strptime._regex_cache), 1) 514 514 515 515 def test_new_localetime(self): 516 516 # A new LocaleTime instance should be created when a new TimeRE object 517 517 # is created. 518 locale_time_id = id(_strptime._TimeRE_cache.locale_time)518 locale_time_id = _strptime._TimeRE_cache.locale_time 519 519 _strptime._TimeRE_cache.locale_time.lang = "Ni" 520 520 _strptime._strptime_time("10", "%d") 521 self.failIfEqual(locale_time_id, 522 id(_strptime._TimeRE_cache.locale_time)) 521 self.assertIsNot(locale_time_id, _strptime._TimeRE_cache.locale_time) 523 522 524 523 def test_TimeRE_recreation(self): … … 532 531 _strptime._strptime_time('10', '%d') 533 532 # Get id of current cache object. 534 first_time_re _id = id(_strptime._TimeRE_cache)533 first_time_re = _strptime._TimeRE_cache 535 534 try: 536 535 # Change the locale and force a recreation of the cache. … … 538 537 _strptime._strptime_time('10', '%d') 539 538 # Get the new cache object's id. 540 second_time_re _id = id(_strptime._TimeRE_cache)539 second_time_re = _strptime._TimeRE_cache 541 540 # They should not be equal. 542 self. failIfEqual(first_time_re_id, second_time_re_id)541 self.assertIsNot(first_time_re, second_time_re) 543 542 # Possible test locale is not supported while initial locale is. 544 543 # If this is the case just suppress the exception and fall-through 545 # to the reset ing to the original locale.544 # to the resetting to the original locale. 546 545 except locale.Error: 547 546 pass
Note:
See TracChangeset
for help on using the changeset viewer.