File: test_methods.py

package info (click to toggle)
jython 2.1.0-20
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 9,388 kB
  • ctags: 12,215
  • sloc: java: 48,499; python: 16,220; xml: 403; makefile: 189; perl: 103; ansic: 24; sh: 14
file content (58 lines) | stat: -rw-r--r-- 1,126 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Python test set -- part 7, bound and unbound methods

from test_support import *

print_test('Bound and unbound methods (test_methods.py)', 1)

class A:
    def one(self): return 'one'

class B(A):
    def two(self): return 'two'

class C(A):
    def one(self): return 'another one'

a = A()
b = B()
c = C()

print_test('unbound method equality', 2)
assert A.one == B.one
assert A.one <> C.one

print_test('method attributes', 2)
assert A.one.im_func == a.one.im_func
assert a.one.im_self == a
assert a.one.im_class == A
assert b.one.im_self == b
assert b.one.im_class == A

print_test('unbound method invocation w/ explicit self', 2)
assert A.one(b) == 'one'
assert B.two(b) == 'two'
assert B.one(b) == 'one'

assert A.one(c) == 'one'
assert C.one(c) == 'another one'

assert A.one(a) == 'one'
assert B.one(a) == 'one'
try:
    C.one(a)
    assert 0
except TypeError:
    pass

print_test('"unbound" methods of builtin types', 2)
w = [1,2,3].append
x = [4,5,6].append
assert w <> x
assert w.__self__ <> x.__self__

y = w.__self__[:]
z = x.__self__[:]

assert y.append.__self__ <> w
z.append(7)
assert z == (x.__self__+[7])