Back to index

python3.2  3.2.2
Public Member Functions | Private Member Functions
doctest.OutputChecker Class Reference

List of all members.

Public Member Functions

def check_output
def output_difference

Private Member Functions

def _toAscii
def _do_a_fancy_diff

Detailed Description

A class used to check the whether the actual output from a doctest
example matches the expected output.  `OutputChecker` defines two
methods: `check_output`, which compares a given pair of outputs,
and returns true if they match; and `output_difference`, which
returns a string describing the differences between two outputs.

Definition at line 1480 of file doctest.py.


Member Function Documentation

def doctest.OutputChecker._do_a_fancy_diff (   self,
  want,
  got,
  optionflags 
) [private]

Definition at line 1557 of file doctest.py.

01557 
01558     def _do_a_fancy_diff(self, want, got, optionflags):
01559         # Not unless they asked for a fancy diff.
01560         if not optionflags & (REPORT_UDIFF |
01561                               REPORT_CDIFF |
01562                               REPORT_NDIFF):
01563             return False
01564 
01565         # If expected output uses ellipsis, a meaningful fancy diff is
01566         # too hard ... or maybe not.  In two real-life failures Tim saw,
01567         # a diff was a major help anyway, so this is commented out.
01568         # [todo] _ellipsis_match() knows which pieces do and don't match,
01569         # and could be the basis for a kick-ass diff in this case.
01570         ##if optionflags & ELLIPSIS and ELLIPSIS_MARKER in want:
01571         ##    return False
01572 
01573         # ndiff does intraline difference marking, so can be useful even
01574         # for 1-line differences.
01575         if optionflags & REPORT_NDIFF:
01576             return True
01577 
01578         # The other diff types need at least a few lines to be helpful.
01579         return want.count('\n') > 2 and got.count('\n') > 2

Here is the caller graph for this function:

def doctest.OutputChecker._toAscii (   self,
  s 
) [private]
Convert string to hex-escaped ASCII string.

Definition at line 1488 of file doctest.py.

01488 
01489     def _toAscii(self, s):
01490         """
01491         Convert string to hex-escaped ASCII string.
01492         """
01493         return str(s.encode('ASCII', 'backslashreplace'), "ASCII")

Here is the caller graph for this function:

def doctest.OutputChecker.check_output (   self,
  want,
  got,
  optionflags 
)
Return True iff the actual output from an example (`got`)
matches the expected output (`want`).  These strings are
always considered to match if they are identical; but
depending on what option flags the test runner is using,
several non-exact match types are also possible.  See the
documentation for `TestRunner` for more information about
option flags.

Definition at line 1494 of file doctest.py.

01494 
01495     def check_output(self, want, got, optionflags):
01496         """
01497         Return True iff the actual output from an example (`got`)
01498         matches the expected output (`want`).  These strings are
01499         always considered to match if they are identical; but
01500         depending on what option flags the test runner is using,
01501         several non-exact match types are also possible.  See the
01502         documentation for `TestRunner` for more information about
01503         option flags.
01504         """
01505 
01506         # If `want` contains hex-escaped character such as "\u1234",
01507         # then `want` is a string of six characters(e.g. [\,u,1,2,3,4]).
01508         # On the other hand, `got` could be an another sequence of
01509         # characters such as [\u1234], so `want` and `got` should
01510         # be folded to hex-escaped ASCII string to compare.
01511         got = self._toAscii(got)
01512         want = self._toAscii(want)
01513 
01514         # Handle the common case first, for efficiency:
01515         # if they're string-identical, always return true.
01516         if got == want:
01517             return True
01518 
01519         # The values True and False replaced 1 and 0 as the return
01520         # value for boolean comparisons in Python 2.3.
01521         if not (optionflags & DONT_ACCEPT_TRUE_FOR_1):
01522             if (got,want) == ("True\n", "1\n"):
01523                 return True
01524             if (got,want) == ("False\n", "0\n"):
01525                 return True
01526 
01527         # <BLANKLINE> can be used as a special sequence to signify a
01528         # blank line, unless the DONT_ACCEPT_BLANKLINE flag is used.
01529         if not (optionflags & DONT_ACCEPT_BLANKLINE):
01530             # Replace <BLANKLINE> in want with a blank line.
01531             want = re.sub('(?m)^%s\s*?$' % re.escape(BLANKLINE_MARKER),
01532                           '', want)
01533             # If a line in got contains only spaces, then remove the
01534             # spaces.
01535             got = re.sub('(?m)^\s*?$', '', got)
01536             if got == want:
01537                 return True
01538 
01539         # This flag causes doctest to ignore any differences in the
01540         # contents of whitespace strings.  Note that this can be used
01541         # in conjunction with the ELLIPSIS flag.
01542         if optionflags & NORMALIZE_WHITESPACE:
01543             got = ' '.join(got.split())
01544             want = ' '.join(want.split())
01545             if got == want:
01546                 return True
01547 
01548         # The ELLIPSIS flag says to let the sequence "..." in `want`
01549         # match any substring in `got`.
01550         if optionflags & ELLIPSIS:
01551             if _ellipsis_match(want, got):
01552                 return True
01553 
01554         # We didn't find any match; return false.
01555         return False

Here is the call graph for this function:

def doctest.OutputChecker.output_difference (   self,
  example,
  got,
  optionflags 
)
Return a string describing the differences between the
expected output for a given example (`example`) and the actual
output (`got`).  `optionflags` is the set of option flags used
to compare `want` and `got`.

Definition at line 1580 of file doctest.py.

01580 
01581     def output_difference(self, example, got, optionflags):
01582         """
01583         Return a string describing the differences between the
01584         expected output for a given example (`example`) and the actual
01585         output (`got`).  `optionflags` is the set of option flags used
01586         to compare `want` and `got`.
01587         """
01588         want = example.want
01589         # If <BLANKLINE>s are being used, then replace blank lines
01590         # with <BLANKLINE> in the actual output string.
01591         if not (optionflags & DONT_ACCEPT_BLANKLINE):
01592             got = re.sub('(?m)^[ ]*(?=\n)', BLANKLINE_MARKER, got)
01593 
01594         # Check if we should use diff.
01595         if self._do_a_fancy_diff(want, got, optionflags):
01596             # Split want & got into lines.
01597             want_lines = want.splitlines(True)  # True == keep line ends
01598             got_lines = got.splitlines(True)
01599             # Use difflib to find their differences.
01600             if optionflags & REPORT_UDIFF:
01601                 diff = difflib.unified_diff(want_lines, got_lines, n=2)
01602                 diff = list(diff)[2:] # strip the diff header
01603                 kind = 'unified diff with -expected +actual'
01604             elif optionflags & REPORT_CDIFF:
01605                 diff = difflib.context_diff(want_lines, got_lines, n=2)
01606                 diff = list(diff)[2:] # strip the diff header
01607                 kind = 'context diff with expected followed by actual'
01608             elif optionflags & REPORT_NDIFF:
01609                 engine = difflib.Differ(charjunk=difflib.IS_CHARACTER_JUNK)
01610                 diff = list(engine.compare(want_lines, got_lines))
01611                 kind = 'ndiff with -expected +actual'
01612             else:
01613                 assert 0, 'Bad diff option'
01614             # Remove trailing whitespace on diff output.
01615             diff = [line.rstrip() + '\n' for line in diff]
01616             return 'Differences (%s):\n' % kind + _indent(''.join(diff))
01617 
01618         # If we're not using diff, then simply list the expected
01619         # output followed by the actual output.
01620         if want and got:
01621             return 'Expected:\n%sGot:\n%s' % (_indent(want), _indent(got))
01622         elif want:
01623             return 'Expected:\n%sGot nothing\n' % _indent(want)
01624         elif got:
01625             return 'Expected nothing\nGot:\n%s' % _indent(got)
01626         else:
01627             return 'Expected nothing\nGot nothing\n'

Here is the call graph for this function:


The documentation for this class was generated from the following file: