Skip to content

Commit

Permalink
❇: Added basic quiz infrastructure (no production impact)
Browse files Browse the repository at this point in the history
  • Loading branch information
m7medVision committed Jan 11, 2024
1 parent 292fe2f commit 854a4fa
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 16 deletions.
14 changes: 13 additions & 1 deletion courses/testcases.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
{
"intro": [
{
"regex": ".*"
"output": {
"regex": ".*",
"output": "Hi"
},
"code": {
"regex": ".*",
"code": [
"console.log('Hi');",
"console.log('Hi')",
"console.log(\"Hi\");",
"console.log(\"Hi\")"
]
}
}
]
}
2 changes: 2 additions & 0 deletions fresh.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as $_app from "./routes/_app.tsx";
import * as $api_test from "./routes/api/test.ts";
import * as $group_slug_ from "./routes/group/[slug].tsx";
import * as $index from "./routes/index.tsx";
import * as $DoTest from "./islands/DoTest.ts";
import * as $Editor from "./islands/Editor.tsx";
import * as $ThemeToggle from "./islands/ThemeToggle.tsx";
import * as $Toast from "./islands/Toast.tsx";
Expand All @@ -25,6 +26,7 @@ const manifest = {
"./routes/index.tsx": $index,
},
islands: {
"./islands/DoTest.ts": $DoTest,
"./islands/Editor.tsx": $Editor,
"./islands/ThemeToggle.tsx": $ThemeToggle,
"./islands/Toast.tsx": $Toast,
Expand Down
67 changes: 67 additions & 0 deletions islands/DoTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

function testRegex(regex: string, text: string): boolean {
const re = new RegExp(regex);
return re.test(text);
}
function checkText(text1: string, text2: string): boolean {
return text1 === text2;
}
/*
{
"output": {
"regex": ".*",
"output": "Hi"
},
"code": {
"regex": ".*",
"code": ["console.log('Hi');"]
}
}
*/
function doTest(testCase: any, code: string, output: string): boolean {
const { code: codeTest, output: outputTest } = testCase;
const codeRegex = codeTest.regex;
const outputRegex = outputTest.regex;
const codesCheck = codeTest.code;
const outputCheck = outputTest.output;
if (!testRegex(codeRegex, code)) {
console.log(codeRegex, code);
console.log("Code is not correct 1");
return false;
}
if (!testRegex(outputRegex, output)) {
console.log(outputRegex, output);
console.log("Output is not correct 1");
return false;
}

let isCodeCorrect = false;
for (const codeCheck of codesCheck) {
if (checkText(codeCheck, code)) {
isCodeCorrect = true;
break;
}
}

if (!isCodeCorrect) {
console.log(codesCheck, code);
console.log("Code is not correct 2");
return false;
}
if (!checkText(outputCheck, output)) {
console.log(outputCheck, output);
console.log("Output is not correct 2");
return false;
}
return true;
}

export function doTests(testCases: any[], code: string, output: string): boolean {
for (const testCase of testCases) {
if (!doTest(testCase, code, output)) {
// console.log(testCase);
return false;
}
}
return true;
}
20 changes: 16 additions & 4 deletions islands/Editor.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { useEffect, useState } from "preact/hooks";
import { useToast } from "./useToast.ts";
import { doTests } from "./DoTest.ts";

interface CounterProps {
preCode: string;
testcases?:
| { regex?: string | undefined; output?: string | undefined }[]
| undefined;
testcases: any[];
}

declare var window: Window & typeof globalThis;
Expand Down Expand Up @@ -33,6 +32,18 @@ export default function Editor(props: CounterProps) {
setOutput("");
}
function handleCodeTest() {
const code: string = window.editor.getValue() || "";
const runOutput = handleCodeRun();
const testcases = props.testcases;
// TODO: work on this later
// const pass = doTests(testcases, code, runOutput);
// if (pass) {
// showToast({
// msg: "تم تجاوز الاختبارات بنجاح",
// type: "success",
// });
// return;
// }
showToast({
msg: "هذه الخاصية غير متوفرة حالياً",
type: "warning",
Expand All @@ -59,10 +70,11 @@ export default function Editor(props: CounterProps) {
eval(code);
}
setOutput(capturedOutput.join("\n"));

console.log = originalConsoleLog;
return capturedOutput.join("\n");
} catch (error) {
setOutput(`${error}`);
return `${error}`;
}
}

Expand Down
19 changes: 8 additions & 11 deletions utils/testcase.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
export function getTestCase(slug: string): { regex?: string | undefined; output?: string | undefined; }[] | undefined {

return [
{
regex: '.*',

},
{
output: '200',
}
]
export function getTestCase(slug: string): any[] {
const testCases = JSON.parse(Deno.readTextFileSync(`./courses/testcases.json`));
try {
return testCases[slug];
}
catch {
return [];
}
}

0 comments on commit 854a4fa

Please sign in to comment.