Skip to content

Instantly share code, notes, and snippets.

@decitrig
Created April 19, 2011 18:55
Show Gist options
  • Save decitrig/929266 to your computer and use it in GitHub Desktop.
Save decitrig/929266 to your computer and use it in GitHub Desktop.
Python test generator for CMSC 420 MeeshQuest project
1024 1024 1 4
city A 100 100
city B 1020 1020
city C 100 1020
city Cp 450 900
city D 800 500
city E 100 1021
city F 2048 1023
city G 2048 1000
road A B
road A C
road A D
savemap before
delete A
savemap after
import sys
from xml.dom import minidom
_IMPL = minidom.getDOMImplementation()
_ROOT_ATTRS = {
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"xsi:noNamespaceSchemaLocation": "part3in.xsd"
}
class Commands(object):
def do_city(self, doc, name, x, y):
me = doc.createElement("createCity")
me.setAttribute("name", name)
me.setAttribute("x", x)
me.setAttribute("y", y)
me.setAttribute("radius", "5")
me.setAttribute("color", "black")
return me
def do_road(self, doc, start, end):
me = doc.createElement("mapRoad")
me.setAttribute("start", start)
me.setAttribute("end", end)
return me
def do_savemap(self, doc, name):
me = doc.createElement("saveMap")
me.setAttribute("name", name)
return me
def do_delete(self, doc, name):
me = doc.createElement("deleteCity")
me.setAttribute("name", name)
return me
def main(f):
doc = _IMPL.createDocument(None, "commands", None)
id = 1
root = doc.documentElement
for (name, attr) in _ROOT_ATTRS.items():
root.setAttribute(name, attr)
cmd_attrs = f.readline().split()
# first line should be WIDTH HEIGHT PMORDER LEAFORDER
# space-separated, in that order
root.setAttribute("spatialWidth", cmd_attrs[0])
root.setAttribute("spatialHeight", cmd_attrs[1])
root.setAttribute("pmOrder", cmd_attrs[2])
root.setAttribute("leafOrder", cmd_attrs[3])
c = Commands()
for line in f:
args = line.split()
cmd = args.pop(0)
fname = "do_" + cmd
if hasattr(c, fname):
fn = getattr(c, fname)
elt = fn(doc, *args)
else:
elt = doc.createElement(cmd)
elt.setAttribute("id", str(id))
id += 1
root.appendChild(elt)
print(doc.toprettyxml())
if __name__ == '__main__':
with open(sys.argv[1], "r") as f:
main(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment