@@ -29,6 +29,26 @@ var tests = null;
2929var expects = null ;
3030var curTestIndex = - 1 ;
3131
32+ // the traces returned by executing the respective 'tests' and 'expects'
33+ // Python code. See resetResultTraces for invariants.
34+ var testsTraces = null ;
35+ var expectsTraces = null ;
36+
37+ // Pre: 'tests' and 'expects' are non-null
38+ function resetResultTraces ( ) {
39+ testsTraces = [ ] ;
40+ expectsTraces = [ ] ;
41+ $ . each ( tests , function ( i ) {
42+ testsTraces . push ( null ) ;
43+ expectsTraces . push ( null ) ;
44+ } ) ;
45+
46+ assert ( testsTraces . length > 0 ) ;
47+ assert ( testsTraces . length == expectsTraces . length ) ;
48+ assert ( testsTraces . length == tests . length ) ;
49+ assert ( expectsTraces . length == expects . length ) ;
50+ }
51+
3252
3353$ ( document ) . ready ( function ( ) {
3454 eduPythonCommonInit ( ) ; // must call this first!
@@ -79,6 +99,9 @@ function enterVisualizeMode() {
7999
80100 $ ( "#HintStatement" ) . show ( ) ;
81101 $ ( "#SolutionStatement" ) . show ( ) ;
102+
103+ $ ( '#submitBtn' ) . html ( "Submit answer" ) ;
104+ $ ( '#submitBtn' ) . attr ( 'disabled' , false ) ;
82105}
83106
84107function enterGradingMode ( ) {
@@ -93,6 +116,42 @@ function enterGradingMode() {
93116}
94117
95118
119+ // returns a curried function!
120+ function genTestResultHandler ( idx ) {
121+ function ret ( traceData ) {
122+ assert ( testsTraces [ idx ] === null ) ;
123+ testsTraces [ idx ] = traceData ;
124+
125+ // if ALL results have been delivered, then call
126+ // readyToGradeSubmission() ...
127+ for ( var i = 0 ; i < testsTraces . length ; i ++ ) {
128+ if ( testsTraces [ i ] === null || expectsTraces [ i ] === null ) {
129+ return ;
130+ }
131+ }
132+ readyToGradeSubmission ( ) ;
133+ }
134+ return ret ;
135+ }
136+
137+ function genExpectResultHandler ( idx ) {
138+ function ret ( traceData ) {
139+ assert ( expectsTraces [ idx ] === null ) ;
140+ expectsTraces [ idx ] = traceData ;
141+
142+ // if ALL results have been delivered, then call
143+ // readyToGradeSubmission() ...
144+ for ( var i = 0 ; i < testsTraces . length ; i ++ ) {
145+ if ( testsTraces [ i ] === null || expectsTraces [ i ] === null ) {
146+ return ;
147+ }
148+ }
149+ readyToGradeSubmission ( ) ;
150+ }
151+ return ret ;
152+ }
153+
154+
96155function finishQuestionsInit ( questionsDat ) {
97156 $ ( "#ProblemName" ) . html ( questionsDat . name ) ;
98157 $ ( "#ProblemStatement" ) . html ( questionsDat . question ) ;
@@ -115,7 +174,9 @@ function finishQuestionsInit(questionsDat) {
115174 tests = questionsDat . tests ;
116175 expects = questionsDat . expects ;
117176 curTestIndex = 0 ;
118- assert ( tests . length == expects . length ) ;
177+
178+ resetResultTraces ( ) ;
179+
119180
120181 $ ( "#testCodeInput" ) . val ( tests [ curTestIndex ] ) ;
121182
@@ -152,9 +213,31 @@ function finishQuestionsInit(questionsDat) {
152213
153214
154215 $ ( "#submitBtn" ) . click ( function ( ) {
155- enterGradingMode ( ) ;
156- console . log ( "SUBMIT!" ) ;
216+ $ ( '#submitBtn' ) . html ( "Please wait ... submitting ..." ) ;
217+ $ ( '#submitBtn' ) . attr ( 'disabled' , true ) ;
218+
219+ // right now I make (2 * tests.length) HTTP POST calls, which might
220+ // be inefficient, so optimize later if necessary
221+ //
222+ // remember to code this up VERY CAREFULLY because the responses
223+ // will come in asynchronously and probably OUT-OF-ORDER
224+ for ( var i = 0 ; i < tests . length ; i ++ ) {
225+ var submittedCode = concatSolnTestCode ( $ ( "#actualCodeInput" ) . val ( ) , tests [ i ] ) ;
226+ $ . post ( "cgi-bin/web_exec.py" ,
227+ { user_script : submittedCode } ,
228+ genTestResultHandler ( i ) ,
229+ "json" ) ;
230+
231+ var expectCode = expects [ i ] ;
232+ $ . post ( "cgi-bin/web_exec.py" ,
233+ { user_script : expectCode } ,
234+ genExpectResultHandler ( i ) ,
235+ "json" ) ;
236+ }
157237
238+ return ;
239+
240+ enterGradingMode ( ) ;
158241 $ ( "#submittedCodeRO" ) . val ( $ ( "#actualCodeInput" ) . val ( ) ) ;
159242
160243 // iterate through all pairs of test and expect code:
@@ -177,3 +260,10 @@ function finishQuestionsInit(questionsDat) {
177260 } ) ;
178261
179262}
263+
264+ // should be called after ALL elements in testsTraces and expectsTraces
265+ // have been populated by their respective AJAX POST calls
266+ function readyToGradeSubmission ( ) {
267+ console . log ( 'readyToGradeSubmission' ) ;
268+ }
269+
0 commit comments