pyWebLayout/tests/examples/test_09_link_navigation_demo.py
2025-11-09 21:40:50 +01:00

180 lines
5.4 KiB
Python

"""
Test for link navigation example (09_link_navigation_demo.py).
This test ensures the link navigation example runs correctly and produces expected output.
"""
import pytest
import sys
from pathlib import Path
import importlib.util
# Add examples to path
examples_dir = Path(__file__).parent.parent.parent / "examples"
sys.path.insert(0, str(examples_dir))
# Load the demo module
spec = importlib.util.spec_from_file_location(
"link_navigation_demo",
examples_dir / "09_link_navigation_demo.py"
)
demo_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(demo_module)
def test_link_demo_imports():
"""Test that the link demo can be imported without errors."""
assert demo_module is not None
def test_create_paragraph_with_links():
"""Test creating a paragraph with mixed text and links."""
create_paragraph_with_links = demo_module.create_paragraph_with_links
from pyWebLayout.abstract.block import Paragraph
from pyWebLayout.abstract.inline import LinkedWord, Word
from pyWebLayout.abstract.functional import LinkType
text_parts = [
('text', "Click"),
('link', "here", "https://example.com", LinkType.EXTERNAL, "test_link"),
('text', "to visit"),
]
para = create_paragraph_with_links(text_parts)
assert isinstance(para, Paragraph)
assert len(para.words) == 4 # "Click", "here" (linked), "to", "visit"
# Check that the second word is a LinkedWord
assert isinstance(para.words[1], LinkedWord)
assert para.words[1].text == "here"
assert para.words[1].location == "https://example.com"
assert para.words[1]._link_type == LinkType.EXTERNAL
def test_link_callback_function():
"""Test that link callbacks work correctly."""
link_callback = demo_module.link_callback
demo_module.link_clicks = []
callback = link_callback("test_id")
callback()
assert "test_id" in demo_module.link_clicks
def test_create_example_1_internal_links():
"""Test creating internal links example."""
create_example_1 = demo_module.create_example_1_internal_links
from pyWebLayout.concrete.page import Page
page = create_example_1()
assert isinstance(page, Page)
assert page.size == (500, 600)
def test_create_example_2_external_links():
"""Test creating external links example."""
create_example_2 = demo_module.create_example_2_external_links
from pyWebLayout.concrete.page import Page
page = create_example_2()
assert isinstance(page, Page)
assert page.size == (500, 600)
def test_create_example_3_api_links():
"""Test creating API links example."""
create_example_3 = demo_module.create_example_3_api_links
from pyWebLayout.concrete.page import Page
page = create_example_3()
assert isinstance(page, Page)
assert page.size == (500, 600)
def test_create_example_4_function_links():
"""Test creating function links example."""
create_example_4 = demo_module.create_example_4_function_links
from pyWebLayout.concrete.page import Page
page = create_example_4()
assert isinstance(page, Page)
assert page.size == (500, 600)
def test_different_link_types():
"""Test that different link types are created correctly."""
create_paragraph_with_links = demo_module.create_paragraph_with_links
from pyWebLayout.abstract.functional import LinkType
# Test each link type
link_types = [
(LinkType.INTERNAL, "#section1"),
(LinkType.EXTERNAL, "https://example.com"),
(LinkType.API, "/api/action"),
(LinkType.FUNCTION, "function()"),
]
for link_type, location in link_types:
para = create_paragraph_with_links([
('link', "test", location, link_type, f"test_{link_type.name}")
])
assert len(para.words) == 1
assert para.words[0]._link_type == link_type
assert para.words[0].location == location
def test_combine_pages_into_grid():
"""Test combining pages into grid."""
combine_pages_into_grid = demo_module.combine_pages_into_grid
from pyWebLayout.concrete.page import Page
from pyWebLayout.style.page_style import PageStyle
# Create a few pages
page_style = PageStyle()
pages = [
Page(size=(200, 200), style=page_style),
Page(size=(200, 200), style=page_style),
Page(size=(200, 200), style=page_style),
Page(size=(200, 200), style=page_style)
]
combined = combine_pages_into_grid(pages, "Test Title")
# Should create a combined image
from PIL import Image
assert isinstance(combined, Image.Image)
# Should be larger than individual pages
assert combined.size[0] > 200
assert combined.size[1] > 200
def test_main_function():
"""Test that the main function runs without errors."""
main = demo_module.main
# Run main (will create output files)
result = main()
# Should return combined image and link clicks
assert result is not None
assert len(result) == 2
combined_image, link_clicks = result
# Should be a PIL image
from PIL import Image
assert isinstance(combined_image, Image.Image)
# Link clicks should be a list (may be empty since we're not actually clicking)
assert isinstance(link_clicks, list)
# Output file should exist
output_dir = Path("docs/images")
assert (output_dir / "example_09_link_navigation.png").exists()
if __name__ == "__main__":
pytest.main([__file__, "-v"])