doctest weirdness

32 views
Skip to first unread message

Ryan

unread,
Feb 13, 2012, 1:51:15 PM2/13/12
to sage-devel
Hi,

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

testcase.py

Michael Orlitzky

unread,
Feb 13, 2012, 2:10:53 PM2/13/12
to sage-...@googlegroups.com
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.


> #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.

William Stein

unread,
Feb 13, 2012, 2:16:54 PM2/13/12
to sage-...@googlegroups.com
On Mon, Feb 13, 2012 at 11:10 AM, Michael Orlitzky <mic...@orlitzky.com> 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: """.

>
>
>> #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

John H Palmieri

unread,
Feb 13, 2012, 3:42:04 PM2/13/12
to sage-...@googlegroups.com


On Monday, February 13, 2012 11:16:54 AM UTC-8, William wrote:
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: """.


Yes, that's right.  See

  <http://trac.sagemath.org/sage_trac/ticket/8708>

--
John

R. Grout

unread,
Feb 13, 2012, 3:46:22 PM2/13/12
to sage-devel
so if the doctest framework doesn't do imports, do I have to import
everything I use in each doctest?

Michael Orlitzky

unread,
Feb 13, 2012, 4:12:25 PM2/13/12
to sage-...@googlegroups.com
On 02/13/12 15:46, R. Grout wrote:
> so if the doctest framework doesn't do imports, do I have to import
> everything I use in each doctest?

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

R. Grout

unread,
Feb 13, 2012, 7:39:59 PM2/13/12
to sage-devel
Thanks! I'll import each thing and hope it works.
> [1]http://www.sagemath.org/doc/developer/conventions.html#testing-py-pyx...

Jason Grout

unread,
Feb 13, 2012, 9:02:16 PM2/13/12
to sage-...@googlegroups.com
On 2/13/12 3:12 PM, Michael Orlitzky wrote:
> On 02/13/12 15:46, R. Grout wrote:
>> so if the doctest framework doesn't do imports, do I have to import
>> everything I use in each doctest?
>
> 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.
>

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

David Roe

unread,
Feb 13, 2012, 9:26:39 PM2/13/12
to sage-...@googlegroups.com

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.

Yeah, this should work in external files.   There was a ticket that addressed doctesting external files (#9739), but it was merged in 4.7.2.
David
Reply all
Reply to author
Forward
0 new messages