Skip to content

Commit 798819c

Browse files
committed
Update code execution
1 parent 920dd80 commit 798819c

File tree

3 files changed

+63
-30
lines changed

3 files changed

+63
-30
lines changed

frontend/src/components/TestCase/index.tsx

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Box, styled, Typography } from "@mui/material";
2+
import { CompilerResult } from "../../contexts/CollabContext";
23

34
type TestCaseProps = {
45
input: string;
5-
output?: string;
6-
stdout: string;
7-
result?: string;
6+
expected: string;
7+
result: CompilerResult;
88
};
99

1010
const StyledBox = styled(Box)(({ theme }) => ({
@@ -18,34 +18,58 @@ const StyledTypography = styled(Typography)(({ theme }) => ({
1818
whiteSpace: "pre-line",
1919
}));
2020

21-
const TestCase: React.FC<TestCaseProps> = ({
22-
input,
23-
output,
24-
stdout,
25-
result,
26-
}) => {
21+
const TestCase: React.FC<TestCaseProps> = ({ input, expected, result }) => {
2722
return (
2823
<Box>
29-
<StyledBox sx={(theme) => ({ marginBottom: theme.spacing(2) })}>
24+
{"isMatch" in result && result.isMatch && (
25+
<StyledBox>
26+
<Typography variant="h4" color="success">
27+
Accepted
28+
</Typography>
29+
</StyledBox>
30+
)}
31+
{"isMatch" in result && !result.isMatch && (
32+
<StyledBox>
33+
<Typography variant="h4" color="error">
34+
Wrong Answer
35+
</Typography>
36+
</StyledBox>
37+
)}
38+
{result.stderr && (
39+
<StyledBox>
40+
<Typography variant="overline">Error</Typography>
41+
<StyledTypography
42+
fontFamily={"monospace"}
43+
sx={{ backgroundColor: "#EDAFAF" }}
44+
>
45+
{result.stderr}
46+
</StyledTypography>
47+
</StyledBox>
48+
)}
49+
<StyledBox>
3050
<Typography variant="overline">Input</Typography>
3151
<StyledTypography fontFamily={"monospace"}>{input}</StyledTypography>
3252
</StyledBox>
33-
{output && (
53+
<StyledBox>
54+
<Typography variant="overline">Expected</Typography>
55+
<StyledTypography fontFamily={"monospace"}>{expected}</StyledTypography>
56+
</StyledBox>
57+
{"actualResult" in result && (
3458
<StyledBox>
35-
<Typography variant="overline">Output</Typography>
36-
<StyledTypography fontFamily={"monospace"}>{output}</StyledTypography>
59+
<Typography variant="overline">Actual</Typography>
60+
<StyledTypography fontFamily={"monospace"}>
61+
{result.actualResult || "\u00A0"}
62+
</StyledTypography>
3763
</StyledBox>
3864
)}
39-
{stdout && (
65+
{"stdout" in result && (
4066
<StyledBox>
4167
<Typography variant="overline">Stdout</Typography>
42-
<StyledTypography fontFamily={"monospace"}>{stdout}</StyledTypography>
68+
<StyledTypography fontFamily={"monospace"}>
69+
{result.stdout || "\u00A0"}
70+
</StyledTypography>
4371
</StyledBox>
4472
)}
45-
<StyledBox>
46-
<Typography variant="overline">Expected</Typography>
47-
<StyledTypography fontFamily={"monospace"}>{result}</StyledTypography>
48-
</StyledBox>
4973
</Box>
5074
);
5175
};

frontend/src/contexts/CollabContext.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { CommunicationEvents } from "../components/Chat";
2020
import { communicationSocket } from "../utils/communicationSocket";
2121
import useAppNavigate from "../components/UseAppNavigate";
2222

23-
type CompilerResult = {
23+
export type CompilerResult = {
2424
status: string;
2525
exception: string | null;
2626
stdout: string;
@@ -30,6 +30,7 @@ type CompilerResult = {
3030
stout: string;
3131
actualResult: string;
3232
expectedResult: string;
33+
isMatch: boolean;
3334
};
3435

3536
type CollabContextType = {
@@ -40,6 +41,7 @@ type CollabContextType = {
4041
checkPartnerStatus: () => void;
4142
setCode: React.Dispatch<React.SetStateAction<string>>;
4243
compilerResult: CompilerResult[];
44+
setCompilerResult: React.Dispatch<React.SetStateAction<CompilerResult[]>>;
4345
isEndSessionModalOpen: boolean;
4446
};
4547

@@ -79,12 +81,12 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
7981
try {
8082
const res = await codeExecutionClient.post("/", {
8183
questionId,
82-
code,
84+
// Replace tabs with 4 spaces to prevent formatting issues
85+
code: code.replace(/\t/g, " ".repeat(4)),
8386
language: matchCriteria?.language.toLowerCase(),
8487
});
85-
86-
console.log(res.data.data);
87-
setCompilerResult(res.data.data);
88+
console.log([...res.data.data]);
89+
setCompilerResult([...res.data.data]);
8890

8991
let isMatch = true;
9092
for (let i = 0; i < res.data.data.length; i++) {
@@ -166,6 +168,7 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
166168
checkPartnerStatus,
167169
setCode,
168170
compilerResult,
171+
setCompilerResult,
169172
isEndSessionModalOpen,
170173
}}
171174
>

frontend/src/pages/CollabSandbox/index.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
Tabs,
1313
} from "@mui/material";
1414
import classes from "./index.module.css";
15-
import { useCollab } from "../../contexts/CollabContext";
15+
import { CompilerResult, useCollab } from "../../contexts/CollabContext";
1616
import { useMatch } from "../../contexts/MatchContext";
1717
import {
1818
COLLAB_CONNECTION_ERROR,
@@ -61,6 +61,8 @@ const CollabSandbox: React.FC = () => {
6161
}
6262

6363
const {
64+
compilerResult,
65+
setCompilerResult,
6466
handleRejectEndSession,
6567
handleConfirmEndSession,
6668
checkPartnerStatus,
@@ -79,6 +81,7 @@ const CollabSandbox: React.FC = () => {
7981
return;
8082
}
8183
getQuestionById(questionId, dispatch);
84+
setCompilerResult([]);
8285

8386
const matchId = getMatchId();
8487
if (!matchUser || !matchId) {
@@ -95,7 +98,7 @@ const CollabSandbox: React.FC = () => {
9598
toast.error(COLLAB_CONNECTION_ERROR);
9699
setIsConnecting(false);
97100
}
98-
} catch (error) {
101+
} catch {
99102
toast.error(COLLAB_CONNECTION_ERROR);
100103
setIsConnecting(false);
101104
}
@@ -122,7 +125,7 @@ const CollabSandbox: React.FC = () => {
122125
return <Navigate to="/home" replace />;
123126
}
124127

125-
if (!selectedQuestion || !editorState) {
128+
if (!selectedQuestion || !editorState || !compilerResult) {
126129
return <Loader />;
127130
}
128131

@@ -262,9 +265,12 @@ const CollabSandbox: React.FC = () => {
262265
{/* display result of each test case in the output (result) and stdout (any print statements executed) */}
263266
<TestCase
264267
input={selectedQuestion.inputs[selectedTestcase]}
265-
output={""}
266-
stdout={""}
267-
result={selectedQuestion.outputs[selectedTestcase]}
268+
expected={selectedQuestion.outputs[selectedTestcase]}
269+
result={
270+
compilerResult.length > 0
271+
? compilerResult[selectedTestcase]
272+
: ({} as CompilerResult)
273+
}
268274
/>
269275
</TabPanel>
270276
<TabPanel value={selectedTab} selected="chat">

0 commit comments

Comments
 (0)