diff options
author | Ned Konz <ned@productcreationstudio.com> | 2023-10-09 10:14:57 -0700 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2023-10-13 15:15:49 +1100 |
commit | 66c62353ce35d72d7322740ad88f3c4986f31e64 (patch) | |
tree | 24d1b0f3077e449d9b365cf87188c058a141ca61 /tests/basics/boundmeth1.py | |
parent | 4f5e165d0bf9ba338df3cdab4266556c4c5a70c7 (diff) | |
download | micropython-66c62353ce35d72d7322740ad88f3c4986f31e64.tar.gz micropython-66c62353ce35d72d7322740ad88f3c4986f31e64.zip |
tests/basics/boundmeth1.py: Add tests for bound method equality/hash.
This commit adds tests for bound method comparison and hashing to support
the changes in the previous commit.
Signed-off-by: Ned Konz <ned@productcreationstudio.com>
Diffstat (limited to 'tests/basics/boundmeth1.py')
-rw-r--r-- | tests/basics/boundmeth1.py | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/tests/basics/boundmeth1.py b/tests/basics/boundmeth1.py index f483ba406d..e72153a41a 100644 --- a/tests/basics/boundmeth1.py +++ b/tests/basics/boundmeth1.py @@ -3,14 +3,18 @@ # uPy and CPython differ when printing a bound method, so just print the type print(type(repr([].append))) + class A: def f(self): return 0 + def g(self, a): return a + def h(self, a, b, c, d, e, f): return a + b + c + d + e + f + # bound method with no extra args m = A().f print(m()) @@ -27,4 +31,36 @@ print(m(1, 2, 3, 4, 5, 6)) try: A().f.x = 1 except AttributeError: - print('AttributeError') + print("AttributeError") + +# bound method comparison with same object +a = A() +m1 = a.f +m2 = a.f +print(m1 == a.f) # should result in True +print(m2 == a.f) # should result in True +print(m1 == m2) # should result in True +print(m1 != a.f) # should result in False + +# bound method comparison with different objects +a1 = A() +a2 = A() +m1 = a1.f +m2 = a2.f +print(m1 == a2.f) # should result in False +print(m2 == a1.f) # should result in False +print(m1 != a2.f) # should result in True + +# bound method hashing +a = A() +m1 = a.f +m2 = a.f +print(hash(m1) == hash(a.f)) # should result in True +print(hash(m2) == hash(a.f)) # should result in True +print(hash(m1) == hash(m2)) # should result in True +print(hash(m1) != hash(a.g)) # should result in True + +# bound method hashing with different objects +a2 = A() +m2 = a2.f +print(hash(m1) == hash(a2.f)) # should result in False |