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

Delta Between Two Patch Sets: Lib/test/test_strptime.py

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 6 years, 9 months ago
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_ssl.py ('k') | Lib/test/test_symtable.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 try: 211 try:
212 _strptime._strptime_time("2005", bad_format) 212 _strptime._strptime_time("2005", bad_format)
213 except ValueError: 213 except ValueError:
214 continue 214 continue
215 except Exception as err: 215 except Exception as err:
216 self.fail("'%s' raised %s, not ValueError" % 216 self.fail("'%s' raised %s, not ValueError" %
217 (bad_format, err.__class__.__name__)) 217 (bad_format, err.__class__.__name__))
218 else: 218 else:
219 self.fail("'%s' did not raise ValueError" % bad_format) 219 self.fail("'%s' did not raise ValueError" % bad_format)
220 220
221 def test_strptime_exception_context(self):
222 # check that this doesn't chain exceptions needlessly (see #17572)
223 with self.assertRaises(ValueError) as e:
224 _strptime._strptime_time('', '%D')
225 self.assertIs(e.exception.__suppress_context__, True)
226 # additional check for IndexError branch (issue #19545)
227 with self.assertRaises(ValueError) as e:
228 _strptime._strptime_time('19', '%Y %')
229 self.assertIs(e.exception.__suppress_context__, True)
230
221 def test_unconverteddata(self): 231 def test_unconverteddata(self):
222 # Check ValueError is raised when there is unconverted data 232 # Check ValueError is raised when there is unconverted data
223 self.assertRaises(ValueError, _strptime._strptime_time, "10 12", "%m") 233 self.assertRaises(ValueError, _strptime._strptime_time, "10 12", "%m")
224 234
225 def helper(self, directive, position): 235 def helper(self, directive, position):
226 """Helper fxn in testing.""" 236 """Helper fxn in testing."""
227 strf_output = time.strftime("%" + directive, self.time_tuple) 237 strf_output = time.strftime("%" + directive, self.time_tuple)
228 strp_output = _strptime._strptime_time(strf_output, "%" + directive) 238 strp_output = _strptime._strptime_time(strf_output, "%" + directive)
229 self.assertTrue(strp_output[position] == self.time_tuple[position], 239 self.assertTrue(strp_output[position] == self.time_tuple[position],
230 "testing of '%s' directive failed; '%s' -> %s != %s" % 240 "testing of '%s' directive failed; '%s' -> %s != %s" %
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 else: 316 else:
307 self.assertEqual(strp_output[8], -1, 317 self.assertEqual(strp_output[8], -1,
308 "LocaleTime().timezone has duplicate values and " 318 "LocaleTime().timezone has duplicate values and "
309 "time.daylight but timezone value not set to -1") 319 "time.daylight but timezone value not set to -1")
310 320
311 def test_bad_timezone(self): 321 def test_bad_timezone(self):
312 # Explicitly test possibility of bad timezone; 322 # Explicitly test possibility of bad timezone;
313 # when time.tzname[0] == time.tzname[1] and time.daylight 323 # when time.tzname[0] == time.tzname[1] and time.daylight
314 tz_name = time.tzname[0] 324 tz_name = time.tzname[0]
315 if tz_name.upper() in ("UTC", "GMT"): 325 if tz_name.upper() in ("UTC", "GMT"):
316 return 326 self.skipTest('need non-UTC/GMT timezone')
317 try: 327 try:
318 original_tzname = time.tzname 328 original_tzname = time.tzname
319 original_daylight = time.daylight 329 original_daylight = time.daylight
320 time.tzname = (tz_name, tz_name) 330 time.tzname = (tz_name, tz_name)
321 time.daylight = 1 331 time.daylight = 1
322 tz_value = _strptime._strptime_time(tz_name, "%Z")[8] 332 tz_value = _strptime._strptime_time(tz_name, "%Z")[8]
323 self.assertEqual(tz_value, -1, 333 self.assertEqual(tz_value, -1,
324 "%s lead to a timezone value of %s instead of -1 when " 334 "%s lead to a timezone value of %s instead of -1 when "
325 "time.daylight set to %s and passing in %s" % 335 "time.daylight set to %s and passing in %s" %
326 (time.tzname, tz_value, time.daylight, tz_name)) 336 (time.tzname, tz_value, time.daylight, tz_name))
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
519 _strptime._TimeRE_cache.locale_time.lang = "Ni" 529 _strptime._TimeRE_cache.locale_time.lang = "Ni"
520 _strptime._strptime_time("10", "%d") 530 _strptime._strptime_time("10", "%d")
521 self.assertIsNot(locale_time_id, _strptime._TimeRE_cache.locale_time) 531 self.assertIsNot(locale_time_id, _strptime._TimeRE_cache.locale_time)
522 532
523 def test_TimeRE_recreation(self): 533 def test_TimeRE_recreation(self):
524 # The TimeRE instance should be recreated upon changing the locale. 534 # The TimeRE instance should be recreated upon changing the locale.
525 locale_info = locale.getlocale(locale.LC_TIME) 535 locale_info = locale.getlocale(locale.LC_TIME)
526 try: 536 try:
527 locale.setlocale(locale.LC_TIME, ('en_US', 'UTF8')) 537 locale.setlocale(locale.LC_TIME, ('en_US', 'UTF8'))
528 except locale.Error: 538 except locale.Error:
529 return 539 self.skipTest('test needs en_US.UTF8 locale')
530 try: 540 try:
531 _strptime._strptime_time('10', '%d') 541 _strptime._strptime_time('10', '%d')
532 # Get id of current cache object. 542 # Get id of current cache object.
533 first_time_re = _strptime._TimeRE_cache 543 first_time_re = _strptime._TimeRE_cache
534 try: 544 try:
535 # Change the locale and force a recreation of the cache. 545 # Change the locale and force a recreation of the cache.
536 locale.setlocale(locale.LC_TIME, ('de_DE', 'UTF8')) 546 locale.setlocale(locale.LC_TIME, ('de_DE', 'UTF8'))
537 _strptime._strptime_time('10', '%d') 547 _strptime._strptime_time('10', '%d')
538 # Get the new cache object's id. 548 # Get the new cache object's id.
539 second_time_re = _strptime._TimeRE_cache 549 second_time_re = _strptime._TimeRE_cache
540 # They should not be equal. 550 # They should not be equal.
541 self.assertIsNot(first_time_re, second_time_re) 551 self.assertIsNot(first_time_re, second_time_re)
542 # Possible test locale is not supported while initial locale is. 552 # Possible test locale is not supported while initial locale is.
543 # If this is the case just suppress the exception and fall-through 553 # If this is the case just suppress the exception and fall-through
544 # to the resetting to the original locale. 554 # to the resetting to the original locale.
545 except locale.Error: 555 except locale.Error:
546 pass 556 self.skipTest('test needs de_DE.UTF8 locale')
547 # Make sure we don't trample on the locale setting once we leave the 557 # Make sure we don't trample on the locale setting once we leave the
548 # test. 558 # test.
549 finally: 559 finally:
550 locale.setlocale(locale.LC_TIME, locale_info) 560 locale.setlocale(locale.LC_TIME, locale_info)
551 561
552 562
553 def test_main(): 563 def test_main():
554 support.run_unittest( 564 support.run_unittest(
555 getlang_Tests, 565 getlang_Tests,
556 LocaleTime_Tests, 566 LocaleTime_Tests,
557 TimeRETests, 567 TimeRETests,
558 StrptimeTests, 568 StrptimeTests,
559 Strptime12AMPMTests, 569 Strptime12AMPMTests,
560 JulianTests, 570 JulianTests,
561 CalculationTests, 571 CalculationTests,
562 CacheTests 572 CacheTests
563 ) 573 )
564 574
565 575
566 if __name__ == '__main__': 576 if __name__ == '__main__':
567 test_main() 577 test_main()
LEFTRIGHT

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