Coverage for value_object_pattern/usables/internet/semantic_version_value_object.py: 62.07%
27 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-11-21 14:31 +0000
« prev ^ index » next coverage.py v7.15.4, created at 2026-11-21 14:31 +0000
1"""
2Provide a value object for normal semantic versions.
3"""
5from re import Pattern, compile as re_compile 1abcdefghij
6from typing import NoReturn, Self 1abcdefghij
8from value_object_pattern.decorators import validation 1abcdefghij
9from value_object_pattern.usables import NotEmptyStringValueObject, TrimmedStringValueObject 1abcdefghij
12class SemanticVersionValueObject(NotEmptyStringValueObject, TrimmedStringValueObject): 1abcdefghij
13 """
14 Validate a normal semantic version in `major.minor.patch` form. Each component is a non-negative ASCII integer
15 without leading zeros. Pre-release identifiers, build metadata, version prefixes, and surrounding whitespace are
16 not accepted. The original string is preserved, while `major`, `minor`, and `patch` expose integer components.
17 Increment methods return a new instance of the same subclass and preserve error metadata. Increasing minor resets
18 patch; increasing major resets both minor and patch.
20 References:
21 Semantic Versioning 2.0.0: https://semver.org/spec/v2.0.0.html
23 Example:
24 ```python
25 from value_object_pattern.usables.internet import SemanticVersionValueObject
27 version = SemanticVersionValueObject(value='1.2.3')
28 print(version.major)
29 # >>> 1
30 print(version.minor)
31 # >>> 2
32 print(version.patch)
33 # >>> 3
34 ```
35 """
37 _VALIDATION_REGEX: Pattern[str] = re_compile(pattern=r'(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)') 1abcdefghij
39 @property 1abcdefghij
40 def major(self) -> int: 1abcdefghij
41 """
42 Return the major version component as an integer.
44 Returns:
45 int: The major version component.
47 Example:
48 ```python
49 from value_object_pattern.usables.internet import SemanticVersionValueObject
51 version = SemanticVersionValueObject(value='1.2.3')
52 print(version.major)
53 # >>> 1
54 ```
55 """
56 return int(self.value.split('.')[0])
58 @property 1abcdefghij
59 def minor(self) -> int: 1abcdefghij
60 """
61 Return the minor version component as an integer.
63 Returns:
64 int: The minor version component.
66 Example:
67 ```python
68 from value_object_pattern.usables.internet import SemanticVersionValueObject
70 version = SemanticVersionValueObject(value='1.2.3')
71 print(version.minor)
72 # >>> 2
73 ```
74 """
75 return int(self.value.split('.')[1])
77 @property 1abcdefghij
78 def patch(self) -> int: 1abcdefghij
79 """
80 Return the patch version component as an integer.
82 Returns:
83 int: The patch version component.
85 Example:
86 ```python
87 from value_object_pattern.usables.internet import SemanticVersionValueObject
89 version = SemanticVersionValueObject(value='1.2.3')
90 print(version.patch)
91 # >>> 3
92 ```
93 """
94 return int(self.value.split('.')[2])
96 def increase_patch(self) -> Self: 1abcdefghij
97 """
98 Return a new version with patch increased by one, preserving major and minor.
100 Returns:
101 Self: A new instance of the same subclass with the patch version increased by one.
103 Example:
104 ```python
105 from value_object_pattern.usables.internet import SemanticVersionValueObject
107 version = SemanticVersionValueObject(value='1.2.3')
108 new_version = version.increase_patch()
109 print(new_version.value)
110 # >>> '1.2.4'
111 ```
112 """
113 return self.__class__(
114 value=f'{self.major}.{self.minor}.{self.patch + 1}',
115 title=self.title,
116 parameter=self.parameter,
117 )
119 def increase_minor(self) -> Self: 1abcdefghij
120 """
121 Return a new version with minor increased by one and patch reset to zero.
123 Returns:
124 Self: A new instance of the same subclass with the minor version increased by one and patch reset to zero.
126 Example:
127 ```python
128 from value_object_pattern.usables.internet import SemanticVersionValueObject
130 version = SemanticVersionValueObject(value='1.2.3')
131 new_version = version.increase_minor()
132 print(new_version.value)
133 # >>> '1.3.0'
134 ```
135 """
136 return self.__class__(
137 value=f'{self.major}.{self.minor + 1}.0',
138 title=self.title,
139 parameter=self.parameter,
140 )
142 def increase_major(self) -> Self: 1abcdefghij
143 """
144 Return a new version with major increased by one and minor and patch reset to zero.
146 Returns:
147 Self: A new instance of the same subclass with the major version increased by one and minor and patch reset
148 to zero.
150 Example:
151 ```python
152 from value_object_pattern.usables.internet import SemanticVersionValueObject
154 version = SemanticVersionValueObject(value='1.2.3')
155 new_version = version.increase_major()
156 print(new_version.value)
157 # >>> '2.0.0'
158 ```
159 """
160 return self.__class__(
161 value=f'{self.major + 1}.0.0',
162 title=self.title,
163 parameter=self.parameter,
164 )
166 @validation(order=0) 1abcdefghij
167 def _ensure_value_is_semantic_version(self, value: str) -> None: 1abcdefghij
168 """
169 Ensure the value is a normal semantic version.
171 Args:
172 value (str): The provided version string.
174 Raises:
175 ValueError: If the value does not use the `major.minor.patch` format.
176 """
177 if not self._VALIDATION_REGEX.fullmatch(string=value):
178 self._raise_value_is_not_semantic_version(value=value)
180 def _raise_value_is_not_semantic_version(self, value: str) -> NoReturn: 1abcdefghij
181 """
182 Raise an error for an invalid normal semantic version.
184 Args:
185 value (str): The invalid version string.
187 Raises:
188 ValueError: Always raised because the value is not a normal semantic version.
189 """
190 raise ValueError(f'SemanticVersionValueObject value <<<{value}>>> must use major.minor.patch format with non-negative ASCII integers and no leading zeros.') # noqa: E501 # fmt: skip