@@ -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
303303A 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,
305305which 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
0 commit comments