diff --git a/tests/test_validators.py b/tests/test_validators.py new file mode 100644 index 0000000..2e0d8d8 --- /dev/null +++ b/tests/test_validators.py @@ -0,0 +1,96 @@ +"""Tests for syntax validation.""" + +import pytest + +from config_convert.validators import validate + + +class TestValidateJSON: + def test_valid_json(self): + is_valid, error = validate('{"name": "test"}', "json") + assert is_valid is True + assert error is None + + def test_valid_json_pretty(self): + is_valid, error = validate('{\n "name": "test\n"}', "json") + assert is_valid is True + + def test_invalid_json(self): + is_valid, error = validate('{invalid}', "json") + assert is_valid is False + assert error is not None + + def test_invalid_json_trailing_comma(self): + is_valid, error = validate('{"a": 1,}', "json") + assert is_valid is False + + +class TestValidateYAML: + def test_valid_yaml(self): + is_valid, error = validate("name: test", "yaml") + assert is_valid is True + assert error is None + + def test_valid_yaml_nested(self): + data = """database: + host: localhost + port: 5432 + """ + is_valid, error = validate(data, "yaml") + assert is_valid is True + + def test_invalid_yaml(self): + is_valid, error = validate("[invalid: yaml", "yaml") + assert is_valid is False + assert error is not None + + +class TestValidateTOML: + def test_valid_toml(self): + is_valid, error = validate('name = "test"', "toml") + assert is_valid is True + assert error is None + + def test_valid_toml_nested(self): + data = """[database] + host = "localhost" + port = 5432 + """ + is_valid, error = validate(data, "toml") + assert is_valid is True + + def test_invalid_toml(self): + is_valid, error = validate('invalid = "toml" extra', "toml") + assert is_valid is False + + +class TestValidateENV: + def test_valid_env(self): + is_valid, error = validate("NAME=test\nVALUE=123", "env") + assert is_valid is True + assert error is None + + def test_valid_env_with_comments(self): + data = "# comment\nNAME=test\n# another comment" + is_valid, error = validate(data, "env") + assert is_valid is True + + def test_valid_env_with_empty_lines(self): + data = "NAME=test\n\nVALUE=123" + is_valid, error = validate(data, "env") + assert is_valid is True + + def test_invalid_env_missing_equals(self): + is_valid, error = validate("NAME test", "env") + assert is_valid is False + + def test_invalid_env_leading_space(self): + is_valid, error = validate(" NAME=test", "env") + assert is_valid is False + + +class TestValidateUnsupported: + def test_unsupported_format(self): + is_valid, error = validate("test", "unsupported") + assert is_valid is False + assert "Unsupported format" in error.message