[Python-checkins] python/dist/src/Lib/test test_strptime.py,1.8,1.9

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Fri, 17 Jan 2003 19:53:51 -0800


Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1:/tmp/cvs-serv14518/python/Lib/test

Modified Files:
	test_strptime.py 
Log Message:
SF patch 670012:  Compatibility changes for _strptime.py.
Patch from Brett Cannon:

    First, the 'y' directive now handles [00, 68] as a suffix for the
    21st century while [69, 99] is treated as the suffix for the 20th
    century (this is for Open Group compatibility).

    strptime now returns default values that make it a valid date ...

    the ability to pass in a regex object to use instead of a format
    string (and the inverse ability to have strptime return a regex object)
    has been removed. This is in preparation for a future patch that will
    add some caching internally to get a speed boost.


Index: test_strptime.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_strptime.py,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** test_strptime.py	26 Dec 2002 16:19:52 -0000	1.8
--- test_strptime.py	18 Jan 2003 03:53:49 -0000	1.9
***************
*** 125,128 ****
--- 125,136 ----
          self.assertRaises(TypeError, _strptime.LocaleTime, timezone=range(3))
  
+     def test_unknowntimezone(self):
+         # Handle timezone set to ('','') properly.
+         # Fixes bug #661354
+         locale_time = _strptime.LocaleTime(timezone=('',''))
+         self.failUnless("%Z" not in locale_time.LC_date,
+                         "when timezone == ('',''), string.replace('','%Z') is "
+                          "occuring")
+ 
  class TimeRETests(unittest.TestCase):
      """Tests for TimeRE."""
***************
*** 181,190 ****
          for directive in ('a','A','b','B','c','d','H','I','j','m','M','p','S',
                            'U','w','W','x','X','y','Y','Z','%'):
!             compiled = self.time_re.compile("%%%s"% directive)
!             found = compiled.match(time.strftime("%%%s" % directive))
              self.failUnless(found, "Matching failed on '%s' using '%s' regex" %
!                                     (time.strftime("%%%s" % directive),
                                       compiled.pattern))
  
  class StrptimeTests(unittest.TestCase):
      """Tests for _strptime.strptime."""
--- 189,205 ----
          for directive in ('a','A','b','B','c','d','H','I','j','m','M','p','S',
                            'U','w','W','x','X','y','Y','Z','%'):
!             compiled = self.time_re.compile("%" + directive)
!             found = compiled.match(time.strftime("%" + directive))
              self.failUnless(found, "Matching failed on '%s' using '%s' regex" %
!                                     (time.strftime("%" + directive),
                                       compiled.pattern))
  
+     def test_blankpattern(self):
+         # Make sure when tuple or something has no values no regex is generated.
+         # Fixes bug #661354
+         test_locale = _strptime.LocaleTime(timezone=('',''))
+         self.failUnless(_strptime.TimeRE(test_locale).pattern("%Z") == '',
+                         "with timezone == ('',''), TimeRE().pattern('%Z') != ''")
+ 
  class StrptimeTests(unittest.TestCase):
      """Tests for _strptime.strptime."""
***************
*** 199,217 ****
                            format="%A")
  
-     def test_returning_RE(self):
-         # Make sure that an re can be returned
-         strp_output = _strptime.strptime(False, "%Y")
-         self.failUnless(isinstance(strp_output, type(re.compile(''))),
-                         "re object not returned correctly")
-         self.failUnless(_strptime.strptime("1999", strp_output),
-                         "Use of re object failed")
-         bad_locale_time = _strptime.LocaleTime(lang="gibberish")
-         self.assertRaises(TypeError, _strptime.strptime, data_string='1999',
-                           format=strp_output, locale_time=bad_locale_time)
- 
      def helper(self, directive, position):
          """Helper fxn in testing."""
!         strf_output = time.strftime("%%%s" % directive, self.time_tuple)
!         strp_output = _strptime.strptime(strf_output, "%%%s" % directive)
          self.failUnless(strp_output[position] == self.time_tuple[position],
                          "testing of '%s' directive failed; '%s' -> %s != %s" %
--- 214,221 ----
                            format="%A")
  
      def helper(self, directive, position):
          """Helper fxn in testing."""
