AARE: add __eq__() to allow checking aare1 == aare2

... and add some tests for it
This commit is contained in:
Christian Boltz 2023-10-11 20:38:38 +02:00
parent 6ac0e0236b
commit bfd72c93be
Failed to generate hash of commit
2 changed files with 30 additions and 0 deletions

View file

@ -47,6 +47,15 @@ class AARE:
"""returns a "printable" representation of object"""
return type(self).__name__ + "('%s')" % self.regex
def __eq__(self, other):
"""check if the given object is equal
Note that the == check is more strict than is_equal() - it doesn't accept if other is a string instead of AARE"""
if isinstance(other, type(self)):
return self.regex == other.regex
return False
def __deepcopy__(self, memo):
# thanks to http://bugs.python.org/issue10076, we need to implement this ourself
if self.orig_regex:

View file

@ -211,6 +211,27 @@ class TestAAREIsEqual(AATest):
aare_obj.is_equal(42)
class TestAAREIs_eq(AATest):
tests = (
# regex is path? check for expected
(('/foo', True, '/foo'), True),
(('@{foo}', True, '@{foo}'), True),
(('/**', True, '/foo'), False),
)
def _run_test(self, params, expected):
regex, is_path, check_for = params
aare_obj_1 = AARE(regex, is_path)
aare_obj_2 = AARE(check_for, is_path)
self.assertEqual(aare_obj_1 == aare_obj_2, expected)
self.assertFalse(aare_obj_1 == check_for)
def test_is_eq_invalid_1(self):
aare_obj = AARE('/foo/**', True)
self.assertFalse(aare_obj == 42)
class TestAAREIsPath(AATest):
tests = (
# regex is path? match for expected