feat: add unit tests

This commit is contained in:
2025-11-12 19:28:48 +01:00
parent 0eca6d6725
commit 6e9ce92cde
12 changed files with 510 additions and 0 deletions

9
t/00-load.t Executable file
View File

@@ -0,0 +1,9 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 1;
use MarkdownParser;
ok( 1, 'Module loaded successfully' );

16
t/01-headers.t Executable file
View File

@@ -0,0 +1,16 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 6;
use MarkdownParser;
my $parser = MarkdownParser->new();
is( $parser->parse("# Header 1"), "<h1>Header 1</h1>\n", "H1 header" );
is( $parser->parse("## Header 2"), "<h2>Header 2</h2>\n", "H2 header" );
is( $parser->parse("### Header 3"), "<h3>Header 3</h3>\n", "H3 header" );
is( $parser->parse("#### Header 4"), "<h4>Header 4</h4>\n", "H4 header" );
is( $parser->parse("##### Header 5"), "<h5>Header 5</h5>\n", "H5 header" );
is( $parser->parse("###### Header 6"), "<h6>Header 6</h6>\n", "H6 header" );

25
t/02-paragraphs.t Executable file
View File

@@ -0,0 +1,25 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 3;
use MarkdownParser;
my $parser = MarkdownParser->new();
is(
$parser->parse("Simple paragraph"),
"<p>Simple paragraph</p>\n",
"Single paragraph"
);
is(
$parser->parse("First paragraph\n\nSecond paragraph"),
"<p>First paragraph</p>\n<p>Second paragraph</p>\n",
"Multiple paragraphs"
);
is(
$parser->parse("Paragraph with\nmultiple lines"),
"<p>Paragraph with multiple lines</p>\n",
"Multi-line paragraph"
);

65
t/03-formatting.t Executable file
View File

@@ -0,0 +1,65 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 11;
use MarkdownParser;
my $parser = MarkdownParser->new();
is(
$parser->parse("**bold text**"),
"<p><strong>bold text</strong></p>\n",
"Bold with **"
);
is(
$parser->parse("__bold text__"),
"<p><strong>bold text</strong></p>\n",
"Bold with __"
);
is(
$parser->parse("*italic text*"),
"<p><em>italic text</em></p>\n",
"Italic with *"
);
is(
$parser->parse("_italic text_"),
"<p><em>italic text</em></p>\n",
"Italic with _"
);
is(
$parser->parse("**bold** and *italic*"),
"<p><strong>bold</strong> and <em>italic</em></p>\n",
"Bold and italic together"
);
is(
$parser->parse("Text with **bold** in middle"),
"<p>Text with <strong>bold</strong> in middle</p>\n",
"Bold in middle of text"
);
is(
$parser->parse("Text with *italic* in middle"),
"<p>Text with <em>italic</em> in middle</p>\n",
"Italic in middle of text"
);
is(
$parser->parse("**bold** *italic* **bold again**"),
"<p><strong>bold</strong> <em>italic</em> <strong>bold again</strong></p>\n",
"Multiple formatting"
);
is(
$parser->parse("***bold text***"),
"<p><strong>bold text</strong></p>\n",
"Bold with ***"
);
is(
$parser->parse("**bold *italic* bold**"),
"<p><strong>bold <em>italic</em> bold</strong></p>\n",
"Nested formatting"
);
is(
$parser->parse("___bold text___"),
"<p><strong>bold text</strong></p>\n",
"Bold with ___"
);

44
t/04-links-images.t Executable file
View File

@@ -0,0 +1,44 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 8;
use MarkdownParser;
my $parser = MarkdownParser->new();
is(
$parser->parse("[link text](http://example.com)"),
"<p><a href=\"http://example.com\">link text</a></p>\n",
"Simple link"
);
is(
$parser->parse("[link with spaces](https://example.com/path)"),
"<p><a href=\"https://example.com/path\">link with spaces</a></p>\n",
"Link with path"
);
is(
$parser->parse("![alt text](image.png)"),
"<p><img src=\"image.png\" alt=\"alt text\"></p>\n",
"Simple image"
);
is(
$parser->parse("![alt with spaces](http://example.com/image.jpg)"),
"<p><img src=\"http://example.com/image.jpg\" alt=\"alt with spaces\"></p>\n",
"Image with URL"
);
is(
$parser->parse("[Click me](javascript:alert('XSS'))"),
"<p>Click me</p>\n",
"JavaScript protocol blocked in links"
);
is(
$parser->parse("[Click me](data:text/html,<script>alert('XSS')</script>)"),
"<p>Click me</p>\n",
"Data protocol blocked in links"
);
is( $parser->parse("![Image](javascript:alert('XSS'))"),
"<p>Image</p>\n", "JavaScript protocol blocked in images" );
is( $parser->parse("![Image](file:///etc/passwd)"),
"<p>Image</p>\n", "File protocol blocked in images" );

