Skip to content

Commit

Permalink
Minor test improvements/refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
zuo committed Oct 1, 2024
1 parent b3b161f commit c16b5e3
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -2125,6 +2125,7 @@ def test_fromisocalendar_type_errors(self):
self.theclass.fromisocalendar(*isocal)

def test_strftime_strptime_roundtrip_concerning_locale_specific_year(self):
# gh-124529
concerned_formats = '%c', '%x'

def run_subtest():
Expand All @@ -2138,38 +2139,40 @@ def run_subtest():
try:
parsed = self.theclass.strptime(formatted, fmt)
except ValueError as exc:
# gh-124529
self.fail(f"{fail_msg}; parsing error: {exc!r}")
self.assertEqual(parsed, expected, fail_msg)

sample = self.theclass.strptime(f'1999-03-17', '%Y-%m-%d')
sample = self.theclass.strptime('1999', '%Y')
for fmt in concerned_formats:
with self.subTest(fmt=fmt):
sample_str = sample.strftime(fmt)
if '1999' in sample_str:
for year in [
1, 9, 10, 99, 100, 999, # <- gh-124529
1000, 1410, 1989, 2024, 2095, 9999,
# gh-124529:
1, 9, 10, 99, 100, 999,
]:
run_subtest()
elif '99' in sample_str:
for year in [
1969, 1999,
2000, 2001, 2009, # <- gh-124529
2068,
1969, 1999, 2068,
# gh-124529:
2000, 2001, 2009,
]:
run_subtest()
else:
self.fail(f"it seems that {sample.strftime(fmt)=} "
f"does not include year={sample.year!r} in "
f"any expected format (is there something "
f"severely wrong with current locale?)")
self.fail(f"it seems that sample.strftime({fmt!r})="
f"{sample_str!r} does not include year="
f"{sample.year!r} in any expected format "
f"(is there something severely wrong with "
f"current locale?)")

def test_strptime_accepting_locale_specific_year_with_fewer_digits(self):
# gh-124529
concerned_formats = '%c', '%x'

def run_subtest():
input_str = sample_str.replace(sample_digits, year_digits)
input_str = sample_str.replace(sample_year_digits, year_digits)
reason = (f"test strptime accepting locale-specific "
f"year representation with fewer digits "
f"- for {fmt=} and {input_str=} ({year=})")
Expand All @@ -2179,27 +2182,27 @@ def run_subtest():
try:
parsed = self.theclass.strptime(input_str, fmt)
except ValueError as exc:
# gh-124529
self.fail(f"{fail_msg}; parsing error: {exc!r}")
self.assertEqual(parsed, expected, fail_msg)

sample = self.theclass.strptime(f'1999-03-17', '%Y-%m-%d')
sample = self.theclass.strptime('1999', '%Y')
for fmt in concerned_formats:
with self.subTest(fmt=fmt):
sample_str = sample.strftime(fmt)
if (sample_digits := '1999') in sample_str:
if (sample_year_digits := '1999') in sample_str:
for year in [1, 9, 10, 99, 100, 999]:
year_digits = str(year)
run_subtest()
elif (sample_digits := '99') in sample_str:
elif (sample_year_digits := '99') in sample_str:
for year in [2000, 2001, 2009]:
year_digits = str(year - 2000)
run_subtest()
else:
self.fail(f"it seems that {sample.strftime(fmt)=} "
f"does not include year={sample.year!r} in "
f"any expected format (is there something "
f"severely wrong with current locale?)")
self.fail(f"it seems that sample.strftime({fmt!r})="
f"{sample_str!r} does not include year="
f"{sample.year!r} in any expected format "
f"(is there something severely wrong with "
f"current locale?)")


#############################################################################
Expand Down

0 comments on commit c16b5e3

Please sign in to comment.