3.1 --- a/tests/test_formatting.txt Mon Jul 16 19:14:35 2018 +0200
3.2 +++ b/tests/test_formatting.txt Mon Jul 16 19:19:31 2018 +0200
3.3 @@ -6,7 +6,7 @@
3.4
3.5 '''Strong''''''text'''.
3.6
3.7 -Some `monospace` text. `Mono, '''strong'''`, __underlined__, __''under''lined__.
3.8 +Some `monospace` text. `Mono, '''strong'''`, `mono, `'''`strong`''', __underlined__, __''under''lined__.
3.9
3.10 H,,2,,O + CO,,2,,
3.11
4.1 --- a/tests/test_parser.py Mon Jul 16 19:14:35 2018 +0200
4.2 +++ b/tests/test_parser.py Mon Jul 16 19:19:31 2018 +0200
4.3 @@ -1,6 +1,6 @@
4.4 #!/usr/bin/env python
4.5
4.6 -from os.path import abspath, join, split
4.7 +from os.path import abspath, exists, join, split
4.8 import sys
4.9
4.10 dirname = split(abspath(sys.argv[0]))[0]
4.11 @@ -13,10 +13,13 @@
4.12 sys.path.append(parent)
4.13
4.14 from moinformat import all_parsers, all_serialisers, parse, serialise
4.15 +from moinformat.tree import Container
4.16 from glob import glob
4.17
4.18 -def test_input(s):
4.19 - d = parse(s, all_parsers)
4.20 +def test_input(d, s):
4.21 +
4.22 + "Compare serialised output from 'd' with its original form 's'."
4.23 +
4.24 o = serialise(d)
4.25
4.26 identical = o == s
4.27 @@ -29,34 +32,166 @@
4.28 print identical
4.29 print "-" * 60
4.30 print o
4.31 - if o != s:
4.32 + if not identical:
4.33 print "-" * 60
4.34 print s
4.35 print "-" * 60
4.36 print serialise(d, all_serialisers["html"])
4.37 print "-" * 60
4.38 - print d.prettyprint()
4.39 print
4.40
4.41 return identical
4.42
4.43 +def test_tree(d, t, ts):
4.44 +
4.45 + "Compare tree structure 'd' with simplified, expected form 't' from 'ts'."
4.46 +
4.47 + failing = t.test(d)
4.48 +
4.49 + if quiet:
4.50 + return not failing
4.51 +
4.52 + # Show tree versus expected forms.
4.53 +
4.54 + print not failing
4.55 + print "-" * 60
4.56 + print d.prettyprint()
4.57 + if failing:
4.58 + simple, tree = failing
4.59 + print "-" * 60
4.60 + print tree.prettyprint()
4.61 + print "-" * 60
4.62 + print simple.prettyprint()
4.63 + print "-" * 60
4.64 + print ts
4.65 + print "-" * 60
4.66 + print
4.67 +
4.68 + return not failing
4.69 +
4.70 +class Node:
4.71 +
4.72 + "A simplified tree node representation."
4.73 +
4.74 + def __init__(self, name):
4.75 + self.name = name
4.76 + self.nodes = []
4.77 +
4.78 + def __repr__(self):
4.79 + return "Node(%r, ...)" % self.name
4.80 +
4.81 + def prettyprint(self, indent=""):
4.82 + l = [indent + self.name]
4.83 + for node in self.nodes:
4.84 + l.append(node.prettyprint(indent + " "))
4.85 + return "\n".join(l)
4.86 +
4.87 + def append(self, node):
4.88 + self.nodes.append(node)
4.89 +
4.90 + def test(self, other):
4.91 +
4.92 + """
4.93 + Test whether this node is considered equivalent to 'other', where
4.94 + 'other' is a moinparser.tree node.
4.95 +
4.96 + Return any failing tree nodes or None.
4.97 + """
4.98 +
4.99 + if other.__class__.__name__ != self.name:
4.100 + return self, other
4.101 +
4.102 + if isinstance(other, Container):
4.103 + for node, other_node in map(None, self.nodes, other.nodes):
4.104 + if node is None or other_node is None:
4.105 + return self, other
4.106 + if node.test(other_node):
4.107 + return node, other_node
4.108 +
4.109 + return None
4.110 +
4.111 +def parse_tree(s):
4.112 +
4.113 + "Parse the tree structure representation in 's'."
4.114 +
4.115 + indent = 0
4.116 + branches = []
4.117 +
4.118 + for line in s.split("\n"):
4.119 + line = line.rstrip()
4.120 + if not line:
4.121 + continue
4.122 +
4.123 + new_indent = line.rfind(" ") + 1
4.124 + node = Node(line[new_indent:])
4.125 +
4.126 + # Establish a branch to add nodes to.
4.127 +
4.128 + if not branches:
4.129 + branches.append(node)
4.130 + else:
4.131 + # Note the current node as outermost branch.
4.132 +
4.133 + if new_indent > indent:
4.134 + branches.append(node)
4.135 + else:
4.136 + # Reduced indent involves obtaining an inner branch again.
4.137 +
4.138 + while indent > new_indent:
4.139 + del branches[-1]
4.140 + indent -= 2
4.141 +
4.142 + # Note the current node as outermost branch.
4.143 +
4.144 + branches[-1] = node
4.145 +
4.146 + # Append the current node to the parent branch.
4.147 +
4.148 + branches[-2].append(node)
4.149 +
4.150 + indent = new_indent
4.151 +
4.152 + return branches[0]
4.153 +
4.154 +def readfile(filename):
4.155 +
4.156 + "Read the contents of 'filename' and return them."
4.157 +
4.158 + f = open(filename)
4.159 + try:
4.160 + return f.read()
4.161 + finally:
4.162 + f.close()
4.163 +
4.164 if __name__ == "__main__":
4.165 args = sys.argv[1:]
4.166 +
4.167 quiet = "-q" in args
4.168 if quiet:
4.169 del args[args.index("-q")]
4.170 +
4.171 filenames = args or glob(join(dirname, "test*.txt"))
4.172 filenames.sort()
4.173
4.174 for filename in filenames:
4.175 - f = open(filename)
4.176 - try:
4.177 - if not quiet:
4.178 - print filename
4.179 - identical = test_input(f.read())
4.180 - if quiet:
4.181 - print "%s: %s" % (identical, filename)
4.182 - finally:
4.183 - f.close()
4.184 + tree_filename = "%s.tree" % filename.rsplit(".", 1)[0]
4.185 +
4.186 + s = readfile(filename)
4.187 + d = parse(s, all_parsers)
4.188 +
4.189 + if exists(tree_filename):
4.190 + ts = readfile(tree_filename)
4.191 + t = parse_tree(ts)
4.192 + else:
4.193 + ts = None
4.194 +
4.195 + if not quiet:
4.196 + print filename
4.197 +
4.198 + identical = test_input(d, s)
4.199 + tree_identical = ts and test_tree(d, t, ts)
4.200 +
4.201 + if quiet:
4.202 + print "%s %s: %s" % (identical, tree_identical, filename)
4.203
4.204 # vim: tabstop=4 expandtab shiftwidth=4