|
1 | 1 | import { describe, expect, it } from 'vitest' |
2 | | -import { parseBracketContent } from '../../src/internal/parse/syntax/brackets' |
| 2 | +import { findClosingBracket, parseBracketContent } from '../../src/internal/parse/syntax/brackets' |
| 3 | + |
| 4 | +describe('findClosingBracket', () => { |
| 5 | + it('should find the closing bracket', () => { |
| 6 | + expect(findClosingBracket('[hello]', 0)).toBe(6) |
| 7 | + }) |
| 8 | + |
| 9 | + it('should skip nested bracket pairs', () => { |
| 10 | + expect(findClosingBracket('[a [b] c]', 0)).toBe(8) |
| 11 | + }) |
| 12 | + |
| 13 | + it('should skip escaped brackets', () => { |
| 14 | + expect(findClosingBracket('[a \\] b]', 0)).toBe(7) |
| 15 | + }) |
| 16 | + |
| 17 | + it('should return -1 when unclosed', () => { |
| 18 | + expect(findClosingBracket('[a [b]', 0)).toBe(-1) |
| 19 | + }) |
| 20 | + |
| 21 | + it('should return -1 when not at an opening bracket', () => { |
| 22 | + expect(findClosingBracket('a]', 0)).toBe(-1) |
| 23 | + }) |
| 24 | + |
| 25 | + it('should work with non-zero start index', () => { |
| 26 | + expect(findClosingBracket('prefix[content]', 6)).toBe(14) |
| 27 | + }) |
| 28 | +}) |
3 | 29 |
|
4 | 30 | describe('parseBracketContent', () => { |
5 | 31 | it('should parse simple bracket content', () => { |
@@ -29,4 +55,20 @@ describe('parseBracketContent', () => { |
29 | 55 | it('should work with non-zero start index', () => { |
30 | 56 | expect(parseBracketContent('prefix[content]suffix', 6)).toEqual({ content: 'content', endIndex: 15 }) |
31 | 57 | }) |
| 58 | + |
| 59 | + it('should include nested bracket pairs in the content', () => { |
| 60 | + expect(parseBracketContent('[a [b] c]', 0)).toEqual({ content: 'a [b] c', endIndex: 9 }) |
| 61 | + }) |
| 62 | + |
| 63 | + it('should include an image in the content', () => { |
| 64 | + expect(parseBracketContent('[]', 0)).toEqual({ content: '', endIndex: 15 }) |
| 65 | + }) |
| 66 | + |
| 67 | + it('should include multiple nested pairs in the content', () => { |
| 68 | + expect(parseBracketContent('[[a] and [b]]', 0)).toEqual({ content: '[a] and [b]', endIndex: 13 }) |
| 69 | + }) |
| 70 | + |
| 71 | + it('should return null when a nested pair is left unclosed', () => { |
| 72 | + expect(parseBracketContent('[a [b]', 0)).toBeNull() |
| 73 | + }) |
32 | 74 | }) |
0 commit comments