diff --git a/Lib/doctest.py b/Lib/doctest.py index 92a2ab4f7e66f8..fd49d1d7a9f3dc 100644 --- a/Lib/doctest.py +++ b/Lib/doctest.py @@ -63,6 +63,7 @@ def _test(): 'REPORT_ONLY_FIRST_FAILURE', 'REPORTING_FLAGS', 'FAIL_FAST', + 'IGNORE_CASE', # 1. Utility Functions # 2. Example & DocTest 'Example', @@ -159,13 +160,15 @@ def register_optionflag(name): ELLIPSIS = register_optionflag('ELLIPSIS') SKIP = register_optionflag('SKIP') IGNORE_EXCEPTION_DETAIL = register_optionflag('IGNORE_EXCEPTION_DETAIL') +IGNORE_CASE = register_optionflag('IGNORE_CASE') COMPARISON_FLAGS = (DONT_ACCEPT_TRUE_FOR_1 | DONT_ACCEPT_BLANKLINE | NORMALIZE_WHITESPACE | ELLIPSIS | SKIP | - IGNORE_EXCEPTION_DETAIL) + IGNORE_EXCEPTION_DETAIL | + IGNORE_CASE) REPORT_UDIFF = register_optionflag('REPORT_UDIFF') REPORT_CDIFF = register_optionflag('REPORT_CDIFF') @@ -1731,12 +1734,22 @@ def check_output(self, want, got, optionflags): if got == want: return True + # Ignore case if flag + # Lowercase got and want + true_line = "True\n" + false_line = "False\n" + if (optionflags & IGNORE_CASE): + got = got.casefold() + want = want.casefold() + true_line = "true\n" + false_line = "false\n" + # The values True and False replaced 1 and 0 as the return # value for boolean comparisons in Python 2.3. if not (optionflags & DONT_ACCEPT_TRUE_FOR_1): - if (got,want) == ("True\n", "1\n"): + if (got, want) == (true_line, "1\n"): return True - if (got,want) == ("False\n", "0\n"): + if (got, want) == (false_line, "0\n"): return True # can be used as a special sequence to signify a diff --git a/Lib/test/test_doctest/test_doctest.py b/Lib/test/test_doctest/test_doctest.py index 0fa74407e3c436..f567746b8091d3 100644 --- a/Lib/test/test_doctest/test_doctest.py +++ b/Lib/test/test_doctest/test_doctest.py @@ -1447,6 +1447,26 @@ def optionflags(): r""" ... # doctest: +NORMALIZE_WHITESPACE [0, 1, ..., 18, 19] +The IGNORE_CASE flag causes the test runner to ignore case when +matching the expected output to the actual output. + + >>> print("HeLlO WoRlD") # doctest: +IGNORE_CASE + hello world + +It can resolve the issues with POSIX and Windows NAN and other types. +On Windows this should fail without the flag. + + >>> float("NaN") # doctest: +IGNORE_CASE + nan + +It also works with boolean values. + + >>> print("tRuE") # doctest: +IGNORE_CASE + 1 + >>> print("FaLsE") # doctest: +IGNORE_CASE + 0 + + The SKIP flag causes an example to be skipped entirely. I.e., the example is not run. It can be useful in contexts where doctest examples serve as both documentation and test cases, and an example diff --git a/Misc/NEWS.d/next/Library/2025-09-02-03-18-00.bpo-13337.rSk6U7.rst b/Misc/NEWS.d/next/Library/2025-09-02-03-18-00.bpo-13337.rSk6U7.rst new file mode 100644 index 00000000000000..e4356aacd307ff --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-09-02-03-18-00.bpo-13337.rSk6U7.rst @@ -0,0 +1 @@ +Adds the :flag:`~doctest.IGNORE_CASE` flag to the :mod:`doctest` module. Patch by Justin Baum.