mirror of
https://github.com/python/cpython.git
synced 2025-08-10 03:49:18 +00:00
[3.12] gh-111159: Fix doctest
output comparison for exceptions with notes (GH-111160) (#111169)
gh-111159: Fix `doctest` output comparison for exceptions with notes (GH-111160)
(cherry picked from commit fd60549c0a
)
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
This commit is contained in:
parent
1ea93024d4
commit
f6cde99bdb
3 changed files with 159 additions and 1 deletions
|
@ -3168,6 +3168,150 @@ def test_run_doctestsuite_multiple_times():
|
|||
"""
|
||||
|
||||
|
||||
def test_exception_with_note(note):
|
||||
"""
|
||||
>>> test_exception_with_note('Note')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: Text
|
||||
Note
|
||||
|
||||
>>> test_exception_with_note('Note') # doctest: +IGNORE_EXCEPTION_DETAIL
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: Text
|
||||
Note
|
||||
|
||||
>>> test_exception_with_note('''Note
|
||||
... multiline
|
||||
... example''')
|
||||
Traceback (most recent call last):
|
||||
ValueError: Text
|
||||
Note
|
||||
multiline
|
||||
example
|
||||
|
||||
Different note will fail the test:
|
||||
|
||||
>>> def f(x):
|
||||
... r'''
|
||||
... >>> exc = ValueError('message')
|
||||
... >>> exc.add_note('note')
|
||||
... >>> raise exc
|
||||
... Traceback (most recent call last):
|
||||
... ValueError: message
|
||||
... wrong note
|
||||
... '''
|
||||
>>> test = doctest.DocTestFinder().find(f)[0]
|
||||
>>> doctest.DocTestRunner(verbose=False).run(test)
|
||||
... # doctest: +ELLIPSIS
|
||||
**********************************************************************
|
||||
File "...", line 5, in f
|
||||
Failed example:
|
||||
raise exc
|
||||
Expected:
|
||||
Traceback (most recent call last):
|
||||
ValueError: message
|
||||
wrong note
|
||||
Got:
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: message
|
||||
note
|
||||
TestResults(failed=1, attempted=...)
|
||||
"""
|
||||
exc = ValueError('Text')
|
||||
exc.add_note(note)
|
||||
raise exc
|
||||
|
||||
|
||||
def test_exception_with_multiple_notes():
|
||||
"""
|
||||
>>> test_exception_with_multiple_notes()
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: Text
|
||||
One
|
||||
Two
|
||||
"""
|
||||
exc = ValueError('Text')
|
||||
exc.add_note('One')
|
||||
exc.add_note('Two')
|
||||
raise exc
|
||||
|
||||
|
||||
def test_syntax_error_with_note(cls, multiline=False):
|
||||
"""
|
||||
>>> test_syntax_error_with_note(SyntaxError)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
SyntaxError: error
|
||||
Note
|
||||
|
||||
>>> test_syntax_error_with_note(SyntaxError)
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: error
|
||||
Note
|
||||
|
||||
>>> test_syntax_error_with_note(SyntaxError)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
File "x.py", line 23
|
||||
bad syntax
|
||||
SyntaxError: error
|
||||
Note
|
||||
|
||||
>>> test_syntax_error_with_note(IndentationError)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
IndentationError: error
|
||||
Note
|
||||
|
||||
>>> test_syntax_error_with_note(TabError, multiline=True)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TabError: error
|
||||
Note
|
||||
Line
|
||||
"""
|
||||
exc = cls("error", ("x.py", 23, None, "bad syntax"))
|
||||
exc.add_note('Note\nLine' if multiline else 'Note')
|
||||
raise exc
|
||||
|
||||
|
||||
def test_syntax_error_with_incorrect_expected_note():
|
||||
"""
|
||||
>>> def f(x):
|
||||
... r'''
|
||||
... >>> exc = SyntaxError("error", ("x.py", 23, None, "bad syntax"))
|
||||
... >>> exc.add_note('note1')
|
||||
... >>> exc.add_note('note2')
|
||||
... >>> raise exc
|
||||
... Traceback (most recent call last):
|
||||
... SyntaxError: error
|
||||
... wrong note
|
||||
... '''
|
||||
>>> test = doctest.DocTestFinder().find(f)[0]
|
||||
>>> doctest.DocTestRunner(verbose=False).run(test)
|
||||
... # doctest: +ELLIPSIS
|
||||
**********************************************************************
|
||||
File "...", line 6, in f
|
||||
Failed example:
|
||||
raise exc
|
||||
Expected:
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: error
|
||||
wrong note
|
||||
Got:
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
SyntaxError: error
|
||||
note1
|
||||
note2
|
||||
TestResults(failed=1, attempted=...)
|
||||
"""
|
||||
|
||||
|
||||
def load_tests(loader, tests, pattern):
|
||||
tests.addTest(doctest.DocTestSuite(doctest))
|
||||
tests.addTest(doctest.DocTestSuite())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue