MoinLight

tests/test_parser.py

70:ab7b40f5af6d
2018-07-17 Paul Boddie Attempt to fix and simplify opaque region handling.
     1 #!/usr/bin/env python     2      3 from os.path import abspath, exists, join, split     4 import sys     5      6 dirname = split(abspath(sys.argv[0]))[0]     7 parent = split(dirname)[0]     8      9 try:    10     import moinformat    11 except ImportError:    12     if split(parent)[1] == "MoinLight":    13         sys.path.append(parent)    14     15 from moinformat import all_parsers, all_serialisers, parse, serialise    16 from moinformat.tree import Container    17 from glob import glob    18     19 def test_input(d, s):    20     21     "Compare serialised output from 'd' with its original form 's'."    22     23     o = serialise(d)    24     25     identical = o == s    26     27     if quiet:    28         return identical    29     30     # Show output versus input comparison result.    31     32     print identical    33     print "-" * 60    34     print o    35     if not identical:    36         print "-" * 60    37         print s    38     print "-" * 60    39     print serialise(d, all_serialisers["html"])    40     print "-" * 60    41     print    42     43     return identical    44     45 def test_tree(d, t, ts):    46     47     "Compare tree structure 'd' with simplified, expected form 't' from 'ts'."    48     49     failing = t.test(d)    50     51     if quiet:    52         return not failing    53     54     # Show tree versus expected forms.    55     56     print not failing    57     print "-" * 60    58     print d.prettyprint()    59     if failing:    60         print "-" * 60    61         print ts    62         simple, tree, error = failing    63         print "-" * 60    64         print error    65         print repr(simple)    66         print repr(tree)    67         print "-" * 60    68         print tree.prettyprint()    69         print "-" * 60    70         print simple.prettyprint()    71     print "-" * 60    72     print    73     74     return not failing    75     76 class Node:    77     78     "A simplified tree node representation."    79     80     def __init__(self, name):    81         self.name = name    82         self.nodes = []    83     84     def __repr__(self):    85         return "Node(%r, %r)" % (self.name, self.nodes)    86     87     def prettyprint(self, indent=""):    88         l = []    89         l.append("%s%s%s" % (indent, self.name, len(self.nodes) and " nodes=%d" % len(self.nodes) or ""))    90         for node in self.nodes:    91             l.append(node.prettyprint(indent + "  "))    92         return "\n".join(l)    93     94     def append(self, node):    95         self.nodes.append(node)    96     97     def test(self, other):    98     99         """   100         Test whether this node is considered equivalent to 'other', where   101         'other' is a moinparser.tree node.   102    103         Return any failing tree nodes or None.   104         """   105    106         if other.__class__.__name__ != self.name:   107             return self, other, "name"   108    109         if isinstance(other, Container):   110             for node, other_node in map(None, self.nodes, other.nodes):   111                 if node is None or other_node is None:   112                     return self, other, node is None and "simple" or "document"   113                 t = node.test(other_node)   114                 if t:   115                     return t   116         elif self.nodes:   117             return self, other, "empty"   118    119         return None   120    121 def parse_tree(s):   122    123     "Parse the tree structure representation in 's'."   124    125     indent = 0   126     branches = []   127    128     for line in s.split("\n"):   129         line = line.rstrip()   130         if not line:   131             continue   132    133         new_indent = line.rfind(" ") + 1   134         node = Node(line[new_indent:])   135    136         # Establish a branch to add nodes to.   137    138         if not branches:   139             branches.append(node)   140         else:   141             # Note the current node as outermost branch.   142    143             if new_indent > indent:   144                 branches.append(node)   145             else:   146                 # Reduced indent involves obtaining an inner branch again.   147    148                 while indent > new_indent:   149                     del branches[-1]   150                     indent -= 2   151    152                 # Note the current node as outermost branch.   153    154                 branches[-1] = node   155    156             # Append the current node to the parent branch.   157    158             branches[-2].append(node)   159    160         indent = new_indent   161    162     return branches[0]   163    164 def readfile(filename):   165    166     "Read the contents of 'filename' and return them."   167    168     f = open(filename)   169     try:   170         return f.read()   171     finally:   172         f.close()   173    174 if __name__ == "__main__":   175     args = sys.argv[1:]   176    177     quiet = "-q" in args   178     if quiet:   179         del args[args.index("-q")]   180    181     filenames = args or glob(join(dirname, "test*.txt"))   182     filenames.sort()   183    184     for filename in filenames:   185         tree_filename = "%s.tree" % filename.rsplit(".", 1)[0]   186    187         s = readfile(filename)   188         d = parse(s, all_parsers)   189    190         if exists(tree_filename):   191             ts = readfile(tree_filename)   192             t = parse_tree(ts)   193         else:   194             ts = None   195    196         if not quiet:   197             print filename   198    199         identical = test_input(d, s)   200         tree_identical = ts and test_tree(d, t, ts)   201    202         if quiet:   203             print "%s %s: %s" % (identical, tree_identical, filename)   204    205 # vim: tabstop=4 expandtab shiftwidth=4