Skip to content

Commit

Permalink
testcase : objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyodu authored Sep 10, 2024
1 parent 2881eff commit a43fa56
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 0 deletions.
35 changes: 35 additions & 0 deletions testcases/objects/accessing-nested-objects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function handleCodeRun(code) {
try {
const capturedOutput = [];
const originalConsoleLog = console.log;
console.log = (...args) => {
capturedOutput.push(
args.map((arg) => {
if (typeof arg === "object" && arg !== null) {
return JSON.stringify(arg);
}
return arg.toString();
}).join(" "),
);
originalConsoleLog(...args);
};
if (code) {
eval(code);
}
console.log = originalConsoleLog;
return capturedOutput.join("\n");
} catch (error) {
return `${error}`;
}
}

const output = handleCodeRun(code)
if (output == "1500") {
if (code.includes("console.log(1500)" || 'console.log("1500")' || "console.log('1500')")) {
msg = "لا تحتال علينا, حاول مرة أخرى";
} else {
isPass = true;
}
} else {
msg = "حاول مرة أخرى";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// the code must contain console.log(fighter[Country])
const codeRegex = /console.log\(fighter\[Country\]\)/;

if (codeRegex.test(code)) {
isPass = true;
} else {
msg = "تاكد من الشروط";
}
32 changes: 32 additions & 0 deletions testcases/objects/accessing-object-properties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function handleCodeRun(code) {
try {
const capturedOutput = [];
const originalConsoleLog = console.log;
console.log = (...args) => {
capturedOutput.push(
args.map((arg) => {
if (typeof arg === "object" && arg !== null) {
return JSON.stringify(arg);
}
return arg.toString();
}).join(" "),
);
originalConsoleLog(...args);
};
if (code) {
eval(code);
}
console.log = originalConsoleLog;
return capturedOutput.join("\n");
} catch (error) {
return `${error}`;
}
}

const output = handleCodeRun(code);
// TODO: make the quiz uncheatable by checking the the code logic
if (output == "19812") {
isPass = true;
} else {
msg = "الناتج خطاء"
}
5 changes: 5 additions & 0 deletions testcases/objects/delete-properties-from-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
if (code.includes("delete") && code.includes("operator") && code.includes("fighter")) {
isPass = true;
} else {
msg = "الله يبارك فيك ركز شوية وحاول مره ثانية"
}
37 changes: 37 additions & 0 deletions testcases/objects/manipulating-complex-objects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function handleCodeRun(code) {
try {
const capturedOutput = [];
const originalConsoleLog = console.log;
console.log = (...args) => {
capturedOutput.push(
args.map((arg) => {
if (typeof arg === "object" && arg !== null) {
return JSON.stringify(arg);
}
return arg.toString();
}).join(" "),
);
originalConsoleLog(...args);
};
if (code) {
eval(code);
}
console.log = originalConsoleLog;
return capturedOutput.join("\n");
} catch (error) {
return `${error}`;
}
}

if (!code.includes('october-73')) {
msg = "هل قمت بتعديل القيمة الصحيحة؟";
} else {
const output = handleCodeRun(code + "\nconsole.log(fighter.wars[0]);")
console.log(output);
if (output.includes("october-73")) {
isPass = true;
} else {
msg = "هل قمت بتعديل القيمة الصحيحة؟";
}

}
7 changes: 7 additions & 0 deletions testcases/objects/testing-objects-for-properties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const regex = /console.log\((\w+)\.hasOwnProperty\("(speed)"\)\)/;

if (regex.test(code)) {
isPass = true;
} else {
msg = "قم بالتأكد من استخدام الدالة hasOwnProperty() للتأكد من وجود الخاصية في الكائن المعطى."
}
33 changes: 33 additions & 0 deletions testcases/objects/updating-object-properties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function handleCodeRun(code) {
try {
const capturedOutput = [];
const originalConsoleLog = console.log;
console.log = (...args) => {
capturedOutput.push(
args.map((arg) => {
if (typeof arg === "object" && arg !== null) {
return JSON.stringify(arg);
}
return arg.toString();
}).join(" "),
);
originalConsoleLog(...args);
};
if (code) {
eval(code);
}
console.log = originalConsoleLog;
return capturedOutput.join("\n");
} catch (error) {
return `${error}`;
}
}

code += "\nconsole.log(fighter.speed+fighter.speedUnit)"
const output = handleCodeRun(code);
const expectedOutput = "2450";
if (output.includes(expectedOutput)) {
isPass = true;
} else {
msg = "الله يبارك فيك ركز شوية وحاول مره ثانية"
}

0 comments on commit a43fa56

Please sign in to comment.