Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(94720)

Side by Side Diff: Lib/test/test_strptime.py

Issue 16510: Using appropriate checks in tests
Patch Set: Created 5 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_ssl.py ('k') | Lib/test/test_symtable.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """PyUnit testing against strptime""" 1 """PyUnit testing against strptime"""
2 2
3 import unittest 3 import unittest
4 import time 4 import time
5 import locale 5 import locale
6 import re 6 import re
7 import sys 7 import sys
8 from test import support 8 from test import support
9 from datetime import date as datetime_date 9 from datetime import date as datetime_date
10 10
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 """Tests for TimeRE.""" 109 """Tests for TimeRE."""
110 110
111 def setUp(self): 111 def setUp(self):
112 """Construct generic TimeRE object.""" 112 """Construct generic TimeRE object."""
113 self.time_re = _strptime.TimeRE() 113 self.time_re = _strptime.TimeRE()
114 self.locale_time = _strptime.LocaleTime() 114 self.locale_time = _strptime.LocaleTime()
115 115
116 def test_pattern(self): 116 def test_pattern(self):
117 # Test TimeRE.pattern 117 # Test TimeRE.pattern
118 pattern_string = self.time_re.pattern(r"%a %A %d") 118 pattern_string = self.time_re.pattern(r"%a %A %d")
119 self.assertTrue(pattern_string.find(self.locale_time.a_weekday[2]) != -1 , 119 self.assertNotEqual(pattern_string.find(self.locale_time.a_weekday[2]), -1,
120 "did not find abbreviated weekday in pattern string '%s' " % 120 "did not find abbreviated weekday in pattern string '%s' " %
121 pattern_string) 121 pattern_string)
122 self.assertTrue(pattern_string.find(self.locale_time.f_weekday[4]) != -1 , 122 self.assertNotEqual(pattern_string.find(self.locale_time.f_weekday[4]), -1,
123 "did not find full weekday in pattern string '%s'" % 123 "did not find full weekday in pattern string '%s'" %
124 pattern_string) 124 pattern_string)
125 self.assertTrue(pattern_string.find(self.time_re['d']) != -1, 125 self.assertNotEqual(pattern_string.find(self.time_re['d']), -1,
126 "did not find 'd' directive pattern string '%s'" % 126 "did not find 'd' directive pattern string '%s'" %
127 pattern_string) 127 pattern_string)
128 128
129 def test_pattern_escaping(self): 129 def test_pattern_escaping(self):
130 # Make sure any characters in the format string that might be taken as 130 # Make sure any characters in the format string that might be taken as
131 # regex syntax is escaped. 131 # regex syntax is escaped.
132 pattern_string = self.time_re.pattern("\d+") 132 pattern_string = self.time_re.pattern("\d+")
133 self.assertIn(r"\\d\+", pattern_string, 133 self.assertIn(r"\\d\+", pattern_string,
134 "%s does not have re characters escaped properly" % 134 "%s does not have re characters escaped properly" %
135 pattern_string) 135 pattern_string)
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 243
244 def test_year(self): 244 def test_year(self):
245 # Test that the year is handled properly 245 # Test that the year is handled properly
246 for directive in ('y', 'Y'): 246 for directive in ('y', 'Y'):
247 self.helper(directive, 0) 247 self.helper(directive, 0)
248 # Must also make sure %y values are correct for bounds set by Open Group 248 # Must also make sure %y values are correct for bounds set by Open Group
249 for century, bounds in ((1900, ('69', '99')), (2000, ('00', '68'))): 249 for century, bounds in ((1900, ('69', '99')), (2000, ('00', '68'))):
250 for bound in bounds: 250 for bound in bounds:
251 strp_output = _strptime._strptime_time(bound, '%y') 251 strp_output = _strptime._strptime_time(bound, '%y')
252 expected_result = century + int(bound) 252 expected_result = century + int(bound)
253 self.assertTrue(strp_output[0] == expected_result, 253 self.assertEqual(strp_output[0], expected_result,
254 "'y' test failed; passed in '%s' " 254 "'y' test failed; passed in '%s' "
255 "and returned '%s'" % (bound, strp_output[0])) 255 "and returned '%s'" % (bound, strp_output[0]))
256 256
257 def test_month(self): 257 def test_month(self):
258 # Test for month directives 258 # Test for month directives
259 for directive in ('B', 'b', 'm'): 259 for directive in ('B', 'b', 'm'):
260 self.helper(directive, 1) 260 self.helper(directive, 1)
261 261
262 def test_day(self): 262 def test_day(self):
263 # Test for day directives 263 # Test for day directives
264 self.helper('d', 2) 264 self.helper('d', 2)
265 265
266 def test_hour(self): 266 def test_hour(self):
267 # Test hour directives 267 # Test hour directives
268 self.helper('H', 3) 268 self.helper('H', 3)
269 strf_output = time.strftime("%I %p", self.time_tuple) 269 strf_output = time.strftime("%I %p", self.time_tuple)
270 strp_output = _strptime._strptime_time(strf_output, "%I %p") 270 strp_output = _strptime._strptime_time(strf_output, "%I %p")
271 self.assertTrue(strp_output[3] == self.time_tuple[3], 271 self.assertEqual(strp_output[3], self.time_tuple[3],
272 "testing of '%%I %%p' directive failed; '%s' -> %s != %s " % 272 "testing of '%%I %%p' directive failed; '%s' -> %s != %s " %
273 (strf_output, strp_output[3], self.time_tuple[3])) 273 (strf_output, strp_output[3], self.time_tuple[3]))
274 274
275 def test_minute(self): 275 def test_minute(self):
276 # Test minute directives 276 # Test minute directives
277 self.helper('M', 4) 277 self.helper('M', 4)
278 278
279 def test_second(self): 279 def test_second(self):
280 # Test second directives 280 # Test second directives
281 self.helper('S', 5) 281 self.helper('S', 5)
(...skipping 21 matching lines...) Expand all
303 # occurs; first found in FreeBSD 4.4. 303 # occurs; first found in FreeBSD 4.4.
304 strp_output = _strptime._strptime_time("UTC", "%Z") 304 strp_output = _strptime._strptime_time("UTC", "%Z")
305 self.assertEqual(strp_output.tm_isdst, 0) 305 self.assertEqual(strp_output.tm_isdst, 0)
306 strp_output = _strptime._strptime_time("GMT", "%Z") 306 strp_output = _strptime._strptime_time("GMT", "%Z")
307 self.assertEqual(strp_output.tm_isdst, 0) 307 self.assertEqual(strp_output.tm_isdst, 0)
308 time_tuple = time.localtime() 308 time_tuple = time.localtime()
309 strf_output = time.strftime("%Z") #UTC does not have a timezone 309 strf_output = time.strftime("%Z") #UTC does not have a timezone
310 strp_output = _strptime._strptime_time(strf_output, "%Z") 310 strp_output = _strptime._strptime_time(strf_output, "%Z")
311 locale_time = _strptime.LocaleTime() 311 locale_time = _strptime.LocaleTime()
312 if time.tzname[0] != time.tzname[1] or not time.daylight: 312 if time.tzname[0] != time.tzname[1] or not time.daylight:
313 self.assertTrue(strp_output[8] == time_tuple[8], 313 self.assertEqual(strp_output[8], time_tuple[8],
314 "timezone check failed; '%s' -> %s != %s" % 314 "timezone check failed; '%s' -> %s != %s" %
315 (strf_output, strp_output[8], time_tuple[8])) 315 (strf_output, strp_output[8], time_tuple[8]))
316 else: 316 else:
317 self.assertTrue(strp_output[8] == -1, 317 self.assertEqual(strp_output[8], -1,
318 "LocaleTime().timezone has duplicate values and " 318 "LocaleTime().timezone has duplicate values and "
319 "time.daylight but timezone value not set to -1") 319 "time.daylight but timezone value not set to -1")
320 320
321 def test_bad_timezone(self): 321 def test_bad_timezone(self):
322 # Explicitly test possibility of bad timezone; 322 # Explicitly test possibility of bad timezone;
323 # when time.tzname[0] == time.tzname[1] and time.daylight 323 # when time.tzname[0] == time.tzname[1] and time.daylight
324 tz_name = time.tzname[0] 324 tz_name = time.tzname[0]
325 if tz_name.upper() in ("UTC", "GMT"): 325 if tz_name.upper() in ("UTC", "GMT"):
326 self.skipTest('need non-UTC/GMT timezone') 326 self.skipTest('need non-UTC/GMT timezone')
327 try: 327 try:
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 "strptime does not handle ALL-CAPS names properly") 368 "strptime does not handle ALL-CAPS names properly")
369 self.assertTrue(_strptime._strptime_time(strf_output.lower(), "%B"), 369 self.assertTrue(_strptime._strptime_time(strf_output.lower(), "%B"),
370 "strptime does not handle lowercase names properly") 370 "strptime does not handle lowercase names properly")
371 self.assertTrue(_strptime._strptime_time(strf_output.capitalize(), "%B") , 371 self.assertTrue(_strptime._strptime_time(strf_output.capitalize(), "%B") ,
372 "strptime does not handle capword names properly") 372 "strptime does not handle capword names properly")
373 373
374 def test_defaults(self): 374 def test_defaults(self):
375 # Default return value should be (1900, 1, 1, 0, 0, 0, 0, 1, 0) 375 # Default return value should be (1900, 1, 1, 0, 0, 0, 0, 1, 0)
376 defaults = (1900, 1, 1, 0, 0, 0, 0, 1, -1) 376 defaults = (1900, 1, 1, 0, 0, 0, 0, 1, -1)
377 strp_output = _strptime._strptime_time('1', '%m') 377 strp_output = _strptime._strptime_time('1', '%m')
378 self.assertTrue(strp_output == defaults, 378 self.assertEqual(strp_output, defaults,
379 "Default values for strptime() are incorrect;" 379 "Default values for strptime() are incorrect;"
380 " %s != %s" % (strp_output, defaults)) 380 " %s != %s" % (strp_output, defaults))
381 381
382 def test_escaping(self): 382 def test_escaping(self):
383 # Make sure all characters that have regex significance are escaped. 383 # Make sure all characters that have regex significance are escaped.
384 # Parentheses are in a purposeful order; will cause an error of 384 # Parentheses are in a purposeful order; will cause an error of
385 # unbalanced parentheses when the regex is compiled if they are not 385 # unbalanced parentheses when the regex is compiled if they are not
386 # escaped. 386 # escaped.
387 # Test instigated by bug #796149 . 387 # Test instigated by bug #796149 .
388 need_escaping = ".^$*+?{}\[]|)(" 388 need_escaping = ".^$*+?{}\[]|)("
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 """Test that strptime() fills in missing info correctly""" 420 """Test that strptime() fills in missing info correctly"""
421 421
422 def setUp(self): 422 def setUp(self):
423 self.time_tuple = time.gmtime() 423 self.time_tuple = time.gmtime()
424 424
425 def test_julian_calculation(self): 425 def test_julian_calculation(self):
426 # Make sure that when Julian is missing that it is calculated 426 # Make sure that when Julian is missing that it is calculated
427 format_string = "%Y %m %d %H %M %S %w %Z" 427 format_string = "%Y %m %d %H %M %S %w %Z"
428 result = _strptime._strptime_time(time.strftime(format_string, self.time _tuple), 428 result = _strptime._strptime_time(time.strftime(format_string, self.time _tuple),
429 format_string) 429 format_string)
430 self.assertTrue(result.tm_yday == self.time_tuple.tm_yday, 430 self.assertEqual(result.tm_yday, self.time_tuple.tm_yday,
431 "Calculation of tm_yday failed; %s != %s" % 431 "Calculation of tm_yday failed; %s != %s" %
432 (result.tm_yday, self.time_tuple.tm_yday)) 432 (result.tm_yday, self.time_tuple.tm_yday))
433 433
434 def test_gregorian_calculation(self): 434 def test_gregorian_calculation(self):
435 # Test that Gregorian date can be calculated from Julian day 435 # Test that Gregorian date can be calculated from Julian day
436 format_string = "%Y %H %M %S %w %j %Z" 436 format_string = "%Y %H %M %S %w %j %Z"
437 result = _strptime._strptime_time(time.strftime(format_string, self.time _tuple), 437 result = _strptime._strptime_time(time.strftime(format_string, self.time _tuple),
438 format_string) 438 format_string)
439 self.assertTrue(result.tm_year == self.time_tuple.tm_year and 439 self.assertTrue(result.tm_year == self.time_tuple.tm_year and
440 result.tm_mon == self.time_tuple.tm_mon and 440 result.tm_mon == self.time_tuple.tm_mon and
441 result.tm_mday == self.time_tuple.tm_mday, 441 result.tm_mday == self.time_tuple.tm_mday,
442 "Calculation of Gregorian date failed;" 442 "Calculation of Gregorian date failed;"
443 "%s-%s-%s != %s-%s-%s" % 443 "%s-%s-%s != %s-%s-%s" %
444 (result.tm_year, result.tm_mon, result.tm_mday, 444 (result.tm_year, result.tm_mon, result.tm_mday,
445 self.time_tuple.tm_year, self.time_tuple.tm_mon, 445 self.time_tuple.tm_year, self.time_tuple.tm_mon,
446 self.time_tuple.tm_mday)) 446 self.time_tuple.tm_mday))
447 447
448 def test_day_of_week_calculation(self): 448 def test_day_of_week_calculation(self):
449 # Test that the day of the week is calculated as needed 449 # Test that the day of the week is calculated as needed
450 format_string = "%Y %m %d %H %S %j %Z" 450 format_string = "%Y %m %d %H %S %j %Z"
451 result = _strptime._strptime_time(time.strftime(format_string, self.time _tuple), 451 result = _strptime._strptime_time(time.strftime(format_string, self.time _tuple),
452 format_string) 452 format_string)
453 self.assertTrue(result.tm_wday == self.time_tuple.tm_wday, 453 self.assertEqual(result.tm_wday, self.time_tuple.tm_wday,
454 "Calculation of day of the week failed;" 454 "Calculation of day of the week failed;"
455 "%s != %s" % (result.tm_wday, self.time_tuple.tm_wday)) 455 "%s != %s" % (result.tm_wday, self.time_tuple.tm_wday))
456 456
457 def test_week_of_year_and_day_of_week_calculation(self): 457 def test_week_of_year_and_day_of_week_calculation(self):
458 # Should be able to infer date if given year, week of year (%U or %W) 458 # Should be able to infer date if given year, week of year (%U or %W)
459 # and day of the week 459 # and day of the week
460 def test_helper(ymd_tuple, test_reason): 460 def test_helper(ymd_tuple, test_reason):
461 for directive in ('W', 'U'): 461 for directive in ('W', 'U'):
462 format_string = "%%Y %%%s %%w" % directive 462 format_string = "%%Y %%%s %%w" % directive
463 dt_date = datetime_date(*ymd_tuple) 463 dt_date = datetime_date(*ymd_tuple)
464 strp_input = dt_date.strftime(format_string) 464 strp_input = dt_date.strftime(format_string)
465 strp_output = _strptime._strptime_time(strp_input, format_string ) 465 strp_output = _strptime._strptime_time(strp_input, format_string )
466 self.assertTrue(strp_output[:3] == ymd_tuple, 466 self.assertEqual(strp_output[:3], ymd_tuple,
467 "%s(%s) test failed w/ '%s': %s != %s (%s != %s)" % 467 "%s(%s) test failed w/ '%s': %s != %s (%s != %s)" %
468 (test_reason, directive, strp_input, 468 (test_reason, directive, strp_input,
469 strp_output[:3], ymd_tuple, 469 strp_output[:3], ymd_tuple,
470 strp_output[7], dt_date.timetuple()[7])) 470 strp_output[7], dt_date.timetuple()[7]))
471 test_helper((1901, 1, 3), "week 0") 471 test_helper((1901, 1, 3), "week 0")
472 test_helper((1901, 1, 8), "common case") 472 test_helper((1901, 1, 8), "common case")
473 test_helper((1901, 1, 13), "day on Sunday") 473 test_helper((1901, 1, 13), "day on Sunday")
474 test_helper((1901, 1, 14), "day on Monday") 474 test_helper((1901, 1, 14), "day on Monday")
475 test_helper((1905, 1, 1), "Jan 1 on Sunday") 475 test_helper((1905, 1, 1), "Jan 1 on Sunday")
476 test_helper((1906, 1, 1), "Jan 1 on Monday") 476 test_helper((1906, 1, 1), "Jan 1 on Monday")
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 StrptimeTests, 568 StrptimeTests,
569 Strptime12AMPMTests, 569 Strptime12AMPMTests,
570 JulianTests, 570 JulianTests,
571 CalculationTests, 571 CalculationTests,
572 CacheTests 572 CacheTests
573 ) 573 )
574 574
575 575
576 if __name__ == '__main__': 576 if __name__ == '__main__':
577 test_main() 577 test_main()
OLDNEW
« no previous file with comments | « Lib/test/test_ssl.py ('k') | Lib/test/test_symtable.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+