Leo + Python: the ultimate scripting tool #5

Edward K. Ream edreamleo at charter.net
Sat Nov 8 11:21:03 EST 2003


Reasons why Leo and Python work so well together:

5. Leo is the ultimate platform on which to run regression tests.

The body text of a node is the perfect place for putting regression test
data.  Headlines describe the data, which is exactly what is needed for
organizing tests.  Indeed, I use a script to create regression tests based
on the structure of the outline of the children of the node containing the
script (!)

For example, I run the following script to create and run unit tests of
Leo's colorizer.

import unittest,leoTest
suite = leoTest.makeColorSuite("colorizerTests","tempNode")
runner = unittest.TextTestRunner()
runner.run(suite)

This works as follows:  the arguments to makeColorSuite tell it to look up
the tree for of an ancestor of the present node (the node containing this
script) for a node whose headline is "colorizerTests".  makeColorSuite then
looks for children of _that_ node for nodes containing test data.  The node
whose headline is "tempNode" is used by the colorizer during the tests.
Here is the actual code of makeColorSuite:

def makeColorSuite(testParentHeadline,tempHeadline):
.."""Create a colorizer test for every descendant of testParentHeadline.."""
..u = testUtils() ; c = top() ; v = c.currentVnode()
..root = u.findRootNode(v)
..temp_v = u.findNodeInTree(root,tempHeadline)
..vList = u.findSubnodesOf(testParentHeadline)
..# Create the suite and add all test cases.
..suite = unittest.makeSuite(unittest.TestCase)
..for v in vList:
....test = colorTestCase(c,v,temp_v)
....suite.addTest(test)
..return suite

The power and simplicity of this code is breathtaking.  All that is required
are two utility routines, findNodeInTree and findSubnodesOf that find
various nodes in an outline based on the outline structure and the headlines
of the nodes:

- findNodeInTree(root,tempHeadline) returns the node in the entire tree
contain the current node whose headline is tempHeadline.

- u.findSubnodesOf(testParentHeadline) returns a list of all children of the
node whose name is testParentHeadline.

In short, it is absolutely trivial for a script embedded in a Leo outline to
generate unit tests based on the data in the outline in which the script is
embedded!!

Notice, please, what does _not_ have to be done:

A.  No unit tests are created statically: everything is driven by data (the
outline itself)
B.  No Python test data is created _at all_.   Before I saw this approach, I
was putting text for the syntax-coloring text in Python strings and passing
those strings to the regression tests.  Now, the _unchanged_ data in the
body text of nodes becomes the regression test data.
C.  There is no need to mark meta-data as separate from test data: headlines
are naturally distinct from body text.

We see now that Reasons 2, 3 and 4 are a lot more potent than they might
appear at first.  Indeed, Leo plus Python is the ultimate merging of code,
structure and data.   Scripts can use data organized in outlines.  Outlines
organize scripts.  Outlines (particularly their structure) are _data_ for
scripts.  Outlines contains metadata for scripts.  Outlines naturally keep
data and meta-data separate.

Please notice, this discussion merely hints at all the games that scripts
could play with outline structure.  Just for example, scripts can easily
insert, delete and reorganize nodes.  And the uses to which outline
structure may be put are practically unlimited, as we shall see later...

Let's turn now to a completely separate topic...

Edward
--------------------------------------------------------------------
Edward K. Ream   email:  edreamleo at charter.net
Leo: Literate Editor with Outlines
Leo: http://webpages.charter.net/edreamleo/front.html
--------------------------------------------------------------------






More information about the Python-list mailing list