Skip to content
19 changes: 16 additions & 3 deletions Lib/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def _test():
'REPORT_ONLY_FIRST_FAILURE',
'REPORTING_FLAGS',
'FAIL_FAST',
'IGNORE_CASE',
# 1. Utility Functions
# 2. Example & DocTest
'Example',
Expand Down Expand Up @@ -139,13 +140,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')
Expand Down Expand Up @@ -1607,12 +1610,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.lower()
want = want.lower()
Comment thread
justinba1010 marked this conversation as resolved.
Outdated
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

# <BLANKLINE> can be used as a special sequence to signify a
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,12 @@ 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


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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Adds an `ignore_case` flag to the `doctest` library.
Comment thread
justinba1010 marked this conversation as resolved.
Outdated