diff --git a/tests/test_template_loader.py b/tests/test_template_loader.py new file mode 100644 index 0000000..09281a3 --- /dev/null +++ b/tests/test_template_loader.py @@ -0,0 +1,115 @@ +"""Tests for template loader module.""" + +import pytest + +from gitignore_cli.template_loader import TemplateLoader, TemplateInfo + + +class TestTemplateInfo: + """Tests for TemplateInfo class.""" + + def test_template_info_creation(self): + """Test creating a TemplateInfo instance.""" + template = TemplateInfo( + name="python", + category="language", + description="Python development", + patterns="__pycache__/\n*.pyc", + ) + + assert template.name == "python" + assert template.category == "language" + assert template.description == "Python development" + assert "__pycache__/" in template.patterns + + def test_template_info_repr(self): + """Test TemplateInfo string representation.""" + template = TemplateInfo( + name="python", + category="language", + description="Python development", + patterns="", + ) + + repr_str = repr(template) + assert "python" in repr_str + assert "language" in repr_str + + +class TestTemplateLoader: + """Tests for TemplateLoader class.""" + + def test_load_single_template(self, template_loader: TemplateLoader): + """Test loading a single template.""" + template = template_loader.load_template("python") + + assert template is not None + assert template.name == "python" + assert template.category == "language" + + def test_load_nonexistent_template(self, template_loader: TemplateLoader): + """Test loading a template that doesn't exist.""" + template = template_loader.load_template("nonexistent") + assert template is None + + def test_load_all_templates(self, template_loader: TemplateLoader): + """Test loading all templates.""" + templates = template_loader.load_all_templates() + + assert len(templates) == 3 + assert "python" in templates + assert "nodejs" in templates + assert "vscode" in templates + + def test_get_templates_by_category(self, template_loader: TemplateLoader): + """Test grouping templates by category.""" + by_category = template_loader.get_templates_by_category() + + assert "language" in by_category + assert "ide" in by_category + + assert len(by_category["language"]) == 2 + assert len(by_category["ide"]) == 1 + + def test_list_template_names(self, template_loader: TemplateLoader): + """Test listing template names.""" + names = template_loader.list_template_names() + + assert len(names) == 3 + assert "python" in names + assert "nodejs" in names + assert "vscode" in names + + def test_get_template_info(self, template_loader: TemplateLoader): + """Test getting template info.""" + info = template_loader.get_template_info("python") + + assert info is not None + assert info.name == "python" + + def test_get_similar_templates(self, template_loader: TemplateLoader): + """Test finding similar template names.""" + similar = template_loader.get_similar_templates("python") + + assert "python" not in similar + assert len(similar) >= 0 + + def test_levenshtein_distance(self, template_loader: TemplateLoader): + """Test Levenshtein distance calculation.""" + distance = template_loader._levenshtein_distance("python", "pyton") + assert distance == 1 + + distance = template_loader._levenshtein_distance("python", "java") + assert distance > 1 + + def test_get_template_path(self, template_loader: TemplateLoader): + """Test getting template file path.""" + path = template_loader.get_template_path("python") + + assert path is not None + assert path.name == "python.yaml" + + def test_get_nonexistent_template_path(self, template_loader: TemplateLoader): + """Test getting path for nonexistent template.""" + path = template_loader.get_template_path("nonexistent") + assert path is None