|
| 1 | +import { describe, it } from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { replLikeEval, checkSyntax } from "./index.ts"; |
| 4 | + |
| 5 | +// --------------------------------------------------------------------------- |
| 6 | +// replLikeEval |
| 7 | +// --------------------------------------------------------------------------- |
| 8 | + |
| 9 | +describe("replLikeEval", () => { |
| 10 | + describe("var declaration", () => { |
| 11 | + it("evaluates var and returns undefined", async () => { |
| 12 | + const result = await replLikeEval("var x = 42"); |
| 13 | + assert.strictEqual(result, undefined); |
| 14 | + }); |
| 15 | + }); |
| 16 | + |
| 17 | + describe("const declaration", () => { |
| 18 | + it("converts const to var and returns undefined", async () => { |
| 19 | + const result = await replLikeEval("const constVar = 1"); |
| 20 | + assert.strictEqual(result, undefined); |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + describe("let declaration", () => { |
| 25 | + it("converts let to var and returns undefined", async () => { |
| 26 | + const result = await replLikeEval("let letVar = 2"); |
| 27 | + assert.strictEqual(result, undefined); |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + describe("undeclared variable assignment", () => { |
| 32 | + it("assigns to global and returns the assigned value", async () => { |
| 33 | + const result = await replLikeEval("undeclaredVar = 99"); |
| 34 | + assert.strictEqual(result, 99); |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + describe("expression evaluation", () => { |
| 39 | + it("returns numeric result", async () => { |
| 40 | + assert.strictEqual(await replLikeEval("1 + 2"), 3); |
| 41 | + }); |
| 42 | + |
| 43 | + it("returns string result", async () => { |
| 44 | + assert.strictEqual(await replLikeEval('"hello"'), "hello"); |
| 45 | + }); |
| 46 | + |
| 47 | + it("returns boolean result", async () => { |
| 48 | + assert.strictEqual(await replLikeEval("true"), true); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe("function declaration", () => { |
| 53 | + it("declares a function and returns undefined", async () => { |
| 54 | + const result = await replLikeEval("function greet() { return 'hi'; }"); |
| 55 | + assert.strictEqual(result, undefined); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe("class declaration", () => { |
| 60 | + it("converts class to var-assigned class expression and returns undefined", async () => { |
| 61 | + const result = await replLikeEval("class MyClass { constructor() {} }"); |
| 62 | + assert.strictEqual(result, undefined); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe("array literal", () => { |
| 67 | + it("returns an array", async () => { |
| 68 | + const result = await replLikeEval("[1, 2, 3]"); |
| 69 | + assert.deepStrictEqual(result, [1, 2, 3]); |
| 70 | + }); |
| 71 | + |
| 72 | + it("returns an empty array", async () => { |
| 73 | + assert.deepStrictEqual(await replLikeEval("[]"), []); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe("object literal", () => { |
| 78 | + it("returns an object for { a: 1 }", async () => { |
| 79 | + const result = await replLikeEval("{ a: 1 }"); |
| 80 | + assert.deepStrictEqual(result, { a: 1 }); |
| 81 | + }); |
| 82 | + |
| 83 | + it("returns an empty object for {}", async () => { |
| 84 | + assert.deepStrictEqual(await replLikeEval("{}"), {}); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe("block that looks like an object", () => { |
| 89 | + it("executes a labelled statement block and returns undefined", async () => { |
| 90 | + // { x: 1 } is ambiguous – first tried as object; if that fails as |
| 91 | + // SyntaxError it falls back to block execution. A true label statement |
| 92 | + // `{ label: expr; }` is a block and eval returns the last expression. |
| 93 | + // { a: 1 } is valid as both, so replLikeEval returns the object. |
| 94 | + const result = await replLikeEval("{ x: 1 }"); |
| 95 | + // Treated as object literal when it is valid JSON-like |
| 96 | + assert.deepStrictEqual(result, { x: 1 }); |
| 97 | + }); |
| 98 | + |
| 99 | + it("executes pure block statement when object parse fails", async () => { |
| 100 | + // A block containing a statement that is invalid as an object expression |
| 101 | + // forces the fallback path. |
| 102 | + const result = await replLikeEval("{ let tmp = 5; tmp; }"); |
| 103 | + assert.strictEqual(result, 5); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe("await expression", () => { |
| 108 | + it("awaits a resolved promise", async () => { |
| 109 | + const result = await replLikeEval("await Promise.resolve(7)"); |
| 110 | + assert.strictEqual(result, 7); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + describe("error propagation", () => { |
| 115 | + it("throws ReferenceError for undefined identifier", async () => { |
| 116 | + await assert.rejects( |
| 117 | + () => replLikeEval("notDefinedAtAllXyz"), |
| 118 | + ReferenceError |
| 119 | + ); |
| 120 | + }); |
| 121 | + }); |
| 122 | +}); |
| 123 | + |
| 124 | +// --------------------------------------------------------------------------- |
| 125 | +// checkSyntax |
| 126 | +// --------------------------------------------------------------------------- |
| 127 | + |
| 128 | +describe("checkSyntax", () => { |
| 129 | + describe("complete inputs", () => { |
| 130 | + it("simple expression is complete", async () => { |
| 131 | + assert.deepStrictEqual(await checkSyntax("1 + 2"), { |
| 132 | + status: "complete", |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + it("function declaration is complete", async () => { |
| 137 | + assert.deepStrictEqual( |
| 138 | + await checkSyntax("function f() { return 1; }"), |
| 139 | + { status: "complete" } |
| 140 | + ); |
| 141 | + }); |
| 142 | + |
| 143 | + it("if-else block is complete", async () => { |
| 144 | + assert.deepStrictEqual( |
| 145 | + await checkSyntax("if (true) { 1; } else { 2; }"), |
| 146 | + { status: "complete" } |
| 147 | + ); |
| 148 | + }); |
| 149 | + |
| 150 | + it("for loop is complete", async () => { |
| 151 | + assert.deepStrictEqual( |
| 152 | + await checkSyntax("for (let i = 0; i < 3; i++) {}"), |
| 153 | + { status: "complete" } |
| 154 | + ); |
| 155 | + }); |
| 156 | + |
| 157 | + it("empty string is complete", async () => { |
| 158 | + assert.deepStrictEqual(await checkSyntax(""), { status: "complete" }); |
| 159 | + }); |
| 160 | + }); |
| 161 | + |
| 162 | + describe("incomplete inputs", () => { |
| 163 | + it("if(1){ is incomplete", async () => { |
| 164 | + assert.deepStrictEqual(await checkSyntax("if(1){"), { |
| 165 | + status: "incomplete", |
| 166 | + }); |
| 167 | + }); |
| 168 | + |
| 169 | + it("function f() { is incomplete", async () => { |
| 170 | + assert.deepStrictEqual(await checkSyntax("function f() {"), { |
| 171 | + status: "incomplete", |
| 172 | + }); |
| 173 | + }); |
| 174 | + |
| 175 | + it("open array bracket is incomplete", async () => { |
| 176 | + assert.deepStrictEqual(await checkSyntax("[1, 2,"), { |
| 177 | + status: "incomplete", |
| 178 | + }); |
| 179 | + }); |
| 180 | + |
| 181 | + it("open object brace is incomplete", async () => { |
| 182 | + assert.deepStrictEqual(await checkSyntax("({ a:"), { |
| 183 | + status: "incomplete", |
| 184 | + }); |
| 185 | + }); |
| 186 | + }); |
| 187 | + |
| 188 | + describe("invalid inputs", () => { |
| 189 | + it("extra closing brace after complete block returns incomplete (extra } matches wrapper)", async () => { |
| 190 | + // The implementation wraps code in `() => {<code>}`. |
| 191 | + // An extra } is interpreted as closing the wrapper early, so the |
| 192 | + // engine reports "Unexpected token '}'" – which the heuristic maps to |
| 193 | + // "incomplete". This is a known limitation of the wrapper approach. |
| 194 | + assert.deepStrictEqual(await checkSyntax("if(1){}}"), { |
| 195 | + status: "incomplete", |
| 196 | + }); |
| 197 | + }); |
| 198 | + |
| 199 | + it("syntax error expression is invalid", async () => { |
| 200 | + assert.deepStrictEqual(await checkSyntax("1 +* 2"), { |
| 201 | + status: "invalid", |
| 202 | + }); |
| 203 | + }); |
| 204 | + }); |
| 205 | +}); |
0 commit comments