Skip to content

Commit 3af5f1e

Browse files
committed
started implementing test cases
1 parent c95e8cd commit 3af5f1e

6 files changed

Lines changed: 185 additions & 0 deletions

File tree

‎v3/css/opt-frontend.css‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,9 @@ button.surveyBtn {
220220
#syntaxErrHideAllLink {
221221
font-size: 8pt;
222222
}
223+
224+
#testCasesPane {
225+
margin-top: 20px;
226+
padding: 5px;
227+
/* border: 1px solid #ddd; */
228+
}

‎v3/css/opt-testcases.css‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
.testCaseEditor {
2+
width: 300px;
3+
height: 90px; /* VERY IMPORTANT so that it works on I.E., ugh! */
4+
border: 1px solid #ddd;
5+
padding: 3px;
6+
}
7+
8+
#testCasesTable thead td {
9+
padding-bottom: 5px;
10+
border-bottom: 1px solid #bbb;
11+
text-align: center;
12+
}
13+
14+
#testCasesTable td {
15+
padding-top: 10px;
16+
padding-right: 5px;
17+
vertical-align: top;
18+
}
19+
20+
#testCasesTable {
21+
margin-bottom: 5px;
22+
}

‎v3/js/opt-frontend-common.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,11 @@ function getAppState() {
10541054
if (ret.curInstr === undefined)
10551055
delete ret.curInstr;
10561056

1057+
// different frontends can optionally AUGMENT the app state with
1058+
// custom fields
1059+
if (typeof(appStateAugmenter) !== 'undefined') {
1060+
appStateAugmenter(ret);
1061+
}
10571062
return ret;
10581063
}
10591064

‎v3/js/opt-frontend.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,4 +779,6 @@ $(document).ready(function() {
779779
$("#chat_div").chatbox("option", "boxManager").addMsg('Tutor', msg.text);
780780
});
781781
}
782+
783+
initTestcasesPane('#testCasesPane');
782784
});

‎v3/js/opt-testcases.js‎

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
3+
Online Python Tutor
4+
https://github.com/pgbovine/OnlinePythonTutor/
5+
6+
Copyright (C) Philip J. Guo (philip@pgbovine.net)
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a
9+
copy of this software and associated documentation files (the
10+
"Software"), to deal in the Software without restriction, including
11+
without limitation the rights to use, copy, modify, merge, publish,
12+
distribute, sublicense, and/or sell copies of the Software, and to
13+
permit persons to whom the Software is furnished to do so, subject to
14+
the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included
17+
in all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26+
27+
*/
28+
29+
// Pre-reqs:
30+
// - jquery-1.8.2.min.js
31+
// - pytutor.js
32+
// - opt-frontend-common.js
33+
// - ace/src-min-noconflict/ace.js
34+
// should all be imported BEFORE this file
35+
36+
37+
var testcasesPaneHtml = '\
38+
<table id="testCasesTable">\
39+
<thead>\
40+
<tr>\
41+
<td style="width: 310px">Tests</td>\
42+
<td><button id="runAllTestsButton" type="button">Run All</button></td>\
43+
<td>Results</td>\
44+
<td></td>\
45+
<td></td>\
46+
</tr>\
47+
</thead>\
48+
<tbody>\
49+
</tbody>\
50+
</table>\
51+
\
52+
<a href="#" id="addNewTestCase">Add new test</a>\
53+
'
54+
55+
var curTestcaseId = 1;
56+
57+
function initTestcasesPane(parentDivId) {
58+
$(parentDivId).html(testcasesPaneHtml);
59+
60+
$("#addNewTestCase").click(function() {
61+
addTestcase(curTestcaseId);
62+
curTestcaseId++;
63+
return false; // to prevent link from being followed
64+
});
65+
}
66+
67+
function addTestcase(id) {
68+
var newTr = $('<tr/>').attr('id', 'testCaseRow_' + id);
69+
$("#testCasesTable tbody").append(newTr);
70+
var editorTd = $('<td/>');
71+
var runBtnTd = $('<td/>');
72+
var outputTd = $('<td/>');
73+
var visualizeTd = $('<td/>');
74+
var deleteTd = $('<td/>');
75+
76+
editorTd.append('<div id="testCaseEditor_' + id + '" class="testCaseEditor">');
77+
runBtnTd.append('<button id="runTestCase_' + id + '" type="button">Run</button>');
78+
outputTd.attr('id', 'outputTd_' + id);
79+
visualizeTd.append('<button id="vizTestCase_' + id + '" type="button">Visualize</button>');
80+
deleteTd.append('<a id="delTestCase_' + id + '" href="javascript:void(0);">Delete test</a></td>');
81+
82+
newTr.append(editorTd);
83+
newTr.append(runBtnTd);
84+
newTr.append(outputTd);
85+
newTr.append(visualizeTd);
86+
newTr.append(deleteTd);
87+
88+
$('#delTestCase_' + id).click(function() {
89+
var res = confirm("Press OK to delete this test.");
90+
if (res) {
91+
$('#testCaseRow_' + id).remove();
92+
}
93+
return false; // to prevent link from being followed
94+
});
95+
96+
// initialize testCaseEditor with Ace:
97+
var te = ace.edit('testCaseEditor_' + id);
98+
// set the size and value ASAP to get alignment working well ...
99+
te.setOptions({minLines: 2, maxLines: 4}); // keep this SMALL
100+
te.setHighlightActiveLine(false);
101+
te.setShowPrintMargin(false);
102+
te.setBehavioursEnabled(false);
103+
te.setFontSize(10);
104+
//te.setReadOnly(true);
105+
106+
var s = te.getSession();
107+
s.setTabSize(2);
108+
s.setUseSoftTabs(true);
109+
// disable extraneous indicators:
110+
s.setFoldStyle('manual'); // no code folding indicators
111+
// don't do real-time syntax checks:
112+
// https://github.com/ajaxorg/ace/wiki/Syntax-validation
113+
s.setOption("useWorker", false);
114+
115+
// TODO: change syntax highlighting mode if the user changes languages:
116+
var lang = $('#pythonVersionSelector').val();
117+
var mod = 'python';
118+
if (lang === 'java') {
119+
mod = 'java';
120+
} else if (lang === 'js') {
121+
mod = 'javascript';
122+
} else if (lang === 'ts') {
123+
mod = 'typescript';
124+
} else if (lang === 'ruby') {
125+
mod = 'ruby';
126+
}
127+
s.setMode("ace/mode/" + mod);
128+
}
129+
130+
// returns a list of strings, each of which is a test case
131+
function getAllTestcases() {
132+
return $.map($("#testCasesTable .testCaseEditor"), function(e) {
133+
var editor = ace.edit($(e).attr('id'));
134+
return editor.getValue();
135+
});
136+
}
137+
138+
// see getAppState to see where it calls out to this function:
139+
function appStateAugmenter(appState) {
140+
var tc = getAllTestcases();
141+
if (tc.length > 0) {
142+
appState['testCases'] = tc;
143+
}
144+
}

‎v3/visualize.html‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@
8383

8484
<script type="text/javascript" src="js/socket.io-client/socket.io.js"></script>
8585

86+
<script type="text/javascript" src="js/opt-testcases.js"></script>
87+
<link rel="stylesheet" href="css/opt-testcases.css"/>
88+
8689

8790
<script type="text/javascript">
8891

@@ -235,6 +238,9 @@
235238
</div>
236239

237240

241+
<div id="testCasesPane"/>
242+
243+
238244
<p style="margin-top: 25px; font-weight: bold;">Python Examples</p>
239245

240246
<p style="margin-top: 10px;">Basic:

0 commit comments

Comments
 (0)