Ticket #12069: trac_12069-sage.patch

File trac_12069-sage.patch, 6.7 KB (added by jhpalmieri, 10 years ago)

Sage library

  • doc/en/developer/doctesting.rst

    # HG changeset patch
    # User J. H. Palmieri <palmieri@math.washington.edu>
    # Date 1317181527 25200
    # Node ID a0f782b8c3b06f63a2bfbdbcc17d63abd63d8042
    # Parent  372d58a01140647336d2577b8ef4a4b34018568f
    #12069: fix doctesting of non-library sage files: update documentation
    and add some doctests.
    
    diff --git a/doc/en/developer/doctesting.rst b/doc/en/developer/doctesting.rst
    a b by the symbolic link ``devel/sage``. 
    549549Beyond the Sage library
    550550=======================
    551551
    552 The doctesting scripts of a Sage installation currently have limited
    553 support for doctesting of modules outside of the Sage library for
    554 that version of Sage. We cannot use the doctesting scripts of Sage
    555 4.1.1 to doctest modules in, say, Sage 4.1. Doing so would result
    556 in errors::
    557 
    558     [mvngu@sage sage-4.1.1]$ ./sage -t ../sage-4.1/devel/sage-main/sage/games/sudoku.py
    559     sage -t  "../sage-4.1/devel/sage-main/sage/games/sudoku.py"
    560       File "./sudoku.py", line 18
    561         from ../sage-4.1/devel/sage-main/sage/games/sudoku import *
    562                ^
    563     SyntaxError: invalid syntax
    564 
    565              [0.2 s]
    566     exit code: 1024
    567 
    568     ----------------------------------------------------------------------
    569     The following tests failed:
    570 
    571 
    572            sage -t  "../sage-4.1/devel/sage-main/sage/games/sudoku.py"
    573     Total time for all tests: 0.2 seconds
    574 
    575 However, suppose we have a Python script called ``my_python_script.py``
    576 that uses the Sage library. Our Python script has the following
    577 content::
     552Doctesting also works fine for files not in the Sage library.  For
     553example, suppose we have a Python script called
     554``my_python_script.py``::
    578555
    579556    [mvngu@sage build]$ cat my_python_script.py
    580557    from sage.all_cmdline import *   # import sage library
    581558
    582559    def square(n):
    583560        """
    584         Return the square of n.
     561        Return the square of n.
    585562
    586         EXAMPLES::
     563        EXAMPLES::
    587564
    588565            sage: square(2)
    589566            4
    590567        """
    591568        return n**2
    592569
    593 We can use any version of Sage to doctest our Python script, so long
    594 as that version of Sage has features that are used in our script. For
    595 example, we can use both Sage 4.1.1 and 4.1 to doctest the above
    596 Python script::
    597 
    598     [mvngu@sage build]$ sage-4.1/sage -t my_python_script.py
     570Then we can doctest it just as with Sage library files::
     571 
     572    [mvngu@sage build]$ sage-4.8/sage -t my_python_script.py
    599573    sage -t  "my_python_script.py"
    600574             [1.3 s]
    601575
    602576    ----------------------------------------------------------------------
    603577    All tests passed!
    604578    Total time for all tests: 1.3 seconds
    605     [mvngu@sage build]$ sage-4.1.1/sage -t my_python_script.py
    606     sage -t  "my_python_script.py"
    607              [1.4 s]
    608 
    609     ----------------------------------------------------------------------
    610     All tests passed!
    611     Total time for all tests: 1.4 seconds
    612579
    613580Doctesting can also be performed on Sage scripts. Say we have a Sage
    614581script called ``my_sage_script.sage`` with the following content::
    script called ``my_sage_script.sage`` wi 
    625592        """
    626593        return n**3
    627594
    628 This must be converted to an equivalent Python script prior to
    629 doctesting. First, we use Sage to convert ``my_sage_script.sage`` to
    630 an equivalent Python script called ``my_sage_script.py``::
     595Then we can doctest it just as for Python files::
    631596
    632     [mvngu@sage build]$ sage-4.1.1/sage my_sage_script.sage
     597    [mvngu@sage build]$ sage-4.8/sage --t my_sage_script.sage
     598    sage -t  "my_python_script.sage"
     599             [1.3 s]
     600
     601    ----------------------------------------------------------------------
     602    All tests passed!
     603    Total time for all tests: 1.3 seconds
     604
     605Alternatively, we can preparse it to convert it to a Python script,
     606and then doctest that::
     607
     608    [mvngu@sage build]$ sage-4.8/sage --preparse my_sage_script.sage
    633609    [mvngu@sage build]$ cat my_sage_script.py
    634610    # This file was *autogenerated* from the file my_sage_script.sage.
    635611    from sage.all_cmdline import *   # import sage library
    an equivalent Python script called ``my_ 
    644620            8
    645621        """
    646622        return n**_sage_const_3
    647 
    648 Doctesting is then performed on that equivalent Python script::
    649 
    650     [mvngu@sage build]$ sage-4.1.1/sage -t my_sage_script.py
     623    [mvngu@sage build]$ sage-4.8/sage -t my_sage_script.py
    651624    sage -t  "my_sage_script.py"
    652625             [1.5 s]
    653626
  • sage/tests/cmdline.py

    diff --git a/sage/tests/cmdline.py b/sage/tests/cmdline.py
    a b test.sage 
    2626--min
    2727--mwrank
    2828--optional
     29--preparse
    2930--python
    3031-q
    3132--R
    test.sage 
    3637--sqlite3
    3738--standard
    3839--startuptime
     40-t
    3941-v
    4042--zzfoobar (illegal option)
    4143
    def test_executable(args, input="", time 
    247249        0
    248250        sage: del F   # Close and delete the file
    249251
     252    Testing "sage --preparse FILE" and "sage -t FILE".  First create a file and preparse it::
     253
     254        sage: import os
     255        sage: s = '\"\"\"\nThis is a test file.\n\"\"\"\ndef my_add(a,b):\n    \"\"\"\n    Add a to b.\n\n        EXAMPLES::\n\n            sage: my_add(2,2)\n            4\n        \"\"\"\n    return a+b\n'
     256        sage: script = os.path.join(SAGE_TMP, 'my_script.sage')
     257        sage: F = open(script, 'w')
     258        sage: F.write(s)
     259        sage: F.close()
     260        sage: os.chdir(SAGE_TMP)
     261        sage: (out, err, ret) = test_executable(["sage", "--preparse", script])
     262        sage: ret
     263        0
     264        sage: os.path.exists(os.path.join(SAGE_TMP, 'my_script.py'))
     265        True
     266
     267    Now test my_script.sage and the preparsed version my_script.py::
     268
     269        sage: (out, err, ret) = test_executable(["sage", "-t", script])
     270        sage: ret
     271        0
     272        sage: out.find("All tests passed!") >= 0
     273        True
     274        sage: (out, err, ret) = test_executable(["sage", "-t", os.path.join(SAGE_TMP, 'my_script.py')])
     275        sage: ret
     276        0
     277        sage: out.find("All tests passed!") >= 0
     278        True
     279
     280    Now for a file which should fail tests.  Run this test with
     281    SAGE_TESTDIR equal to a temporary directory, because failed doctests
     282    leave files lying around in SAGE_TESTDIR::
     283
     284        sage: s = s.replace('4', '5') # (2+2 != 5)
     285        sage: F = open(script, 'w')
     286        sage: F.write(s)
     287        sage: F.close()
     288        sage: OLD_TESTDIR = os.environ['SAGE_TESTDIR']
     289        sage: os.environ['SAGE_TESTDIR'] = SAGE_TMP
     290        sage: (out, err, ret) = test_executable(["sage", "-t", script])
     291        sage: ret
     292        128
     293        sage: out.find("1 items had failures:") >= 0
     294        True
     295        sage: os.environ['SAGE_TESTDIR'] = OLD_TESTDIR  # just in case
     296
    250297    Test external programs being called by Sage::
    251298   
    252299        sage: (out, err, ret) = test_executable(["sage", "--sh"], "echo Hello World\nexit 42\n")