From 27d66984e26dfdbba4f50aa2ac30cff576959584 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Sat, 31 Jan 2026 21:39:53 +0000 Subject: [PATCH] Initial upload: man-card CLI tool with PDF/PNG generation, templates, and tests --- tests/test_card_generator.py | 169 +++++++++++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 tests/test_card_generator.py diff --git a/tests/test_card_generator.py b/tests/test_card_generator.py new file mode 100644 index 0000000..c64d454 --- /dev/null +++ b/tests/test_card_generator.py @@ -0,0 +1,169 @@ +"""Tests for card generator.""" + +import pytest +import os +import tempfile +from unittest.mock import MagicMock, patch + +from man_card.card_generator import PDFCardGenerator, PNGCardGenerator +from man_card.templates import TemplateLoader, Template +from man_card.man_parser import CommandInfo, Option + + +class TestPDFCardGenerator: + """Tests for PDFCardGenerator class.""" + + def setup_method(self): + """Set up test fixtures.""" + template_loader = TemplateLoader() + self.template = template_loader.load("default") + self.generator = PDFCardGenerator(self.template) + + def test_init_with_default_template(self): + """Test initialization with default template.""" + gen = PDFCardGenerator() + assert gen.template is not None + + def test_init_with_custom_template(self): + """Test initialization with custom template.""" + template = Template(name="custom") + gen = PDFCardGenerator(template) + assert gen.template.name == "custom" + + def test_page_sizes(self): + """Test that page sizes are defined.""" + assert "a4" in self.generator.PAGE_SIZES + assert "letter" in self.generator.PAGE_SIZES + + def test_generate_pdf_command_info(self): + """Test PDF generation with CommandInfo.""" + command_info = CommandInfo( + name="ls", + synopsis="ls [OPTION]... [FILE]...", + description="List directory contents", + options=[ + Option(flag="-a", description="show all files"), + Option(flag="-l", description="long listing format") + ], + examples=["ls -la", "ls -lh"] + ) + + with tempfile.TemporaryDirectory() as tmpdir: + output_path = os.path.join(tmpdir, "test_card.pdf") + self.generator.generate(command_info, output_path, page_size="a4") + assert os.path.exists(output_path) + assert os.path.getsize(output_path) > 0 + + def test_generate_pdf_with_minimal_info(self): + """Test PDF generation with minimal CommandInfo.""" + command_info = CommandInfo(name="test") + + with tempfile.TemporaryDirectory() as tmpdir: + output_path = os.path.join(tmpdir, "minimal_card.pdf") + self.generator.generate(command_info, output_path) + assert os.path.exists(output_path) + + def test_hex_to_rgb_conversion(self): + """Test hex to RGB color conversion.""" + rgb = self.generator._hex_to_rgb("#FF0000") + assert rgb == (255, 0, 0) + + rgb = self.generator._hex_to_rgb("#00FF00") + assert rgb == (0, 255, 0) + + rgb = self.generator._hex_to_rgb("#0000FF") + assert rgb == (0, 0, 255) + + def test_hex_to_rgb_without_hash(self): + """Test hex to RGB conversion without hash prefix.""" + rgb = self.generator._hex_to_rgb("FFFFFF") + assert rgb == (255, 255, 255) + + +class TestPNGCardGenerator: + """Tests for PNGCardGenerator class.""" + + def setup_method(self): + """Set up test fixtures.""" + template_loader = TemplateLoader() + self.template = template_loader.load("default") + self.generator = PNGCardGenerator(self.template) + + def test_init_with_default_template(self): + """Test initialization with default template.""" + gen = PNGCardGenerator() + assert gen.template is not None + + def test_dpi_sizes_defined(self): + """Test that DPI sizes are defined.""" + assert 72 in self.generator.DPI_SIZES + assert 150 in self.generator.DPI_SIZES + assert 300 in self.generator.DPI_SIZES + + def test_generate_png_command_info(self): + """Test PNG generation with CommandInfo.""" + command_info = CommandInfo( + name="ls", + synopsis="ls [OPTION]... [FILE]...", + description="List directory contents", + options=[ + Option(flag="-a", description="show all files"), + Option(flag="-l", description="long listing format") + ], + examples=["ls -la", "ls -lh"] + ) + + with tempfile.TemporaryDirectory() as tmpdir: + output_path = os.path.join(tmpdir, "test_card.png") + self.generator.generate(command_info, output_path, dpi=72) + assert os.path.exists(output_path) + assert os.path.getsize(output_path) > 0 + + def test_generate_png_with_minimal_info(self): + """Test PNG generation with minimal CommandInfo.""" + command_info = CommandInfo(name="test") + + with tempfile.TemporaryDirectory() as tmpdir: + output_path = os.path.join(tmpdir, "minimal_card.png") + self.generator.generate(command_info, output_path, dpi=72) + assert os.path.exists(output_path) + + def test_hex_to_rgb_conversion(self): + """Test hex to RGB color conversion.""" + rgb = self.generator._hex_to_rgb("#FF0000") + assert rgb == (255, 0, 0) + + +class TestCardGenerationIntegration: + """Integration tests for card generation.""" + + def test_pdf_and_png_consistency(self): + """Test that both generators can handle the same data.""" + command_info = CommandInfo( + name="git", + synopsis="git [OPTIONS] COMMAND [ARGUMENTS]", + description="the stupid content tracker", + options=[ + Option(flag="-v", description="display version"), + Option(flag="-h", description="display help") + ], + examples=["git status", "git commit -m 'message'"] + ) + + template_loader = TemplateLoader() + template = template_loader.load("default") + + pdf_gen = PDFCardGenerator(template) + png_gen = PNGCardGenerator(template) + + with tempfile.TemporaryDirectory() as tmpdir: + pdf_path = os.path.join(tmpdir, "test.pdf") + png_path = os.path.join(tmpdir, "test.png") + + pdf_gen.generate(command_info, pdf_path) + png_gen.generate(command_info, png_path, dpi=72) + + assert os.path.exists(pdf_path) + assert os.path.exists(png_path) + assert os.path.getsize(pdf_path) > 0 + assert os.path.getsize(png_path) > 0