From 9ec4d2741ceb9c66b2c8a022f2b642d46fff518c Mon Sep 17 00:00:00 2001 From: plondon Date: Mon, 4 Oct 2021 08:45:55 -0400 Subject: [PATCH] Give testing a shot --- backend/api.js | 22 ++++++++++++++++++++-- frontend/src/app.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/backend/api.js b/backend/api.js index 9b63574..f8933b4 100644 --- a/backend/api.js +++ b/backend/api.js @@ -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) => { diff --git a/frontend/src/app.js b/frontend/src/app.js index 1aa5a23..1012417 100644 --- a/frontend/src/app.js +++ b/frontend/src/app.js @@ -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();