forked from hcientist/OnlinePythonTutor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopt-testcases.ts
More file actions
211 lines (180 loc) · 6.97 KB
/
opt-testcases.ts
File metadata and controls
211 lines (180 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Python Tutor: https://github.com/pgbovine/OnlinePythonTutor/
// Copyright (C) Philip Guo (philip@pgbovine.net)
// LICENSE: https://github.com/pgbovine/OnlinePythonTutor/blob/master/LICENSE.txt
export const redSadFace = require('./images/red-sad-face.jpg');
export const yellowHappyFace = require('./images/yellow-happy-face.jpg');
const testcasesPaneHtml = '\
<table id="testCasesTable">\
<thead>\
<tr>\
<td style="width: 310px">Tests</td>\
<td><button id="runAllTestsButton" type="button">Run All Tests</button></td>\
<td>Results</td>\
<td></td>\
<td></td>\
</tr>\
</thead>\
<tbody>\
</tbody>\
</table>\
\
<a href="#" id="addNewTestCase">Add new test</a>\
'
export class OptTestcases {
parent: any; // type: OptFrontendWithTestcases
curTestcaseId: number = 1;
constructor(parent) {
this.parent = parent;
$("#testCasesParent")
.empty() // just to be paranoid, empty this out (and its event handlers, too, supposedly)
.html('<p style="margin-top: 10px;"><a href="#" id="createTestsLink">Create test cases</a></p><div id="testCasesPane"></div>');
$("#testCasesParent #createTestsLink").click(() => {
this.initTestcasesPane();
$("#testCasesParent #createTestsLink").hide();
return false;
});
}
initTestcasesPane() {
var _me = this;
$("#testCasesParent #testCasesPane")
.empty() // just to be paranoid, empty this out (and its event handlers, too, supposedly)
.html(testcasesPaneHtml);
$("#addNewTestCase").click(function() {
_me.addTestcase(null);
return false; // to prevent link from being followed
});
$("#runAllTestsButton").click(function() {
$(".runTestCase").click();
});
}
loadTestCases(lst: string[]) {
this.initTestcasesPane();
$("#testCasesParent #createTestsLink").hide();
lst.forEach((e) => {
this.addTestcase(e);
});
}
addTestcase(initialCod /* optional code to pre-seed this test */) {
var _me = this;
var id = this.curTestcaseId;
this.curTestcaseId++;
var newTr = $('<tr/>').attr('id', 'testCaseRow_' + id);
$("#testCasesParent #testCasesTable tbody").append(newTr);
var editorTd = $('<td/>');
var runBtnTd = $('<td/>');
var outputTd = $('<td/>');
var visualizeTd = $('<td/>');
var deleteTd = $('<td/>');
editorTd.append('<div id="testCaseEditor_' + id + '" class="testCaseEditor">');
runBtnTd.append('<button id="runTestCase_' + id + '" class="runTestCase" type="button">Run</button>');
outputTd.attr('id', 'outputTd_' + id);
outputTd.attr('class', 'outputTd');
visualizeTd.append('<button id="vizTestCase_' + id + '" class="vizTestCase" type="button">Visualize</button>');
deleteTd.append('<a id="delTestCase_' + id + '" href="javascript:void(0);">Delete test</a></td>');
newTr.append(editorTd);
newTr.append(runBtnTd);
newTr.append(outputTd);
newTr.append(visualizeTd);
newTr.append(deleteTd);
// initialize testCaseEditor with Ace:
var te = ace.edit('testCaseEditor_' + id);
// set the size and value ASAP to get alignment working well ...
te.setOptions({minLines: 2, maxLines: 4}); // keep this SMALL
te.setHighlightActiveLine(false);
te.setShowPrintMargin(false);
te.setBehavioursEnabled(false);
te.setFontSize('11px');
te.$blockScrolling = Infinity; // kludgy to shut up weird warnings
var s = te.getSession();
s.setTabSize(2);
s.setUseSoftTabs(true);
// disable extraneous indicators:
s.setFoldStyle('manual'); // no code folding indicators
// don't do real-time syntax checks:
// https://github.com/ajaxorg/ace/wiki/Syntax-validation
s.setOption("useWorker", false);
s.on("change", (e) => {
$('#outputTd_' + id).empty(); // remove all test output indicators
});
// TODO: change syntax highlighting mode if the user changes languages:
var lang = $('#pythonVersionSelector').val();
var mod = 'python';
var defaultVal = '\n# assert <test condition>';
if (lang === 'java') {
mod = 'java';
defaultVal = '// sorry, Java tests not yet supported';
} else if (lang === 'c' || lang === 'cpp') {
mod = 'c_cpp';
defaultVal = '// sorry, C/C++ tests not yet supported';
} else if (lang === 'js') {
mod = 'javascript';
defaultVal = '\n// console.assert(<test condition>);';
} else if (lang === 'ts') {
mod = 'typescript';
defaultVal = '\n// console.assert(<test condition>);';
} else if (lang === 'ruby') {
mod = 'ruby';
defaultVal = "\n# raise 'fail' unless <test condition>";
}
s.setMode("ace/mode/" + mod);
te.setValue(initialCod ? initialCod.rtrim() : defaultVal,
-1 /* do NOT select after setting text */);
te.focus();
function runOrVizTestCase(isViz /* true for visualize, false for run */) {
$("#runAllTestsButton,.runTestCase,.vizTestCase").attr('disabled', true);
var e = ace.edit('testCaseEditor_' + id);
e.getSession().clearAnnotations();
$('#outputTd_' + id).html('');
var dat = _me.getCombinedCode(id);
var cod = dat.cod;
if (isViz) {
$('#vizTestCase_' + id).html("Visualizing ...");
_me.parent.vizTestCase(id, cod, dat.firstTestLine);
} else {
$('#runTestCase_' + id).html("Running ...");
_me.parent.runTestCase(id, cod, dat.firstTestLine);
}
}
$('#runTestCase_' + id).click(runOrVizTestCase.bind(this, false));
$('#vizTestCase_' + id).click(runOrVizTestCase.bind(this, true));
$('#delTestCase_' + id).click(function() {
$('#testCaseRow_' + id).remove();
return false; // to prevent link from being followed
});
}
doneRunningTest() {
$("#runAllTestsButton,.runTestCase,.vizTestCase").attr('disabled', false);
$(".runTestCase").html('Run');
$(".vizTestCase").html('Visualize');
}
getCombinedCode(id) {
var userCod = this.parent.pyInputGetValue();
var testCod = ace.edit('testCaseEditor_' + id).getValue();
// for reporting syntax errors separately for user and test code
var userCodNumLines = userCod.split('\n').length;
var lang = $('#pythonVersionSelector').val();
if (lang === 'ts' || lang === 'js' || lang === 'java' || lang === 'c' || lang === 'cpp') {
var bufferCod = '\n\n// Test code //\n';
} else {
var bufferCod = '\n\n## Test code ##\n';
}
var bufferCodNumLines = bufferCod.split('\n').length;
var combinedCod = userCod + bufferCod + testCod;
return {cod: combinedCod,
firstTestLine: userCodNumLines + bufferCodNumLines - 1};
}
appStateAugmenter(appState) {
// returns a list of strings, each of which is a test case
function getAllTestcases() {
return $.map($("#testCasesParent #testCasesTable .testCaseEditor"),
(e) => {
var editor = ace.edit($(e).attr('id'));
return editor.getValue();
});
}
var tc = getAllTestcases();
if (tc.length > 0) {
appState['testCasesJSON'] = JSON.stringify(tc);
}
}
} // END class OptTestcases