Skip to content

Commit 2bec4bf

Browse files
committed
Remove duplicate tests
Add unit tests for each kind Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent 54facef commit 2bec4bf

3 files changed

Lines changed: 46 additions & 23 deletions

File tree

src/univers/version_constraint.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,28 @@ def __lt__(self, other):
132132
# we compare tuples, version first
133133
return (self.version, self.comparator).__lt__((other.version, other.comparator))
134134

135+
def is_star(self):
136+
return self.comparator == "*"
137+
138+
def invert(self):
139+
INVERTED_COMPARATORS = {
140+
">=": "<",
141+
"<=": ">",
142+
"!=": "=",
143+
"<": ">=",
144+
">": "<=",
145+
"=": "!=",
146+
}
147+
148+
if self.is_star():
149+
return None
150+
151+
inverted_comparator = INVERTED_COMPARATORS[self.comparator]
152+
return self.__class__(
153+
comparator=inverted_comparator,
154+
version=self.version,
155+
)
156+
135157
@classmethod
136158
def from_string(cls, string, version_class):
137159
"""

src/univers/version_range.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -174,31 +174,25 @@ def from_versions(cls, sequence):
174174
constraints.append(constraint)
175175
return cls(constraints=constraints)
176176

177+
def is_star(self):
178+
return len(self.constraints) == 1 and self.constraints[0].is_star()
179+
177180
def invert(self):
178181
"""
179-
Return the inverse of this VersionRange. For example, if this range is
182+
Return the inverse or complement of this VersionRange. For example, if this range is
180183
">=1.0.0", the inverse is "<1.0.0".
181-
>>> VersionRange.from_string("vers:npm/>=1.0.0").invert()
182-
NpmVersionRange(constraints=(VersionConstraint(comparator='<', version=SemverVersion(string='1.0.0')),))
184+
>>> str(VersionRange.from_string("vers:npm/>=1.0.0").invert())
185+
'vers:npm/<1.0.0'
183186
"""
184187
inverted_constraints = []
185188

186-
if len(self.constraints) == 1 and self.constraints[0].comparator == "*":
189+
if self.is_star():
187190
# The inverse of "*" is an empty range.
188191
return None
189192

190193
for constraint in self.constraints:
191-
if constraint.comparator in INVERTED_COMPARATORS:
192-
inverted_comparator = INVERTED_COMPARATORS[constraint.comparator]
193-
else:
194-
raise NotImplementedError(
195-
f"Cannot invert a range with a {constraint.comparator!r} comparator."
196-
)
197-
inverted_constraint = VersionConstraint(
198-
comparator=inverted_comparator,
199-
version=constraint.version,
200-
)
201-
inverted_constraints.append(inverted_constraint)
194+
inverted_constraints.append(constraint.invert())
195+
202196
return self.__class__(constraints=inverted_constraints)
203197

204198
def __str__(self):

tests/test_version_range.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -395,11 +395,18 @@ def test_npm_advisory_version_range_parse(test_case):
395395
assert str(result) == test_case["expected_vers"]
396396

397397

398-
@pytest.mark.parametrize("test_case", json.load(open("./tests/data/inverse-data.json")))
399-
def test_inverse(test_case):
400-
result = NpmVersionRange.from_string(test_case["original"])
401-
if test_case["inverted"]:
402-
inverted = NpmVersionRange.from_string(test_case["inverted"])
403-
assert result.invert() == inverted
404-
else:
405-
assert result.invert() is None
398+
def test_inverse():
399+
vers_with_equal_operator = VersionRange.from_string("vers:gem/1.0")
400+
assert str(vers_with_equal_operator.invert()) == "vers:gem/!=1.0"
401+
assert VersionRange.from_string("vers:gem/!=1.0").invert() == vers_with_equal_operator
402+
403+
vers_with_less_than_operator = VersionRange.from_string("vers:gem/<1.0")
404+
assert str(vers_with_less_than_operator.invert()) == "vers:gem/>=1.0"
405+
assert VersionRange.from_string("vers:gem/>=1.0").invert() == vers_with_less_than_operator
406+
407+
vers_with_greater_than_operator = VersionRange.from_string("vers:gem/>1.0")
408+
assert str(vers_with_greater_than_operator.invert()) == "vers:gem/<=1.0"
409+
assert VersionRange.from_string("vers:gem/<=1.0").invert() == vers_with_greater_than_operator
410+
411+
vers_with_complex_constraints = VersionRange.from_string("vers:gem/<=1.0|>=3.0|<4.0|!=5.0")
412+
assert str(vers_with_complex_constraints.invert()) == "vers:gem/>1.0|<3.0|>=4.0|5.0"

0 commit comments

Comments
 (0)