Skip to content

Commit 68c3eab

Browse files
author
Alessio Michelini
committed
test: enhance unit tests for MarkdownParser to cover numbered list detection and whitespace handling
1 parent 682f038 commit 68c3eab

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/classes/markdown.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class MarkdownParser {
7474
* @returns The style according to the type
7575
*/
7676
getListStyle(text: string): string {
77-
const regex = /^(\d.\s)/;
77+
const regex = /^(\d+\.\s)/;
7878
const match = text.match(regex);
7979
if (match) {
8080
return match[1];

test/classes/markdown.test.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,29 @@ describe('MarkdownParser (Class)', () => {
3939
const mockHeadings = ['## Heading 1', 'Some text'];
4040
expect(parser.getHeadings(mockHeadings).length).toEqual(1);
4141
});
42+
43+
it('should handle headings with multiple spaces', () => {
44+
const parser = new MarkdownParser(mockFile);
45+
const mockHeadings = ['## Heading With Extra Spaces', '## Normal Heading', 'Some text'];
46+
const result = parser.getHeadings(mockHeadings);
47+
expect(result.length).toEqual(2);
48+
expect(result).toContain('## Heading With Extra Spaces');
49+
});
50+
});
51+
52+
describe('getListStyle', () => {
53+
it('should detect numbered list items', () => {
54+
const parser = new MarkdownParser(mockFile);
55+
expect(parser.getListStyle('1. Item')).toEqual('1. ');
56+
expect(parser.getListStyle('2. Item')).toEqual('2. ');
57+
expect(parser.getListStyle('10. Item')).toEqual('10. ');
58+
});
59+
60+
it('should return default bullet style for non-numbered items', () => {
61+
const parser = new MarkdownParser(mockFile);
62+
expect(parser.getListStyle('Normal Text')).toEqual('- ');
63+
expect(parser.getListStyle('- Bulleted item')).toEqual('- ');
64+
});
4265
});
4366

4467
describe('parseHeadings', () => {
@@ -79,14 +102,32 @@ describe('MarkdownParser (Class)', () => {
79102
expect(result[1]).toEqual(' - [Heading 2](#heading-2)');
80103
});
81104

82-
it('should parse numbered heaedings', () => {
105+
it('should parse numbered headings', () => {
83106
const parser = new MarkdownParser(mockNumberedFile);
84107
const mockHeadings = ['## Index', '## 1. Heading 1', '## 2. Heading 2'];
85108
const result = parser.parseHeadings(mockHeadings);
86109
expect(result.length).toEqual(2);
87110
expect(result[0]).toEqual('1. [Heading 1](#1-heading-1)');
88111
expect(result[1]).toEqual('2. [Heading 2](#2-heading-2)');
89112
});
113+
114+
it('should handle headings with multiple spaces', () => {
115+
const parser = new MarkdownParser(mockFile);
116+
const mockHeadings = ['## Heading With Extra Spaces', '## Normal Heading'];
117+
const result = parser.parseHeadings(mockHeadings);
118+
expect(result.length).toEqual(2);
119+
expect(result[0]).toEqual('- [Heading With Extra Spaces](#heading-with-extra-spaces)');
120+
});
121+
122+
it('should filter out headings that match the custom title', () => {
123+
const parser = new MarkdownParser(mockFile);
124+
parser.setTitle('Custom Title');
125+
const mockHeadings = ['## Custom Title', '## Heading 1', '### Heading 2'];
126+
const result = parser.parseHeadings(mockHeadings);
127+
expect(result.length).toEqual(2);
128+
expect(result[0]).toEqual('- [Heading 1](#heading-1)');
129+
expect(result[1]).toEqual(' - [Heading 2](#heading-2)');
130+
});
90131
});
91132

92133
describe('parse', () => {
@@ -97,6 +138,14 @@ describe('MarkdownParser (Class)', () => {
97138
expect(result[0]).toEqual('- [Heading 2](#heading-2)');
98139
});
99140

141+
it('should handle headings with excessive whitespace', async () => {
142+
const parser = new MarkdownParser(mockFile);
143+
const result = await parser.parse();
144+
const headingWithSpaces = result.find(link => link.includes('Heading with spaces'));
145+
expect(headingWithSpaces).toBeDefined();
146+
expect(headingWithSpaces).toEqual('- [Heading with spaces](#heading-with-spaces)');
147+
});
148+
100149
it('should fail if the file is not found', async () => {
101150
const parser = new MarkdownParser('sometest.md');
102151
expect(await parser.parse().catch).toBeDefined();

0 commit comments

Comments
 (0)