I was recently working with some doctests and noticed the following
weirdness. Any idea as to what might be going on. I've tested this
with sage 4.8 and 5.0.
The issue seems to be with the use of double triple quotes vs single
triple quotes for the doctest. I'm not sure why it makes a
difference, but it seems to be the difference between a pass and a
fail.
Best regards,
Ryan Grout
Nesting single quotes like this is bound to fail in surprising ways.
This test isn't even run.
> #this test case will fail
> def TestCase2():
> r"""
> Examples::
>
> sage: ascii()
> 'abcdefghijklmnopqrstuvwxyz'
>
> """
> pass
This one fails because the doctest framework doesn't do imports for you.
Assuming you stick this in devel/sage-main/sage/ascii_test.py *and run
sage -b* you can do,
def ascii():
r"""
Examples::
sage: from sage.ascii_test import ascii
sage: ascii()
'abcdefghijklmnopqrstuvwxyz'
"""
return string.ascii_lowercase
Note the import within the doctest.
I think the problem isn't the nesting of single quote, but just using
singleq quotes ''' at all in a doctest. I think the doctest script
only extracts tests with double quotes: """.
>
>
>> #this test case will fail
>> def TestCase2():
>> r"""
>> Examples::
>>
>> sage: ascii()
>> 'abcdefghijklmnopqrstuvwxyz'
>>
>> """
>> pass
>
> This one fails because the doctest framework doesn't do imports for you.
>
> Assuming you stick this in devel/sage-main/sage/ascii_test.py *and run
> sage -b* you can do,
>
> def ascii():
> r"""
>
> Examples::
>
> sage: from sage.ascii_test import ascii
> sage: ascii()
> 'abcdefghijklmnopqrstuvwxyz'
>
> """
> return string.ascii_lowercase
>
>
> Note the import within the doctest.
>
> --
> To post to this group, send an email to sage-...@googlegroups.com
> To unsubscribe from this group, send an email to sage-devel+...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/sage-devel
> URL: http://www.sagemath.org
--
William Stein
Professor of Mathematics
University of Washington
http://wstein.org
On Mon, Feb 13, 2012 at 11:10 AM, Michael Orlitzky wrote:
> On 02/13/12 13:51, Ryan wrote:
>> #this test case will pass
>> def TestCase1():
>> r'''
>> Examples::
>>
>> sage: ascii()
>> 'abcdefghijklmnopqrstuvwxyz'
>>
>> '''
>> pass
>
>
> Nesting single quotes like this is bound to fail in surprising ways.
> This test isn't even run.I think the problem isn't the nesting of single quote, but just using
singleq quotes ''' at all in a doctest. I think the doctest script
only extracts tests with double quotes: """.
There is some[1] magic, but I gather this was a new file? Unless you add
your module to sage.all somehow, "from sage.all import *" won't pull it in.
You can see examples of this in the sage library, for example,
def is_Field(x):
"""
Return True if x is a field.
EXAMPLES::
sage: from sage.rings.ring import is_Field
sage: is_Field(QQ)
True
sage: is_Field(ZZ)
False
sage: is_Field(pAdicField(2))
True
sage: is_Field(5)
False
"""
[1]
http://www.sagemath.org/doc/developer/conventions.html#testing-py-pyx-and-sage-files
I was positive we had some way to run doctests on external files based
on what they defined. I just tried a test and it didn't seem to work.
I know there has been discussion in the past about how to get doctests
in external files working well.
Ryan: You might use something like nose to have a "real" testing
framework. It can even run doctests for you too. Lots of other python
projects use nose pretty heavily.
Jason
I was positive we had some way to run doctests on external files based on what they defined. I just tried a test and it didn't seem to work. I know there has been discussion in the past about how to get doctests in external files working well.