Kaydet (Commit) 38330fe5 authored tarafından Tim Peters's avatar Tim Peters

The distinction between comparison flags and reporting flags isn't unique

to unittest, so make it official:  new module constants COMPARISON_FLAGS
and REPORTING_FLAGS, which are bitmasks or'ing together the relevant
individual option flags.

set_unittest_reportflags():  Reworked to use REPORTING_FLAGS, and
simplified overly complicated flag logic.

class FakeModule:  Removed this; neither documented nor used.
üst ed047486
...@@ -365,6 +365,10 @@ example's expected output: ...@@ -365,6 +365,10 @@ example's expected output:
is prone to in regular expressions. is prone to in regular expressions.
\end{datadesc} \end{datadesc}
\begin{datadesc}{COMPARISON_FLAGS}
A bitmask or'ing together all the comparison flags above.
\end{datadesc}
The second group of options controls how test failures are reported: The second group of options controls how test failures are reported:
\begin{datadesc}{REPORT_UDIFF} \begin{datadesc}{REPORT_UDIFF}
...@@ -398,6 +402,10 @@ The second group of options controls how test failures are reported: ...@@ -398,6 +402,10 @@ The second group of options controls how test failures are reported:
failures reported; only the output is suppressed. failures reported; only the output is suppressed.
\end{datadesc} \end{datadesc}
\begin{datadesc}{REPORTING_FLAGS}
A bitmask or'ing together all the reporting flags above.
\end{datadesc}
A "doctest directive" is a trailing Python comment on a line of a doctest A "doctest directive" is a trailing Python comment on a line of a doctest
example: example:
...@@ -456,7 +464,8 @@ can be useful. ...@@ -456,7 +464,8 @@ can be useful.
\versionchanged[Constants \constant{DONT_ACCEPT_BLANKLINE}, \versionchanged[Constants \constant{DONT_ACCEPT_BLANKLINE},
\constant{NORMALIZE_WHITESPACE}, \constant{ELLIPSIS}, \constant{NORMALIZE_WHITESPACE}, \constant{ELLIPSIS},
\constant{REPORT_UDIFF}, \constant{REPORT_CDIFF}, \constant{REPORT_UDIFF}, \constant{REPORT_CDIFF},
\constant{REPORT_NDIFF}, and \constant{REPORT_ONLY_FIRST_FAILURE} \constant{REPORT_NDIFF}, \constant{REPORT_ONLY_FIRST_FAILURE},
\constant{COMPARISON_FLAGS} and \constant{REPORTING_FLAGS}
were added; by default \code{<BLANKLINE>} in expected output were added; by default \code{<BLANKLINE>} in expected output
matches an empty line in actual output; and doctest directives matches an empty line in actual output; and doctest directives
were added]{2.4} were added]{2.4}
......
...@@ -248,6 +248,7 @@ real_pdb_set_trace = pdb.set_trace ...@@ -248,6 +248,7 @@ real_pdb_set_trace = pdb.set_trace
# +---------+ # +---------+
# Option constants. # Option constants.
OPTIONFLAGS_BY_NAME = {} OPTIONFLAGS_BY_NAME = {}
def register_optionflag(name): def register_optionflag(name):
flag = 1 << len(OPTIONFLAGS_BY_NAME) flag = 1 << len(OPTIONFLAGS_BY_NAME)
...@@ -258,11 +259,22 @@ DONT_ACCEPT_TRUE_FOR_1 = register_optionflag('DONT_ACCEPT_TRUE_FOR_1') ...@@ -258,11 +259,22 @@ DONT_ACCEPT_TRUE_FOR_1 = register_optionflag('DONT_ACCEPT_TRUE_FOR_1')
DONT_ACCEPT_BLANKLINE = register_optionflag('DONT_ACCEPT_BLANKLINE') DONT_ACCEPT_BLANKLINE = register_optionflag('DONT_ACCEPT_BLANKLINE')
NORMALIZE_WHITESPACE = register_optionflag('NORMALIZE_WHITESPACE') NORMALIZE_WHITESPACE = register_optionflag('NORMALIZE_WHITESPACE')
ELLIPSIS = register_optionflag('ELLIPSIS') ELLIPSIS = register_optionflag('ELLIPSIS')
COMPARISON_FLAGS = (DONT_ACCEPT_TRUE_FOR_1 |
DONT_ACCEPT_BLANKLINE |
NORMALIZE_WHITESPACE |
ELLIPSIS)
REPORT_UDIFF = register_optionflag('REPORT_UDIFF') REPORT_UDIFF = register_optionflag('REPORT_UDIFF')
REPORT_CDIFF = register_optionflag('REPORT_CDIFF') REPORT_CDIFF = register_optionflag('REPORT_CDIFF')
REPORT_NDIFF = register_optionflag('REPORT_NDIFF') REPORT_NDIFF = register_optionflag('REPORT_NDIFF')
REPORT_ONLY_FIRST_FAILURE = register_optionflag('REPORT_ONLY_FIRST_FAILURE') REPORT_ONLY_FIRST_FAILURE = register_optionflag('REPORT_ONLY_FIRST_FAILURE')
REPORTING_FLAGS = (REPORT_UDIFF |
REPORT_CDIFF |
REPORT_NDIFF |
REPORT_ONLY_FIRST_FAILURE)
# Special string markers for use in `want` strings: # Special string markers for use in `want` strings:
BLANKLINE_MARKER = '<BLANKLINE>' BLANKLINE_MARKER = '<BLANKLINE>'
ELLIPSIS_MARKER = '...' ELLIPSIS_MARKER = '...'
...@@ -1993,14 +2005,9 @@ class Tester: ...@@ -1993,14 +2005,9 @@ class Tester:
###################################################################### ######################################################################
_unittest_reportflags = 0 _unittest_reportflags = 0
valid_unittest_reportflags = (
REPORT_CDIFF |
REPORT_UDIFF |
REPORT_NDIFF |
REPORT_ONLY_FIRST_FAILURE
)
def set_unittest_reportflags(flags): def set_unittest_reportflags(flags):
"""Sets the unit test option flags """Sets the unittest option flags.
The old flag is returned so that a runner could restore the old The old flag is returned so that a runner could restore the old
value if it wished to: value if it wished to:
...@@ -2020,37 +2027,21 @@ def set_unittest_reportflags(flags): ...@@ -2020,37 +2027,21 @@ def set_unittest_reportflags(flags):
>>> set_unittest_reportflags(ELLIPSIS) >>> set_unittest_reportflags(ELLIPSIS)
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValueError: ('Invalid flags passed', 8) ValueError: ('Only reporting flags allowed', 8)
>>> set_unittest_reportflags(old) == (REPORT_NDIFF | >>> set_unittest_reportflags(old) == (REPORT_NDIFF |
... REPORT_ONLY_FIRST_FAILURE) ... REPORT_ONLY_FIRST_FAILURE)
True True
""" """
# extract the valid reporting flags:
rflags = flags & valid_unittest_reportflags
# Now remove these flags from the given flags
nrflags = flags ^ rflags
if nrflags:
raise ValueError("Invalid flags passed", flags)
global _unittest_reportflags global _unittest_reportflags
if (flags & REPORTING_FLAGS) != flags:
raise ValueError("Only reporting flags allowed", flags)
old = _unittest_reportflags old = _unittest_reportflags
_unittest_reportflags = flags _unittest_reportflags = flags
return old return old
class FakeModule:
"""Fake module created by tests
"""
def __init__(self, dict, name):
self.__dict__ = dict
self.__name__ = name
class DocTestCase(unittest.TestCase): class DocTestCase(unittest.TestCase):
def __init__(self, test, optionflags=0, setUp=None, tearDown=None, def __init__(self, test, optionflags=0, setUp=None, tearDown=None,
...@@ -2083,7 +2074,7 @@ class DocTestCase(unittest.TestCase): ...@@ -2083,7 +2074,7 @@ class DocTestCase(unittest.TestCase):
new = StringIO() new = StringIO()
optionflags = self._dt_optionflags optionflags = self._dt_optionflags
if not (optionflags & valid_unittest_reportflags): if not (optionflags & REPORTING_FLAGS):
# The option flags don't include any reporting flags, # The option flags don't include any reporting flags,
# so add the default reporting flags # so add the default reporting flags
optionflags |= _unittest_reportflags optionflags |= _unittest_reportflags
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment