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