MoinLight

Annotated tests/test_parser.py

62:008768d9e137
2018-07-16 Paul Boddie Removed annoying re-use of module names.
paul@0 1
#!/usr/bin/env python
paul@0 2
paul@44 3
from os.path import abspath, join, split
paul@44 4
import sys
paul@44 5
paul@44 6
dirname = split(abspath(sys.argv[0]))[0]
paul@44 7
parent = split(dirname)[0]
paul@44 8
paul@44 9
try:
paul@44 10
    import moinformat
paul@44 11
except ImportError:
paul@44 12
    if split(parent)[1] == "MoinLight":
paul@44 13
        sys.path.append(parent)
paul@44 14
paul@62 15
from moinformat import all_parsers, all_serialisers, parse, serialise
paul@26 16
from glob import glob
paul@16 17
paul@26 18
def test_input(s):
paul@62 19
    d = parse(s, all_parsers)
paul@26 20
    o = serialise(d)
paul@12 21
paul@60 22
    identical = o == s
paul@38 23
paul@38 24
    if quiet:
paul@60 25
        return identical
paul@38 26
paul@60 27
    # Show output versus input comparison result.
paul@60 28
paul@60 29
    print identical
paul@26 30
    print "-" * 60
paul@26 31
    print o
paul@26 32
    if o != s:
paul@26 33
        print "-" * 60
paul@26 34
        print s
paul@26 35
    print "-" * 60
paul@62 36
    print serialise(d, all_serialisers["html"])
paul@26 37
    print "-" * 60
paul@26 38
    print d.prettyprint()
paul@26 39
    print
paul@20 40
paul@60 41
    return identical
paul@60 42
paul@26 43
if __name__ == "__main__":
paul@38 44
    args = sys.argv[1:]
paul@38 45
    quiet = "-q" in args
paul@38 46
    if quiet:
paul@38 47
        del args[args.index("-q")]
paul@38 48
    filenames = args or glob(join(dirname, "test*.txt"))
paul@26 49
    filenames.sort()
paul@0 50
paul@26 51
    for filename in filenames:
paul@26 52
        f = open(filename)
paul@26 53
        try:
paul@60 54
            if not quiet:
paul@60 55
                print filename
paul@60 56
            identical = test_input(f.read())
paul@60 57
            if quiet:
paul@60 58
                print "%s: %s" % (identical, filename)
paul@26 59
        finally:
paul@26 60
            f.close()
paul@3 61
paul@0 62
# vim: tabstop=4 expandtab shiftwidth=4