Skip to content

Commit 637d362

Browse files
committed
Add tests and docstring for VersionConstraint
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent 4218340 commit 637d362

3 files changed

Lines changed: 36 additions & 1 deletion

File tree

src/univers/version_constraint.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ def is_star(self):
136136
return self.comparator == "*"
137137

138138
def invert(self):
139+
"""
140+
Return a new VersionConstraint instance with the comparator inverted.
141+
For example::
142+
>>> assert str(VersionConstraint(comparator=">=", version=Version("2.3")).invert()) == "<2.3"
143+
"""
139144
INVERTED_COMPARATORS = {
140145
">=": "<",
141146
"<=": ">",

tests/test_version_constraint.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,30 @@ def test_semver_comparison(version, spec, expected):
6060
version_class=versions.SemverVersion,
6161
)
6262
assert (version in constraint) is expected
63+
64+
65+
@pytest.mark.parametrize(
66+
"original, inverted",
67+
[
68+
(">2.7.1", "<=2.7.1"),
69+
("!=1.1.0", "=1.1.0"),
70+
("=2.0.0", "!=2.0.0"),
71+
("<=0.9999.9999", ">0.9999.9999"),
72+
(">=0.2.9", "<0.2.9"),
73+
("<1.9999.9999", ">=1.9999.9999"),
74+
("*", None),
75+
],
76+
)
77+
def test_invert_opertaion(original, inverted):
78+
constraint = VersionConstraint.from_string(
79+
string=original,
80+
version_class=versions.SemverVersion,
81+
)
82+
if inverted:
83+
inverted_constraint = VersionConstraint.from_string(
84+
string=inverted,
85+
version_class=versions.SemverVersion,
86+
)
87+
assert constraint.invert() == inverted_constraint
88+
else:
89+
assert constraint.invert() is None

tests/test_version_range.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ def test_npm_advisory_version_range_parse(test_case):
395395
assert str(result) == test_case["expected_vers"]
396396

397397

398-
def test_inverse():
398+
def test_invert():
399399
vers_with_equal_operator = VersionRange.from_string("vers:gem/1.0")
400400
assert str(vers_with_equal_operator.invert()) == "vers:gem/!=1.0"
401401
assert VersionRange.from_string("vers:gem/!=1.0").invert() == vers_with_equal_operator
@@ -410,3 +410,6 @@ def test_inverse():
410410

411411
vers_with_complex_constraints = VersionRange.from_string("vers:gem/<=1.0|>=3.0|<4.0|!=5.0")
412412
assert str(vers_with_complex_constraints.invert()) == "vers:gem/>1.0|<3.0|>=4.0|5.0"
413+
414+
vers_with_star_operator = VersionRange.from_string("vers:gem/*")
415+
assert vers_with_star_operator.invert() == None

0 commit comments

Comments
 (0)