From 913489deb2ee9dbf44455de5f407ddaf4bd8c540 Mon Sep 17 00:00:00 2001 From: "Rafael H. Schloming" Date: Tue, 19 Sep 2006 22:06:50 +0000 Subject: Import of qpid from etp: URL: https://etp.108.redhat.com/svn/etp/trunk/blaze Repository Root: https://etp.108.redhat.com/svn/etp Repository UUID: 06e15bec-b515-0410-bef0-cc27a458cf48 Revision: 608 git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@447994 13f79535-47bb-0310-9956-ffa450edef68 --- python/rule2test | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100755 python/rule2test (limited to 'python/rule2test') diff --git a/python/rule2test b/python/rule2test new file mode 100755 index 0000000000..b57ea9e24e --- /dev/null +++ b/python/rule2test @@ -0,0 +1,89 @@ +#!/usr/bin/env python + +# +# Convert rules to tests +# +import sys, re, os.path +from getopt import getopt, GetoptError +from string import capitalize +from xml import dom +from xml.dom.minidom import parse + +def camelcase(s): + """Convert 'string like this' to 'StringLikeThis'""" + return "".join([capitalize(w) for w in re.split(re.compile("\W*"), s)]) + +def uncapitalize(s): return s[0].lower()+s[1:] + +def ancestors(node): + "Return iterator of ancestors from top-level element to node" + def generator(node): + while node and node.parentNode: + yield node + node = node.parentNode + return reversed(list(generator(node))) + +def tagAndName(element): + nameAttr = element.getAttribute("name"); + if (nameAttr) : return camelcase(nameAttr) + camelcase(element.tagName) + else: return camelcase(element.tagName) + +def nodeText(n): + """Recursively collect text from all text nodes under n""" + if n.nodeType == dom.Node.TEXT_NODE: + return n.data + if n.childNodes: + return reduce(lambda t, c: t + nodeText(c), n.childNodes, "") + return "" + +def cleanup(docString, level=8): + unindent = re.sub("\n[ \t]*", "\n", docString.strip()) + emptyLines = re.sub("\n\n\n", "\n\n", unindent) + indented = re.sub("\n", "\n"+level*" ", emptyLines) + return level*" " + indented + +def printTest(test, docstring): + print "class %s(TestBase):" % test + print ' """' + print docstring + print ' """' + print + print + +def printTests(doc, module): + """Returns dictionary { classname : [ (methodname, docstring)* ] * }""" + tests = {} + rules = doc.getElementsByTagName("rule") + for r in rules: + path = list(ancestors(r)) + if module == path[1].getAttribute("name").lower(): + test = "".join(map(tagAndName, path[2:])) + "Tests" + docstring = cleanup(nodeText(r), 4) + printTest(test, docstring) + +def usage(message=None): + if message: print >>sys.stderr, message + print >>sys.stderr, """ +rule2test [options] + +Print test classes for each rule for the amqpclass in amqp.xml. + +Options: + -?/-h/--help : this message + -s/--spec : file containing amqp XML spec +""" + return 1 + +def main(argv): + try: opts, args = getopt(argv[1:], "h?s:", ["help", "spec="]) + except GetoptError, e: return usage(e) + spec = "../specs/amqp.xml" # Default + for opt, val in opts: + if (opt in ("-h", "-?", "--help")): return usage() + if (opt in ("-s", "--spec")): spec = val + doc = parse(spec) + if len(args) == 0: return usage() + printTests(doc, args[0]) + return 0 + +if (__name__ == "__main__"): sys.exit(main(sys.argv)) -- cgit v1.2.1