1
from xml.dom.minidom import *
2
from TestFramework import *
4
DEFAULT_TEST_TYPE = 'norm'
5
DEFAULT_CASE_TYPE = 'match'
7
class ParseException(Exception):
8
" Error when parsing the xml problem file "
9
def __init___(self, reason):
15
def getTextData(element):
16
""" Get the text and cdata inside an element
17
Leading and trailing whitespace are stripped
20
for child in element.childNodes:
21
if child.nodeType == child.CDATA_SECTION_NODE:
23
if child.nodeType == child.TEXT_NODE:
28
def getCasePartData(partNode):
29
""" Create an TestCasePaart instance from test part xml data
32
func_desc = partNode.getAttribute('desc')
33
default = partNode.getAttribute('default')
34
if default == '': default = DEFAULT_CASE_TYPE
36
part = TestCasePart(func_desc, default)
38
for child in partNode.childNodes:
39
if child.nodeType != child.ELEMENT_NODE:
42
if child.tagName == 'stdout':
43
test_type = child.getAttribute('type')
44
if test_type == '': test_type = DEFAULT_TEST_TYPE
45
part.add_stdout_test(getTextData(child), test_type)
46
elif child.tagName == 'stderr':
47
test_type = child.getAttribute('type')
48
if test_type == '': test_type = DEFAULT_TEST_TYPE
49
part.add_stderr_test(getTextData(child), test_type)
50
elif child.tagName == 'file':
51
test_type = child.getAttribute('type')
52
if test_type == '': test_type = DEFAULT_TEST_TYPE
53
filename = child.getAttribute('name')
55
raise ParseException("File without name in case %s" %case_name)
56
part.add_file_test(filename, getTextData(child), test_type)
60
def getCaseData(caseNode):
61
""" Creare a TestCase instance from test case xml data
64
case_name = caseNode.getAttribute('name')
65
function = caseNode.getAttribute('function')
66
if function == '': function = None
67
case = TestCase(case_name, function)
69
for child in caseNode.childNodes:
70
if child.nodeType != child.ELEMENT_NODE:
73
if child.tagName == 'stdin':
75
case.set_stdin(getTextData(child))
76
elif child.tagName == 'file':
78
filename = child.getAttribute('name')
80
raise ParseException("File without name in case %s" %case_name)
81
case.add_file(filename, getTextData(child))
82
elif child.tagName == 'var':
84
var_name = child.getAttribute('name')
85
var_val = child.getAttribute('value')
86
if not var_name or not var_val:
87
raise ParseException("Incomplete variable in case %s" %case_name)
88
case.add_variable(var_name, var_val)
89
elif child.tagName == 'arg':
91
arg_val = child.getAttribute('value')
92
arg_name = child.getAttribute('name')
93
if arg_name == '': arg_name = None
96
raise ParseException("Incomplete argument in case %s" %case_name)
97
case.add_arg(arg_val, arg_name)
98
elif child.tagName == 'function':
99
# specify a test case part
100
case.add_part(getCasePartData(child))
104
def parse_tutorial_file(filename, prob_num=1):
105
""" Parse an xml problem file and return a testsuite for that problem """
106
dom = parse(filename)
111
for child in dom.childNodes:
112
if child.nodeType == child.ELEMENT_NODE and child.tagName == 'problem':
114
if count == prob_num:
119
raise ParseException("Not enough problems")
122
problem_name = problem.getAttribute('name')
125
raise ParseException('Problem name not supplied')
127
problem_suite = TestSuite(problem_name)
129
# get solution and include info
130
for child in problem.childNodes:
131
if child.nodeType != child.ELEMENT_NODE:
133
if child.tagName == 'solution':
134
problem_suite.add_solution(getTextData(child))
135
elif child.tagName == 'include':
136
problem_suite.add_include_code(getTextData(child))
137
elif child.tagName == 'case':
138
problem_suite.add_case(getCaseData(child))