Skip to content

Commit 82d4bbc

Browse files
committed
bam
1 parent c31b66d commit 82d4bbc

2 files changed

Lines changed: 217 additions & 217 deletions

File tree

‎v5-unity/js/opt-frontend.ts‎

Lines changed: 5 additions & 217 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
/* TODO:
66
7-
- make sure the "create test cases" feature still works
8-
97
- parse Java viz_options in users' java code:
108
https://github.com/daveagp/java_visualize/blob/1489078712310eda44391f09405e0f71b2b190c9/jv-frontend.js#L101
119
@@ -53,6 +51,7 @@ require('script!./lib/socket.io-client/socket.io.js');
5351

5452
// need to directly import the class for type checking to work
5553
import {AbstractBaseFrontend, generateUUID, supports_html5_storage} from './opt-frontend-common.ts';
54+
import {OptTestcases, redSadFace, yellowHappyFace} from './opt-testcases.ts';
5655

5756
var pytutor = require('./pytutor.ts');
5857
var assert = pytutor.assert;
@@ -101,7 +100,7 @@ function optFrontendTogetherjsCloseHandler() {
101100
}
102101
*/
103102

104-
class OptFrontend extends AbstractBaseFrontend {
103+
export class OptFrontend extends AbstractBaseFrontend {
105104
originFrontendJsFile: string = 'opt-frontend.js';
106105
pyInputAceEditor; // Ace editor object that contains the user's code
107106

@@ -791,7 +790,7 @@ class OptFrontend extends AbstractBaseFrontend {
791790

792791

793792
// OptFrontend augmented with a "Create test cases" pane
794-
class OptFrontendWithTestcases extends OptFrontend {
793+
export class OptFrontendWithTestcases extends OptFrontend {
795794
optTests: OptTestcases;
796795

797796
constructor(params) {
@@ -906,217 +905,6 @@ class OptFrontendWithTestcases extends OptFrontend {
906905
} // END Class OptFrontendWithTestcases
907906

908907

909-
910-
// TODO: maybe extract to a new file called opt-testcases.ts ?
911-
912-
const redSadFace = require('./images/red-sad-face.jpg');
913-
const yellowHappyFace = require('./images/yellow-happy-face.jpg');
914-
915-
const testcasesPaneHtml = '\
916-
<table id="testCasesTable">\
917-
<thead>\
918-
<tr>\
919-
<td style="width: 310px">Tests</td>\
920-
<td><button id="runAllTestsButton" type="button">Run All Tests</button></td>\
921-
<td>Results</td>\
922-
<td></td>\
923-
<td></td>\
924-
</tr>\
925-
</thead>\
926-
<tbody>\
927-
</tbody>\
928-
</table>\
929-
\
930-
<a href="#" id="addNewTestCase">Add new test</a>\
931-
'
932-
933-
class OptTestcases {
934-
parent: OptFrontendWithTestcases;
935-
curTestcaseId: number = 1;
936-
937-
constructor(parent) {
938-
this.parent = parent;
939-
$("#testCasesParent")
940-
.empty() // just to be paranoid, empty this out (and its event handlers, too, supposedly)
941-
.html('<p style="margin-top: 25px;"><a href="#" id="createTestsLink">Create test cases</a></p><div id="testCasesPane"></div>');
942-
943-
$("#testCasesParent #createTestsLink").click(() => {
944-
this.initTestcasesPane();
945-
$("#testCasesParent #createTestsLink").hide();
946-
return false;
947-
});
948-
}
949-
950-
initTestcasesPane() {
951-
var _me = this;
952-
$("#testCasesParent #testCasesPane")
953-
.empty() // just to be paranoid, empty this out (and its event handlers, too, supposedly)
954-
.html(testcasesPaneHtml);
955-
956-
$("#addNewTestCase").click(function() {
957-
_me.addTestcase(null);
958-
return false; // to prevent link from being followed
959-
});
960-
961-
$("#runAllTestsButton").click(function() {
962-
$(".runTestCase").click();
963-
});
964-
}
965-
966-
loadTestCases(lst: string[]) {
967-
this.initTestcasesPane();
968-
$("#testCasesParent #createTestsLink").hide();
969-
lst.forEach((e) => {
970-
this.addTestcase(e);
971-
});
972-
}
973-
974-
addTestcase(initialCod /* optional code to pre-seed this test */) {
975-
var _me = this;
976-
977-
var id = this.curTestcaseId;
978-
this.curTestcaseId++;
979-
var newTr = $('<tr/>').attr('id', 'testCaseRow_' + id);
980-
$("#testCasesParent #testCasesTable tbody").append(newTr);
981-
var editorTd = $('<td/>');
982-
var runBtnTd = $('<td/>');
983-
var outputTd = $('<td/>');
984-
var visualizeTd = $('<td/>');
985-
var deleteTd = $('<td/>');
986-
987-
editorTd.append('<div id="testCaseEditor_' + id + '" class="testCaseEditor">');
988-
runBtnTd.append('<button id="runTestCase_' + id + '" class="runTestCase" type="button">Run</button>');
989-
outputTd.attr('id', 'outputTd_' + id);
990-
outputTd.attr('class', 'outputTd');
991-
visualizeTd.append('<button id="vizTestCase_' + id + '" class="vizTestCase" type="button">Visualize</button>');
992-
deleteTd.append('<a id="delTestCase_' + id + '" href="javascript:void(0);">Delete test</a></td>');
993-
994-
newTr.append(editorTd);
995-
newTr.append(runBtnTd);
996-
newTr.append(outputTd);
997-
newTr.append(visualizeTd);
998-
newTr.append(deleteTd);
999-
1000-
// initialize testCaseEditor with Ace:
1001-
var te = ace.edit('testCaseEditor_' + id);
1002-
// set the size and value ASAP to get alignment working well ...
1003-
te.setOptions({minLines: 2, maxLines: 4}); // keep this SMALL
1004-
te.setHighlightActiveLine(false);
1005-
te.setShowPrintMargin(false);
1006-
te.setBehavioursEnabled(false);
1007-
te.setFontSize('11px');
1008-
te.$blockScrolling = Infinity; // kludgy to shut up weird warnings
1009-
1010-
var s = te.getSession();
1011-
s.setTabSize(2);
1012-
s.setUseSoftTabs(true);
1013-
// disable extraneous indicators:
1014-
s.setFoldStyle('manual'); // no code folding indicators
1015-
// don't do real-time syntax checks:
1016-
// https://github.com/ajaxorg/ace/wiki/Syntax-validation
1017-
s.setOption("useWorker", false);
1018-
s.on("change", (e) => {
1019-
$('#outputTd_' + id).empty(); // remove all test output indicators
1020-
});
1021-
1022-
// TODO: change syntax highlighting mode if the user changes languages:
1023-
var lang = $('#pythonVersionSelector').val();
1024-
var mod = 'python';
1025-
var defaultVal = '\n# assert <test condition>';
1026-
if (lang === 'java') {
1027-
mod = 'java';
1028-
defaultVal = '// sorry, Java tests not yet supported';
1029-
} else if (lang === 'c' || lang === 'cpp') {
1030-
mod = 'c_cpp';
1031-
defaultVal = '// sorry, C/C++ tests not yet supported';
1032-
} else if (lang === 'js') {
1033-
mod = 'javascript';
1034-
defaultVal = '\n// console.assert(<test condition>);';
1035-
} else if (lang === 'ts') {
1036-
mod = 'typescript';
1037-
defaultVal = '\n// console.assert(<test condition>);';
1038-
} else if (lang === 'ruby') {
1039-
mod = 'ruby';
1040-
defaultVal = "\n# raise 'fail' unless <test condition>";
1041-
}
1042-
s.setMode("ace/mode/" + mod);
1043-
1044-
te.setValue(initialCod ? initialCod.rtrim() : defaultVal,
1045-
-1 /* do NOT select after setting text */);
1046-
te.focus();
1047-
1048-
function runOrVizTestCase(isViz /* true for visualize, false for run */) {
1049-
$("#runAllTestsButton,.runTestCase,.vizTestCase").attr('disabled', true);
1050-
var e = ace.edit('testCaseEditor_' + id);
1051-
e.getSession().clearAnnotations();
1052-
$('#outputTd_' + id).html('');
1053-
1054-
var dat = _me.getCombinedCode(id);
1055-
var cod = dat.cod;
1056-
1057-
if (isViz) {
1058-
$('#vizTestCase_' + id).html("Visualizing ...");
1059-
_me.parent.vizTestCase(id, cod, dat.firstTestLine);
1060-
} else {
1061-
$('#runTestCase_' + id).html("Running ...");
1062-
_me.parent.runTestCase(id, cod, dat.firstTestLine);
1063-
}
1064-
}
1065-
1066-
$('#runTestCase_' + id).click(runOrVizTestCase.bind(this, false));
1067-
$('#vizTestCase_' + id).click(runOrVizTestCase.bind(this, true));
1068-
1069-
$('#delTestCase_' + id).click(function() {
1070-
$('#testCaseRow_' + id).remove();
1071-
return false; // to prevent link from being followed
1072-
});
1073-
}
1074-
1075-
doneRunningTest() {
1076-
$("#runAllTestsButton,.runTestCase,.vizTestCase").attr('disabled', false);
1077-
$(".runTestCase").html('Run');
1078-
$(".vizTestCase").html('Visualize');
1079-
}
1080-
1081-
getCombinedCode(id) {
1082-
var userCod = this.parent.pyInputGetValue();
1083-
var testCod = ace.edit('testCaseEditor_' + id).getValue();
1084-
// for reporting syntax errors separately for user and test code
1085-
var userCodNumLines = userCod.split('\n').length;
1086-
1087-
var lang = $('#pythonVersionSelector').val();
1088-
if (lang === 'ts' || lang === 'js' || lang === 'java' || lang === 'c' || lang === 'cpp') {
1089-
var bufferCod = '\n\n// Test code //\n';
1090-
} else {
1091-
var bufferCod = '\n\n## Test code ##\n';
1092-
}
1093-
1094-
var bufferCodNumLines = bufferCod.split('\n').length;
1095-
1096-
var combinedCod = userCod + bufferCod + testCod;
1097-
return {cod: combinedCod,
1098-
firstTestLine: userCodNumLines + bufferCodNumLines - 1};
1099-
}
1100-
1101-
appStateAugmenter(appState) {
1102-
// returns a list of strings, each of which is a test case
1103-
function getAllTestcases() {
1104-
return $.map($("#testCasesParent #testCasesTable .testCaseEditor"),
1105-
(e) => {
1106-
var editor = ace.edit($(e).attr('id'));
1107-
return editor.getValue();
1108-
});
1109-
}
1110-
1111-
var tc = getAllTestcases();
1112-
if (tc.length > 0) {
1113-
appState['testCasesJSON'] = JSON.stringify(tc);
1114-
}
1115-
}
1116-
1117-
} // END class OptTestcases
1118-
1119-
1120908
var JS_EXAMPLES = {
1121909
jsFactExLink: 'fact.js',
1122910
jsDatatypesExLink: 'data-types.js',
@@ -1345,8 +1133,8 @@ $(document).ready(function() {
13451133
assert(mod == 'display' || mod == 'visualize' /* deprecated */);
13461134
var myArgs = optFrontend.getAppState();
13471135
delete myArgs.mode;
1348-
myArgs.codeDivWidth = optFrontend.myVisualizer.DEFAULT_EMBEDDED_CODE_DIV_WIDTH;
1349-
myArgs.codeDivHeight = optFrontend.myVisualizer.DEFAULT_EMBEDDED_CODE_DIV_HEIGHT;
1136+
(myArgs as any).codeDivWidth = optFrontend.myVisualizer.DEFAULT_EMBEDDED_CODE_DIV_WIDTH;
1137+
(myArgs as any).codeDivHeight = optFrontend.myVisualizer.DEFAULT_EMBEDDED_CODE_DIV_HEIGHT;
13501138

13511139
var domain = "http://pythontutor.com/"; // for deployment
13521140
var embedUrlStr = $.param.fragment(domain + "iframe-embed.html", myArgs, 2 /* clobber all */);

0 commit comments

Comments
 (0)