40
t/05-lists.t Executable file
View File

@@ -0,0 +1,40 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 6;
use MarkdownParser;
my $parser = MarkdownParser->new();
is(
$parser->parse("- Item 1\n- Item 2\n- Item 3"),
"<ul>\n<li>Item 1</li>\n<li>Item 2</li>\n<li>Item 3</li>\n</ul>\n",
"Unordered list with -"
);
is(
$parser->parse("* Item 1\n* Item 2"),
"<ul>\n<li>Item 1</li>\n<li>Item 2</li>\n</ul>\n",
"Unordered list with *"
);
is(
$parser->parse("+ Item 1\n+ Item 2"),
"<ul>\n<li>Item 1</li>\n<li>Item 2</li>\n</ul>\n",
"Unordered list with +"
);
is(
$parser->parse("1. First item\n2. Second item\n3. Third item"),
"<ol>\n<li>First item</li>\n<li>Second item</li>\n<li>Third item</li>\n</ol>\n",
"Ordered list"
);
is(
$parser->parse("- Item 1\n\n- Item 2"),
"<ul>\n<li>Item 1</li>\n</ul>\n<ul>\n<li>Item 2</li>\n</ul>\n",
"Multiple list blocks"
);
is(
$parser->parse("- **Bold item**\n- *Italic item*"),
"<ul>\n<li><strong>Bold item</strong></li>\n<li><em>Italic item</em></li>\n</ul>\n",
"List items with formatting"
);

35
t/06-code.t Executable file
View File

@@ -0,0 +1,35 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 5;
use MarkdownParser;
my $parser = MarkdownParser->new();
is(
$parser->parse("`inline code`"),
"<p><code>inline code</code></p>\n",
"Inline code"
);
is(
$parser->parse("Text with `code` in it"),
"<p>Text with <code>code</code> in it</p>\n",
"Inline code in text"
);
is(
$parser->parse("```\ncode block\n```"),
"<pre><code>\ncode block\n</code></pre>\n",
"Code block"
);
is(
$parser->parse("```\nline 1\nline 2\nline 3\n```"),
"<pre><code>\nline 1\nline 2\nline 3\n</code></pre>\n",
"Multi-line code block"
);
is(
$parser->parse("```\n```"),
"<pre><code>\n</code></pre>\n",
"Empty code block"
);

25
t/07-blockquotes.t Executable file
View File

@@ -0,0 +1,25 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 3;
use MarkdownParser;
my $parser = MarkdownParser->new();
is(
$parser->parse("> Quote text"),
"<blockquote>\n<p>Quote text</p>\n</blockquote>\n",
"Simple blockquote"
);
is(
$parser->parse("> First line\n> Second line"),
"<blockquote>\n<p>First line</p>\n<p>Second line</p>\n</blockquote>\n",
"Multi-line blockquote"
);
is(
$parser->parse("> Quote with **bold**"),
"<blockquote>\n<p>Quote with <strong>bold</strong></p>\n</blockquote>\n",
"Blockquote with formatting"
);

13
t/08-horizontal-rules.t Executable file
View File

@@ -0,0 +1,13 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 3;
use MarkdownParser;
my $parser = MarkdownParser->new();
is( $parser->parse("---"), "<hr>\n", "Horizontal rule with ---" );
is( $parser->parse("***"), "<hr>\n", "Horizontal rule with ***" );
is( $parser->parse("___"), "<hr>\n", "Horizontal rule with ___" );

97
t/09-complex.t Executable file
View File

