File: test_class.py

package info (click to toggle)
pypy3 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 111,848 kB
  • sloc: python: 1,291,746; ansic: 74,281; asm: 5,187; cpp: 3,017; sh: 2,533; makefile: 544; xml: 243; lisp: 45; csh: 21; awk: 4
file content (125 lines) | stat: -rw-r--r-- 3,126 bytes parent folder | download
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

class AppTestClass: 

    def test_class(self):
        class C(object):
            pass
        assert C.__class__ == type
        c = C()
        assert c.__class__ == C

    def test_metaclass_explicit(self):
        """
        class M(type):
            pass
        class C(metaclass=M):
            pass
        assert C.__class__ == M
        c = C()
        assert c.__class__ == C
        """

    def test_metaclass_inherited(self):
        """
        class M(type):
            pass
        class B(metaclass=M):
            pass
        class C(B):
            pass
        assert C.__class__ == M
        c = C()
        assert c.__class__ == C
        """

    def test_method(self):
        class C(object):
            def meth(self):
                return 1
        c = C()
        assert c.meth() == 1

    def test_method_exc(self):
        class C(object):
            def meth(self):
                raise RuntimeError
        c = C()
        raises(RuntimeError, c.meth)

    def test_class_attr(self):
        class C(object):
            a = 42
        c = C()
        assert c.a == 42
        assert C.a == 42

    def test_class_attr_inherited(self):
        class C(object):
            a = 42
        class D(C):
            pass
        d = D()
        assert d.a == 42
        assert D.a == 42

    def test___new__(self):
        class A(object):
            pass
        assert isinstance(A(), A)
        assert isinstance(object.__new__(A), A)
        assert isinstance(A.__new__(A), A)

    def test_int_subclass(self):
        class R(int):
            pass
        x = R(5)
        assert type(x) == R
        assert x == 5
        assert type(int(x)) == int
        assert int(x) == 5

    def test_float_subclass(self):
        class R(float):
            pass
        x = R(5.5)
        assert type(x) == R
        assert x == 5.5
        assert type(float(x)) == float
        assert float(x) == 5.5

    def test_meth_doc(self):
        class C(object):
            def meth_no_doc(self):
                pass
            def meth_doc(self):
                """this is a docstring"""
                pass
        c = C()
        assert C.meth_no_doc.__doc__ == None
        assert c.meth_no_doc.__doc__ == None
        assert C.meth_doc.__doc__ == """this is a docstring"""
        assert c.meth_doc.__doc__ == """this is a docstring"""

    def test_getattribute(self):
        class C(object):
            def __getattribute__(self, attr):
                if attr == 'one':
                    return 'two'
                return super(C, self).__getattribute__(attr)
        c = C()
        assert c.one == "two"
        raises(AttributeError, getattr, c, "two")

    def test___class__(self):
        class C(object):
            def get_class(self):
                return __class__
        assert C().get_class()

    def test_qualname(self):
        class C:
            class D:
                pass
        assert C.__qualname__ == 'test_qualname.<locals>.C'
        assert C.D.__qualname__ == 'test_qualname.<locals>.C.D'
        assert not hasattr(C(), '__qualname__')