summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Huber <sebastian.huber@embedded-brains.de>2020-10-02 16:31:00 +0200
committerSebastian Huber <sebastian.huber@embedded-brains.de>2020-10-07 11:31:07 +0200
commit8aa5be0c9dfbdc6fac36b4ab00479e9f530ec3f4 (patch)
tree94644f2cc3a4234789cf18357d79bb28f3b17f17
parentcontent: Improve automatically generated warning (diff)
downloadrtems-central-8aa5be0c9dfbdc6fac36b4ab00479e9f530ec3f4.tar.bz2
items: Make Item comparable and hashable
-rw-r--r--rtemsspec/items.py13
-rw-r--r--rtemsspec/tests/test_items_item.py21
2 files changed, 34 insertions, 0 deletions
diff --git a/rtemsspec/items.py b/rtemsspec/items.py
index f44728cd..45653164 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 a388b9bd..0a376d23 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"