x.reload: a possible pattern for test-driven development

27 views
Skip to first unread message

Edward K. Ream

unread,
Dec 6, 2016, 7:03:49 PM12/6/16
to leo-e...@googlegroups.com
tl;dr: Important core classes, including importer classes, could support a reload method, that would reload the file containing the class, and return a new instance of the class, ready to be used by unit tests.

This is an experimental idea. It won't revolutionize Leo's unit tests, but it might make them lighter and stronger.

Details

At present, Leo's unit tests support TDD using preamble code, like this:

if 0: # Preamble...
    g.cls()
    if c.isChanged(): c.save()
    import leo.core.leoImport as leoImport
    import leo.plugins.importers.linescanner as linescanner
    import leo.plugins.importers.markdown
    import leo.plugins.writers.markdown
    import imp
    imp.reload(leo.plugins.importers.linescanner)
    imp.reload(leo.plugins.importers.markdown)
    imp.reload(leo.plugins.writers.markdown)
    imp.reload(leoImport)
    ic = leoImport.LeoImportCommands(c)
else:
    ic = c.importCommands

Cutting, pasting and modifying this template has been good enough, but we would like something better, maybe:

if 0: # Preamble...
    g.cls()
    if c.isChanged(): c.save()
    ic.importers.linescanner.reload()
    ic.importers.markdown.reload()
    ic.writers.markdown.reload()
    ic = c.importCommands.reload()
else:
    ic = c.importCommands

To make this work, the importCommands class would have to have ivars for all readers & writers, or a __getattr__ method. It's worth looking into.

EKR

Edward K. Ream

unread,
Dec 7, 2016, 5:43:08 AM12/7/16
to leo-editor
On Tuesday, December 6, 2016 at 6:03:49 PM UTC-6, Edward K. Ream wrote:

Cutting, pasting and modifying [preamble code] has been good enough, but we would like something better, maybe:


if 0: # Preamble...
    g.cls()
    if c.isChanged(): c.save()
    ic.importers.linescanner.reload()
    ic.importers.markdown.reload()
    ic.writers.markdown.reload()
    ic = c.importCommands.reload()
else:
    ic = c.importCommands

To make this work, the importCommands class would have to have ivars for all readers & writers, or a __getattr__ method. It's worth looking into.

Hmm. This might be simplified to:


if 0: # Preamble...
    g.cls()
    if c.isChanged(): c.save()
    ic = c.importCommands.reload()
else:
    ic = c.importCommands

The call to c.importCommands.reload() could return a new GeneralTestCase instance and this instance could set a reload flag.  The later call to ic.markdownUnitTest(p...) would then reload markdown and linescanner when the reload flag is set.

But... leoTest.leo defines several other subclasses of unittest.TestCase besides GeneralTestCase.  So maybe we need variants of c.importCommands.reload to handle the other kinds of TestCase.

I'm not in a hurry to do any of this, since the present importer cases work.  Still it's a good idea to keep these ideas in the back of our minds. This kind of simplification could be useful when creating other kinds of unit tests.

EKR
Reply all
Reply to author
Forward
0 new messages