Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: REPL should not crash on runtime errors #47

Merged
merged 7 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as fs from "node:fs/promises";
import * as readline from "node:readline";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

import { main, maybeParseAsExpression, resetErrors } from "./main.js";
Expand All @@ -7,6 +8,9 @@ import { mockError, mockExit, mockLog } from "./test-utils.js";
vi.mock("node:fs/promises");
const mockFs = vi.mocked(fs);

vi.mock("node:readline");
const mockReadline = vi.mocked(readline);

describe("main", () => {
let stashedArgv = process.argv;
let exit: ReturnType<typeof mockExit>;
Expand Down Expand Up @@ -62,6 +66,64 @@ describe("main", () => {
);
});

describe("REPL", () => {
let closeFn: () => void;
let lineFn: (line: string) => void;
const prompt = vi.fn();
beforeEach(() => {
mockReadline.createInterface.mockReturnValue({
on: (event: string, fn: () => void) => {
if (event === "line") {
lineFn = fn;
}
return this;
},
once: (event: string, fn: () => void) => {
if (event === "close") {
closeFn = fn;
}
return this;
},
prompt,
} as unknown as readline.Interface);
});
afterEach(() => {
vi.resetAllMocks();
});

it("should run the REPL when invoked with no arguments", async () => {
const p = main();
lineFn("1 + 1");
closeFn();
await p;
expect(log).toHaveBeenCalledWith("2");
expect(error).not.toHaveBeenCalled();
});

it("should report an error without quitting", async () => {
const p = main();
lineFn("1 + nil");
closeFn();
await p;
expect(error).toHaveBeenCalledWith(
"Operands must be two numbers/currencies or two strings.\n[line 1]",
);
expect(log).not.toHaveBeenCalled();
expect(prompt).toHaveBeenCalledTimes(2);
});

it("should interpret a program", async () => {
const p = main();
lineFn("var x = $1,234;");
lineFn("print x + $2,345;");
closeFn();
await p;
expect(log).toHaveBeenCalledWith("$3,579");
expect(error).not.toHaveBeenCalled();
expect(prompt).toHaveBeenCalledTimes(3);
});
});

describe("maybeParseAsExpression", () => {
it("should parse an expressions", () => {
const error = mockError();
Expand Down
12 changes: 10 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@ export async function runPrompt(interpreter: Interpreter) {
rl.on("line", (line) => {
const expr = maybeParseAsExpression(line);
if (expr) {
console.log(stringify(interpreter.evaluate(expr)));
try {
console.log(stringify(interpreter.evaluate(expr)));
} catch (e) {
if (e instanceof RuntimeError) {
runtimeError(e);
} else {
throw e;
}
}
} else {
run(interpreter, line);
}
Expand Down Expand Up @@ -122,7 +130,7 @@ export async function main() {
if (args.length > 1) {
console.error("Usage:", process.argv[1], "[script]");
// eslint-disable-next-line n/no-process-exit
process.exit(64);
return process.exit(64);
}

const interpreter = new Interpreter();
Expand Down
Loading