"""Tests for the validator module.""" import pytest from configconverter.validators import Validator from configconverter.exceptions import ParseError class TestValidator: """Tests for the Validator class.""" @pytest.fixture def validator(self): return Validator() def test_valid_json(self, validator): content = '{"name": "test", "value": 123}' is_valid, error = validator.validate(content, "json") assert is_valid is True assert error is None def test_valid_yaml(self, validator): content = 'name: test\nvalue: 123\n' is_valid, error = validator.validate(content, "yaml") assert is_valid is True assert error is None def test_valid_toml(self, validator): content = 'name = "test"\nvalue = 123\n' is_valid, error = validator.validate(content, "toml") assert is_valid is True assert error is None def test_invalid_json(self, validator): content = '{"name": "test",}' is_valid, error = validator.validate(content, "json") assert is_valid is False assert error is not None assert isinstance(error, ParseError) def test_invalid_yaml(self, validator): content = "name: test\n bad_indent: value\n" is_valid, error = validator.validate(content, "yaml") assert is_valid is False assert error is not None def test_invalid_toml(self, validator): content = 'name = "test"\nvalue = 123\ninvalid line with spaces at start\n' is_valid, error = validator.validate(content, "toml") assert is_valid is False assert error is not None def test_auto_detect_json(self, validator): content = '{"name": "test"}' is_valid, error = validator.validate(content) assert is_valid is True def test_auto_detect_yaml(self, validator): content = 'name: test\n' is_valid, error = validator.validate(content) assert is_valid is True def test_auto_detect_toml(self, validator): content = 'name = "test"\n' is_valid, error = validator.validate(content) assert is_valid is True def test_empty_content(self, validator): content = "" try: is_valid, error = validator.validate(content) assert is_valid is True except Exception: pass def test_whitespace_content(self, validator): content = " \n\t\n " try: is_valid, error = validator.validate(content) assert is_valid is True except Exception: pass def test_validate_file(self, validator, tmp_path): file_path = tmp_path / "test.json" file_path.write_text('{"name": "test"}') is_valid, error = validator.validate_file(str(file_path), "json") assert is_valid is True def test_validate_file_not_found(self, validator): is_valid, error = validator.validate_file("/nonexistent/path.json", "json") assert is_valid is False assert error is not None def test_complex_json_structure(self, validator): content = ''' { "server": { "host": "localhost", "port": 8080, "ssl": { "enabled": true, "cert": "/path/to/cert" } }, "databases": [ {"name": "primary", "replica": false}, {"name": "replica", "replica": true} ] } ''' is_valid, error = validator.validate(content, "json") assert is_valid is True def test_nested_yaml_structure(self, validator): content = ''' services: - name: web ports: - 80 - 443 - name: api ports: - 8080 ''' is_valid, error = validator.validate(content, "yaml") assert is_valid is True