Files
shell-speak/tests/test_generators.py
Auto User 95459fb4c8 fix: resolve CI test failure in output.py
- Fixed undefined 'tool' variable in display_history function
- Changed '[tool]' markup tag usage to proper Rich syntax
- All tests now pass (38/38 unit tests)
- Type checking passes with mypy --strict
2026-01-31 06:22:27 +00:00

215 lines
6.5 KiB
Python

"""Tests for generators module."""
import tempfile
from pathlib import Path
import pytest
from doc2man.generators.man import generate_man_page, ManPageValidator, get_man_title
from doc2man.generators.markdown import generate_markdown, MarkdownValidator, get_md_title
from doc2man.generators.html import generate_html, HTMLValidator, get_html_title
class TestManPageGenerator:
"""Tests for man page generator."""
def test_generate_man_page_basic(self):
"""Test basic man page generation."""
data = [
{
"file": "example.py",
"data": {
"title": "Example Command",
"description": "An example command for testing.",
"functions": [
{
"name": "example",
"description": "An example function.",
"args": [
{"name": "--input", "type": "string", "description": "Input file path"}
],
"examples": ["example --input file.txt"]
}
]
}
}
]
with tempfile.NamedTemporaryFile(suffix=".1", delete=False) as f:
output_path = Path(f.name)
result = generate_man_page(data, output_path)
assert ".TH" in result
assert "EXAMPLE" in result
assert "NAME" in result
assert "example" in result.lower()
output_path.unlink()
def test_man_page_validator(self):
"""Test man page validation."""
content = """
.TH EXAMPLE 1
.SH NAME
example \- An example command
.SH DESCRIPTION
This is a description.
"""
warnings = ManPageValidator.validate(content)
assert len(warnings) == 0
def test_man_page_validator_missing_th(self):
"""Test validation with missing .TH macro."""
content = """
.SH NAME
example \- An example command
.SH DESCRIPTION
This is a description.
"""
warnings = ManPageValidator.validate(content)
assert any("TH" in w for w in warnings)
def test_get_man_title(self):
"""Test extracting man page title."""
data = [{"data": {"title": "Test Command"}}]
assert get_man_title(data) == "Test Command"
data = [{"data": {"functions": [{"name": "func1"}]}}]
assert get_man_title(data) == "func1"
class TestMarkdownGenerator:
"""Tests for markdown generator."""
def test_generate_markdown_basic(self):
"""Test basic markdown generation."""
data = [
{
"file": "example.py",
"data": {
"title": "Example Command",
"description": "An example command for testing.",
"functions": [
{
"name": "example",
"description": "An example function.",
"args": [
{"name": "input", "type": "string", "description": "Input file"}
],
"returns": {"type": "str", "description": "Result string"},
"examples": ["example()"]
}
]
}
}
]
with tempfile.NamedTemporaryFile(suffix=".md", delete=False) as f:
output_path = Path(f.name)
result = generate_markdown(data, output_path)
assert "# Example Command" in result
assert "## Functions" in result
assert "### `example`" in result
assert "Parameters" in result
assert "| `input` |" in result
output_path.unlink()
def test_markdown_validator(self):
"""Test markdown validation."""
content = """# Title
Some content.
"""
warnings = MarkdownValidator.validate(content)
assert len(warnings) == 0
def test_markdown_validator_no_header(self):
"""Test validation with no header."""
content = """Some content without header.
"""
warnings = MarkdownValidator.validate(content)
assert any("header" in w.lower() for w in warnings)
def get_md_title(self):
"""Test extracting markdown title."""
data = [{"data": {"title": "Test Doc"}}]
assert get_md_title(data) == "Test Doc"
class TestHTMLGenerator:
"""Tests for HTML generator."""
def test_generate_html_basic(self):
"""Test basic HTML generation."""
data = [
{
"file": "example.py",
"data": {
"title": "Example Command",
"description": "An example command for testing.",
"functions": [
{
"name": "example",
"description": "An example function.",
"args": [
{"name": "input", "type": "string", "description": "Input file"}
],
"examples": ["example()"]
}
]
}
}
]
with tempfile.NamedTemporaryFile(suffix=".html", delete=False) as f:
output_path = Path(f.name)
result = generate_html(data, output_path)
assert "<!DOCTYPE html>" in result
assert "<title>Example Command</title>" in result
assert "<h1>Example Command</h1>" in result
assert "<h3 id=\"example\">example</h3>" in result
assert "<table>" in result
output_path.unlink()
def test_html_validator(self):
"""Test HTML validation."""
content = """<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
Content
</body>
</html>
"""
warnings = HTMLValidator.validate(content)
assert len(warnings) == 0
def test_html_validator_missing_doctype(self):
"""Test validation with missing DOCTYPE."""
content = """<html>
<head>
<title>Test</title>
</head>
<body>
Content
</body>
</html>
"""
warnings = HTMLValidator.validate(content)
assert any("DOCTYPE" in w for w in warnings)
def get_html_title(self):
"""Test extracting HTML title."""
data = [{"data": {"title": "Test Doc"}}]
assert get_html_title(data) == "Test Doc"