From 6e9ce92cdecb7689cb18fe38b40615f54a75fe7a Mon Sep 17 00:00:00 2001 From: Kharec Date: Wed, 12 Nov 2025 19:28:48 +0100 Subject: [PATCH] feat: add unit tests --- t/00-load.t | 9 ++++ t/01-headers.t | 16 ++++++ t/02-paragraphs.t | 25 +++++++++ t/03-formatting.t | 65 +++++++++++++++++++++++ t/04-links-images.t | 44 ++++++++++++++++ t/05-lists.t | 40 +++++++++++++++ t/06-code.t | 35 +++++++++++++ t/07-blockquotes.t | 25 +++++++++ t/08-horizontal-rules.t | 13 +++++ t/09-complex.t | 97 +++++++++++++++++++++++++++++++++++ t/10-html-escape.t | 30 +++++++++++ t/11-tables.t | 111 ++++++++++++++++++++++++++++++++++++++++ 12 files changed, 510 insertions(+) create mode 100755 t/00-load.t create mode 100755 t/01-headers.t create mode 100755 t/02-paragraphs.t create mode 100755 t/03-formatting.t create mode 100755 t/04-links-images.t create mode 100755 t/05-lists.t create mode 100755 t/06-code.t create mode 100755 t/07-blockquotes.t create mode 100755 t/08-horizontal-rules.t create mode 100755 t/09-complex.t create mode 100755 t/10-html-escape.t create mode 100644 t/11-tables.t diff --git a/t/00-load.t b/t/00-load.t new file mode 100755 index 0000000..6aa8d60 --- /dev/null +++ b/t/00-load.t @@ -0,0 +1,9 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +use Test::More tests => 1; +use MarkdownParser; + +ok( 1, 'Module loaded successfully' ); + diff --git a/t/01-headers.t b/t/01-headers.t new file mode 100755 index 0000000..76e2110 --- /dev/null +++ b/t/01-headers.t @@ -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"), "

Header 1

\n", "H1 header" ); +is( $parser->parse("## Header 2"), "

Header 2

\n", "H2 header" ); +is( $parser->parse("### Header 3"), "

Header 3

\n", "H3 header" ); +is( $parser->parse("#### Header 4"), "

Header 4

\n", "H4 header" ); +is( $parser->parse("##### Header 5"), "
Header 5
\n", "H5 header" ); +is( $parser->parse("###### Header 6"), "
Header 6
\n", "H6 header" ); + diff --git a/t/02-paragraphs.t b/t/02-paragraphs.t new file mode 100755 index 0000000..cb5240a --- /dev/null +++ b/t/02-paragraphs.t @@ -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"), + "

Simple paragraph

\n", + "Single paragraph" +); +is( + $parser->parse("First paragraph\n\nSecond paragraph"), + "

First paragraph

\n

Second paragraph

\n", + "Multiple paragraphs" +); +is( + $parser->parse("Paragraph with\nmultiple lines"), + "

Paragraph with multiple lines

\n", + "Multi-line paragraph" +); + diff --git a/t/03-formatting.t b/t/03-formatting.t new file mode 100755 index 0000000..f17edcc --- /dev/null +++ b/t/03-formatting.t @@ -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**"), + "

bold text

\n", + "Bold with **" +); +is( + $parser->parse("__bold text__"), + "

bold text

\n", + "Bold with __" +); +is( + $parser->parse("*italic text*"), + "

italic text

\n", + "Italic with *" +); +is( + $parser->parse("_italic text_"), + "

italic text

\n", + "Italic with _" +); +is( + $parser->parse("**bold** and *italic*"), + "

bold and italic

\n", + "Bold and italic together" +); +is( + $parser->parse("Text with **bold** in middle"), + "

Text with bold in middle

\n", + "Bold in middle of text" +); +is( + $parser->parse("Text with *italic* in middle"), + "

Text with italic in middle

\n", + "Italic in middle of text" +); +is( + $parser->parse("**bold** *italic* **bold again**"), +"

bold italic bold again

\n", + "Multiple formatting" +); +is( + $parser->parse("***bold text***"), + "

bold text

\n", + "Bold with ***" +); +is( + $parser->parse("**bold *italic* bold**"), + "

bold italic bold

\n", + "Nested formatting" +); +is( + $parser->parse("___bold text___"), + "

bold text

\n", + "Bold with ___" +); + diff --git a/t/04-links-images.t b/t/04-links-images.t new file mode 100755 index 0000000..60b9949 --- /dev/null +++ b/t/04-links-images.t @@ -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)"), + "

link text

