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

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

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