@@ -0,0 +1,97 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 4;
use MarkdownParser;
my $parser = MarkdownParser->new();
my $input = <<'EOF';
# Title
This is a paragraph with **bold** and *italic* text.
- List item 1
- List item 2
[Link](http://example.com)
EOF
my $expected = <<'EOF';
<h1>Title</h1>
<p>This is a paragraph with <strong>bold</strong> and <em>italic</em> text.</p>
<ul>
<li>List item 1</li>
<li>List item 2</li>
</ul>
<p><a href="http://example.com">Link</a></p>
EOF
is( $parser->parse($input), $expected, "Complex document" );
$input = <<'EOF';
## Section
Paragraph one.
> Blockquote here
Another paragraph.
EOF
$expected = <<'EOF';
<h2>Section</h2>
<p>Paragraph one.</p>
<blockquote>
<p>Blockquote here</p>
</blockquote>
<p>Another paragraph.</p>
EOF
is( $parser->parse($input), $expected, "Document with blockquote" );
$input = <<'EOF';
# Code Example
Here is some `inline code` and a code block:
```
function test() {
return true;
}
```
EOF
$expected = <<'EOF';
<h1>Code Example</h1>
<p>Here is some <code>inline code</code> and a code block:</p>
<pre><code>
function test() {
return true;
}
</code></pre>
EOF
is( $parser->parse($input), $expected, "Document with code" );
$input = <<'EOF';
# Header
Paragraph with [link](url) and ![image](img.png).
---
## Another Header
EOF
$expected = <<'EOF';
<h1>Header</h1>
<p>Paragraph with <a href="url">link</a> and <img src="img.png" alt="image">.</p>
<hr>
<h2>Another Header</h2>
EOF
is( $parser->parse($input),
$expected, "Document with links, images, and horizontal rule" );

30
t/10-html-escape.t Executable file
View File

@@ -0,0 +1,30 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 4;
use MarkdownParser;
my $parser = MarkdownParser->new();
is(
$parser->parse("Text with <tag>"),
"<p>Text with &lt;tag&gt;</p>\n",
"HTML tags escaped"
);
is(
$parser->parse("Text with & symbol"),
"<p>Text with &amp; symbol</p>\n",
"Ampersand escaped"
);
is(
$parser->parse('Text with "quotes"'),
"<p>Text with &quot;quotes&quot;</p>\n",
"Quotes escaped"
);
is(
$parser->parse("Text with 'apostrophe'"),
"<p>Text with &#39;apostrophe&#39;</p>\n",
"Apostrophe escaped"
);

111
t/11-tables.t Normal file
View File

@@ -0,0 +1,111 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 4;
use MarkdownParser;
my $parser = MarkdownParser->new();
my $input = <<'EOF';
| Header 1 | Header 2 |
|----------|----------|
| Cell 1 | Cell 2 |
| Cell 3 | Cell 4 |
EOF
my $expected = <<'EOF';
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
</tr>
<tr>
<td>Cell 3</td>
<td>Cell 4</td>
</tr>
</table>
EOF
is( $parser->parse($input), $expected, "Basic table" );
$input = <<'EOF';
| Name | Age |
|------|-----|
| John | 25 |
| Jane | 30 |
EOF
$expected = <<'EOF';
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>John</td>
<td>25</td>
</tr>
<tr>
<td>Jane</td>
<td>30</td>
</tr>
</table>
EOF
is( $parser->parse($input), $expected, "Table with different content" );
$input = <<'EOF';
# Title
| Col1 | Col2 |
|------|------|
| Data | Info |
More text.
EOF
$expected = <<'EOF';
<h1>Title</h1>
<table>
<tr>
<th>Col1</th>
<th>Col2</th>
</tr>
<tr>
<td>Data</td>
<td>Info</td>
</tr>
</table>
<p>More text.</p>
EOF
is( $parser->parse($input), $expected, "Table with surrounding content" );
$input = <<'EOF';
| **Bold** | *Italic* | [Link](url) |
|----------|----------|-------------|
| Text | More | Info |
EOF
$expected = <<'EOF';
<table>
<tr>
<th><strong>Bold</strong></th>
<th><em>Italic</em></th>
<th><a href="url">Link</a></th>
</tr>
<tr>
<td>Text</td>
<td>More</td>
<td>Info</td>
</tr>
</table>
EOF
is( $parser->parse($input), $expected, "Table with inline formatting" );