!         strf_output = time.strftime("%" + directive, self.time_tuple)
!         strp_output = _strptime.strptime(strf_output, "%" + directive)
          self.failUnless(strp_output[position] == self.time_tuple[position],
                          "testing of '%s' directive failed; '%s' -> %s != %s" %
***************
*** 223,226 ****
--- 227,238 ----
          for directive in ('y', 'Y'):
              self.helper(directive, 0)
+         # Must also make sure %y values are correct for bounds set by Open Group
+         for century, bounds in ((1900, ('69', '99')), (2000, ('00', '68'))):
+             for bound in bounds:
+                 strp_output = _strptime.strptime(bound, '%y')
+                 expected_result = century + int(bound)
+                 self.failUnless(strp_output[0] == expected_result,
+                                 "'y' test failed; passed in '%s' "
+                                 "and returned '%s'" % (bound, strp_output[0]))
  
      def test_month(self):
***************
*** 263,267 ****
          # When gmtime() is used with %Z, entire result of strftime() is empty.
          # Check for equal timezone names deals with bad locale info when this
!         # occurs; first found in FreeBSD 4.4 -current
          time_tuple = time.localtime()
          strf_output = time.strftime("%Z")  #UTC does not have a timezone
--- 275,279 ----
          # When gmtime() is used with %Z, entire result of strftime() is empty.
          # Check for equal timezone names deals with bad locale info when this
!         # occurs; first found in FreeBSD 4.4.
          time_tuple = time.localtime()
          strf_output = time.strftime("%Z")  #UTC does not have a timezone
***************
*** 275,279 ****
              self.failUnless(strp_output[8] == -1,
                              "LocaleTime().timezone has duplicate values but "
!                              "timzone value not set to -1")
  
      def test_date_time(self):
--- 287,291 ----
              self.failUnless(strp_output[8] == -1,
                              "LocaleTime().timezone has duplicate values but "
!                              "timzone value not set to 0")
  
      def test_date_time(self):
***************
*** 310,313 ****
--- 322,333 ----
                          "strptime does not handle capword names properly")
  
+     def test_defaults(self):
+         # Default return value should be (1900, 1, 1, 0, 0, 0, 0, 1, 0)
+         defaults = (1900, 1, 1, 0, 0, 0, 0, 1, -1)
+         strp_output = _strptime.strptime('1', '%m')
+         self.failUnless(strp_output == defaults,
+                         "Default values for strptime() are incorrect;"
+                         " %s != %s" % (strp_output, defaults))
+ 
  class FxnTests(unittest.TestCase):
      """Test functions that fill in info by validating result and are triggered
***************
*** 326,337 ****
                           (result, self.time_tuple[7]))
  
-     def test_julianday_trigger(self):
-         # Make sure julianday is called
-         strf_output = time.strftime("%Y-%m-%d", self.time_tuple)
-         strp_output = _strptime.strptime(strf_output, "%Y-%m-%d")
-         self.failUnless(strp_output[7] == self.time_tuple[7],
-                         "strptime did not trigger julianday(); %s != %s" %
-                          (strp_output[7], self.time_tuple[7]))
- 
      def test_gregorian_result(self):
          # Test gregorian
--- 346,349 ----
***************
*** 341,355 ****
                          "gregorian() failed; %s != %s" % (result, comparison))
  
-     def test_gregorian_trigger(self):
-         # Test that gregorian() is triggered
-         strf_output = time.strftime("%j %Y", self.time_tuple)
-         strp_output = _strptime.strptime(strf_output, "%j %Y")
-         self.failUnless(strp_output[1] == self.time_tuple[1] and
-                         strp_output[2] == self.time_tuple[2],
-                         "gregorian() not triggered; month -- %s != %s, "
-                          "day -- %s != %s" %
-                           (strp_output[1], self.time_tuple[1], strp_output[2],
-                            self.time_tuple[2]))
- 
      def test_dayofweek_result(self):
          # Test dayofweek
--- 353,356 ----
***************
*** 360,372 ****
                          "dayofweek() failed; %s != %s" % (result, comparison))
  
-     def test_dayofweek_trigger(self):
-         # Make sure dayofweek() gets triggered
-         strf_output = time.strftime("%Y-%m-%d", self.time_tuple)
-         strp_output = _strptime.strptime(strf_output, "%Y-%m-%d")
-         self.failUnless(strp_output[6] == self.time_tuple[6],
-                         "triggering of dayofweek() failed; %s != %s" %
-                          (strp_output[6], self.time_tuple[6]))
- 
- 
  class Strptime12AMPMTests(unittest.TestCase):
      """Test a _strptime regression in '%I %p' at 12 noon (12 PM)"""
--- 361,364 ----
***************
*** 385,389 ****
      def test_all_julian_days(self):
          eq = self.assertEqual
-         # XXX: should 0 be accepted?
          for i in range(1, 367):
              # use 2004, since it is a leap year, we have 366 days
--- 377,380 ----