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

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

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 5 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 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
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): 221 def test_strptime_exception_context(self):
222 # check that this doesn't chain exceptions needlessly (see #17572) 222 # check that this doesn't chain exceptions needlessly (see #17572)
223 with self.assertRaises(ValueError) as e: 223 with self.assertRaises(ValueError) as e:
224 _strptime._strptime_time('', '%D') 224 _strptime._strptime_time('', '%D')
225 self.assertIs(e.exception.__suppress_context__, True) 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)
226 230
227 def test_unconverteddata(self): 231 def test_unconverteddata(self):
228 # Check ValueError is raised when there is unconverted data 232 # Check ValueError is raised when there is unconverted data
229 self.assertRaises(ValueError, _strptime._strptime_time, "10 12", "%m") 233 self.assertRaises(ValueError, _strptime._strptime_time, "10 12", "%m")
230 234
231 def helper(self, directive, position): 235 def helper(self, directive, position):
232 """Helper fxn in testing.""" 236 """Helper fxn in testing."""
233 strf_output = time.strftime("%" + directive, self.time_tuple) 237 strf_output = time.strftime("%" + directive, self.time_tuple)
234 strp_output = _strptime._strptime_time(strf_output, "%" + directive) 238 strp_output = _strptime._strptime_time(strf_output, "%" + directive)
235 self.assertTrue(strp_output[position] == self.time_tuple[position], 239 self.assertTrue(strp_output[position] == self.time_tuple[position],
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 else: 316 else:
313 self.assertEqual(strp_output[8], -1, 317 self.assertEqual(strp_output[8], -1,
314 "LocaleTime().timezone has duplicate values and " 318 "LocaleTime().timezone has duplicate values and "
315 "time.daylight but timezone value not set to -1") 319 "time.daylight but timezone value not set to -1")
316 320
317 def test_bad_timezone(self): 321 def test_bad_timezone(self):
318 # Explicitly test possibility of bad timezone; 322 # Explicitly test possibility of bad timezone;
319 # when time.tzname[0] == time.tzname[1] and time.daylight 323 # when time.tzname[0] == time.tzname[1] and time.daylight
320 tz_name = time.tzname[0] 324 tz_name = time.tzname[0]
321 if tz_name.upper() in ("UTC", "GMT"): 325 if tz_name.upper() in ("UTC", "GMT"):
322 return 326 self.skipTest('need non-UTC/GMT timezone')
323 try: 327 try:
324 original_tzname = time.tzname 328 original_tzname = time.tzname
325 original_daylight = time.daylight 329 original_daylight = time.daylight
326 time.tzname = (tz_name, tz_name) 330 time.tzname = (tz_name, tz_name)
327 time.daylight = 1 331 time.daylight = 1
328 tz_value = _strptime._strptime_time(tz_name, "%Z")[8] 332 tz_value = _strptime._strptime_time(tz_name, "%Z")[8]
329 self.assertEqual(tz_value, -1, 333 self.assertEqual(tz_value, -1,
330 "%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 "
331 "time.daylight set to %s and passing in %s" % 335 "time.daylight set to %s and passing in %s" %
332 (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
525 _strptime._TimeRE_cache.locale_time.lang = "Ni" 529 _strptime._TimeRE_cache.locale_time.lang = "Ni"
526 _strptime._strptime_time("10", "%d") 530 _strptime._strptime_time("10", "%d")
527 self.assertIsNot(locale_time_id, _strptime._TimeRE_cache.locale_time) 531 self.assertIsNot(locale_time_id, _strptime._TimeRE_cache.locale_time)
528 532
529 def test_TimeRE_recreation(self): 533 def test_TimeRE_recreation(self):
530 # The TimeRE instance should be recreated upon changing the locale. 534 # The TimeRE instance should be recreated upon changing the locale.
531 locale_info = locale.getlocale(locale.LC_TIME) 535 locale_info = locale.getlocale(locale.LC_TIME)
532 try: 536 try:
533 locale.setlocale(locale.LC_TIME, ('en_US', 'UTF8')) 537 locale.setlocale(locale.LC_TIME, ('en_US', 'UTF8'))
534 except locale.Error: 538 except locale.Error:
535 return 539 self.skipTest('test needs en_US.UTF8 locale')
536 try: 540 try:
537 _strptime._strptime_time('10', '%d') 541 _strptime._strptime_time('10', '%d')
538 # Get id of current cache object. 542 # Get id of current cache object.
539 first_time_re = _strptime._TimeRE_cache 543 first_time_re = _strptime._TimeRE_cache
540 try: 544 try:
541 # Change the locale and force a recreation of the cache. 545 # Change the locale and force a recreation of the cache.
542 locale.setlocale(locale.LC_TIME, ('de_DE', 'UTF8')) 546 locale.setlocale(locale.LC_TIME, ('de_DE', 'UTF8'))
543 _strptime._strptime_time('10', '%d') 547 _strptime._strptime_time('10', '%d')
544 # Get the new cache object's id. 548 # Get the new cache object's id.
545 second_time_re = _strptime._TimeRE_cache 549 second_time_re = _strptime._TimeRE_cache
546 # They should not be equal. 550 # They should not be equal.
547 self.assertIsNot(first_time_re, second_time_re) 551 self.assertIsNot(first_time_re, second_time_re)
548 # Possible test locale is not supported while initial locale is. 552 # Possible test locale is not supported while initial locale is.
549 # 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
550 # to the resetting to the original locale. 554 # to the resetting to the original locale.
551 except locale.Error: 555 except locale.Error:
552 pass 556 self.skipTest('test needs de_DE.UTF8 locale')
553 # 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
554 # test. 558 # test.
555 finally: 559 finally:
556 locale.setlocale(locale.LC_TIME, locale_info) 560 locale.setlocale(locale.LC_TIME, locale_info)
557 561
558 562
559 def test_main(): 563 def test_main():
560 support.run_unittest( 564 support.run_unittest(
561 getlang_Tests, 565 getlang_Tests,
562 LocaleTime_Tests, 566 LocaleTime_Tests,
563 TimeRETests, 567 TimeRETests,
564 StrptimeTests, 568 StrptimeTests,
565 Strptime12AMPMTests, 569 Strptime12AMPMTests,
566 JulianTests, 570 JulianTests,
567 CalculationTests, 571 CalculationTests,
568 CacheTests 572 CacheTests
569 ) 573 )
570 574
571 575
572 if __name__ == '__main__': 576 if __name__ == '__main__':
573 test_main() 577 test_main()
LEFTRIGHT

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