Skip to content

Commit 3c2dd92

Browse files
committed
make nuget version hashable
Signed-off-by: Keshav Priyadarshi <[email protected]>
1 parent f06c9af commit 3c2dd92

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/univers/nuget.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,14 @@ def __lt__(self, other):
175175
# Revision is the same, so ignore it for comparison purposes.
176176
return self._base_semver < other._base_semver
177177

178+
def __hash__(self):
179+
return hash(
180+
(
181+
self._base_semver.to_tuple(),
182+
self._revision,
183+
)
184+
)
185+
178186
@classmethod
179187
def from_string(cls, str_version):
180188
if not str_version:

tests/test_nuget.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import unittest
2121
from univers.versions import NugetVersion
22+
from univers.nuget import Version
2223

2324

2425
class NuGetTest(unittest.TestCase):
@@ -76,3 +77,13 @@ def test_less(self):
7677
self.check_order(self.assertLess, "1.0.0-pre", "1.0.0.1-alpha")
7778
self.check_order(self.assertLess, "1.0.0", "1.0.0.1-alpha")
7879
self.check_order(self.assertLess, "0.9.9.1", "1.0.0")
80+
81+
def test_NugetVersion_hash(self):
82+
vers1 = NugetVersion("1.0.1+23")
83+
vers2 = NugetVersion("1.0.1+23")
84+
assert hash(vers1) == hash(vers2)
85+
86+
def test_nuget_semver_hash(self):
87+
vers1 = Version.from_string("51.0.0+2")
88+
vers2 = Version.from_string("51.0.0+2")
89+
assert hash(vers1) == hash(vers2)

tests/test_python_semver.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#
2+
# Copyright (c) nexB Inc. and others.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
# Visit https://aboutcode.org and https://github.com/nexB/univers for support and download.
6+
7+
from unittest import TestCase
8+
import semver
9+
10+
11+
class TestPythonSemver(TestCase):
12+
def test_semver_hash(self):
13+
# python-semver doesn't consider build while hashing
14+
vers1 = semver.VersionInfo.parse("1.2.3")
15+
vers2 = semver.VersionInfo.parse("1.2.3+1")
16+
assert hash(vers1) == hash(vers2)

0 commit comments

Comments
 (0)