From f095dcd08fb280795c48196e9034e07bc5aa3a3c Mon Sep 17 00:00:00 2001 From: Aliou Diallo Date: Tue, 3 Feb 2026 22:49:39 +0100 Subject: [PATCH] test: add while and until cases --- src/parser.test.ts | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/parser.test.ts b/src/parser.test.ts index 5fbbbdc..48f4c8e 100644 --- a/src/parser.test.ts +++ b/src/parser.test.ts @@ -24,6 +24,12 @@ type IfClause = { then: Statement[]; else?: Statement[]; }; +type WhileClause = { + type: "WhileClause"; + cond: Statement[]; + body: Statement[]; + until?: boolean; +}; type Pipeline = { type: "Pipeline"; commands: Statement[] }; type Logical = { type: "Logical"; @@ -37,7 +43,14 @@ type Statement = { background?: boolean; }; type Program = { type: "Program"; body: Statement[] }; -type Command = SimpleCommand | Subshell | Block | IfClause | Pipeline | Logical; +type Command = + | SimpleCommand + | Subshell + | Block + | IfClause + | WhileClause + | Pipeline + | Logical; const lit = (value: string): Literal => ({ type: "Literal", value }); const word = (value: string): Word => ({ type: "Word", parts: [lit(value)] }); @@ -77,6 +90,14 @@ const ifClause = ( elseBranch ? { type: "IfClause", cond, then, else: elseBranch } : { type: "IfClause", cond, then }; +const whileClause = ( + cond: Statement[], + body: Statement[], + until?: boolean, +): WhileClause => + until + ? { type: "WhileClause", cond, body, until } + : { type: "WhileClause", cond, body }; const stmt = (command: Command, background = false): Statement => background ? { type: "Statement", command, background } @@ -334,3 +355,19 @@ describe("parse (phase 5: if clauses)", () => { }); }); }); + +describe("parse (phase 6: while/until clauses)", () => { + it("parses while/do/done", () => { + expect(parse("while a; do b; done")).toEqual({ + ast: program(stmt(whileClause([stmt(simple("a"))], [stmt(simple("b"))]))), + }); + }); + + it("parses until/do/done", () => { + expect(parse("until a; do b; done")).toEqual({ + ast: program( + stmt(whileClause([stmt(simple("a"))], [stmt(simple("b"))], true)), + ), + }); + }); +}); -- 2.51.2