Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions tests/test_variable_set_equality.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import typing as t
import unittest

from uritemplate import URITemplate
from uritemplate import variables
from uritemplate.orderedset import OrderedSet


class TestVariableSetEquality(unittest.TestCase):
def test_builtin_set_equality(self) -> None:
cases = [
("", set(), True),
("{a}", {"a"}, True),
("{a,b}", {"a", "b"}, True),
("{a}", {"a", "b"}, False),
("{a,b}", {"a"}, False),
("{a,b}", {"a", "c"}, False),
("{a}", {"b"}, False),
("", {"a"}, False),
("{a}", set(), False),
]
for uri, names, expected in cases:
for operand in (names, frozenset(names)):
with self.subTest(uri=uri, names=operand):
result = variables(uri)
self.assertEqual(result == operand, expected)
self.assertEqual(operand == result, expected)
self.assertEqual(result != operand, not expected)
self.assertEqual(
URITemplate(uri).variable_names == operand, expected
)

def test_ordered_comparison_keeps_order(self) -> None:
self.assertEqual(variables("{a,b}"), OrderedSet(["a", "b"]))
self.assertNotEqual(variables("{a,b}"), OrderedSet(["b", "a"]))
self.assertEqual(variables(""), OrderedSet())

def test_iterable_comparison(self) -> None:
for names, expected in [
(["a", "a"], True),
(["a", "b"], False),
([], False),
]:
operands: t.List[t.Iterable[str]] = [
names,
tuple(names),
iter(names),
]
for operand in operands:
with self.subTest(names=names, operand=type(operand)):
self.assertEqual(variables("{a}") == operand, expected)
2 changes: 1 addition & 1 deletion uritemplate/orderedset.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ def __eq__(self, other: object) -> bool:
if isinstance(other, OrderedSet):
return len(self) == len(other) and list(self) == list(other)
other = t.cast(t.Iterable[str], other)
return not self.isdisjoint(other)
return set(self) == set(other)