294
by mattgiuca
Added application: tutorialservice. Will be used as the Ajax backend for |
1 |
# IVLE - Informatics Virtual Learning Environment
|
2 |
# Copyright (C) 2007-2008 The University of Melbourne
|
|
3 |
#
|
|
4 |
# This program is free software; you can redistribute it and/or modify
|
|
5 |
# it under the terms of the GNU General Public License as published by
|
|
6 |
# the Free Software Foundation; either version 2 of the License, or
|
|
7 |
# (at your option) any later version.
|
|
8 |
#
|
|
9 |
# This program is distributed in the hope that it will be useful,
|
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
# GNU General Public License for more details.
|
|
13 |
#
|
|
14 |
# You should have received a copy of the GNU General Public License
|
|
15 |
# along with this program; if not, write to the Free Software
|
|
16 |
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
17 |
||
18 |
# Module: parse_tute
|
|
19 |
# Author: Dilshan Angampitiya
|
|
20 |
# Date: 24/1/2008
|
|
21 |
||
22 |
"""
|
|
23 |
This file provides the function parse_tutorial_file which takes an
|
|
24 |
xml specification of a tutorial problem file and returns a test suite object
|
|
25 |
for that problem. It throws a ParseException if there was a problem parsing
|
|
26 |
the file.
|
|
27 |
"""
|
|
28 |
||
29 |
from xml.dom.minidom import * |
|
30 |
from TestFramework import * |
|
31 |
||
32 |
DEFAULT_TEST_TYPE = 'norm' |
|
33 |
DEFAULT_CASE_TYPE = 'match' |
|
34 |
||
35 |
class ParseException(Exception): |
|
36 |
" Error when parsing the xml problem file "
|
|
37 |
def __init___(self, reason): |
|
38 |
self._reason = reason |
|
39 |
||
40 |
def __str__(self): |
|
41 |
return self._reason |
|
42 |
||
43 |
def getTextData(element): |
|
44 |
""" Get the text and cdata inside an element
|
|
45 |
Leading and trailing whitespace are stripped
|
|
46 |
"""
|
|
47 |
data = '' |
|
48 |
for child in element.childNodes: |
|
49 |
if child.nodeType == child.CDATA_SECTION_NODE: |
|
50 |
data += child.data |
|
51 |
if child.nodeType == child.TEXT_NODE: |
|
52 |
data += child.data |
|
53 |
||
54 |
return data.strip() |
|
55 |
||
56 |
def getCasePartData(partNode): |
|
57 |
""" Create an TestCasePaart instance from test part xml data
|
|
58 |
"""
|
|
59 |
||
60 |
func_desc = partNode.getAttribute('desc') |
|
61 |
default = partNode.getAttribute('default') |
|
62 |
if default == '': default = DEFAULT_CASE_TYPE |
|
63 |
||
64 |
part = TestCasePart(func_desc, default) |
|
65 |
||
66 |
for child in partNode.childNodes: |
|
67 |
if child.nodeType != child.ELEMENT_NODE: |
|
68 |
continue
|
|
69 |
||
70 |
if child.tagName == 'stdout': |
|
71 |
test_type = child.getAttribute('type') |
|
72 |
if test_type == '': test_type = DEFAULT_TEST_TYPE |
|
73 |
part.add_stdout_test(getTextData(child), test_type) |
|
74 |
elif child.tagName == 'stderr': |
|
75 |
test_type = child.getAttribute('type') |
|
76 |
if test_type == '': test_type = DEFAULT_TEST_TYPE |
|
77 |
part.add_stderr_test(getTextData(child), test_type) |
|
299
by dilshan_a
Test framework now handles exceptions as valid outputs for scripts. |
78 |
elif child.tagName == 'exception': |
79 |
test_type = child.getAttribute('type') |
|
80 |
if test_type == '': test_type = DEFAULT_TEST_TYPE |
|
81 |
part.add_exception_test(getTextData(child), test_type) |
|
294
by mattgiuca
Added application: tutorialservice. Will be used as the Ajax backend for |
82 |
elif child.tagName == 'file': |
83 |
test_type = child.getAttribute('type') |
|
84 |
if test_type == '': test_type = DEFAULT_TEST_TYPE |
|
85 |
filename = child.getAttribute('name') |
|
86 |
if filename == '': |
|
87 |
raise ParseException("File without name in case %s" %case_name) |
|
88 |
part.add_file_test(filename, getTextData(child), test_type) |
|
89 |
||
90 |
return part |
|
91 |
||
92 |
def getCaseData(caseNode): |
|
93 |
""" Creare a TestCase instance from test case xml data
|
|
94 |
"""
|
|
95 |
||
96 |
case_name = caseNode.getAttribute('name') |
|
97 |
function = caseNode.getAttribute('function') |
|
98 |
if function == '': function = None |
|
99 |
case = TestCase(case_name, function) |
|
100 |
||
101 |
for child in caseNode.childNodes: |
|
102 |
if child.nodeType != child.ELEMENT_NODE: |
|
103 |
continue
|
|
104 |
||
105 |
if child.tagName == 'stdin': |
|
106 |
# standard input
|
|
107 |
case.set_stdin(getTextData(child)) |
|
108 |
elif child.tagName == 'file': |
|
109 |
# file
|
|
110 |
filename = child.getAttribute('name') |
|
111 |
if filename == '': |
|
112 |
raise ParseException("File without name in case %s" %case_name) |
|
113 |
case.add_file(filename, getTextData(child)) |
|
114 |
elif child.tagName == 'var': |
|
115 |
# global variable
|
|
116 |
var_name = child.getAttribute('name') |
|
117 |
var_val = child.getAttribute('value') |
|
118 |
if not var_name or not var_val: |
|
119 |
raise ParseException("Incomplete variable in case %s" %case_name) |
|
120 |
case.add_variable(var_name, var_val) |
|
121 |
elif child.tagName == 'arg': |
|
122 |
# function argument
|
|
123 |
arg_val = child.getAttribute('value') |
|
124 |
arg_name = child.getAttribute('name') |
|
125 |
if arg_name == '': arg_name = None |
|
126 |
||
127 |
if not arg_val: |
|
128 |
raise ParseException("Incomplete argument in case %s" %case_name) |
|
129 |
case.add_arg(arg_val, arg_name) |
|
299
by dilshan_a
Test framework now handles exceptions as valid outputs for scripts. |
130 |
elif child.tagName == 'exception': |
131 |
exception_name = child.getAttribute('name') |
|
132 |
if not exception_name: |
|
133 |
raise ParseException("Incomplete exception in case %s" %case_name) |
|
134 |
case.add_exception(exception_name) |
|
294
by mattgiuca
Added application: tutorialservice. Will be used as the Ajax backend for |
135 |
elif child.tagName == 'function': |
136 |
# specify a test case part
|
|
137 |
case.add_part(getCasePartData(child)) |
|
138 |
||
139 |
return case |
|
140 |
||
141 |
def parse_tutorial_file(filename, prob_num=1): |
|
142 |
""" Parse an xml problem file and return a testsuite for that problem """
|
|
143 |
dom = parse(filename) |
|
144 |
||
145 |
# get problem
|
|
146 |
count = 0 |
|
147 |
problem = None |
|
148 |
for child in dom.childNodes: |
|
149 |
if child.nodeType == child.ELEMENT_NODE and child.tagName == 'problem': |
|
150 |
count += 1 |
|
151 |
if count == prob_num: |
|
152 |
problem = child |
|
153 |
break
|
|
154 |
||
155 |
if problem == None: |
|
156 |
raise ParseException("Not enough problems") |
|
157 |
||
158 |
# get name
|
|
159 |
problem_name = problem.getAttribute('name') |
|
160 |
||
161 |
if not problem_name: |
|
162 |
raise ParseException('Problem name not supplied') |
|
163 |
||
164 |
problem_suite = TestSuite(problem_name) |
|
165 |
||
166 |
# get solution and include info
|
|
167 |
for child in problem.childNodes: |
|
168 |
if child.nodeType != child.ELEMENT_NODE: |
|
169 |
continue
|
|
170 |
if child.tagName == 'solution': |
|
171 |
problem_suite.add_solution(getTextData(child)) |
|
172 |
elif child.tagName == 'include': |
|
173 |
problem_suite.add_include_code(getTextData(child)) |
|
174 |
elif child.tagName == 'case': |
|
175 |
problem_suite.add_case(getCaseData(child)) |
|
176 |
||
177 |
return problem_suite |
|
178 |