Skip to content

Commit b16cab9

Browse files
committed
improve docs & tests
1 parent 19c440d commit b16cab9

File tree

2 files changed

+77
-10
lines changed

2 files changed

+77
-10
lines changed

Doc/library/doctest.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,29 @@ doctest decides whether actual output matches an example's expected output:
689689
'the string
690690
to split'
691691

692+
Note that any leading whitespaces on each expected output line are retained.
693+
In other words, the following expected outputs are equivalent under the
694+
:data:`!IGNORE_LINEBREAK`:
695+
696+
[
697+
'a', 'b', 'c',
698+
'1', '2', '3'
699+
]
700+
701+
[ 'a', 'b', 'c', '1', '2', '3']
702+
703+
To break a list-like output with :data:`!IGNORE_LINEBREAK`,
704+
leading whitespaces for visual indentation purposes should
705+
be avoided, for instance:
706+
707+
.. code-block:: pycon
708+
709+
>>> list("abc123") # doctest: +IGNORE_LINEBREAK
710+
['a', 'b', 'c',
711+
'1', '2', '3']
712+
713+
For more complex outputs, consider using :func:`pprint.pprint` instead.
714+
692715
.. versionadded:: next
693716

694717

Lib/test/test_doctest/test_doctest.py

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,17 +1396,23 @@ def optionflags(): r"""
13961396
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
13971397
10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
13981398
1399-
The IGNORE_LINEBREAK flag causes all sequences of newlines to be removed:
1399+
The IGNORE_LINEBREAK flag causes all sequences of newlines to be removed,
1400+
but retains the leading whitespaces as they cannot be distinguished from
1401+
real textual whitespaces:
14001402
1401-
>>> def f(x):
1402-
... '\n>>> "foobar"\n\'foo\nbar\''
1403+
>>> def f(x): pass
1404+
>>> f.__doc__ = '''
1405+
... >>> "foobar"
1406+
... 'foo
1407+
... bar'
1408+
... '''.strip()
14031409
14041410
>>> # Without the flag:
14051411
>>> test = doctest.DocTestFinder().find(f)[0]
14061412
>>> doctest.DocTestRunner(verbose=False).run(test)
14071413
... # doctest: +ELLIPSIS
14081414
**********************************************************************
1409-
File ..., line 3, in f
1415+
File ..., line ?, in f
14101416
Failed example:
14111417
"foobar"
14121418
Expected:
@@ -1454,12 +1460,50 @@ def optionflags(): r"""
14541460
14551461
... mixing flags:
14561462
1457-
>>> import string
1458-
>>> print(list(string.ascii_letters)) # doctest: +IGNORE_LINEBREAK
1459-
... # doctest: +ELLIPSIS
1460-
... # doctest: +NORMALIZE_WHITESPACE
1461-
['a', ..., 'z',
1462-
'A', ..., 'Z']
1463+
>>> print(list("abc123")) # doctest: +IGNORE_LINEBREAK
1464+
... # doctest: +ELLIPSIS
1465+
... # doctest: +NORMALIZE_WHITESPACE
1466+
['a', ..., 'c',
1467+
'1', ..., '3']
1468+
1469+
>>> prelude = r'''
1470+
... >>> print(list("abc123")) # doctest: +IGNORE_LINEBREAK
1471+
... ... # doctest: +ELLIPSIS
1472+
... ... # doctest: +NORMALIZE_WHITESPACE
1473+
... '''.strip()
1474+
1475+
>>> def good(x): pass
1476+
>>> good.__doc__ = '\n'.join([prelude, r'''
1477+
... ['a', ..., 'c',
1478+
... '1', ..., '3']
1479+
... '''.lstrip()]).lstrip()
1480+
>>> test = doctest.DocTestFinder().find(good)[0]
1481+
>>> doctest.DocTestRunner(verbose=False).run(test)
1482+
TestResults(failed=0, attempted=1)
1483+
1484+
>>> def fail(x): pass
1485+
>>> fail.__doc__ = '\n'.join([prelude, '''
1486+
... [
1487+
... 'a', ..., 'c',
1488+
... '1', ..., '3'
1489+
... ]\n'''.lstrip()])
1490+
>>> test = doctest.DocTestFinder().find(fail)[0]
1491+
>>> doctest.DocTestRunner(verbose=False).run(test)
1492+
... # doctest: +ELLIPSIS
1493+
**********************************************************************
1494+
File ..., line ?, in fail
1495+
Failed example:
1496+
print(list("abc123")) # doctest: +IGNORE_LINEBREAK
1497+
# doctest: +ELLIPSIS
1498+
# doctest: +NORMALIZE_WHITESPACE
1499+
Expected:
1500+
[
1501+
'a', ..., 'c',
1502+
'1', ..., '3'
1503+
]
1504+
Got:
1505+
['a', 'b', 'c', '1', '2', '3']
1506+
TestResults(failed=1, attempted=1)
14631507
14641508
The ELLIPSIS flag causes ellipsis marker ("...") in the expected
14651509
output to match any substring in the actual output:

0 commit comments

Comments
 (0)