1.1 --- a/tests/test_parser.py Mon Jul 16 19:14:35 2018 +0200
1.2 +++ b/tests/test_parser.py Mon Jul 16 19:19:31 2018 +0200
1.3 @@ -1,6 +1,6 @@
1.4 #!/usr/bin/env python
1.5
1.6 -from os.path import abspath, join, split
1.7 +from os.path import abspath, exists, join, split
1.8 import sys
1.9
1.10 dirname = split(abspath(sys.argv[0]))[0]
1.11 @@ -13,10 +13,13 @@
1.12 sys.path.append(parent)
1.13
1.14 from moinformat import all_parsers, all_serialisers, parse, serialise
1.15 +from moinformat.tree import Container
1.16 from glob import glob
1.17
1.18 -def test_input(s):
1.19 - d = parse(s, all_parsers)
1.20 +def test_input(d, s):
1.21 +
1.22 + "Compare serialised output from 'd' with its original form 's'."
1.23 +
1.24 o = serialise(d)
1.25
1.26 identical = o == s
1.27 @@ -29,34 +32,166 @@
1.28 print identical
1.29 print "-" * 60
1.30 print o
1.31 - if o != s:
1.32 + if not identical:
1.33 print "-" * 60
1.34 print s
1.35 print "-" * 60
1.36 print serialise(d, all_serialisers["html"])
1.37 print "-" * 60
1.38 - print d.prettyprint()
1.39 print
1.40
1.41 return identical
1.42
1.43 +def test_tree(d, t, ts):
1.44 +
1.45 + "Compare tree structure 'd' with simplified, expected form 't' from 'ts'."
1.46 +
1.47 + failing = t.test(d)
1.48 +
1.49 + if quiet:
1.50 + return not failing
1.51 +
1.52 + # Show tree versus expected forms.
1.53 +
1.54 + print not failing
1.55 + print "-" * 60
1.56 + print d.prettyprint()
1.57 + if failing:
1.58 + simple, tree = failing
1.59 + print "-" * 60
1.60 + print tree.prettyprint()
1.61 + print "-" * 60
1.62 + print simple.prettyprint()
1.63 + print "-" * 60
1.64 + print ts
1.65 + print "-" * 60
1.66 + print
1.67 +
1.68 + return not failing
1.69 +
1.70 +class Node:
1.71 +
1.72 + "A simplified tree node representation."
1.73 +
1.74 + def __init__(self, name):
1.75 + self.name = name
1.76 + self.nodes = []
1.77 +
1.78 + def __repr__(self):
1.79 + return "Node(%r, ...)" % self.name
1.80 +
1.81 + def prettyprint(self, indent=""):
1.82 + l = [indent + self.name]
1.83 + for node in self.nodes:
1.84 + l.append(node.prettyprint(indent + " "))
1.85 + return "\n".join(l)
1.86 +
1.87 + def append(self, node):
1.88 + self.nodes.append(node)
1.89 +
1.90 + def test(self, other):
1.91 +
1.92 + """
1.93 + Test whether this node is considered equivalent to 'other', where
1.94 + 'other' is a moinparser.tree node.
1.95 +
1.96 + Return any failing tree nodes or None.
1.97 + """
1.98 +
1.99 + if other.__class__.__name__ != self.name:
1.100 + return self, other
1.101 +
1.102 + if isinstance(other, Container):
1.103 + for node, other_node in map(None, self.nodes, other.nodes):
1.104 + if node is None or other_node is None:
1.105 + return self, other
1.106 + if node.test(other_node):
1.107 + return node, other_node
1.108 +
1.109 + return None
1.110 +
1.111 +def parse_tree(s):
1.112 +
1.113 + "Parse the tree structure representation in 's'."
1.114 +
1.115 + indent = 0
1.116 + branches = []
1.117 +
1.118 + for line in s.split("\n"):
1.119 + line = line.rstrip()
1.120 + if not line:
1.121 + continue
1.122 +
1.123 + new_indent = line.rfind(" ") + 1
1.124 + node = Node(line[new_indent:])
1.125 +
1.126 + # Establish a branch to add nodes to.
1.127 +
1.128 + if not branches:
1.129 + branches.append(node)
1.130 + else:
1.131 + # Note the current node as outermost branch.
1.132 +
1.133 + if new_indent > indent:
1.134 + branches.append(node)
1.135 + else:
1.136 + # Reduced indent involves obtaining an inner branch again.
1.137 +
1.138 + while indent > new_indent:
1.139 + del branches[-1]
1.140 + indent -= 2
1.141 +
1.142 + # Note the current node as outermost branch.
1.143 +
1.144 + branches[-1] = node
1.145 +
1.146 + # Append the current node to the parent branch.
1.147 +
1.148 + branches[-2].append(node)
1.149 +
1.150 + indent = new_indent
1.151 +
1.152 + return branches[0]
1.153 +
1.154 +def readfile(filename):
1.155 +
1.156 + "Read the contents of 'filename' and return them."
1.157 +
1.158 + f = open(filename)
1.159 + try:
1.160 + return f.read()
1.161 + finally:
1.162 + f.close()
1.163 +
1.164 if __name__ == "__main__":
1.165 args = sys.argv[1:]
1.166 +
1.167 quiet = "-q" in args
1.168 if quiet:
1.169 del args[args.index("-q")]
1.170 +
1.171 filenames = args or glob(join(dirname, "test*.txt"))
1.172 filenames.sort()
1.173
1.174 for filename in filenames:
1.175 - f = open(filename)
1.176 - try:
1.177 - if not quiet:
1.178 - print filename
1.179 - identical = test_input(f.read())
1.180 - if quiet:
1.181 - print "%s: %s" % (identical, filename)
1.182 - finally:
1.183 - f.close()
1.184 + tree_filename = "%s.tree" % filename.rsplit(".", 1)[0]
1.185 +
1.186 + s = readfile(filename)
1.187 + d = parse(s, all_parsers)
1.188 +
1.189 + if exists(tree_filename):
1.190 + ts = readfile(tree_filename)
1.191 + t = parse_tree(ts)
1.192 + else:
1.193 + ts = None
1.194 +
1.195 + if not quiet:
1.196 + print filename
1.197 +
1.198 + identical = test_input(d, s)
1.199 + tree_identical = ts and test_tree(d, t, ts)
1.200 +
1.201 + if quiet:
1.202 + print "%s %s: %s" % (identical, tree_identical, filename)
1.203
1.204 # vim: tabstop=4 expandtab shiftwidth=4