Skip to content

Commit e9809b9

Browse files
committed
Merge pull request python-semver#29 from robi-wan/match_not_equal
Support matching 'not equal' with '!='
2 parents d6daf25 + becd708 commit e9809b9

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

semver.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,22 @@ def compare_by_keys(d1, d2):
7676

7777
def match(version, match_expr):
7878
prefix = match_expr[:2]
79-
if prefix in ('>=', '<=', '=='):
79+
if prefix in ('>=', '<=', '==', '!='):
8080
match_version = match_expr[2:]
81-
elif prefix and prefix[0] in ('>', '<', '='):
81+
elif prefix and prefix[0] in ('>', '<'):
8282
prefix = prefix[0]
8383
match_version = match_expr[1:]
8484
else:
8585
raise ValueError("match_expr parameter should be in format <op><ver>, "
86-
"where <op> is one of ['<', '>', '==', '<=', '>=']. "
86+
"where <op> is one of "
87+
"['<', '>', '==', '<=', '>=', '!=']. "
8788
"You provided: %r" % match_expr)
8889

8990
possibilities_dict = {
9091
'>': (1,),
9192
'<': (-1,),
9293
'==': (0,),
94+
'!=': (-1, 1),
9395
'>=': (0, 1),
9496
'<=': (-1, 0)
9597
}

tests.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,31 @@ def test_should_no_match_simple():
4949
assert match("2.3.7", ">=2.3.8") is False
5050

5151

52+
def test_should_match_not_equal():
53+
assert match("2.3.7", "!=2.3.8") is True
54+
assert match("2.3.7", "!=2.3.6") is True
55+
assert match("2.3.7", "!=2.3.7") is False
56+
57+
58+
def test_should_not_raise_value_error_for_expected_match_expression():
59+
assert match("2.3.7", "<2.4.0") is True
60+
assert match("2.3.7", ">2.3.5") is True
61+
62+
assert match("2.3.7", "<=2.3.9") is True
63+
assert match("2.3.7", ">=2.3.5") is True
64+
assert match("2.3.7", "==2.3.7") is True
65+
assert match("2.3.7", "!=2.3.7") is False
66+
67+
68+
def test_should_raise_value_error_for_unexpected_match_expression():
69+
with pytest.raises(ValueError):
70+
match("2.3.7", "=2.3.7")
71+
with pytest.raises(ValueError):
72+
match("2.3.7", "~2.3.7")
73+
with pytest.raises(ValueError):
74+
match("2.3.7", "^2.3.7")
75+
76+
5277
def test_should_raise_value_error_for_zero_prefixed_versions():
5378
with pytest.raises(ValueError):
5479
parse("01.2.3")

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy