Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 026f8dc

Browse files
committed
Now that they've settled down, document doctest directives.
1 parent 3caf9c1 commit 026f8dc

2 files changed

Lines changed: 83 additions & 11 deletions

File tree

Doc/lib/libdoctest.tex

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,12 @@ \subsection{What About Exceptions?}
298298
exception detail, were added]{2.4}
299299

300300

301-
\subsection{Option Flags and Directive Names\label{doctest-options}}
301+
\subsection{Option Flags and Directives\label{doctest-options}}
302302

303303
A number of option flags control various aspects of doctest's comparison
304-
behavior. Symbolic names for the flags are supplied as module constants,
304+
behavior. Symbolic names for the flags are supplied as module constants,
305305
which can be or'ed together and passed to various functions. The names
306-
can also be used in doctest directives.
306+
can also be used in doctest directives (see below).
307307

308308
\begin{datadesc}{DONT_ACCEPT_TRUE_FOR_1}
309309
By default, if an expected output block contains just \code{1},
@@ -340,9 +340,10 @@ \subsection{Option Flags and Directive Names\label{doctest-options}}
340340
\begin{datadesc}{ELLIPSIS}
341341
When specified, an ellipsis marker (\code{...}) in the expected output
342342
can match any substring in the actual output. This includes
343-
substrings that span line boundaries, so it's best to keep usage of
344-
this simple. Complicated uses can lead to the same kinds of
345-
surprises that \regexp{.*} is prone to in regular expressions.
343+
substrings that span line boundaries, and empty substrings, so it's
344+
best to keep usage of this simple. Complicated uses can lead to the
345+
same kinds of "oops, it matched too much!" surprises that \regexp{.*}
346+
is prone to in regular expressions.
346347
\end{datadesc}
347348

348349
\begin{datadesc}{UNIFIED_DIFF}
@@ -356,11 +357,68 @@ \subsection{Option Flags and Directive Names\label{doctest-options}}
356357
\end{datadesc}
357358

358359

360+
A "doctest directive" is a trailing Python comment on a line of a doctest
361+
example:
362+
363+
\begin{productionlist}[doctest]
364+
\production{directive}
365+
{"#" "doctest:" \token{on_or_off} \token{directive_name}}
366+
\production{on_or_off}
367+
{"+" | "-"}
368+
\production{directive_name}
369+
{"DONT_ACCEPT_BLANKLINE" | "NORMALIZE_WHITESPACE" | ...}
370+
\end{productionlist}
371+
372+
Whitespace is not allowed between the \code{+} or \code{-} and the
373+
directive name. The directive name can be any of the option names
374+
explained above.
375+
376+
The doctest directives appearing in a single example modify doctest's
377+
behavior for that single example. Use \code{+} to enable the named
378+
behavior, or \code{-} to disable it.
379+
380+
For example, this test passes:
381+
382+
\begin{verbatim}
383+
>>> print range(20) #doctest: +NORMALIZE_WHITESPACE
384+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
385+
10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
386+
\end{verbatim}
387+
388+
Without the directive it would fail, both because the actual output
389+
doesn't have two blanks before the single-digit list elements, and
390+
because the actual output is on a single line. This test also passes,
391+
and requires a directive to do so:
392+
393+
\begin{verbatim}
394+
>>> print range(20) # doctest:+ELLIPSIS
395+
[0, 1, ..., 18, 19]
396+
\end{verbatim}
397+
398+
Only one directive per physical line is accepted. If you want to
399+
use multiple directives for a single example, you can add
400+
\samp{...} lines to your example containing only directives:
401+
402+
\begin{verbatim}
403+
>>> print range(20) #doctest: +ELLIPSIS
404+
... #doctest: +NORMALIZE_WHITESPACE
405+
[0, 1, ..., 18, 19]
406+
\end{verbatim}
407+
408+
Note that since all options are disabled by default, and directives apply
409+
only to the example they appear in, enabling options (via \code{+} in a
410+
directive) is usually the only meaningful choice. However, option flags
411+
can also be passed to functions that run doctests, establishing different
412+
defaults. In such cases, disabling an option via \code{-} in a directive
413+
can be useful.
414+
359415
\versionchanged[Constants \constant{DONT_ACCEPT_BLANKLINE},
360416
\constant{NORMALIZE_WHITESPACE}, \constant{ELLIPSIS},
361417
\constant{UNIFIED_DIFF}, and \constant{CONTEXT_DIFF}
362-
were added, and by default \code{<BLANKLINE>} in expected output
363-
matches an empty line in actual output]{2.4}
418+
were added; by default \code{<BLANKLINE>} in expected output
419+
matches an empty line in actual output; and doctest directives
420+
were added]{2.4}
421+
364422

365423
\subsection{Advanced Usage}
366424

Lib/test/test_doctest.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,11 @@ def optionflags(): r"""
758758
>>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
759759
(0, 1)
760760
761+
An example from the docs:
762+
>>> print range(20) #doctest: +NORMALIZE_WHITESPACE
763+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
764+
10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
765+
761766
The ELLIPSIS flag causes ellipsis marker ("...") in the expected
762767
output to match any substring in the actual output:
763768
@@ -780,8 +785,8 @@ def optionflags(): r"""
780785
>>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
781786
(0, 1)
782787
783-
... should also match nothing gracefully (note that a regular-expression
784-
implementation of ELLIPSIS would take a loooong time to match this one!):
788+
... should also match nothing gracefully (note that a regular-expression
789+
implementation of ELLIPSIS would take a loooong time to match this one!):
785790
786791
>>> for i in range(100):
787792
... print i**2 #doctest: +ELLIPSIS
@@ -801,7 +806,7 @@ def optionflags(): r"""
801806
9801
802807
...
803808
804-
... can be surprising; e.g., this test passes:
809+
... can be surprising; e.g., this test passes:
805810
806811
>>> for i in range(21): #doctest: +ELLIPSIS
807812
... print i
@@ -815,6 +820,15 @@ def optionflags(): r"""
815820
...
816821
0
817822
823+
Examples from the docs:
824+
825+
>>> print range(20) # doctest:+ELLIPSIS
826+
[0, 1, ..., 18, 19]
827+
828+
>>> print range(20) # doctest: +ELLIPSIS
829+
... # doctest: +NORMALIZE_WHITESPACE
830+
[0, 1, ..., 18, 19]
831+
818832
The UNIFIED_DIFF flag causes failures that involve multi-line expected
819833
and actual outputs to be displayed using a unified diff:
820834

0 commit comments

Comments
 (0)