Skip to content

Commit 66c6235

Browse files
ned-pcsdpgeorge
authored andcommitted
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>
1 parent 4f5e165 commit 66c6235

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

tests/basics/boundmeth1.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
# uPy and CPython differ when printing a bound method, so just print the type
44
print(type(repr([].append)))
55

6+
67
class A:
78
def f(self):
89
return 0
10+
911
def g(self, a):
1012
return a
13+
1114
def h(self, a, b, c, d, e, f):
1215
return a + b + c + d + e + f
1316

17+
1418
# bound method with no extra args
1519
m = A().f
1620
print(m())
@@ -27,4 +31,36 @@ def h(self, a, b, c, d, e, f):
2731
try:
2832
A().f.x = 1
2933
except AttributeError:
30-
print('AttributeError')
34+
print("AttributeError")
35+
36+
# bound method comparison with same object
37+
a = A()
38+
m1 = a.f
39+
m2 = a.f
40+
print(m1 == a.f) # should result in True
41+
print(m2 == a.f) # should result in True
42+
print(m1 == m2) # should result in True
43+
print(m1 != a.f) # should result in False
44+
45+
# bound method comparison with different objects
46+
a1 = A()
47+
a2 = A()
48+
m1 = a1.f
49+
m2 = a2.f
50+
print(m1 == a2.f) # should result in False
51+
print(m2 == a1.f) # should result in False
52+
print(m1 != a2.f) # should result in True
53+
54+
# bound method hashing
55+
a = A()
56+
m1 = a.f
57+
m2 = a.f
58+
print(hash(m1) == hash(a.f)) # should result in True
59+
print(hash(m2) == hash(a.f)) # should result in True
60+
print(hash(m1) == hash(m2)) # should result in True
61+
print(hash(m1) != hash(a.g)) # should result in True
62+
63+
# bound method hashing with different objects
64+
a2 = A()
65+
m2 = a2.f
66+
print(hash(m1) == hash(a2.f)) # should result in False

0 commit comments

Comments
 (0)