From b5f69d408e6c8d61de074fd48dcfed151e7669fe Mon Sep 17 00:00:00 2001 From: robi-wan Date: Mon, 23 May 2016 08:47:13 +0200 Subject: [PATCH] Support matching 'not equal' with '!=' Remove check for unused/unsupported match expression '='. --- semver.py | 7 ++++--- tests.py | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/semver.py b/semver.py index d068317..7fdae5b 100644 --- a/semver.py +++ b/semver.py @@ -76,20 +76,21 @@ def compare(ver1, ver2): def match(version, match_expr): prefix = match_expr[:2] - if prefix in ('>=', '<=', '=='): + if prefix in ('>=', '<=', '==', '!='): match_version = match_expr[2:] - elif prefix and prefix[0] in ('>', '<', '='): + elif prefix and prefix[0] in ('>', '<'): prefix = prefix[0] match_version = match_expr[1:] else: raise ValueError("match_expr parameter should be in format , " - "where is one of ['<', '>', '==', '<=', '>=']. " + "where is one of ['<', '>', '==', '<=', '>=', '!=']. " "You provided: %r" % match_expr) possibilities_dict = { '>': (1,), '<': (-1,), '==': (0,), + '!=': (-1, 1), '>=': (0, 1), '<=': (-1, 0) } diff --git a/tests.py b/tests.py index 253b371..9d4f1a7 100644 --- a/tests.py +++ b/tests.py @@ -49,6 +49,31 @@ def test_should_no_match_simple(): assert match("2.3.7", ">=2.3.8") is False +def test_should_match_not_equal(): + assert match("2.3.7", "!=2.3.8") is True + assert match("2.3.7", "!=2.3.6") is True + assert match("2.3.7", "!=2.3.7") is False + + +def test_should_not_raise_value_error_for_expected_match_expression(): + assert match("2.3.7", "<2.4.0") is True + assert match("2.3.7", ">2.3.5") is True + + assert match("2.3.7", "<=2.3.9") is True + assert match("2.3.7", ">=2.3.5") is True + assert match("2.3.7", "==2.3.7") is True + assert match("2.3.7", "!=2.3.7") is False + + +def test_should_raise_value_error_for_unexpected_match_expression(): + with pytest.raises(ValueError): + match("2.3.7", "=2.3.7") + with pytest.raises(ValueError): + match("2.3.7", "~2.3.7") + with pytest.raises(ValueError): + match("2.3.7", "^2.3.7") + + def test_should_raise_value_error_for_zero_prefixed_versions(): with pytest.raises(ValueError): parse("01.2.3")