MCPcopy Index your code
hub / github.com/RustPython/RustPython / output_difference

Method output_difference

Lib/doctest.py:1790–1834  ·  view source on GitHub ↗

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`.

(self, example, got, optionflags)

Source from the content-addressed store, hash-verified

1788 return want.count('\n') > 2 and got.count('\n') > 2
1789
1790 def output_difference(self, example, got, optionflags):
1791 """
1792 Return a string describing the differences between the
1793 expected output for a given example (`example`) and the actual
1794 output (`got`). `optionflags` is the set of option flags used
1795 to compare `want` and `got`.
1796 """
1797 want = example.want
1798 # If <BLANKLINE>s are being used, then replace blank lines
1799 # with <BLANKLINE> in the actual output string.
1800 if not (optionflags & DONT_ACCEPT_BLANKLINE):
1801 got = re.sub('(?m)^[ ]*(?=\n)', BLANKLINE_MARKER, got)
1802
1803 # Check if we should use diff.
1804 if self._do_a_fancy_diff(want, got, optionflags):
1805 # Split want & got into lines.
1806 want_lines = want.splitlines(keepends=True)
1807 got_lines = got.splitlines(keepends=True)
1808 # Use difflib to find their differences.
1809 if optionflags & REPORT_UDIFF:
1810 diff = difflib.unified_diff(want_lines, got_lines, n=2)
1811 diff = list(diff)[2:] # strip the diff header
1812 kind = 'unified diff with -expected +actual'
1813 elif optionflags & REPORT_CDIFF:
1814 diff = difflib.context_diff(want_lines, got_lines, n=2)
1815 diff = list(diff)[2:] # strip the diff header
1816 kind = 'context diff with expected followed by actual'
1817 elif optionflags & REPORT_NDIFF:
1818 engine = difflib.Differ(charjunk=difflib.IS_CHARACTER_JUNK)
1819 diff = list(engine.compare(want_lines, got_lines))
1820 kind = 'ndiff with -expected +actual'
1821 else:
1822 assert 0, 'Bad diff option'
1823 return 'Differences (%s):\n' % kind + _indent(''.join(diff))
1824
1825 # If we're not using diff, then simply list the expected
1826 # output followed by the actual output.
1827 if want and got:
1828 return 'Expected:\n%sGot:\n%s' % (_indent(want), _indent(got))
1829 elif want:
1830 return 'Expected:\n%sGot nothing\n' % _indent(want)
1831 elif got:
1832 return 'Expected nothing\nGot:\n%s' % _indent(got)
1833 else:
1834 return 'Expected nothing\nGot nothing\n'
1835
1836class DocTestFailure(Exception):
1837 """A DocTest example has failed in debugging mode.

Callers 1

report_failureMethod · 0.80

Calls 7

_do_a_fancy_diffMethod · 0.95
compareMethod · 0.95
listClass · 0.85
_indentFunction · 0.85
subMethod · 0.45
splitlinesMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected