File: test_decorators.py

package info (click to toggle)
decoratortools 1.8-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 188 kB
  • ctags: 129
  • sloc: python: 667; makefile: 12
file content (287 lines) | stat: -rw-r--r-- 5,627 bytes parent folder | download | duplicates (4)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
from unittest import TestCase, makeSuite, TestSuite
from peak.util.decorators import *
import sys

def ping(log, value):

    """Class decorator for testing"""

    def pong(klass):
        log.append((value,klass))
        return [klass]

    decorate_class(pong)


def additional_tests():
    import doctest
    return doctest.DocFileSuite(
        'README.txt',
        optionflags=doctest.ELLIPSIS|doctest.NORMALIZE_WHITESPACE,
    )




















class DecoratorTests(TestCase):

    def testAssignAdvice(self):

        log = []
        def track(f,k,v,d):
            log.append((f,k,v))
            if k in f.f_locals:
                del f.f_locals[k]   # simulate old-style advisor

        decorate_assignment(track,frame=sys._getframe())
        test_var = 1
        self.assertEqual(log, [(sys._getframe(),'test_var',1)])
        log = []
        decorate_assignment(track,1)
        test2 = 42
        self.assertEqual(log, [(sys._getframe(),'test2',42)])

        # Try doing double duty, redefining an existing variable...
        log = []
        decorate_assignment(track,1)
        decorate_assignment(track,1)

        test2 = 42
        self.assertEqual(log, [(sys._getframe(),'test2',42)]*2)


    def testAs(self):

        def f(): pass

        [decorate(lambda x: [x])]
        f1 = f

        self.assertEqual(f1, [f])

        [decorate(list, lambda x: (x,))]
        f1 = f
        self.assertEqual(f1, [f])


    def test24DecoratorMode(self):
        log = []
        def track(f,k,v,d):
            log.append((f,k,v))
            return v

        def foo(x): pass

        decorate_assignment(track,1)(foo)
        x = 1

        self.assertEqual(log, [(sys._getframe(),'foo',foo)])





























moduleLevelFrameInfo = frameinfo(sys._getframe())

class FrameInfoTest(TestCase):

    classLevelFrameInfo = frameinfo(sys._getframe())

    def testModuleInfo(self):
        kind,module,f_locals,f_globals = moduleLevelFrameInfo
        assert kind=="module"
        for d in module.__dict__, f_locals, f_globals:
            assert d is globals()

    def testClassInfo(self):
        kind,module,f_locals,f_globals = self.classLevelFrameInfo
        assert kind=="class"
        assert f_locals['classLevelFrameInfo'] is self.classLevelFrameInfo
        for d in module.__dict__, f_globals:
            assert d is globals()


    def testCallInfo(self):
        kind,module,f_locals,f_globals = frameinfo(sys._getframe())
        assert kind=="function call"
        assert f_locals is locals() # ???
        for d in module.__dict__, f_globals:
            assert d is globals()


    def testClassExec(self):
        d = {'sys':sys, 'frameinfo':frameinfo}
        exec "class Foo: info=frameinfo(sys._getframe())" in d
        kind,module,f_locals,f_globals = d['Foo'].info
        assert kind=="class", kind








class ClassDecoratorTests(TestCase):

    def testOrder(self):
        log = []
        class Foo:
            ping(log, 1)
            ping(log, 2)
            ping(log, 3)

        # Strip the list nesting
        for i in 1,2,3:
            assert isinstance(Foo,list)
            Foo, = Foo

        assert log == [
            (1, Foo),
            (2, [Foo]),
            (3, [[Foo]]),
        ]

    def testOutside(self):
        try:
            ping([], 1)
        except SyntaxError:
            pass
        else:
            raise AssertionError(
                "Should have detected advice outside class body"
            )

    def testDoubleType(self):
        if sys.hexversion >= 0x02030000:
            return  # you can't duplicate bases in 2.3
        class aType(type,type):
            ping([],1)
        aType, = aType
        assert aType.__class__ is type




    def testSingleExplicitMeta(self):

        class M(type): pass

        class C(M):
            __metaclass__ = M
            ping([],1)

        C, = C
        assert C.__class__ is M


    def testMixedMetas(self):

        class M1(type): pass
        class M2(type): pass

        class B1: __metaclass__ = M1
        class B2: __metaclass__ = M2

        try:
            class C(B1,B2):
                ping([],1)
        except TypeError:
            pass
        else:
            raise AssertionError("Should have gotten incompatibility error")

        class M3(M1,M2): pass

        class C(B1,B2):
            __metaclass__ = M3
            ping([],1)

        assert isinstance(C,list)
        C, = C
        assert isinstance(C,M3)




    def testMetaOfClass(self):

        class metameta(type):
            pass

        class meta(type):
            __metaclass__ = metameta

        assert metaclass_for_bases((meta,type))==metameta



class ClassyMetaTests(TestCase):
    """Test subclass/instance checking of classy for Python 2.6+ ABC mixin"""

    def setUp(self):
        class x(classy): pass
        class y(x): pass
        class cc(type(classy)): pass
        self.__dict__.update(locals())

    def test_subclassing(self):
        self.failUnless(issubclass(self.x, classy))
        self.failUnless(issubclass(self.y, self.x))
        self.failIf(issubclass(self.x, self.y))
        self.failIf(issubclass(classy, self.x))
        self.failIf(issubclass(self.x, type(classy)))

    def test_instancing(self):
        self.failIf(isinstance(self.x, classy))
        self.failUnless(isinstance(self.x, type(classy)))
        self.failIf(isinstance(self.x(), type(classy)))
        self.failIf(isinstance(object, type(classy)))
        self.failIf(isinstance(self.x(),self.y))
        self.failUnless(isinstance(self.y(),self.x))