@@ -102,7 +102,9 @@ export function CodeEditor({ problem }: CodeEditorProps) {
102
102
const [ executionError , setExecutionError ] = useState < string | null > ( null ) ;
103
103
104
104
// Add this state to track test case results
105
- const [ testCaseResults , setTestCaseResults ] = useState < Record < number , TestCaseResult > > ( { } ) ;
105
+ const [ testCaseResults , setTestCaseResults ] = useState <
106
+ Record < number , TestCaseResult >
107
+ > ( { } ) ;
106
108
107
109
const languageVersions = {
108
110
javascript : "18.15.0" ,
@@ -140,7 +142,9 @@ export function CodeEditor({ problem }: CodeEditorProps) {
140
142
}
141
143
} ;
142
144
143
- const runCode = async ( testCaseInput : string ) : Promise < { success : boolean , output : string } > => {
145
+ const runCode = async (
146
+ testCaseInput : string ,
147
+ ) : Promise < { success : boolean ; output : string } > => {
144
148
console . log ( "Starting code execution..." ) ;
145
149
setIsRunning ( true ) ;
146
150
setOutput ( "" ) ;
@@ -201,56 +205,61 @@ export function CodeEditor({ problem }: CodeEditorProps) {
201
205
// Add this function to run a specific test case
202
206
const runTestCase = async ( testCase : TestCase , index : number ) => {
203
207
if ( isRunning ) return ;
204
-
208
+
205
209
const result = await runCode ( testCase . input ) ;
206
-
210
+
207
211
// Update the results for this specific test case
208
- setTestCaseResults ( prev => ( {
212
+ setTestCaseResults ( ( prev ) => ( {
209
213
...prev ,
210
214
[ index ] : {
211
215
output : result . output ,
212
- success : result . output . trim ( ) === testCase . output . trim ( )
213
- }
216
+ success : result . output . trim ( ) === testCase . output . trim ( ) ,
217
+ } ,
214
218
} ) ) ;
215
-
219
+
216
220
// Update the overall output summary
217
221
const updatedResults = {
218
222
...testCaseResults ,
219
223
[ index ] : {
220
224
output : result . output ,
221
- success : result . output . trim ( ) === testCase . output . trim ( )
222
- }
225
+ success : result . output . trim ( ) === testCase . output . trim ( ) ,
226
+ } ,
223
227
} ;
224
-
228
+
225
229
const totalCases = problem . testCases ?. length || 0 ;
226
- const passedCases = Object . values ( updatedResults ) . filter ( r => r . success ) . length ;
227
-
230
+ const passedCases = Object . values ( updatedResults ) . filter (
231
+ ( r ) => r . success ,
232
+ ) . length ;
233
+
228
234
setOutput ( `Passed ${ passedCases } of ${ totalCases } test cases` ) ;
229
235
} ;
230
236
231
237
// Add a function to run all test cases
232
238
const runAllTestCases = async ( ) => {
233
- if ( isRunning || ! problem . testCases || problem . testCases . length === 0 ) return ;
234
-
239
+ if ( isRunning || ! problem . testCases || problem . testCases . length === 0 )
240
+ return ;
241
+
235
242
setIsRunning ( true ) ;
236
243
const newResults : Record < number , TestCaseResult > = { } ;
237
244
let passedCount = 0 ;
238
-
245
+
239
246
for ( let i = 0 ; i < problem . testCases . length ; i ++ ) {
240
247
const testCase = problem . testCases [ i ] ;
241
248
const result = await runCode ( testCase . input ) ;
242
249
const isSuccess = result . output . trim ( ) === testCase . output . trim ( ) ;
243
-
250
+
244
251
newResults [ i ] = {
245
252
output : result . output ,
246
- success : isSuccess
253
+ success : isSuccess ,
247
254
} ;
248
-
255
+
249
256
if ( isSuccess ) passedCount ++ ;
250
257
}
251
-
258
+
252
259
setTestCaseResults ( newResults ) ;
253
- setOutput ( `Passed ${ passedCount } of ${ problem . testCases . length } test cases` ) ;
260
+ setOutput (
261
+ `Passed ${ passedCount } of ${ problem . testCases . length } test cases` ,
262
+ ) ;
254
263
setIsRunning ( false ) ;
255
264
} ;
256
265
@@ -380,13 +389,15 @@ export function CodeEditor({ problem }: CodeEditorProps) {
380
389
< div key = { index } className = "space-y-2" >
381
390
< div className = "flex items-center justify-between gap-2 px-2 py-1" >
382
391
< div className = "flex items-center gap-2" >
383
- < div className = { `h-2 w-2 rounded-full ${ testCaseResults [ index ] ?. success ? 'bg-green-500' : testCaseResults [ index ] ? 'bg-red-500' : 'bg-muted' } ` } > </ div >
392
+ < div
393
+ className = { `h-2 w-2 rounded-full ${ testCaseResults [ index ] ?. success ? "bg-green-500" : testCaseResults [ index ] ? "bg-red-500" : "bg-muted" } ` }
394
+ > </ div >
384
395
< span className = "text-sm text-foreground" >
385
396
Case { index + 1 }
386
397
</ span >
387
398
</ div >
388
- < Button
389
- variant = "outline"
399
+ < Button
400
+ variant = "outline"
390
401
size = "sm"
391
402
onClick = { ( ) => runTestCase ( testCase , index ) }
392
403
disabled = { isRunning }
@@ -397,22 +408,34 @@ export function CodeEditor({ problem }: CodeEditorProps) {
397
408
</ div >
398
409
< div className = "space-y-1" >
399
410
< div className = "bg-muted rounded p-2" >
400
- < div className = "text-xs text-muted-foreground mb-1" > Input:</ div >
411
+ < div className = "text-xs text-muted-foreground mb-1" >
412
+ Input:
413
+ </ div >
401
414
< div className = "text-sm font-mono" >
402
- < span className = "text-foreground" > { testCase . input } </ span >
415
+ < span className = "text-foreground" >
416
+ { testCase . input }
417
+ </ span >
403
418
</ div >
404
419
</ div >
405
420
< div className = "bg-muted rounded p-2" >
406
- < div className = "text-xs text-muted-foreground mb-1" > Expected Output:</ div >
421
+ < div className = "text-xs text-muted-foreground mb-1" >
422
+ Expected Output:
423
+ </ div >
407
424
< div className = "text-sm font-mono" >
408
- < span className = "text-foreground" > { testCase . output } </ span >
425
+ < span className = "text-foreground" >
426
+ { testCase . output }
427
+ </ span >
409
428
</ div >
410
429
</ div >
411
430
{ testCaseResults [ index ] && (
412
431
< div className = "bg-muted rounded p-2" >
413
- < div className = "text-xs text-muted-foreground mb-1" > Output:</ div >
432
+ < div className = "text-xs text-muted-foreground mb-1" >
433
+ Output:
434
+ </ div >
414
435
< div className = "text-sm font-mono" >
415
- < span className = { `${ testCaseResults [ index ] ?. success ? 'text-green-500' : 'text-red-500' } ` } >
436
+ < span
437
+ className = { `${ testCaseResults [ index ] ?. success ? "text-green-500" : "text-red-500" } ` }
438
+ >
416
439
{ testCaseResults [ index ] ?. output }
417
440
</ span >
418
441
</ div >
0 commit comments