~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/apps/tutorial/test/parse_tute.py

  • Committer: dilshan_a
  • Date: 2008-01-23 04:54:10 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:270
Initial check in for test framework, and example problems

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from xml.dom.minidom import *
 
2
from TestFramework import *
 
3
 
 
4
DEFAULT_TEST_TYPE = 'norm'
 
5
DEFAULT_CASE_TYPE = 'match'
 
6
 
 
7
class ParseException(Exception):
 
8
    " Error when parsing the xml problem file "
 
9
    def __init___(self, reason):
 
10
        self._reason = reason
 
11
 
 
12
    def __str__(self):
 
13
        return self._reason
 
14
 
 
15
def getTextData(element):
 
16
    """ Get the text and cdata inside an element
 
17
    Leading and trailing whitespace are stripped
 
18
    """
 
19
    data = ''
 
20
    for child in element.childNodes:
 
21
        if child.nodeType == child.CDATA_SECTION_NODE:
 
22
            data += child.data
 
23
        if child.nodeType == child.TEXT_NODE:
 
24
            data += child.data
 
25
 
 
26
    return data.strip()
 
27
 
 
28
def getCasePartData(partNode):
 
29
    """ Create an TestCasePaart instance from test part xml data
 
30
    """
 
31
    
 
32
    func_desc = partNode.getAttribute('desc')
 
33
    default = partNode.getAttribute('default')
 
34
    if default == '': default = DEFAULT_CASE_TYPE
 
35
    
 
36
    part = TestCasePart(func_desc, default)
 
37
 
 
38
    for child in partNode.childNodes:
 
39
        if child.nodeType != child.ELEMENT_NODE:
 
40
            continue
 
41
 
 
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')
 
54
            if filename == '':
 
55
                raise ParseException("File without name in case %s" %case_name)
 
56
            part.add_file_test(filename, getTextData(child), test_type)
 
57
 
 
58
    return part
 
59
 
 
60
def getCaseData(caseNode):
 
61
    """ Creare a TestCase instance from test case xml data
 
62
    """
 
63
    
 
64
    case_name = caseNode.getAttribute('name')
 
65
    function = caseNode.getAttribute('function')
 
66
    if function == '': function = None
 
67
    case = TestCase(case_name, function)
 
68
    
 
69
    for child in caseNode.childNodes:
 
70
        if child.nodeType != child.ELEMENT_NODE:
 
71
            continue
 
72
 
 
73
        if child.tagName == 'stdin':
 
74
            # standard input
 
75
            case.set_stdin(getTextData(child))
 
76
        elif child.tagName == 'file':
 
77
            # file
 
78
            filename = child.getAttribute('name')
 
79
            if filename == '':
 
80
                raise ParseException("File without name in case %s" %case_name)
 
81
            case.add_file(filename, getTextData(child))
 
82
        elif child.tagName == 'var':
 
83
            # global variable
 
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':
 
90
            # function argument
 
91
            arg_val = child.getAttribute('value')
 
92
            arg_name = child.getAttribute('name')
 
93
            if arg_name == '': arg_name = None
 
94
            
 
95
            if not arg_val:
 
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))
 
101
 
 
102
    return case
 
103
                
 
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)
 
107
 
 
108
    # get problem
 
109
    count = 0
 
110
    problem = None
 
111
    for child in dom.childNodes:
 
112
        if child.nodeType == child.ELEMENT_NODE and child.tagName == 'problem':
 
113
            count += 1
 
114
            if count == prob_num:
 
115
                problem = child
 
116
                break
 
117
 
 
118
    if problem == None:
 
119
        raise ParseException("Not enough problems")
 
120
 
 
121
    # get name
 
122
    problem_name = problem.getAttribute('name')
 
123
 
 
124
    if not problem_name:
 
125
        raise ParseException('Problem name not supplied')
 
126
 
 
127
    problem_suite = TestSuite(problem_name)
 
128
 
 
129
    # get solution and include info
 
130
    for child in problem.childNodes:
 
131
        if child.nodeType != child.ELEMENT_NODE:
 
132
            continue
 
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))
 
139
 
 
140
    return problem_suite
 
141