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 simple, tree = failing 61 print "-" * 60 62 print tree.prettyprint() 63 print "-" * 60 64 print simple.prettyprint() 65 print "-" * 60 66 print ts 67 print "-" * 60 68 print 69 70 return not failing 71 72 class Node: 73 74 "A simplified tree node representation." 75 76 def __init__(self, name): 77 self.name = name 78 self.nodes = [] 79 80 def __repr__(self): 81 return "Node(%r, ...)" % self.name 82 83 def prettyprint(self, indent=""): 84 l = [indent + self.name] 85 for node in self.nodes: 86 l.append(node.prettyprint(indent + " ")) 87 return "\n".join(l) 88 89 def append(self, node): 90 self.nodes.append(node) 91 92 def test(self, other): 93 94 """ 95 Test whether this node is considered equivalent to 'other', where 96 'other' is a moinparser.tree node. 97 98 Return any failing tree nodes or None. 99 """ 100 101 if other.__class__.__name__ != self.name: 102 return self, other 103 104 if isinstance(other, Container): 105 for node, other_node in map(None, self.nodes, other.nodes): 106 if node is None or other_node is None: 107 return self, other 108 if node.test(other_node): 109 return node, other_node 110 111 return None 112 113 def parse_tree(s): 114 115 "Parse the tree structure representation in 's'." 116 117 indent = 0 118 branches = [] 119 120 for line in s.split("\n"): 121 line = line.rstrip() 122 if not line: 123 continue 124 125 new_indent = line.rfind(" ") + 1 126 node = Node(line[new_indent:]) 127 128 # Establish a branch to add nodes to. 129 130 if not branches: 131 branches.append(node) 132 else: 133 # Note the current node as outermost branch. 134 135 if new_indent > indent: 136 branches.append(node) 137 else: 138 # Reduced indent involves obtaining an inner branch again. 139 140 while indent > new_indent: 141 del branches[-1] 142 indent -= 2 143 144 # Note the current node as outermost branch. 145 146 branches[-1] = node 147 148 # Append the current node to the parent branch. 149 150 branches[-2].append(node) 151 152 indent = new_indent 153 154 return branches[0] 155 156 def readfile(filename): 157 158 "Read the contents of 'filename' and return them." 159 160 f = open(filename) 161 try: 162 return f.read() 163 finally: 164 f.close() 165 166 if __name__ == "__main__": 167 args = sys.argv[1:] 168 169 quiet = "-q" in args 170 if quiet: 171 del args[args.index("-q")] 172 173 filenames = args or glob(join(dirname, "test*.txt")) 174 filenames.sort() 175 176 for filename in filenames: 177 tree_filename = "%s.tree" % filename.rsplit(".", 1)[0] 178 179 s = readfile(filename) 180 d = parse(s, all_parsers) 181 182 if exists(tree_filename): 183 ts = readfile(tree_filename) 184 t = parse_tree(ts) 185 else: 186 ts = None 187 188 if not quiet: 189 print filename 190 191 identical = test_input(d, s) 192 tree_identical = ts and test_tree(d, t, ts) 193 194 if quiet: 195 print "%s %s: %s" % (identical, tree_identical, filename) 196 197 # vim: tabstop=4 expandtab shiftwidth=4