File size: 3,647 Bytes
a7d7463 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | """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"])
|