\n", + "Simple link" +); +is( + $parser->parse("[link with spaces](https://example.com/path)"), + "

link with spaces

\n", + "Link with path" +); +is( + $parser->parse("![alt text](image.png)"), + "

\"alt

\n", + "Simple image" +); +is( + $parser->parse("![alt with spaces](http://example.com/image.jpg)"), +"

\"alt

\n", + "Image with URL" +); +is( + $parser->parse("[Click me](javascript:alert('XSS'))"), + "

Click me

\n", + "JavaScript protocol blocked in links" +); +is( + $parser->parse("[Click me](data:text/html,)"), + "

Click me

\n", + "Data protocol blocked in links" +); +is( $parser->parse("![Image](javascript:alert('XSS'))"), + "

Image

\n", "JavaScript protocol blocked in images" ); +is( $parser->parse("![Image](file:///etc/passwd)"), + "

Image

\n", "File protocol blocked in images" ); + diff --git a/t/05-lists.t b/t/05-lists.t new file mode 100755 index 0000000..8d9a77c --- /dev/null +++ b/t/05-lists.t @@ -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"), + "\n", + "Unordered list with -" +); +is( + $parser->parse("* Item 1\n* Item 2"), + "\n", + "Unordered list with *" +); +is( + $parser->parse("+ Item 1\n+ Item 2"), + "\n", + "Unordered list with +" +); +is( + $parser->parse("1. First item\n2. Second item\n3. Third item"), +"
    \n
  1. First item
  2. \n
  3. Second item
  4. \n
  5. Third item
  6. \n
\n", + "Ordered list" +); +is( + $parser->parse("- Item 1\n\n- Item 2"), + "\n\n", + "Multiple list blocks" +); +is( + $parser->parse("- **Bold item**\n- *Italic item*"), +"\n", + "List items with formatting" +); + diff --git a/t/06-code.t b/t/06-code.t new file mode 100755 index 0000000..f2855a0 --- /dev/null +++ b/t/06-code.t @@ -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`"), + "

inline code

\n", + "Inline code" +); +is( + $parser->parse("Text with `code` in it"), + "

Text with code in it

\n", + "Inline code in text" +); +is( + $parser->parse("```\ncode block\n```"), + "
\ncode block\n
\n", + "Code block" +); +is( + $parser->parse("```\nline 1\nline 2\nline 3\n```"), + "
\nline 1\nline 2\nline 3\n
\n", + "Multi-line code block" +); +is( + $parser->parse("```\n```"), + "
\n
\n", + "Empty code block" +); + diff --git a/t/07-blockquotes.t b/t/07-blockquotes.t new file mode 100755 index 0000000..512986c --- /dev/null +++ b/t/07-blockquotes.t @@ -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"), + "
\n

Quote text

\n
\n", + "Simple blockquote" +); +is( + $parser->parse("> First line\n> Second line"), + "
\n

First line

\n

Second line

\n
\n", + "Multi-line blockquote" +); +is( + $parser->parse("> Quote with **bold**"), + "
\n

Quote with bold

\n
\n", + "Blockquote with formatting" +); + diff --git a/t/08-horizontal-rules.t b/t/08-horizontal-rules.t new file mode 100755 index 0000000..2f09b94 --- /dev/null +++ b/t/08-horizontal-rules.t @@ -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("---"), "
\n", "Horizontal rule with ---" ); +is( $parser->parse("***"), "
\n", "Horizontal rule with ***" ); +is( $parser->parse("___"), "
\n", "Horizontal rule with ___" ); + diff --git a/t/09-complex.t b/t/09-complex.t new file mode 100755 index 0000000..e8364de --- /dev/null +++ b/t/09-complex.t @@ -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'; +

Title

+

This is a paragraph with bold and italic text.

+ +

Link

+EOF + +is( $parser->parse($input), $expected, "Complex document" ); + +$input = <<'EOF'; +## Section + +Paragraph one. + +> Blockquote here + +Another paragraph. +EOF + +$expected = <<'EOF'; +

Section

+

Paragraph one.

+
+

Blockquote here

+
+

Another paragraph.

+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'; +

Code Example

+

Here is some inline code and a code block:

+

+function test() {
+    return true;
+}
+
+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'; +

Header

+

Paragraph with link and image.

+
+

Another Header

+EOF + +is( $parser->parse($input), + $expected, "Document with links, images, and horizontal rule" ); + diff --git a/t/10-html-escape.t b/t/10-html-escape.t new file mode 100755 index 0000000..909281d --- /dev/null +++ b/t/10-html-escape.t @@ -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 "), + "

Text with <tag>

\n", + "HTML tags escaped" +); +is( + $parser->parse("Text with & symbol"), + "

Text with & symbol

\n", + "Ampersand escaped" +); +is( + $parser->parse('Text with "quotes"'), + "

Text with "quotes"

\n", + "Quotes escaped" +); +is( + $parser->parse("Text with 'apostrophe'"), + "

Text with 'apostrophe'

\n", + "Apostrophe escaped" +); + diff --git a/t/11-tables.t b/t/11-tables.t new file mode 100644 index 0000000..c0017ab --- /dev/null +++ b/t/11-tables.t @@ -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'; + + + + + + + + + + + + + +
Header 1Header 2
Cell 1Cell 2
Cell 3Cell 4
+EOF + +is( $parser->parse($input), $expected, "Basic table" ); + +$input = <<'EOF'; +| Name | Age | +|------|-----| +| John | 25 | +| Jane | 30 | +EOF + +$expected = <<'EOF'; + + + + + + + + + + + + + +
NameAge
John25
Jane30
+EOF + +is( $parser->parse($input), $expected, "Table with different content" ); + +$input = <<'EOF'; +# Title + +| Col1 | Col2 | +|------|------| +| Data | Info | + +More text. +EOF + +$expected = <<'EOF'; +

Title

+ + + + + + + + + +
Col1Col2
DataInfo
+

More text.

+EOF + +is( $parser->parse($input), $expected, "Table with surrounding content" ); + +$input = <<'EOF'; +| **Bold** | *Italic* | [Link](url) | +|----------|----------|-------------| +| Text | More | Info | +EOF + +$expected = <<'EOF'; + + + + + + + + + + + +
BoldItalicLink
TextMoreInfo
+EOF + +is( $parser->parse($input), $expected, "Table with inline formatting" ); +