|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | 2 | import { Bash } from "../../Bash.js"; |
3 | 3 | import { parse } from "../query-engine/parser.js"; |
| 4 | +import type { ObjectNode } from "../query-engine/parser-types.js"; |
| 5 | + |
| 6 | +function expectShorthandEntry( |
| 7 | + entry: ObjectNode["entries"][number], |
| 8 | + keyName: string, |
| 9 | +) { |
| 10 | + expect(entry.key).toBe(keyName); |
| 11 | + expect(entry.value).toEqual({ type: "Field", name: keyName }); |
| 12 | +} |
4 | 13 |
|
5 | 14 | describe("jq string key shorthand in object construction", () => { |
6 | 15 | describe("parser: quoted string keys without colon", () => { |
7 | | - it('should parse {"name"} as shorthand', () => { |
8 | | - const ast = parse('{"name"}'); |
| 16 | + it('should parse {"name"} as shorthand with correct AST', () => { |
| 17 | + const ast = parse('{"name"}') as ObjectNode; |
9 | 18 | expect(ast.type).toBe("Object"); |
| 19 | + expect(ast.entries).toHaveLength(1); |
| 20 | + expectShorthandEntry(ast.entries[0], "name"); |
10 | 21 | }); |
11 | 22 |
|
12 | | - it('should parse {"name", "label"} as shorthand', () => { |
13 | | - const ast = parse('{"name", "label"}'); |
| 23 | + it('should parse {"name", "label"} as shorthand with correct AST', () => { |
| 24 | + const ast = parse('{"name", "label"}') as ObjectNode; |
14 | 25 | expect(ast.type).toBe("Object"); |
| 26 | + expect(ast.entries).toHaveLength(2); |
| 27 | + expectShorthandEntry(ast.entries[0], "name"); |
| 28 | + expectShorthandEntry(ast.entries[1], "label"); |
15 | 29 | }); |
16 | 30 |
|
17 | 31 | it('should parse {"if"} as shorthand (keyword as string key)', () => { |
18 | | - const ast = parse('{"if"}'); |
| 32 | + const ast = parse('{"if"}') as ObjectNode; |
19 | 33 | expect(ast.type).toBe("Object"); |
| 34 | + expect(ast.entries).toHaveLength(1); |
| 35 | + expectShorthandEntry(ast.entries[0], "if"); |
20 | 36 | }); |
21 | 37 |
|
22 | 38 | it('should parse {"as"} as shorthand (keyword as string key)', () => { |
23 | | - const ast = parse('{"as"}'); |
| 39 | + const ast = parse('{"as"}') as ObjectNode; |
24 | 40 | expect(ast.type).toBe("Object"); |
| 41 | + expect(ast.entries).toHaveLength(1); |
| 42 | + expectShorthandEntry(ast.entries[0], "as"); |
25 | 43 | }); |
26 | 44 |
|
27 | 45 | it('should parse {"try"} as shorthand (keyword as string key)', () => { |
28 | | - const ast = parse('{"try"}'); |
| 46 | + const ast = parse('{"try"}') as ObjectNode; |
29 | 47 | expect(ast.type).toBe("Object"); |
| 48 | + expect(ast.entries).toHaveLength(1); |
| 49 | + expectShorthandEntry(ast.entries[0], "try"); |
30 | 50 | }); |
31 | 51 |
|
32 | 52 | it('should parse {"true"} as shorthand (keyword as string key)', () => { |
33 | | - const ast = parse('{"true"}'); |
| 53 | + const ast = parse('{"true"}') as ObjectNode; |
34 | 54 | expect(ast.type).toBe("Object"); |
| 55 | + expect(ast.entries).toHaveLength(1); |
| 56 | + expectShorthandEntry(ast.entries[0], "true"); |
35 | 57 | }); |
36 | 58 |
|
37 | 59 | it('should parse {"null"} as shorthand (keyword as string key)', () => { |
38 | | - const ast = parse('{"null"}'); |
| 60 | + const ast = parse('{"null"}') as ObjectNode; |
39 | 61 | expect(ast.type).toBe("Object"); |
| 62 | + expect(ast.entries).toHaveLength(1); |
| 63 | + expectShorthandEntry(ast.entries[0], "null"); |
40 | 64 | }); |
41 | 65 |
|
42 | 66 | it('should parse mixed: {"name", "label": .x}', () => { |
43 | | - const ast = parse('{"name", "label": .x}'); |
| 67 | + const ast = parse('{"name", "label": .x}') as ObjectNode; |
| 68 | + expect(ast.type).toBe("Object"); |
| 69 | + expect(ast.entries).toHaveLength(2); |
| 70 | + expectShorthandEntry(ast.entries[0], "name"); |
| 71 | + expect(ast.entries[1].key).toBe("label"); |
| 72 | + expect(ast.entries[1].value).toEqual({ type: "Field", name: "x" }); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should parse non-identifier key {"a-b"} as shorthand', () => { |
| 76 | + const ast = parse('{"a-b"}') as ObjectNode; |
| 77 | + expect(ast.type).toBe("Object"); |
| 78 | + expect(ast.entries).toHaveLength(1); |
| 79 | + expectShorthandEntry(ast.entries[0], "a-b"); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should parse numeric string key {"1"} as shorthand', () => { |
| 83 | + const ast = parse('{"1"}') as ObjectNode; |
| 84 | + expect(ast.type).toBe("Object"); |
| 85 | + expect(ast.entries).toHaveLength(1); |
| 86 | + expectShorthandEntry(ast.entries[0], "1"); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should parse empty string key {""} as shorthand', () => { |
| 90 | + const ast = parse('{""}') as ObjectNode; |
| 91 | + expect(ast.type).toBe("Object"); |
| 92 | + expect(ast.entries).toHaveLength(1); |
| 93 | + expectShorthandEntry(ast.entries[0], ""); |
| 94 | + }); |
| 95 | + |
| 96 | + it("should still parse explicit key-value with string key", () => { |
| 97 | + const ast = parse('{"name": .x}') as ObjectNode; |
44 | 98 | expect(ast.type).toBe("Object"); |
| 99 | + expect(ast.entries).toHaveLength(1); |
| 100 | + expect(ast.entries[0].key).toBe("name"); |
| 101 | + expect(ast.entries[0].value).toEqual({ type: "Field", name: "x" }); |
45 | 102 | }); |
46 | 103 | }); |
47 | 104 |
|
@@ -104,5 +161,32 @@ describe("jq string key shorthand in object construction", () => { |
104 | 161 | expect(result.exitCode).toBe(0); |
105 | 162 | expect(result.stdout).toBe('{"name":"foo","label":"bar"}\n'); |
106 | 163 | }); |
| 164 | + |
| 165 | + it('should evaluate non-identifier key {"a-b"} shorthand', async () => { |
| 166 | + const env = new Bash(); |
| 167 | + const result = await env.exec( |
| 168 | + `echo '{"a-b":"val","extra":"x"}' | jq -c '{"a-b"}'`, |
| 169 | + ); |
| 170 | + expect(result.exitCode).toBe(0); |
| 171 | + expect(result.stdout).toBe('{"a-b":"val"}\n'); |
| 172 | + }); |
| 173 | + |
| 174 | + it('should evaluate numeric string key {"1"} shorthand', async () => { |
| 175 | + const env = new Bash(); |
| 176 | + const result = await env.exec( |
| 177 | + `echo '{"1":"val","extra":"x"}' | jq -c '{"1"}'`, |
| 178 | + ); |
| 179 | + expect(result.exitCode).toBe(0); |
| 180 | + expect(result.stdout).toBe('{"1":"val"}\n'); |
| 181 | + }); |
| 182 | + |
| 183 | + it('should evaluate empty string key {""} shorthand', async () => { |
| 184 | + const env = new Bash(); |
| 185 | + const result = await env.exec( |
| 186 | + `echo '{"":"val","extra":"x"}' | jq -c '{""}'`, |
| 187 | + ); |
| 188 | + expect(result.exitCode).toBe(0); |
| 189 | + expect(result.stdout).toBe('{"":"val"}\n'); |
| 190 | + }); |
107 | 191 | }); |
108 | 192 | }); |
0 commit comments