Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/test/test_strptime.py

    r2 r391  
    1414    """Test _getlang"""
    1515    def test_basic(self):
    16         self.failUnlessEqual(_strptime._getlang(), locale.getlocale(locale.LC_TIME))
     16        self.assertEqual(_strptime._getlang(), locale.getlocale(locale.LC_TIME))
    1717
    1818class LocaleTime_Tests(unittest.TestCase):
     
    3737        strftime_output = time.strftime(directive, self.time_tuple).lower()
    3838        comparison = testing[self.time_tuple[tuple_position]]
    39         self.failUnless(strftime_output in testing, "%s: not found in tuple" %
    40                                                     error_msg)
    41         self.failUnless(comparison == strftime_output,
    42                         "%s: position within tuple incorrect; %s != %s" %
    43                         (error_msg, comparison, strftime_output))
     39        self.assertIn(strftime_output, testing,
     40                      "%s: not found in tuple" % error_msg)
     41        self.assertEqual(comparison, strftime_output,
     42                         "%s: position within tuple incorrect; %s != %s" %
     43                         (error_msg, comparison, strftime_output))
    4444
    4545    def test_weekday(self):
     
    6262        # Make sure AM/PM representation done properly
    6363        strftime_output = time.strftime("%p", self.time_tuple).lower()
    64         self.failUnless(strftime_output in self.LT_ins.am_pm,
    65                         "AM/PM representation not in tuple")
     64        self.assertIn(strftime_output, self.LT_ins.am_pm,
     65                      "AM/PM representation not in tuple")
    6666        if self.time_tuple[3] < 12: position = 0
    6767        else: position = 1
    68         self.failUnless(strftime_output == self.LT_ins.am_pm[position],
    69                         "AM/PM representation in the wrong position within the tuple")
     68        self.assertEqual(self.LT_ins.am_pm[position], strftime_output,
     69                         "AM/PM representation in the wrong position within the tuple")
    7070
    7171    def test_timezone(self):
     
    7373        timezone = time.strftime("%Z", self.time_tuple).lower()
    7474        if timezone:
    75             self.failUnless(timezone in self.LT_ins.timezone[0] or \
     75            self.assertTrue(timezone in self.LT_ins.timezone[0] or
    7676                            timezone in self.LT_ins.timezone[1],
    7777                            "timezone %s not found in %s" %
     
    8787        magic_date = (1999, 3, 17, 22, 44, 55, 2, 76, 0)
    8888        strftime_output = time.strftime("%c", magic_date)
    89         self.failUnless(strftime_output == time.strftime(self.LT_ins.LC_date_time,
    90                                                          magic_date),
    91                         "LC_date_time incorrect")
     89        self.assertEqual(time.strftime(self.LT_ins.LC_date_time, magic_date),
     90                         strftime_output, "LC_date_time incorrect")
    9291        strftime_output = time.strftime("%x", magic_date)
    93         self.failUnless(strftime_output == time.strftime(self.LT_ins.LC_date,
    94                                                          magic_date),
    95                         "LC_date incorrect")
     92        self.assertEqual(time.strftime(self.LT_ins.LC_date, magic_date),
     93                         strftime_output, "LC_date incorrect")
    9694        strftime_output = time.strftime("%X", magic_date)
    97         self.failUnless(strftime_output == time.strftime(self.LT_ins.LC_time,
    98                                                          magic_date),
    99                         "LC_time incorrect")
     95        self.assertEqual(time.strftime(self.LT_ins.LC_time, magic_date),
     96                         strftime_output, "LC_time incorrect")
    10097        LT = _strptime.LocaleTime()
    10198        LT.am_pm = ('', '')
    102         self.failUnless(LT.LC_time, "LocaleTime's LC directives cannot handle "
     99        self.assertTrue(LT.LC_time, "LocaleTime's LC directives cannot handle "
    103100                                    "empty strings")
    104101
     
    106103        # Make sure lang is set to what _getlang() returns
    107104        # Assuming locale has not changed between now and when self.LT_ins was created
    108         self.failUnlessEqual(self.LT_ins.lang, _strptime._getlang())
     105        self.assertEqual(self.LT_ins.lang, _strptime._getlang())
    109106
    110107
     
    120117        # Test TimeRE.pattern
    121118        pattern_string = self.time_re.pattern(r"%a %A %d")
    122         self.failUnless(pattern_string.find(self.locale_time.a_weekday[2]) != -1,
     119        self.assertTrue(pattern_string.find(self.locale_time.a_weekday[2]) != -1,
    123120                        "did not find abbreviated weekday in pattern string '%s'" %
    124121                         pattern_string)
    125         self.failUnless(pattern_string.find(self.locale_time.f_weekday[4]) != -1,
     122        self.assertTrue(pattern_string.find(self.locale_time.f_weekday[4]) != -1,
    126123                        "did not find full weekday in pattern string '%s'" %
    127124                         pattern_string)
    128         self.failUnless(pattern_string.find(self.time_re['d']) != -1,
     125        self.assertTrue(pattern_string.find(self.time_re['d']) != -1,
    129126                        "did not find 'd' directive pattern string '%s'" %
    130127                         pattern_string)
     
    134131        # regex syntax is escaped.
    135132        pattern_string = self.time_re.pattern("\d+")
    136         self.failUnless(r"\\d\+" in pattern_string,
    137                         "%s does not have re characters escaped properly" %
    138                         pattern_string)
     133        self.assertIn(r"\\d\+", pattern_string,
     134                      "%s does not have re characters escaped properly" %
     135                      pattern_string)
    139136
    140137    def test_compile(self):
    141138        # Check that compiled regex is correct
    142139        found = self.time_re.compile(r"%A").match(self.locale_time.f_weekday[6])
    143         self.failUnless(found and found.group('A') == self.locale_time.f_weekday[6],
     140        self.assertTrue(found and found.group('A') == self.locale_time.f_weekday[6],
    144141                        "re object for '%A' failed")
    145142        compiled = self.time_re.compile(r"%a %b")
    146143        found = compiled.match("%s %s" % (self.locale_time.a_weekday[4],
    147144                               self.locale_time.a_month[4]))
    148         self.failUnless(found,
     145        self.assertTrue(found,
    149146            "Match failed with '%s' regex and '%s' string" %
    150147             (compiled.pattern, "%s %s" % (self.locale_time.a_weekday[4],
    151148                                           self.locale_time.a_month[4])))
    152         self.failUnless(found.group('a') == self.locale_time.a_weekday[4] and
     149        self.assertTrue(found.group('a') == self.locale_time.a_weekday[4] and
    153150                         found.group('b') == self.locale_time.a_month[4],
    154151                        "re object couldn't find the abbreviated weekday month in "
     
    160157            compiled = self.time_re.compile("%" + directive)
    161158            found = compiled.match(time.strftime("%" + directive))
    162             self.failUnless(found, "Matching failed on '%s' using '%s' regex" %
     159            self.assertTrue(found, "Matching failed on '%s' using '%s' regex" %
    163160                                    (time.strftime("%" + directive),
    164161                                     compiled.pattern))
     
    169166        test_locale = _strptime.LocaleTime()
    170167        test_locale.timezone = (frozenset(), frozenset())
    171         self.failUnless(_strptime.TimeRE(test_locale).pattern("%Z") == '',
    172                         "with timezone == ('',''), TimeRE().pattern('%Z') != ''")
     168        self.assertEqual(_strptime.TimeRE(test_locale).pattern("%Z"), '',
     169                         "with timezone == ('',''), TimeRE().pattern('%Z') != ''")
    173170
    174171    def test_matching_with_escapes(self):
     
    176173        compiled_re = self.time_re.compile("\w+ %m")
    177174        found = compiled_re.match("\w+ 10")
    178         self.failUnless(found, "Escaping failed of format '\w+ 10'")
     175        self.assertTrue(found, "Escaping failed of format '\w+ 10'")
    179176
    180177    def test_locale_data_w_regex_metacharacters(self):
     
    187184                                frozenset("Tokyo (daylight time)"))
    188185        time_re = _strptime.TimeRE(locale_time)
    189         self.failUnless(time_re.compile("%Z").match("Tokyo (standard time)"),
     186        self.assertTrue(time_re.compile("%Z").match("Tokyo (standard time)"),
    190187                        "locale data that contains regex metacharacters is not"
    191188                        " properly escaped")
     
    196193        # "steal" characters from each other.
    197194        pattern = self.time_re.pattern('%j %H')
    198         self.failUnless(not re.match(pattern, "180"))
    199         self.failUnless(re.match(pattern, "18 0"))
     195        self.assertFalse(re.match(pattern, "180"))
     196        self.assertTrue(re.match(pattern, "18 0"))
    200197
    201198
     
    230227        strf_output = time.strftime("%" + directive, self.time_tuple)
    231228        strp_output = _strptime._strptime_time(strf_output, "%" + directive)
    232         self.failUnless(strp_output[position] == self.time_tuple[position],
     229        self.assertTrue(strp_output[position] == self.time_tuple[position],
    233230                        "testing of '%s' directive failed; '%s' -> %s != %s" %
    234231                         (directive, strf_output, strp_output[position],
     
    244241                strp_output = _strptime._strptime_time(bound, '%y')
    245242                expected_result = century + int(bound)
    246                 self.failUnless(strp_output[0] == expected_result,
     243                self.assertTrue(strp_output[0] == expected_result,
    247244                                "'y' test failed; passed in '%s' "
    248245                                "and returned '%s'" % (bound, strp_output[0]))
     
    262259        strf_output = time.strftime("%I %p", self.time_tuple)
    263260        strp_output = _strptime._strptime_time(strf_output, "%I %p")
    264         self.failUnless(strp_output[3] == self.time_tuple[3],
     261        self.assertTrue(strp_output[3] == self.time_tuple[3],
    265262                        "testing of '%%I %%p' directive failed; '%s' -> %s != %s" %
    266263                         (strf_output, strp_output[3], self.time_tuple[3]))
     
    296293        # occurs; first found in FreeBSD 4.4.
    297294        strp_output = _strptime._strptime_time("UTC", "%Z")
    298         self.failUnlessEqual(strp_output.tm_isdst, 0)
     295        self.assertEqual(strp_output.tm_isdst, 0)
    299296        strp_output = _strptime._strptime_time("GMT", "%Z")
    300         self.failUnlessEqual(strp_output.tm_isdst, 0)
    301         if sys.platform == "mac":
    302             # Timezones don't really work on MacOS9
    303             return
     297        self.assertEqual(strp_output.tm_isdst, 0)
    304298        time_tuple = time.localtime()
    305299        strf_output = time.strftime("%Z")  #UTC does not have a timezone
     
    307301        locale_time = _strptime.LocaleTime()
    308302        if time.tzname[0] != time.tzname[1] or not time.daylight:
    309             self.failUnless(strp_output[8] == time_tuple[8],
     303            self.assertTrue(strp_output[8] == time_tuple[8],
    310304                            "timezone check failed; '%s' -> %s != %s" %
    311305                             (strf_output, strp_output[8], time_tuple[8]))
    312306        else:
    313             self.failUnless(strp_output[8] == -1,
     307            self.assertTrue(strp_output[8] == -1,
    314308                            "LocaleTime().timezone has duplicate values and "
    315309                             "time.daylight but timezone value not set to -1")
     
    318312        # Explicitly test possibility of bad timezone;
    319313        # when time.tzname[0] == time.tzname[1] and time.daylight
    320         if sys.platform == "mac":
    321             return #MacOS9 has severely broken timezone support.
    322314        tz_name = time.tzname[0]
    323315        if tz_name.upper() in ("UTC", "GMT"):
     
    329321            time.daylight = 1
    330322            tz_value = _strptime._strptime_time(tz_name, "%Z")[8]
    331             self.failUnlessEqual(tz_value, -1,
     323            self.assertEqual(tz_value, -1,
    332324                    "%s lead to a timezone value of %s instead of -1 when "
    333325                    "time.daylight set to %s and passing in %s" %
     
    356348        strf_output = time.strftime("%m %% %Y", self.time_tuple)
    357349        strp_output = _strptime._strptime_time(strf_output, "%m %% %Y")
    358         self.failUnless(strp_output[0] == self.time_tuple[0] and
     350        self.assertTrue(strp_output[0] == self.time_tuple[0] and
    359351                         strp_output[1] == self.time_tuple[1],
    360352                        "handling of percent sign failed")
     
    363355        # Should handle names case-insensitively.
    364356        strf_output = time.strftime("%B", self.time_tuple)
    365         self.failUnless(_strptime._strptime_time(strf_output.upper(), "%B"),
     357        self.assertTrue(_strptime._strptime_time(strf_output.upper(), "%B"),
    366358                        "strptime does not handle ALL-CAPS names properly")
    367         self.failUnless(_strptime._strptime_time(strf_output.lower(), "%B"),
     359        self.assertTrue(_strptime._strptime_time(strf_output.lower(), "%B"),
    368360                        "strptime does not handle lowercase names properly")
    369         self.failUnless(_strptime._strptime_time(strf_output.capitalize(), "%B"),
     361        self.assertTrue(_strptime._strptime_time(strf_output.capitalize(), "%B"),
    370362                        "strptime does not handle capword names properly")
    371363
     
    374366        defaults = (1900, 1, 1, 0, 0, 0, 0, 1, -1)
    375367        strp_output = _strptime._strptime_time('1', '%m')
    376         self.failUnless(strp_output == defaults,
     368        self.assertTrue(strp_output == defaults,
    377369                        "Default values for strptime() are incorrect;"
    378370                        " %s != %s" % (strp_output, defaults))
     
    385377        # Test instigated by bug #796149 .
    386378        need_escaping = ".^$*+?{}\[]|)("
    387         self.failUnless(_strptime._strptime_time(need_escaping, need_escaping))
     379        self.assertTrue(_strptime._strptime_time(need_escaping, need_escaping))
     380
     381    def test_feb29_on_leap_year_without_year(self):
     382        time.strptime("Feb 29", "%b %d")
     383
     384    def test_mar1_comes_after_feb29_even_when_omitting_the_year(self):
     385        self.assertLess(
     386                time.strptime("Feb 29", "%b %d"),
     387                time.strptime("Mar 1", "%b %d"))
    388388
    389389class Strptime12AMPMTests(unittest.TestCase):
     
    418418        result = _strptime._strptime_time(time.strftime(format_string, self.time_tuple),
    419419                                    format_string)
    420         self.failUnless(result.tm_yday == self.time_tuple.tm_yday,
     420        self.assertTrue(result.tm_yday == self.time_tuple.tm_yday,
    421421                        "Calculation of tm_yday failed; %s != %s" %
    422422                         (result.tm_yday, self.time_tuple.tm_yday))
     
    427427        result = _strptime._strptime_time(time.strftime(format_string, self.time_tuple),
    428428                                    format_string)
    429         self.failUnless(result.tm_year == self.time_tuple.tm_year and
     429        self.assertTrue(result.tm_year == self.time_tuple.tm_year and
    430430                         result.tm_mon == self.time_tuple.tm_mon and
    431431                         result.tm_mday == self.time_tuple.tm_mday,
     
    441441        result = _strptime._strptime_time(time.strftime(format_string, self.time_tuple),
    442442                                    format_string)
    443         self.failUnless(result.tm_wday == self.time_tuple.tm_wday,
     443        self.assertTrue(result.tm_wday == self.time_tuple.tm_wday,
    444444                        "Calculation of day of the week failed;"
    445445                         "%s != %s" % (result.tm_wday, self.time_tuple.tm_wday))
     
    454454                strp_input = dt_date.strftime(format_string)
    455455                strp_output = _strptime._strptime_time(strp_input, format_string)
    456                 self.failUnless(strp_output[:3] == ymd_tuple,
     456                self.assertTrue(strp_output[:3] == ymd_tuple,
    457457                        "%s(%s) test failed w/ '%s': %s != %s (%s != %s)" %
    458458                            (test_reason, directive, strp_input,
     
    495495        _strptime._strptime_time("2005", "%Y")
    496496        _strptime._TimeRE_cache.locale_time.lang = "Ni"
    497         original_time_re = id(_strptime._TimeRE_cache)
     497        original_time_re = _strptime._TimeRE_cache
    498498        _strptime._strptime_time("10", "%d")
    499         self.failIfEqual(original_time_re, id(_strptime._TimeRE_cache))
    500         self.failUnlessEqual(len(_strptime._regex_cache), 1)
     499        self.assertIsNot(original_time_re, _strptime._TimeRE_cache)
     500        self.assertEqual(len(_strptime._regex_cache), 1)
    501501
    502502    def test_regex_cleanup(self):
     
    511511            bogus_key += 1
    512512        _strptime._strptime_time("10", "%d")
    513         self.failUnlessEqual(len(_strptime._regex_cache), 1)
     513        self.assertEqual(len(_strptime._regex_cache), 1)
    514514
    515515    def test_new_localetime(self):
    516516        # A new LocaleTime instance should be created when a new TimeRE object
    517517        # is created.
    518         locale_time_id = id(_strptime._TimeRE_cache.locale_time)
     518        locale_time_id = _strptime._TimeRE_cache.locale_time
    519519        _strptime._TimeRE_cache.locale_time.lang = "Ni"
    520520        _strptime._strptime_time("10", "%d")
    521         self.failIfEqual(locale_time_id,
    522                          id(_strptime._TimeRE_cache.locale_time))
     521        self.assertIsNot(locale_time_id, _strptime._TimeRE_cache.locale_time)
    523522
    524523    def test_TimeRE_recreation(self):
     
    532531            _strptime._strptime_time('10', '%d')
    533532            # Get id of current cache object.
    534             first_time_re_id = id(_strptime._TimeRE_cache)
     533            first_time_re = _strptime._TimeRE_cache
    535534            try:
    536535                # Change the locale and force a recreation of the cache.
     
    538537                _strptime._strptime_time('10', '%d')
    539538                # Get the new cache object's id.
    540                 second_time_re_id = id(_strptime._TimeRE_cache)
     539                second_time_re = _strptime._TimeRE_cache
    541540                # They should not be equal.
    542                 self.failIfEqual(first_time_re_id, second_time_re_id)
     541                self.assertIsNot(first_time_re, second_time_re)
    543542            # Possible test locale is not supported while initial locale is.
    544543            # If this is the case just suppress the exception and fall-through
    545             # to the reseting to the original locale.
     544            # to the resetting to the original locale.
    546545            except locale.Error:
    547546                pass
Note: See TracChangeset for help on using the changeset viewer.