Unit tests can often be simplified by sharing common code. Suppose, for example, that I want my unit tests to have access to this class::
class Hello: def __init__(self,name='john'): self.name=name print('hello %s' % name)
Before yesterday's Aha, I would have defined the class Hello in an external file, and then imported the file. For example, a complete unit test (in an @test node) might be::
import leo.core.leoTest as leoTest h = leoTest.Hello('Bob') assert h.name == 'Bob'
Aside: Leo's users will know that putting this code in an @test node makes it an official unit test. Leo automatically creates a subclass of UnitTest.TestCase from the body text of any @test node.
Importing code this way works, but it's a static, plodding solution. To change class Hello, I have to switch to another file, make the changes and save that file, and reload the outline that uses it. I've been wanting a better solution for years. Yesterday I saw the answer: it's completely dynamic, it's totally simple and it's completely Leonine.
The idea is this. Suppose the node '@common code for x tests' contains a list of nodes, each of which defines a class or function to be shared by unit tests. A unit test can gain access to the compiled code in these nodes as follows::
p = g.findNodeAnywhere(c,'@common code for x tests') script = g.getScript(c,p) exec(script) h = Hello('Bob') assert h.name == 'Bob'
Let's look at these lines:
1. The first line finds the node whose headline is '@common code for x tests'. As usual in a Leo script, 'c' and 'g' are predefined. 'c' is bound to the Leo outline itself, and 'g' is bound to Leo's globals module, leo.core.leoGlobals.
2. The second line converts this node and all its descendants into a script. g.getScript handles Leo's section references and @others directives correctly--I can use all of Leo's code-organization features as usual.
3-5 The third line executes the script in the context of the unit test. This defines Hello in the @test node, that is, in the unit test itself! There is no need to qualify Hello. The actual test can be::
h = Hello('Bob') assert h.name == 'Bob'
That's all there is to it. Naturally, I wanted to make this scheme a bit more concise, so I created g.findTestScript function, defined as follows::
def findTestScript(c,h): p = g.findNodeAnywhere(c,h) return p and g.getScript(c,p)
The unit test then becomes::
exec(g.findTestScript('@common code for x tests')) h = Hello('Bob') assert h.name == 'Bob'
This shows, I think the power of leveraging outlines with scripts. It would be hard even to think of this in emacs, vim, Eclipse, or Idle.
The difference in the new work-flow is substantial. Any changes I make in the common code instantly become available to all the unit tests that use it. I can modify shared code and run the unit tests that depend on it without any "compilation" step at all. I don't even have to save the outline that I'm working on. Everything just works.