From 355a8a4707fba7c6ff7aeba6d81d6c6f2a33606b Mon Sep 17 00:00:00 2001 From: orion Date: Fri, 24 Jul 2026 04:45:19 +0200 Subject: [PATCH] parser test suite done, all green --- test/core/util/parser.test.ts | 70 +++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 test/core/util/parser.test.ts diff --git a/test/core/util/parser.test.ts b/test/core/util/parser.test.ts new file mode 100644 index 0000000..13cafbf --- /dev/null +++ b/test/core/util/parser.test.ts @@ -0,0 +1,70 @@ +import { parseCommand } from "@core/util/parser"; +import { describe, expect, test } from "vitest"; + +describe("parseCommand", () => { + test("returns an empty ParsedCommand object", () => { + expect(parseCommand("")).toEqual({ command: "", flags: [], args: [] }); + }); + test("returns an empty ParsedCommand object", () => { + expect(parseCommand("")).toEqual({ command: "", flags: [], args: [] }); + }); + test("returns an empty ParsedCommand object", () => { + expect(parseCommand("")).toEqual({ command: "", flags: [], args: [] }); + }); + + test("returns a parsed ls command without any args", () => { + expect(parseCommand("ls")).toEqual({ command: "ls", flags: [], args: [] }); + }); + test("returns a parsed ls command with arg /tmp", () => { + expect(parseCommand("ls /tmp")).toEqual({ command: "ls", flags: [], args: ["/tmp"] }); + }); + + test("returns a parsed ls command with flags l and no args", () => { + expect(parseCommand("ls -l")).toEqual({ command: "ls", flags: ["l"], args: [] }); + }); + test("returns a parsed command with flags l,a and no args", () => { + expect(parseCommand("ls -la")).toEqual({ command: "ls", flags: ["l", "a"], args: [] }); + }); + test("returns a parsed command with flags l,a and no args", () => { + expect(parseCommand("ls -l -a")).toEqual({ command: "ls", flags: ["l", "a"], args: [] }); + }); + test("returns a parsed command with flags l,a and a /tmp arg", () => { + expect(parseCommand("ls -la /tmp")).toEqual({ command: "ls", flags: ["l", "a"], args: ["/tmp"] }); + }); + + test("returns a parsed grep command with a valued color flag", () => { + expect(parseCommand("grep --color=auto")).toEqual({ command: "grep", flags: ["color=auto"], args: [] }); + }); + test("returns a parsed grep command with a valued color and r flags", () => { + expect(parseCommand("grep --color=auto -r")).toEqual({ + command: "grep", + flags: ["color=auto", "r"], + args: [], + }); + }); + + test("returns a parsed echo command with a --version arg", () => { + expect(parseCommand("echo -- --version")).toEqual({ command: "echo", flags: [], args: ["--version"] }); + }); + test("returns a parsed echo command with a -f arg", () => { + expect(parseCommand("echo -- -f")).toEqual({ command: "echo", flags: [], args: ["-f"] }); + }); + test("returns a parsed echo command without any args", () => { + expect(parseCommand("echo --")).toEqual({ command: "echo", flags: [], args: [] }); + }); + + test("returns a parsed ls command with flags l,a and a --tmp arg", () => { + expect(parseCommand("ls -la -- --tmp")).toEqual({ command: "ls", flags: ["l", "a"], args: ["--tmp"] }); + }); + test("returns a parsed rm command with flags r,f,no-preserve-root and a / arg", () => { + expect(parseCommand("rm -rf --no-preserve-root /")).toEqual({ + command: "rm", + flags: ["r", "f", "no-preserve-root"], + args: ["/"], + }); + }); + + test("throws an UnclosedQuote error", () => { + expect(() => parseCommand('echo "hello')).toThrow(expect.objectContaining({ type: "UnclosedQuote" })); + }); +});