The fstring branch now contains a more flexible test runner.  It's 
signature is:

def test_token_traversers(contents, reports=None):

The post script shows the entire code.  Here is the test script that calls 
the test runner:

g.cls()
import imp
import leo.core.leoAst as leoAst
imp.reload(leoAst)
small = False
<< define contents >>
contents = contents.strip() + '\n'
# Define the ordered list of reports.
# 'coverage', 'tokens','contents', 'diff','results', 'lines', 'tree'
reports =  ['contents', 'diff'] if small else ['diff']
# Run the tests.
leoAst.test_token_traversers(contents, reports + ['summary'])

Edward

P. S. Here is the test runner:

def test_token_traversers(contents, reports=None):
    """
    A testing framework for TokenOrderGenerator and related classes.
    
    The caller should call imp.reload if desired.
    
    Reports is a list of reports. A suggested order is shown below.
    """
    # pylint: disable=import-self
    import leo.core.leoAst as leoAst
    
    reports = [z.lower() for z in reports or []]
    assert isinstance(reports, list), repr(reports)

    # Start test.
    print('\nleoAst.py:test_token_traversers...\n')
    contents = contents.strip() + '\n'
    # Create tokens and tree.
    x = leoAst.TokenOrderInjector()
    tokens = x.make_tokens(contents)
    tree = leoAst.parse_ast(contents)
    # Catch exceptions so we can get data late.
    try:
        ok = True
        list(x.create_links(tokens, tree))
    except Exception:
        g.es_exception()
        ok = False
    # Print reports, in the order they appear in the results list.
    # The following is a reasoable order.
    bad_reports = []
    while reports:
        report = reports.pop(0)
        if report == 'coverage':
            x.report_coverage(report_missing=False)
        elif report == 'tokens':
            print('\nTokens...\n')
            # pylint: disable=not-an-iterable
            for z in x.tokens:
                print(z.dump())
        elif report == 'contents':
            print('\nContents...\n')
            for i, z in enumerate(g.splitLines(contents)):
                print(f"{i+1:<3} ", z.rstrip())
        elif report == 'diff':
            print('\nDiff...\n')
            x.verify()
        elif report == 'results':
            print('\nResults...\n')
            results = ''.join([b for a, b in x.results])
            for i, z in enumerate(g.splitLines(results)):
                 print(f"{i+1:<3} ", g.truncate(z.rstrip(), 60))
        elif report == 'lines':
            print('\nTOKEN lines...\n')
            for z in tokens:
                if z.line.strip():
                    print(z.line.rstrip())
                else:
                    print(repr(z.line))
        elif report == 'tree':
            print('\nPatched tree...\n')
            print(leoAst.AstDumper().brief_dump(tree))
        elif report == 'summary':
            if x.errors:
                print('\nErrors...\n')
                for z in x.errors:
                    print('  ' + z)
                print('')
            ok = ok and not x.errors
            print('')
            print('PASS' if ok else 'FAIL')
        else:
            bad_reports.append(report)
    if bad_reports:
        print(f"\nIgnoring unknown reports {','.join(bad_reports)}\n")

EKR

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/d6475656-8c64-4582-b505-f3826cecb4b1%40googlegroups.com.

Reply via email to