# HG changeset patch # User Paul Boddie # Date 1531761571 -7200 # Node ID c4440c1768b191f3bf00d1f62fc5b83f1aff99f7 # Parent 2f628db8fa920900755bf3ef347253ef7f8c746d Added comparisons of parse trees with simplified tree representations. Updated the formatting test to demonstrate monospace formatting changes. diff -r 2f628db8fa92 -r c4440c1768b1 tests/test_deflists.tree --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_deflists.tree Mon Jul 16 19:19:31 2018 +0200 @@ -0,0 +1,13 @@ +Region + DefTerm + Text + DefItem + Text + Block + Text + DefTerm + Text + DefItem + Text + Block + Text diff -r 2f628db8fa92 -r c4440c1768b1 tests/test_formatting.tree --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_formatting.tree Mon Jul 16 19:19:31 2018 +0200 @@ -0,0 +1,93 @@ +Region + Block + FontStyle + Text + Text + FontStyle + Text + FontStyle + Text + Text + Break + Block + FontStyle + Text + FontStyle + Text + Text + Text + Break + Block + FontStyle + Text + FontStyle + Text + Text + FontStyle + FontStyle + Text + Text + Text + FontStyle + FontStyle + Text + Text + Text + Break + Block + FontStyle + Text + FontStyle + Text + Text + Break + Block + Text + Monospace + Text + Text + Monospace + Text + Text + Monospace + Text + FontStyle + Monospace + Text + Text + Underline + Text + Text + Underline + FontStyle + Text + Text + Text + Break + Block + Text + Subscript + Text + Text + Subscript + Text + Text + Break + Heading + Text + Superscript + Text + Break + Block + Larger + Text + Text + Smaller + Text + Text + Break + Block + Text + Strikethrough + Text + Text diff -r 2f628db8fa92 -r c4440c1768b1 tests/test_formatting.txt --- a/tests/test_formatting.txt Mon Jul 16 19:14:35 2018 +0200 +++ b/tests/test_formatting.txt Mon Jul 16 19:19:31 2018 +0200 @@ -6,7 +6,7 @@ '''Strong''''''text'''. -Some `monospace` text. `Mono, '''strong'''`, __underlined__, __''under''lined__. +Some `monospace` text. `Mono, '''strong'''`, `mono, `'''`strong`''', __underlined__, __''under''lined__. H,,2,,O + CO,,2,, diff -r 2f628db8fa92 -r c4440c1768b1 tests/test_parser.py --- a/tests/test_parser.py Mon Jul 16 19:14:35 2018 +0200 +++ b/tests/test_parser.py Mon Jul 16 19:19:31 2018 +0200 @@ -1,6 +1,6 @@ #!/usr/bin/env python -from os.path import abspath, join, split +from os.path import abspath, exists, join, split import sys dirname = split(abspath(sys.argv[0]))[0] @@ -13,10 +13,13 @@ sys.path.append(parent) from moinformat import all_parsers, all_serialisers, parse, serialise +from moinformat.tree import Container from glob import glob -def test_input(s): - d = parse(s, all_parsers) +def test_input(d, s): + + "Compare serialised output from 'd' with its original form 's'." + o = serialise(d) identical = o == s @@ -29,34 +32,166 @@ print identical print "-" * 60 print o - if o != s: + if not identical: print "-" * 60 print s print "-" * 60 print serialise(d, all_serialisers["html"]) print "-" * 60 - print d.prettyprint() print return identical +def test_tree(d, t, ts): + + "Compare tree structure 'd' with simplified, expected form 't' from 'ts'." + + failing = t.test(d) + + if quiet: + return not failing + + # Show tree versus expected forms. + + print not failing + print "-" * 60 + print d.prettyprint() + if failing: + simple, tree = failing + print "-" * 60 + print tree.prettyprint() + print "-" * 60 + print simple.prettyprint() + print "-" * 60 + print ts + print "-" * 60 + print + + return not failing + +class Node: + + "A simplified tree node representation." + + def __init__(self, name): + self.name = name + self.nodes = [] + + def __repr__(self): + return "Node(%r, ...)" % self.name + + def prettyprint(self, indent=""): + l = [indent + self.name] + for node in self.nodes: + l.append(node.prettyprint(indent + " ")) + return "\n".join(l) + + def append(self, node): + self.nodes.append(node) + + def test(self, other): + + """ + Test whether this node is considered equivalent to 'other', where + 'other' is a moinparser.tree node. + + Return any failing tree nodes or None. + """ + + if other.__class__.__name__ != self.name: + return self, other + + if isinstance(other, Container): + for node, other_node in map(None, self.nodes, other.nodes): + if node is None or other_node is None: + return self, other + if node.test(other_node): + return node, other_node + + return None + +def parse_tree(s): + + "Parse the tree structure representation in 's'." + + indent = 0 + branches = [] + + for line in s.split("\n"): + line = line.rstrip() + if not line: + continue + + new_indent = line.rfind(" ") + 1 + node = Node(line[new_indent:]) + + # Establish a branch to add nodes to. + + if not branches: + branches.append(node) + else: + # Note the current node as outermost branch. + + if new_indent > indent: + branches.append(node) + else: + # Reduced indent involves obtaining an inner branch again. + + while indent > new_indent: + del branches[-1] + indent -= 2 + + # Note the current node as outermost branch. + + branches[-1] = node + + # Append the current node to the parent branch. + + branches[-2].append(node) + + indent = new_indent + + return branches[0] + +def readfile(filename): + + "Read the contents of 'filename' and return them." + + f = open(filename) + try: + return f.read() + finally: + f.close() + if __name__ == "__main__": args = sys.argv[1:] + quiet = "-q" in args if quiet: del args[args.index("-q")] + filenames = args or glob(join(dirname, "test*.txt")) filenames.sort() for filename in filenames: - f = open(filename) - try: - if not quiet: - print filename - identical = test_input(f.read()) - if quiet: - print "%s: %s" % (identical, filename) - finally: - f.close() + tree_filename = "%s.tree" % filename.rsplit(".", 1)[0] + + s = readfile(filename) + d = parse(s, all_parsers) + + if exists(tree_filename): + ts = readfile(tree_filename) + t = parse_tree(ts) + else: + ts = None + + if not quiet: + print filename + + identical = test_input(d, s) + tree_identical = ts and test_tree(d, t, ts) + + if quiet: + print "%s %s: %s" % (identical, tree_identical, filename) # vim: tabstop=4 expandtab shiftwidth=4