From 2ee4cbf2ef5e6046979283c26457f0ec41aea0db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominic=20Steinh=C3=B6fel?= Date: Wed, 22 Dec 2021 10:24:28 +0100 Subject: [PATCH] Changed equals(): Can compare to lists and sets --- .github/workflows/test_implementation.yml | 30 +++++++++++++++++++++++ orderedset/orderedset.py | 8 ++++-- orderedset/tests/test_orderedset.py | 6 +++++ requirements.txt | 10 ++++++++ 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/test_implementation.yml diff --git a/.github/workflows/test_implementation.yml b/.github/workflows/test_implementation.yml new file mode 100644 index 0000000..602a7d8 --- /dev/null +++ b/.github/workflows/test_implementation.yml @@ -0,0 +1,30 @@ +name: Test toolchain for all examples + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + workflow_dispatch: + +jobs: + test: + + runs-on: ubuntu-latest + env: + DISPLAY: ':99' + steps: + - uses: actions/checkout@v2 + - name: Set up Python 3.10 + uses: actions/setup-python@v2 + with: + python-version: "3.10" + - name: Test with pytest + run: | + python3 -m pytest --html=report.html --self-contained-html + - name: Upload report + if: always() + uses: actions/upload-artifact@v2 + with: + name: Test Report + path: report.html \ No newline at end of file diff --git a/orderedset/orderedset.py b/orderedset/orderedset.py index 0ebc13e..55ef03a 100644 --- a/orderedset/orderedset.py +++ b/orderedset/orderedset.py @@ -14,8 +14,12 @@ def __init__(self, base: Optional[Union[Dict[T, None], Iterable[T]]] = None): else: self.the_dict = dict.fromkeys(base) - def __eq__(self, o: object) -> bool: - return isinstance(o, OrderedSet) and list(self.the_dict) == list(o.the_dict) + def __eq__(self, other: object) -> bool: + if isinstance(other, (OrderedSet, list)): + return len(self) == len(other) and list(self) == list(other) + elif isinstance(other, Set): + return set(self) == set(other) + return NotImplemented def __ne__(self, o: object) -> bool: return not self.__eq__(o) diff --git a/orderedset/tests/test_orderedset.py b/orderedset/tests/test_orderedset.py index b9dbf46..1b777f8 100644 --- a/orderedset/tests/test_orderedset.py +++ b/orderedset/tests/test_orderedset.py @@ -13,6 +13,12 @@ def test_tolist(self): def test_eq(self): self.assertTrue(OrderedSet([1, 2, 3]) == OrderedSet([1, 2, 3])) + def test_eq_set(self): + self.assertTrue({1, 2, 3} == OrderedSet([3, 1, 2])) + + def test_eq_list(self): + self.assertTrue([3, 1, 2] == OrderedSet([3, 1, 2])) + def test_ne(self): self.assertTrue(OrderedSet([1, 2, 3]) != OrderedSet([3, 2, 1])) diff --git a/requirements.txt b/requirements.txt index e69de29..1c75f0b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -0,0 +1,10 @@ +attrs>=21.2.0 +iniconfig>=1.1.1 +packaging>=21.3 +pluggy>=1.0.0 +py>=1.11.0 +pyparsing>=3.0.6 +pytest>=6.2.5 +pytest-html>=3.1.1 +pytest-metadata>=1.11.0 +toml>=0.10.2