import tempfile from pathlib import Path from src.config import Config class TestConfig: def test_init_defaults(self): config = Config() assert config.custom_templates == {} assert config.default_types == [] assert config.exclude_patterns == [] assert config.include_patterns == {} def test_init_with_values(self): config = Config( custom_templates={"test": "# test"}, default_types=["python"], exclude_patterns=["*.pyc"], include_patterns={"python": ["src/"]}, ) assert config.custom_templates == {"test": "# test"} assert config.default_types == ["python"] assert config.exclude_patterns == ["*.pyc"] assert config.include_patterns == {"python": ["src/"]} def test_load_valid_yaml(self): with tempfile.NamedTemporaryFile( mode="w", suffix=".yaml", delete=False ) as f: f.write("custom_templates:\n test: '# test'\n") f.write("default_types:\n - python\n") f.flush() config = Config.load(f.name) assert config.custom_templates == {"test": "# test"} assert config.default_types == ["python"] Path(f.name).unlink() def test_load_file_not_found(self): with pytest.raises(FileNotFoundError): Config.load("/nonexistent/path/config.yaml") def test_load_invalid_yaml(self): with tempfile.NamedTemporaryFile( mode="w", suffix=".yaml", delete=False ) as f: f.write("invalid: yaml: content: [[[") f.flush() with pytest.raises(ValueError): Config.load(f.name) Path(f.name).unlink() def test_load_not_dict(self): with tempfile.NamedTemporaryFile( mode="w", suffix=".yaml", delete=False ) as f: f.write("- item1\n- item2\n") f.flush() with pytest.raises(ValueError): Config.load(f.name) Path(f.name).unlink() def test_save(self): config = Config( custom_templates={"test": "# test"}, default_types=["python"], ) with tempfile.NamedTemporaryFile( mode="w", suffix=".yaml", delete=False ) as f: config.save(f.name) content = Path(f.name).read_text() assert "test:" in content assert "python" in content Path(f.name).unlink() def test_get(self): config = Config(custom_templates={"key": "value"}) assert config.get("custom_templates") == {"key": "value"} assert config.get("nonexistent") is None assert config.get("nonexistent", "default") == "default" def test_set(self): config = Config() config.set("default_types", ["python"]) assert config.default_types == ["python"] def test_merge(self): config1 = Config( custom_templates={"t1": "# test1"}, default_types=["python"], exclude_patterns=["*.pyc"], ) config2 = Config( custom_templates={"t2": "# test2"}, default_types=["javascript"], include_patterns={"python": ["src/"]}, ) merged = config1.merge(config2) assert "t1" in merged.custom_templates assert "t2" in merged.custom_templates assert merged.default_types == ["python"] assert "*.pyc" in merged.exclude_patterns assert "src/" in merged.include_patterns.get("python", []) def test_validate_valid(self): config = Config( custom_templates={"test": "# test"}, default_types=["python"], exclude_patterns=["*.pyc"], include_patterns={"python": ["src/"]}, ) errors = config.validate() assert len(errors) == 0 def test_validate_invalid_custom_templates(self): config = Config(custom_templates=123) errors = config.validate() assert any("custom_templates" in e for e in errors) def test_validate_invalid_default_types(self): config = Config(default_types="python") errors = config.validate() assert any("default_types" in e for e in errors) def test_validate_invalid_exclude_patterns(self): config = Config(exclude_patterns="*.pyc") errors = config.validate() assert any("exclude_patterns" in e for e in errors) def test_validate_invalid_include_patterns(self): config = Config(include_patterns="invalid") errors = config.validate() assert any("include_patterns" in e for e in errors) def test_to_dict(self): config = Config( custom_templates={"test": "# test"}, default_types=["python"], ) data = config.to_dict() assert isinstance(data, dict) assert data["custom_templates"] == {"test": "# test"} assert data["default_types"] == ["python"] def test_from_dict(self): data = { "custom_templates": {"test": "# test"}, "default_types": ["python"], } config = Config.from_dict(data) assert config.custom_templates == {"test": "# test"} assert config.default_types == ["python"] import pytest