diff --git a/tests/unit/test_templates.py b/tests/unit/test_templates.py new file mode 100644 index 0000000..06f2174 --- /dev/null +++ b/tests/unit/test_templates.py @@ -0,0 +1,136 @@ +"""Unit tests for the templates module.""" + +import pytest +from unittest.mock import patch, MagicMock +from pathlib import Path + +from scaffoldforge.templates import TemplateEngine +from scaffoldforge.parsers import IssueData, ChecklistItem + + +class TestTemplateEngine: + """Tests for TemplateEngine class.""" + + def test_engine_initialization(self): + """Test engine initialization.""" + engine = TemplateEngine() + assert engine.env is not None + + def test_engine_initialization_with_custom_dir(self): + """Test engine initialization with custom directory.""" + engine = TemplateEngine(template_dir="/tmp/templates") + assert engine.template_dir == "/tmp/templates" + + def test_list_available_languages(self): + """Test listing available languages.""" + languages = TemplateEngine.list_available_languages() + assert isinstance(languages, list) + assert "python" in languages + assert "javascript" in languages + assert "go" in languages + assert "rust" in languages + + def test_list_available_templates_for_python(self): + """Test listing templates for Python.""" + templates = TemplateEngine.list_available_templates("python") + assert isinstance(templates, list) + assert "default" in templates + + def test_list_available_templates_for_unknown_language(self): + """Test listing templates for unknown language.""" + templates = TemplateEngine.list_available_templates("unknown") + assert templates == [] + + def test_get_template_context(self): + """Test generating template context from issue data.""" + issue_data = IssueData( + number=123, + title="Create New Feature", + body="This is a test issue", + body_html="", + labels=["python"], + state="open", + url="https://github.com/user/repo/issues/123", + repository="user/repo", + author="testuser", + created_at="2024-01-01T00:00:00", + updated_at="2024-01-02T00:00:00", + checklist=[ + ChecklistItem(text="Task 1", completed=False), + ChecklistItem(text="Task 2", completed=True), + ] + ) + + engine = TemplateEngine() + context = engine.get_template_context(issue_data) + + assert context["project_name"] is not None + assert context["issue_number"] == 123 + assert context["issue_title"] == "Create New Feature" + assert "Task 1" in context["todo_items"] + assert "Task 2" in context["completed_items"] + + def test_generate_project_name(self): + """Test project name generation from issue title.""" + issue_data = IssueData( + number=1, title="Create a new feature for user", body="", + body_html="", labels=[], state="open", url="", repository="", + author="", created_at="", updated_at="" + ) + + engine = TemplateEngine() + project_name = engine._generate_project_name(issue_data) + assert "create" in project_name + assert "new" in project_name + assert "feature" in project_name + + def test_to_kebab_case(self): + """Test kebab-case conversion.""" + engine = TemplateEngine() + assert engine._to_kebab_case("Hello World") == "hello-world" + assert engine._to_kebab_case("Test 123") == "test-123" + + def test_to_snake_case(self): + """Test snake_case conversion.""" + engine = TemplateEngine() + assert engine._to_snake_case("Hello World") == "hello_world" + assert engine._to_snake_case("Test 123") == "test_123" + + def test_to_pascal_case(self): + """Test PascalCase conversion.""" + engine = TemplateEngine() + assert engine._to_pascal_case("hello world") == "HelloWorld" + assert engine._to_pascal_case("test_123") == "Test123" + + def test_render_string(self): + """Test rendering a template string.""" + engine = TemplateEngine() + content = "Hello {{ name }}" + rendered = engine.render_string(content, {"name": "World"}) + assert rendered == "Hello World" + + def test_render_with_special_characters(self): + """Test rendering with special characters in context.""" + engine = TemplateEngine() + issue_data = IssueData( + number=1, + title="Feature with 'quotes' and \"double quotes\"", + body="", + body_html="", + labels=[], + state="open", + url="", + repository="", + author="", + created_at="", + updated_at="" + ) + context = engine.get_template_context(issue_data) + assert context["project_name"] is not None + + def test_load_templates_caching(self): + """Test that templates are cached.""" + engine = TemplateEngine() + templates1 = engine.load_templates("python", "default") + templates2 = engine.load_templates("python", "default") + assert templates1 is templates2