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

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

Issue 16510: Using appropriate checks in tests
Patch Set: Created 5 years, 10 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_structseq.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 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 239
240 def test_year(self): 240 def test_year(self):
241 # Test that the year is handled properly 241 # Test that the year is handled properly
242 for directive in ('y', 'Y'): 242 for directive in ('y', 'Y'):
243 self.helper(directive, 0) 243 self.helper(directive, 0)
244 # Must also make sure %y values are correct for bounds set by Open Group 244 # Must also make sure %y values are correct for bounds set by Open Group
245 for century, bounds in ((1900, ('69', '99')), (2000, ('00', '68'))): 245 for century, bounds in ((1900, ('69', '99')), (2000, ('00', '68'))):
246 for bound in bounds: 246 for bound in bounds:
247 strp_output = _strptime._strptime_time(bound, '%y') 247 strp_output = _strptime._strptime_time(bound, '%y')
248 expected_result = century + int(bound) 248 expected_result = century + int(bound)
249 self.assertTrue(strp_output[0] == expected_result, 249 self.assertEqual(strp_output[0], expected_result,
250 "'y' test failed; passed in '%s' " 250 "'y' test failed; passed in '%s' "
251 "and returned '%s'" % (bound, strp_output[0])) 251 "and returned '%s'" % (bound, strp_output[0]))
252 252
253 def test_month(self): 253 def test_month(self):
254 # Test for month directives 254 # Test for month directives
255 for directive in ('B', 'b', 'm'): 255 for directive in ('B', 'b', 'm'):
256 self.helper(directive, 1) 256 self.helper(directive, 1)
257 257
258 def test_day(self): 258 def test_day(self):
259 # Test for day directives 259 # Test for day directives
260 self.helper('d', 2) 260 self.helper('d', 2)
261 261
262 def test_hour(self): 262 def test_hour(self):
263 # Test hour directives 263 # Test hour directives
264 self.helper('H', 3) 264 self.helper('H', 3)
265 strf_output = time.strftime("%I %p", self.time_tuple) 265 strf_output = time.strftime("%I %p", self.time_tuple)
266 strp_output = _strptime._strptime_time(strf_output, "%I %p") 266 strp_output = _strptime._strptime_time(strf_output, "%I %p")
267 self.assertTrue(strp_output[3] == self.time_tuple[3], 267 self.assertEqual(strp_output[3], self.time_tuple[3],
268 "testing of '%%I %%p' directive failed; '%s' -> %s != %s " % 268 "testing of '%%I %%p' directive failed; '%s' -> %s != %s " %
269 (strf_output, strp_output[3], self.time_tuple[3])) 269 (strf_output, strp_output[3], self.time_tuple[3]))
270 270
271 def test_minute(self): 271 def test_minute(self):
272 # Test minute directives 272 # Test minute directives
273 self.helper('M', 4) 273 self.helper('M', 4)
274 274
275 def test_second(self): 275 def test_second(self):
276 # Test second directives 276 # Test second directives
277 self.helper('S', 5) 277 self.helper('S', 5)
(...skipping 21 matching lines...) Expand all
299 # occurs; first found in FreeBSD 4.4. 299 # occurs; first found in FreeBSD 4.4.
300 strp_output = _strptime._strptime_time("UTC", "%Z") 300 strp_output = _strptime._strptime_time("UTC", "%Z")
301 self.assertEqual(strp_output.tm_isdst, 0) 301 self.assertEqual(strp_output.tm_isdst, 0)
302 strp_output = _strptime._strptime_time("GMT", "%Z") 302 strp_output = _strptime._strptime_time("GMT", "%Z")
303 self.assertEqual(strp_output.tm_isdst, 0) 303 self.assertEqual(strp_output.tm_isdst, 0)
304 time_tuple = time.localtime() 304 time_tuple = time.localtime()
305 strf_output = time.strftime("%Z") #UTC does not have a timezone 305 strf_output = time.strftime("%Z") #UTC does not have a timezone
306 strp_output = _strptime._strptime_time(strf_output, "%Z") 306 strp_output = _strptime._strptime_time(strf_output, "%Z")
307 locale_time = _strptime.LocaleTime() 307 locale_time = _strptime.LocaleTime()
308 if time.tzname[0] != time.tzname[1] or not time.daylight: 308 if time.tzname[0] != time.tzname[1] or not time.daylight:
309 self.assertTrue(strp_output[8] == time_tuple[8], 309 self.assertEqual(strp_output[8], time_tuple[8],
310 "timezone check failed; '%s' -> %s != %s" % 310 "timezone check failed; '%s' -> %s != %s" %
311 (strf_output, strp_output[8], time_tuple[8])) 311 (strf_output, strp_output[8], time_tuple[8]))
312 else: 312 else:
313 self.assertTrue(strp_output[8] == -1, 313 self.assertEqual(strp_output[8], -1,
314 "LocaleTime().timezone has duplicate values and " 314 "LocaleTime().timezone has duplicate values and "
315 "time.daylight but timezone value not set to -1") 315 "time.daylight but timezone value not set to -1")
316 316
317 def test_bad_timezone(self): 317 def test_bad_timezone(self):
318 # Explicitly test possibility of bad timezone; 318 # Explicitly test possibility of bad timezone;
319 # when time.tzname[0] == time.tzname[1] and time.daylight 319 # when time.tzname[0] == time.tzname[1] and time.daylight
320 tz_name = time.tzname[0] 320 tz_name = time.tzname[0]
321 if tz_name.upper() in ("UTC", "GMT"): 321 if tz_name.upper() in ("UTC", "GMT"):
322 return 322 return
323 try: 323 try:
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 "strptime does not handle ALL-CAPS names properly") 364 "strptime does not handle ALL-CAPS names properly")
365 self.assertTrue(_strptime._strptime_time(strf_output.lower(), "%B"), 365 self.assertTrue(_strptime._strptime_time(strf_output.lower(), "%B"),
366 "strptime does not handle lowercase names properly") 366 "strptime does not handle lowercase names properly")
367 self.assertTrue(_strptime._strptime_time(strf_output.capitalize(), "%B") , 367 self.assertTrue(_strptime._strptime_time(strf_output.capitalize(), "%B") ,
368 "strptime does not handle capword names properly") 368 "strptime does not handle capword names properly")
369 369
370 def test_defaults(self): 370 def test_defaults(self):
371 # Default return value should be (1900, 1, 1, 0, 0, 0, 0, 1, 0) 371 # Default return value should be (1900, 1, 1, 0, 0, 0, 0, 1, 0)
372 defaults = (1900, 1, 1, 0, 0, 0, 0, 1, -1) 372 defaults = (1900, 1, 1, 0, 0, 0, 0, 1, -1)
373 strp_output = _strptime._strptime_time('1', '%m') 373 strp_output = _strptime._strptime_time('1', '%m')
374 self.assertTrue(strp_output == defaults, 374 self.assertEqual(strp_output, defaults,
375 "Default values for strptime() are incorrect;" 375 "Default values for strptime() are incorrect;"
376 " %s != %s" % (strp_output, defaults)) 376 " %s != %s" % (strp_output, defaults))
377 377
378 def test_escaping(self): 378 def test_escaping(self):
379 # Make sure all characters that have regex significance are escaped. 379 # Make sure all characters that have regex significance are escaped.
380 # Parentheses are in a purposeful order; will cause an error of 380 # Parentheses are in a purposeful order; will cause an error of
381 # unbalanced parentheses when the regex is compiled if they are not 381 # unbalanced parentheses when the regex is compiled if they are not
382 # escaped. 382 # escaped.
383 # Test instigated by bug #796149 . 383 # Test instigated by bug #796149 .
384 need_escaping = ".^$*+?{}\[]|)(" 384 need_escaping = ".^$*+?{}\[]|)("
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 """Test that strptime() fills in missing info correctly""" 416 """Test that strptime() fills in missing info correctly"""
417 417
418 def setUp(self): 418 def setUp(self):
419 self.time_tuple = time.gmtime() 419 self.time_tuple = time.gmtime()
420 420
421 def test_julian_calculation(self): 421 def test_julian_calculation(self):
422 # Make sure that when Julian is missing that it is calculated 422 # Make sure that when Julian is missing that it is calculated
423 format_string = "%Y %m %d %H %M %S %w %Z" 423 format_string = "%Y %m %d %H %M %S %w %Z"
424 result = _strptime._strptime_time(time.strftime(format_string, self.time _tuple), 424 result = _strptime._strptime_time(time.strftime(format_string, self.time _tuple),
425 format_string) 425 format_string)
426 self.assertTrue(result.tm_yday == self.time_tuple.tm_yday, 426 self.assertEqual(result.tm_yday, self.time_tuple.tm_yday,
427 "Calculation of tm_yday failed; %s != %s" % 427 "Calculation of tm_yday failed; %s != %s" %
428 (result.tm_yday, self.time_tuple.tm_yday)) 428 (result.tm_yday, self.time_tuple.tm_yday))
429 429
430 def test_gregorian_calculation(self): 430 def test_gregorian_calculation(self):
431 # Test that Gregorian date can be calculated from Julian day 431 # Test that Gregorian date can be calculated from Julian day
432 format_string = "%Y %H %M %S %w %j %Z" 432 format_string = "%Y %H %M %S %w %j %Z"
433 result = _strptime._strptime_time(time.strftime(format_string, self.time _tuple), 433 result = _strptime._strptime_time(time.strftime(format_string, self.time _tuple),
434 format_string) 434 format_string)
435 self.assertTrue(result.tm_year == self.time_tuple.tm_year and 435 self.assertTrue(result.tm_year == self.time_tuple.tm_year and
436 result.tm_mon == self.time_tuple.tm_mon and 436 result.tm_mon == self.time_tuple.tm_mon and
437 result.tm_mday == self.time_tuple.tm_mday, 437 result.tm_mday == self.time_tuple.tm_mday,
438 "Calculation of Gregorian date failed;" 438 "Calculation of Gregorian date failed;"
439 "%s-%s-%s != %s-%s-%s" % 439 "%s-%s-%s != %s-%s-%s" %
440 (result.tm_year, result.tm_mon, result.tm_mday, 440 (result.tm_year, result.tm_mon, result.tm_mday,
441 self.time_tuple.tm_year, self.time_tuple.tm_mon, 441 self.time_tuple.tm_year, self.time_tuple.tm_mon,
442 self.time_tuple.tm_mday)) 442 self.time_tuple.tm_mday))
443 443
444 def test_day_of_week_calculation(self): 444 def test_day_of_week_calculation(self):
445 # Test that the day of the week is calculated as needed 445 # Test that the day of the week is calculated as needed
446 format_string = "%Y %m %d %H %S %j %Z" 446 format_string = "%Y %m %d %H %S %j %Z"
447 result = _strptime._strptime_time(time.strftime(format_string, self.time _tuple), 447 result = _strptime._strptime_time(time.strftime(format_string, self.time _tuple),
448 format_string) 448 format_string)
449 self.assertTrue(result.tm_wday == self.time_tuple.tm_wday, 449 self.assertEqual(result.tm_wday, self.time_tuple.tm_wday,
450 "Calculation of day of the week failed;" 450 "Calculation of day of the week failed;"
451 "%s != %s" % (result.tm_wday, self.time_tuple.tm_wday)) 451 "%s != %s" % (result.tm_wday, self.time_tuple.tm_wday))
452 452
453 def test_week_of_year_and_day_of_week_calculation(self): 453 def test_week_of_year_and_day_of_week_calculation(self):
454 # Should be able to infer date if given year, week of year (%U or %W) 454 # Should be able to infer date if given year, week of year (%U or %W)
455 # and day of the week 455 # and day of the week
456 def test_helper(ymd_tuple, test_reason): 456 def test_helper(ymd_tuple, test_reason):
457 for directive in ('W', 'U'): 457 for directive in ('W', 'U'):
458 format_string = "%%Y %%%s %%w" % directive 458 format_string = "%%Y %%%s %%w" % directive
459 dt_date = datetime_date(*ymd_tuple) 459 dt_date = datetime_date(*ymd_tuple)
460 strp_input = dt_date.strftime(format_string) 460 strp_input = dt_date.strftime(format_string)
461 strp_output = _strptime._strptime_time(strp_input, format_string ) 461 strp_output = _strptime._strptime_time(strp_input, format_string )
462 self.assertTrue(strp_output[:3] == ymd_tuple, 462 self.assertEqual(strp_output[:3], ymd_tuple,
463 "%s(%s) test failed w/ '%s': %s != %s (%s != %s)" % 463 "%s(%s) test failed w/ '%s': %s != %s (%s != %s)" %
464 (test_reason, directive, strp_input, 464 (test_reason, directive, strp_input,
465 strp_output[:3], ymd_tuple, 465 strp_output[:3], ymd_tuple,
466 strp_output[7], dt_date.timetuple()[7])) 466 strp_output[7], dt_date.timetuple()[7]))
467 test_helper((1901, 1, 3), "week 0") 467 test_helper((1901, 1, 3), "week 0")
468 test_helper((1901, 1, 8), "common case") 468 test_helper((1901, 1, 8), "common case")
469 test_helper((1901, 1, 13), "day on Sunday") 469 test_helper((1901, 1, 13), "day on Sunday")
470 test_helper((1901, 1, 14), "day on Monday") 470 test_helper((1901, 1, 14), "day on Monday")
471 test_helper((1905, 1, 1), "Jan 1 on Sunday") 471 test_helper((1905, 1, 1), "Jan 1 on Sunday")
472 test_helper((1906, 1, 1), "Jan 1 on Monday") 472 test_helper((1906, 1, 1), "Jan 1 on Monday")
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
564 StrptimeTests, 564 StrptimeTests,
565 Strptime12AMPMTests, 565 Strptime12AMPMTests,
566 JulianTests, 566 JulianTests,
567 CalculationTests, 567 CalculationTests,
568 CacheTests 568 CacheTests
569 ) 569 )
570 570
571 571
572 if __name__ == '__main__': 572 if __name__ == '__main__':
573 test_main() 573 test_main()
OLDNEW
« no previous file with comments | « Lib/test/test_ssl.py ('k') | Lib/test/test_structseq.py » ('j') | no next file with comments »

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