Give testing a shot
This commit is contained in:
parent
ba21821df7
commit
9ec4d2741c
|
@ -229,6 +229,13 @@ export class Session {
|
|||
}
|
||||
await this.runCode(msg.code);
|
||||
break;
|
||||
case "testCode":
|
||||
if (typeof msg.code !== "string") {
|
||||
this.logBadMessage(msg);
|
||||
break;
|
||||
}
|
||||
await this.runCode(msg.code, true, msg.expectedOutput);
|
||||
break;
|
||||
case "formatCode":
|
||||
if (typeof msg.code !== "string") {
|
||||
this.logBadMessage(msg);
|
||||
|
@ -279,7 +286,7 @@ export class Session {
|
|||
await this.run(this.privilegedExec(`cat > ${file}`), { input: code });
|
||||
};
|
||||
|
||||
runCode = async (code) => {
|
||||
runCode = async (code, isTest = false, expectedOutput) => {
|
||||
try {
|
||||
const { name, repl, suffix, createEmpty, compile, run, template } =
|
||||
this.config;
|
||||
|
@ -322,7 +329,18 @@ export class Session {
|
|||
// Capture term in closure so that we don't keep sending output
|
||||
// from the old pty even after it's been killed (see ghci).
|
||||
if (term.live) {
|
||||
this.send({ event: "terminalOutput", output: data.toString() });
|
||||
if (isTest) {
|
||||
this.send({
|
||||
event: "testTerminalOutput",
|
||||
output: data.toString(),
|
||||
expectedOutput
|
||||
});
|
||||
} else {
|
||||
this.send({
|
||||
event: "terminalOutput",
|
||||
output: data.toString()
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
this.term.pty.stderr.on("data", (data) => {
|
||||
|
|
|
@ -113,6 +113,40 @@ async function main() {
|
|||
}
|
||||
term.write(message.output);
|
||||
return;
|
||||
case "testTerminalOutput":
|
||||
if (typeof message.output !== "string") {
|
||||
console.error("Unexpected message from server:", message);
|
||||
return;
|
||||
}
|
||||
term.write(message.output);
|
||||
|
||||
const pass = message.output == message.expectedOutput
|
||||
|
||||
window.parent.postMessage({
|
||||
event: "total_test_start",
|
||||
type: "test",
|
||||
});
|
||||
|
||||
window.parent.postMessage({
|
||||
$id: 0,
|
||||
codesandbox: true,
|
||||
event: "test_end",
|
||||
test: {
|
||||
blocks: ["Output"],
|
||||
duration: 1,
|
||||
errors: [],
|
||||
name: `should be ${message.expectedOutput}.`,
|
||||
path: "",
|
||||
status: pass ? "pass" : "fail",
|
||||
},
|
||||
type: "test",
|
||||
});
|
||||
|
||||
window.parent.postMessage({
|
||||
event: "total_test_end",
|
||||
type: "test",
|
||||
});
|
||||
return;
|
||||
case "lspStopped":
|
||||
if (clientDisposable) {
|
||||
clientDisposable.dispose();
|
||||
|
|
Loading…
Reference in New Issue