From: Thomas Jollans on
On 07/15/2010 11:14 PM, python(a)bdurham.com wrote:
> I'm working with some Python 2.6 code that is using the doctest module
> for unittests.
>
> I tried adding tests whose code and results required newlines (\n).
> Apparently newlines in string constants breaks the doctest code.
>
> Any workarounds other than using another char for newlines and wrapping
> all my strings with .replace( someChar, chr(10) )?

Recall that doctest doesn't parse the code, it extracts the docstrings.
And docstrings are just strings, and are parsed like strings.

Remember that the two following strings are identical:

"""a
b"""

"""a\nb"""

As are these two:

r"\n"
"\\n"

Anyway, you need to escape the backslash if you want backslash-n to be
in your docstring. Otherwise you'll get a newline like any other
newline, not a character sequence doctest can eval() to a newline while
processing.
From: python on
Thomas,

> Recall that doctest doesn't parse the code, it extracts the docstrings. And docstrings are just strings, and are parsed like strings.

I understand what you're saying, but I'm struggling with how to
represent the following strings in doctest code and doctest results. No
matter what combination of backslashes or raw strings I use, I am unable
to find a way to code the following.

>>> encode( '", \, \t, \n' )
'\", \\, \\t, \\n'

Thank you,
Malcolm
From: Steven D'Aprano on
On Fri, 16 Jul 2010 01:26:40 -0400, python wrote:

> Thomas,
>
>> Recall that doctest doesn't parse the code, it extracts the docstrings.
>> And docstrings are just strings, and are parsed like strings.
>
> I understand what you're saying, but I'm struggling with how to
> represent the following strings in doctest code and doctest results. No
> matter what combination of backslashes or raw strings I use, I am unable
> to find a way to code the following.
>
>>>> encode( '", \, \t, \n' )
> '\", \\, \\t, \\n'

I don't believe that this is a valid example. The interactive interpreter
prints the repr() of objects unless you explicitly call print, which you
have not done. I don't believe that there is any string whose repr() is
what you have given. Consequently, there's no way to get that output in
the interpreter (without using print), and the doctest MUST fail.

But even if I'm wrong... when you run up against the limitations of
doctests, don't use doctests. Is there any reason why you need to show
that specific example out of the infinite number of possible examples?

Remember that doctests aren't intended for a comprehensive test suite.
Use unit tests for that. Or put the tests in a plain text file, not a
docstring, and point doctest at that. That will reduce the amount of
awkward escaping needed.

If you insist on using that example, including it in the docstring should
be simple. Call up the interactive interpreter, and run this:

>>> from mymodule import encode # whatever your module happens to be
>>> encode( '", \, \t, \n' )
something prints here

Now copy the last two lines (the encode line, and the output). Paste it
into your docstring, adjusting the indentation if needed. Then edit the
two lines, doubling EVERY backslash. That should do it.

If it doesn't, please show us the code of the encode function, the
doctest failure, and the two lines copied from the interactive
interpreter.



--
Steven
From: Albert Hopkins on
On Fri, 2010-07-16 at 01:26 -0400, python(a)bdurham.com wrote:
> I understand what you're saying, but I'm struggling with how to
> represent the following strings in doctest code and doctest results.
> No
> matter what combination of backslashes or raw strings I use, I am
> unable
> to find a way to code the following.
>
> >>> encode( '", \, \t, \n' )
> '\", \\, \\t, \\n'

>>> encode(', '.join([chr(i) for i in (34, 92, 9, 10)]))

But, as another poster already said, if the limitations of doctests are
causing you to struggle, then don't use doctests and instead use "real"
tests.

From: Thomas Jollans on
On 07/16/2010 07:26 AM, python(a)bdurham.com wrote:
> Thomas,
>
>> Recall that doctest doesn't parse the code, it extracts the docstrings. And docstrings are just strings, and are parsed like strings.
>
> I understand what you're saying, but I'm struggling with how to
> represent the following strings in doctest code and doctest results. No
> matter what combination of backslashes or raw strings I use, I am unable
> to find a way to code the following.
>
>>>> encode( '", \, \t, \n' )
> '\", \\, \\t, \\n'

Does either of these docstrings work:

def encode(s):
"""description

>>> encode( '", \\, \\t, \\n' )
'\\", \\\\, \\\\t, \\\\n'
"""
...

def encode(s):
r"""description

>>> encode( '", \, \t, \n' )
'\", \\, \\t, \\n'
"""
...