summaryrefslogtreecommitdiff
path: root/python/rule2test
diff options
context:
space:
mode:
authorRafael H. Schloming <rhs@apache.org>2006-09-19 22:06:50 +0000
committerRafael H. Schloming <rhs@apache.org>2006-09-19 22:06:50 +0000
commit913489deb2ee9dbf44455de5f407ddaf4bd8c540 (patch)
tree7ea442d6867d0076f1c9ea4f4265664059e7aff5 /python/rule2test
downloadqpid-python-913489deb2ee9dbf44455de5f407ddaf4bd8c540.tar.gz
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
Diffstat (limited to 'python/rule2test')
-rwxr-xr-xpython/rule2test89
1 files changed, 89 insertions, 0 deletions
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] <amqpclass>
+
+Print test classes for each rule for the amqpclass in amqp.xml.
+
+Options:
+ -?/-h/--help : this message
+ -s/--spec <spec.xml> : 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))