-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathtest_subclass.py
More file actions
71 lines (55 loc) · 2.25 KB
/
test_subclass.py
File metadata and controls
71 lines (55 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from semver import Version
import pytest
def test_subclass_from_versioninfo():
class SemVerWithVPrefix(Version):
@classmethod
def parse(cls, version):
if not version[0] in ("v", "V"):
raise ValueError(
"{v!r}: version must start with 'v' or 'V'".format(v=version)
)
return super().parse(version[1:])
def __str__(self):
# Reconstruct the tag.
return "v" + super().__str__()
v = SemVerWithVPrefix.parse("v1.2.3")
assert str(v) == "v1.2.3"
def test_replace_from_subclass():
# Issue#426
# Taken from the example "Creating Subclasses from Version"
class SemVerWithVPrefix(Version):
"""
A subclass of Version which allows a "v" prefix
"""
@classmethod
def parse(cls, version: str) -> "SemVerWithVPrefix":
"""
Parse version string to a Version instance.
:param version: version string with "v" or "V" prefix
:raises ValueError: when version does not start with "v" or "V"
:return: a new instance
"""
if not version[0] in ("v", "V"):
raise ValueError(
f"{version!r}: not a valid semantic version tag. "
"Must start with 'v' or 'V'"
)
return super().parse(version[1:], optional_minor_and_patch=True)
def __str__(self) -> str:
# Reconstruct the tag
return "v" + super().__str__()
version = SemVerWithVPrefix.parse("v1.1.0")
dev_version = version.replace(prerelease="dev.0")
assert str(dev_version) == "v1.1.0-dev.0"
def test_compare_with_subclass():
class SemVerSubclass(Version):
pass
with pytest.raises(TypeError):
SemVerSubclass.parse("1.0.0").compare(Version.parse("1.0.0"))
assert Version.parse("1.0.0").compare(SemVerSubclass.parse("1.0.0")) == 0
assert (
SemVerSubclass.parse("1.0.0").__eq__(Version.parse("1.0.0")) is NotImplemented
)
assert Version.parse("1.0.0").__eq__(SemVerSubclass.parse("1.0.0")) is True
assert SemVerSubclass.parse("1.0.0") == Version.parse("1.0.0")
assert Version.parse("1.0.0") == SemVerSubclass.parse("1.0.0")