[rtems-central commit] items: Make Item comparable and hashable

Sebastian Huber sebh at rtems.org
Thu Oct 8 13:25:40 UTC 2020


Module:    rtems-central
Branch:    master
Commit:    8aa5be0c9dfbdc6fac36b4ab00479e9f530ec3f4
Changeset: http://git.rtems.org/rtems-central/commit/?id=8aa5be0c9dfbdc6fac36b4ab00479e9f530ec3f4

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Fri Oct  2 16:31:00 2020 +0200

items: Make Item comparable and hashable

---

 rtemsspec/items.py                 | 13 +++++++++++++
 rtemsspec/tests/test_items_item.py | 21 +++++++++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/rtemsspec/items.py b/rtemsspec/items.py
index f44728c..4565316 100644
--- a/rtemsspec/items.py
+++ b/rtemsspec/items.py
@@ -146,6 +146,19 @@ class Item:
         self._links_to_parents = []  # type: List[Link]
         self._links_to_children = []  # type: List[Link]
 
+    def __eq__(self, other: Any) -> bool:
+        if not isinstance(other, Item):
+            return NotImplemented
+        return self._uid == other._uid  # pylint: disable=protected-access
+
+    def __lt__(self, other: Any) -> bool:
+        if not isinstance(other, Item):
+            return NotImplemented
+        return self._uid < other._uid  # pylint: disable=protected-access
+
+    def __hash__(self) -> int:
+        return hash(self._uid)
+
     def __contains__(self, key: str) -> bool:
         return key in self._data
 
diff --git a/rtemsspec/tests/test_items_item.py b/rtemsspec/tests/test_items_item.py
index a388b9b..0a376d2 100644
--- a/rtemsspec/tests/test_items_item.py
+++ b/rtemsspec/tests/test_items_item.py
@@ -39,6 +39,27 @@ def test_to_abs_uid():
     assert item.to_abs_uid("../../z") == "/z"
 
 
+def test_eq():
+    a = Item(EmptyItemCache(), "a", {})
+    b = Item(EmptyItemCache(), "b", {})
+    assert a == a
+    assert a != b
+    assert a != 0
+
+
+def test_lt():
+    a = Item(EmptyItemCache(), "a", {})
+    b = Item(EmptyItemCache(), "b", {})
+    assert a < b
+    with pytest.raises(TypeError):
+        b = a < 0
+
+
+def test_hash():
+    a = Item(EmptyItemCache(), "a", {})
+    assert hash(a) == hash("a")
+
+
 def test_uid():
     item = Item(EmptyItemCache(), "x", {})
     assert item.uid == "x"



More information about the vc mailing list