Skip to content

Commit

Permalink
make nuget version hashable
Browse files Browse the repository at this point in the history
Signed-off-by: Keshav Priyadarshi <git@keshav.space>
  • Loading branch information
keshav-space committed Aug 2, 2022
1 parent f06c9af commit 3c2dd92
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/univers/nuget.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ def __lt__(self, other):
# Revision is the same, so ignore it for comparison purposes.
return self._base_semver < other._base_semver

def __hash__(self):
return hash(
(
self._base_semver.to_tuple(),
self._revision,
)
)

@classmethod
def from_string(cls, str_version):
if not str_version:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_nuget.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import unittest
from univers.versions import NugetVersion
from univers.nuget import Version


class NuGetTest(unittest.TestCase):
Expand Down Expand Up @@ -76,3 +77,13 @@ def test_less(self):
self.check_order(self.assertLess, "1.0.0-pre", "1.0.0.1-alpha")
self.check_order(self.assertLess, "1.0.0", "1.0.0.1-alpha")
self.check_order(self.assertLess, "0.9.9.1", "1.0.0")

def test_NugetVersion_hash(self):
vers1 = NugetVersion("1.0.1+23")
vers2 = NugetVersion("1.0.1+23")
assert hash(vers1) == hash(vers2)

def test_nuget_semver_hash(self):
vers1 = Version.from_string("51.0.0+2")
vers2 = Version.from_string("51.0.0+2")
assert hash(vers1) == hash(vers2)
16 changes: 16 additions & 0 deletions tests/test_python_semver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#
# Copyright (c) nexB Inc. and others.
# SPDX-License-Identifier: Apache-2.0
#
# Visit https://aboutcode.org and https://github.com/nexB/univers for support and download.

from unittest import TestCase
import semver


class TestPythonSemver(TestCase):
def test_semver_hash(self):
# python-semver doesn't consider build while hashing
vers1 = semver.VersionInfo.parse("1.2.3")
vers2 = semver.VersionInfo.parse("1.2.3+1")
assert hash(vers1) == hash(vers2)

0 comments on commit 3c2dd92

Please sign in to comment.