| """Tests for markdown parser module""" |
|
|
| import pytest |
| import sys |
| from pathlib import Path |
|
|
| sys.path.insert(0, str(Path(__file__).parent.parent / "src")) |
|
|
| from utils import MarkdownParser |
|
|
|
|
| class TestMarkdownParser: |
| """Test MarkdownParser class""" |
|
|
| def test_parser_initialization(self): |
| parser = MarkdownParser() |
| assert parser.content == "" |
|
|
| def test_parse_method(self): |
| parser = MarkdownParser() |
| parser.parse("# Hello\nWorld") |
| assert parser.content == "# Hello\nWorld" |
|
|
| def test_extract_code_blocks(self): |
| parser = MarkdownParser() |
| parser.parse( |
| """ |
| # Code Example |
| |
| ```python |
| def hello(): |
| print("Hello") |
| ``` |
| |
| More text |
| |
| ```javascript |
| console.log("test"); |
| ``` |
| """ |
| ) |
| blocks = parser.extract_code_blocks() |
| assert len(blocks) == 2 |
| assert blocks[0].language == "python" |
| assert blocks[1].language == "javascript" |
|
|
| def test_extract_headings(self): |
| parser = MarkdownParser() |
| parser.parse( |
| """ |
| # Heading 1 |
| ## Heading 2 |
| ### Heading 3 |
| """ |
| ) |
| headings = parser.extract_headings() |
| assert len(headings) == 3 |
| assert headings[0].level == 1 |
| assert headings[1].level == 2 |
| assert headings[2].level == 3 |
|
|
| def test_extract_links(self): |
| parser = MarkdownParser() |
| parser.parse( |
| """ |
| Check out [this link](https://example.com) |
| and [another link](https://test.com). |
| """ |
| ) |
| links = parser.extract_links() |
| assert len(links) == 2 |
| assert links[0] == ("this link", "https://example.com") |
|
|
| def test_extract_images(self): |
| parser = MarkdownParser() |
| parser.parse("") |
| images = parser.extract_images() |
| assert len(images) == 1 |
| assert images[0] == ("Alt text", "image.png/path") |
|
|
| def test_search(self): |
| parser = MarkdownParser() |
| parser.parse("Line 1\nPython code here\nLine 3") |
| results = parser.search("Python") |
| assert len(results) == 1 |
| assert results[0]["line"] == 2 |
|
|
| def test_search_not_found(self): |
| parser = MarkdownParser() |
| parser.parse("No match here") |
| results = parser.search("missing") |
| assert len(results) == 0 |
|
|
| def test_convert_to_plain_text(self): |
| parser = MarkdownParser() |
| parser.parse( |
| """ |
| # Title |
| |
| **bold** and *italic* |
| |
| ``` |
| code block |
| ``` |
| """ |
| ) |
| plain = parser.convert_to_plain_text() |
| assert "# Title" in plain |
| assert "bold" in plain |
| assert "code block" in plain |
|
|
| def test_from_file_method(self, tmp_path): |
| md_file = tmp_path / "test.md" |
| md_file.write_text("# Test File\n\nContent here") |
|
|
| parser = MarkdownParser.from_file(str(md_file)) |
| assert "# Test File" in parser.content |
|
|
| def test_get_table_of_contents(self): |
| parser = MarkdownParser() |
| parser.parse( |
| """ |
| # Main Title |
| ## Section 1 |
| ### Subsection 1.1 |
| ## Section 2 |
| """ |
| ) |
| toc = parser.get_table_of_contents() |
| assert len(toc) == 4 |
| assert toc[0]["level"] == 1 |
| assert toc[0]["text"] == "Main Title" |
|
|
|
|
| class TestCodeBlock: |
| """Test CodeBlock dataclass""" |
|
|
| def test_code_block_creation(self): |
| from utils.markdown_parser import CodeBlock |
|
|
| block = CodeBlock( |
| language="python", content="print('hello')", start_line=1, end_line=3 |
| ) |
| assert block.language == "python" |
| assert block.content == "print('hello')" |
|
|
|
|
| if __name__ == "__main__": |
| pytest.main([__file__, "-v"]) |
|
|