28
28
function submitproblem(problemid, filename)
30
30
/* Get the source code the student is submitting */
31
problemdiv = document.getElementById(problemid);
32
problembox = problemdiv.getElementsByTagName("textarea")[0];
33
code = problembox.value;
34
alert("code = \"" + code + "\"\nproblem = \"" + filename + "\"\n"
35
+ "action = \"test\"");
31
var problemdiv = document.getElementById(problemid);
32
var problembox = problemdiv.getElementsByTagName("textarea")[0];
33
var code = problembox.value;
35
var args = {"code": code, "problem": filename, "action": "test"};
37
/* Send the form as multipart/form-data, since we are sending a whole lump
38
* of Python code, it should be treated like a file upload. */
39
var xhr = ajax_call("tutorialservice", "", args, "POST",
40
"multipart/form-data");
41
var testresponse = JSON.parse(xhr.responseText);
42
handle_testresponse(problemdiv, testresponse);
45
/** Given a problem div, return the testoutput div which is its child.
46
* (The div which is its child whose class is "testoutput".
48
function get_testoutput(problemdiv)
50
var childs = problemdiv.childNodes;
53
for (i=0; i<childs.length; i++)
54
if (childs[i].nodeType == problemdiv.ELEMENT_NODE &&
55
childs[i].getAttribute("class") == "testoutput")
60
/** Given a response object (JSON-parsed object), displays the result of the
61
* test to the user. This modifies the given problemdiv's children.
63
function handle_testresponse(problemdiv, testresponse)
65
var testoutput = get_testoutput(problemdiv);
69
if (testoutput == null) return; /* should not happen */
70
dom_removechildren(testoutput);
72
ul = document.createElement("ul");
73
testoutput.appendChild(ul);
75
if ("critical_error" in testresponse)
77
/* Only one error - and it's bad.
78
* Just print and stop */
79
ul.appendChild(create_response_item("critical",
80
testresponse.critical_error.name,
81
testresponse.critical_error.detail));
85
for (i=0; i<testresponse.cases.length; i++)
87
var testcase = testresponse.cases[i];
88
if ("exception" in testcase)
90
/* User's code threw an exception */
91
fail_li = create_response_item("fail", testcase.name);
92
ul.appendChild(fail_li);
93
/* Create a sub-ul to display the failing cases. */
94
case_ul = document.createElement("ul");
95
fail_li.appendChild(case_ul);
96
case_ul.appendChild(create_response_item("exception",
97
testcase.exception.name, testcase.exception.detail));
99
else if (testcase.passed)
101
/* All parts of the test case passed. Just report the overall case
103
ul.appendChild(create_response_item("pass", testcase.name));
107
var fail_li = create_response_item("fail", testcase.name);
108
ul.appendChild(fail_li);
109
/* Create a sub-ul to display the failing cases. */
110
case_ul = document.createElement("ul");
111
fail_li.appendChild(case_ul);
113
for (j=0; j<testcase.parts.length; j++)
115
var part = testcase.parts[j];
118
case_ul.appendChild(create_response_item("pass",
123
case_ul.appendChild(create_response_item("fail",
124
part.description, part.error_message));
131
/* DOM creators for test case response elements */
133
/** Create a <li> element for the result of a test case.
134
* type: "pass", "fail", "exception" or "critical"
135
* detail should be null for passing cases.
136
* For exceptions and crits, "desc" is the exception name,
137
* detail is the message.
139
function create_response_item(type, desc, detail)
142
if (type == "critical")
144
/* Crits look like exceptions, but are slightly different */
148
var li = document.createElement("li");
149
li.setAttribute("class", type);
150
var b = document.createElement("b");
151
var text = type[0].toUpperCase() + type.substr(1) + ":";
152
b.appendChild(document.createTextNode(text));
156
else if (type == "fail")
157
text = desc + (detail == null ? "" : ":");
158
else if (type == "exception")
161
text = "Your code could not be executed, "
162
+ "due to the following error:";
164
text = "The following exception occured "
165
+ "while running your code:";
167
li.appendChild(document.createTextNode(" " + text));
168
if (type == "pass" || (type == "fail" && detail == null))
171
/* Non-passes, display the error message */
172
li.appendChild(document.createElement("br"));
173
if (type == "exception")
175
b = document.createElement("b");
176
b.appendChild(document.createTextNode(desc + ":"));
179
li.appendChild(document.createTextNode(detail));