|
| 1 | +/* eslint-disable vitest/expect-expect */ |
| 2 | + |
1 | 3 | import type { ParserOptions } from 'prettier' |
2 | 4 | import type { Node } from 'sh-syntax' |
3 | 5 |
|
4 | 6 | import { parsers } from 'prettier-plugin-sh' |
5 | 7 |
|
6 | | -describe('Parser location functions', () => { |
7 | | - const { locStart, locEnd } = parsers.sh |
| 8 | +const { locStart, locEnd } = parsers.sh |
8 | 9 |
|
9 | | - it('should return correct offsets for simple shell scripts', async () => { |
10 | | - const script = '#!/bin/sh\necho "Hello, world!"' |
11 | | - const node = await parsers.sh.parse(script, { |
12 | | - filepath: 'test.sh', |
13 | | - } as ParserOptions<Node>) |
| 10 | +const testCase = async (script: string) => { |
| 11 | + const node = await parsers.sh.parse(script, { |
| 12 | + filepath: 'test.sh', |
| 13 | + } as ParserOptions<Node>) |
14 | 14 |
|
15 | | - // Root node should start at position 0 |
16 | | - expect(locStart(node)).toBe(0) |
| 15 | + expect(locStart(node)).toBe(0) |
| 16 | + expect(locEnd(node)).toBe(script.length) |
| 17 | +} |
17 | 18 |
|
18 | | - // Root node should end at the script length |
19 | | - expect(locEnd(node)).toBe(script.length) |
| 19 | +describe('Parser location functions', () => { |
| 20 | + it('should handle simple commands', () => { |
| 21 | + const script = 'echo "simple"' |
| 22 | + return testCase(script) |
20 | 23 | }) |
21 | 24 |
|
22 | | - it('should handle scripts with various syntax constructs', async () => { |
23 | | - const scripts = [ |
24 | | - // Simple command |
25 | | - 'echo "simple"', |
26 | | - |
27 | | - // If statement |
28 | | - 'if [ "$1" = "test" ]; then\n echo "test"\nfi', |
29 | | - |
30 | | - // Function definition |
31 | | - 'function say_hello() {\n echo "Hello"\n}', |
32 | | - ] |
| 25 | + it('should handle if statements', () => { |
| 26 | + const script = 'if [ "$1" = "test" ]; then\n echo "test"\nfi' |
| 27 | + return testCase(script) |
| 28 | + }) |
33 | 29 |
|
34 | | - for (const script of scripts) { |
35 | | - const node = await parsers.sh.parse(script, { |
36 | | - filepath: 'test.sh', |
37 | | - } as ParserOptions<Node>) |
| 30 | + it('should handle function definitions', async () => { |
| 31 | + const script = 'function say_hello() {\n echo "Hello"\n}' |
| 32 | + return testCase(script) |
| 33 | + }) |
38 | 34 |
|
39 | | - expect(locStart(node)).toBe(0) |
40 | | - expect(locEnd(node)).toBe(script.length) |
41 | | - } |
| 35 | + it('should handle more complex constructs', async () => { |
| 36 | + // Add test for a more complex example, e.g. with loops and nesting |
| 37 | + const script = |
| 38 | + 'for i in $(seq 1 3); do\n if [ "$i" -eq 2 ]; then\n echo "Found $i"\n fi\ndone' |
| 39 | + return testCase(script) |
42 | 40 | }) |
43 | 41 | }) |
0 commit comments