|
| 1 | +from dataclasses import dataclass |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from pytest_examples.config import ExamplesConfig |
| 7 | + |
| 8 | + |
| 9 | +@dataclass |
| 10 | +class TargetVersionTestCase: |
| 11 | + id: str |
| 12 | + target_version: Any |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.parametrize( |
| 16 | + 'case', |
| 17 | + [ |
| 18 | + # Valid target versions |
| 19 | + TargetVersionTestCase('py37', 'py37'), |
| 20 | + TargetVersionTestCase('py38', 'py38'), |
| 21 | + TargetVersionTestCase('py39', 'py39'), |
| 22 | + TargetVersionTestCase('py310', 'py310'), |
| 23 | + TargetVersionTestCase('py311', 'py311'), |
| 24 | + TargetVersionTestCase('py312', 'py312'), |
| 25 | + TargetVersionTestCase('py313', 'py313'), |
| 26 | + TargetVersionTestCase('py314', 'py314'), |
| 27 | + TargetVersionTestCase('py3100', 'py3100'), |
| 28 | + ], |
| 29 | + ids=lambda case: case.id, |
| 30 | +) |
| 31 | +def test_examples_config_valid_target_version(case: TargetVersionTestCase): |
| 32 | + """Test that ExamplesConfig validates target_version correctly during initialization.""" |
| 33 | + config = ExamplesConfig(target_version=case.target_version) |
| 34 | + assert config.target_version == case.target_version |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.parametrize( |
| 38 | + 'case', |
| 39 | + [ |
| 40 | + TargetVersionTestCase('missing_py', '37'), |
| 41 | + TargetVersionTestCase('python_word', 'python37'), |
| 42 | + TargetVersionTestCase('single_digit', 'py3'), |
| 43 | + TargetVersionTestCase('dots', 'py3.7'), |
| 44 | + TargetVersionTestCase('spaces', 'py 37'), |
| 45 | + TargetVersionTestCase('uppercase', 'PY37'), |
| 46 | + TargetVersionTestCase('mixed_case', 'Py37'), |
| 47 | + TargetVersionTestCase('letters_before_digits', 'py3a7'), |
| 48 | + TargetVersionTestCase('hyphen', 'py-37'), |
| 49 | + TargetVersionTestCase('underscore', 'py_37'), |
| 50 | + TargetVersionTestCase('suffix', 'py37!'), |
| 51 | + TargetVersionTestCase('text_suffix', 'py37abc'), |
| 52 | + ], |
| 53 | + ids=lambda case: case.id, |
| 54 | +) |
| 55 | +def test_examples_config_invalid_target_version(case: TargetVersionTestCase): |
| 56 | + """Test that ExamplesConfig validates target_version correctly during initialization.""" |
| 57 | + with pytest.raises(ValueError, match=f'Invalid target version: {case.target_version!r}'): |
| 58 | + ExamplesConfig(target_version=case.target_version) |
| 59 | + |
| 60 | + |
| 61 | +def test_examples_config_empty_string_target_version(): |
| 62 | + """Test that empty string target_version is accepted without validation.""" |
| 63 | + # Based on the validation logic, empty string should not raise an error |
| 64 | + # because the check is 'if self.target_version' which is falsy for empty string |
| 65 | + config = ExamplesConfig(target_version='') |
| 66 | + assert config.target_version == '' |
| 67 | + |
| 68 | + |
| 69 | +def test_examples_config_target_version_error_message(): |
| 70 | + """Test that the error message includes the expected format.""" |
| 71 | + with pytest.raises(ValueError, match='must be like "py37"'): |
| 72 | + ExamplesConfig(target_version='invalid') |
0 commit comments