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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
##############################################################################
#
# Copyright (c) 2003 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Tests for advice
This module was adapted from 'protocols.tests.advice', part of the Python
Enterprise Application Kit (PEAK). Please notify the PEAK authors
(pje@telecommunity.com and tsarna@sarna.org) if bugs are found or
Zope-specific changes are required, so that the PEAK version of this module
can be kept in sync.
PEAK is a Python application framework that interoperates with (but does
not require) Zope 3 and Twisted. It provides tools for manipulating UML
models, object-relational persistence, aspect-oriented programming, and more.
Visit the PEAK home page at http://peak.telecommunity.com for more information.
"""
import unittest
import sys
from zope.interface._compat import _skip_under_py2
from zope.interface._compat import _skip_under_py3k
class FrameInfoTest(unittest.TestCase):
def test_w_module(self):
from . import advisory_testing
(kind, module,
f_locals, f_globals) = advisory_testing.moduleLevelFrameInfo
self.assertEqual(kind, "module")
for d in module.__dict__, f_locals, f_globals:
self.assertTrue(d is advisory_testing.my_globals)
@_skip_under_py3k
def test_w_ClassicClass(self):
from . import advisory_testing
(kind,
module,
f_locals,
f_globals) = advisory_testing.ClassicClass.classLevelFrameInfo
self.assertEqual(kind, "class")
self.assertTrue(
f_locals is advisory_testing.ClassicClass.__dict__) # ???
for d in module.__dict__, f_globals:
self.assertTrue(d is advisory_testing.my_globals)
def test_w_NewStyleClass(self):
from . import advisory_testing
(kind,
module,
f_locals,
f_globals) = advisory_testing.NewStyleClass.classLevelFrameInfo
self.assertEqual(kind, "class")
for d in module.__dict__, f_globals:
self.assertTrue(d is advisory_testing.my_globals)
def test_inside_function_call(self):
from zope.interface.advice import getFrameInfo
kind, module, f_locals, f_globals = getFrameInfo(sys._getframe())
self.assertEqual(kind, "function call")
self.assertTrue(f_locals is locals()) # ???
for d in module.__dict__, f_globals:
self.assertTrue(d is globals())
def test_inside_exec(self):
from zope.interface.advice import getFrameInfo
_globals = {'getFrameInfo': getFrameInfo}
_locals = {}
exec(_FUNKY_EXEC, _globals, _locals)
self.assertEqual(_locals['kind'], "exec")
self.assertTrue(_locals['f_locals'] is _locals)
self.assertTrue(_locals['module'] is None)
self.assertTrue(_locals['f_globals'] is _globals)
_FUNKY_EXEC = """\
import sys
kind, module, f_locals, f_globals = getFrameInfo(sys._getframe())
"""
class AdviceTests(unittest.TestCase):
@_skip_under_py3k
def test_order(self):
from .advisory_testing import ping
log = []
class Foo(object):
ping(log, 1)
ping(log, 2)
ping(log, 3)
# Strip the list nesting
for i in 1, 2, 3:
self.assertTrue(isinstance(Foo, list))
Foo, = Foo
self.assertEqual(log, [(1, Foo), (2, [Foo]), (3, [[Foo]])])
@_skip_under_py3k
def test_single_explicit_meta(self):
from .advisory_testing import ping
class Metaclass(type):
pass
class Concrete(Metaclass):
__metaclass__ = Metaclass
ping([],1)
Concrete, = Concrete
self.assertTrue(Concrete.__class__ is Metaclass)
@_skip_under_py3k
def test_mixed_metas(self):
from .advisory_testing import ping
class Metaclass1(type):
pass
class Metaclass2(type):
pass
class Base1:
__metaclass__ = Metaclass1
class Base2:
__metaclass__ = Metaclass2
try:
class Derived(Base1, Base2):
ping([], 1)
self.fail("Should have gotten incompatibility error")
except TypeError:
pass
class Metaclass3(Metaclass1, Metaclass2):
pass
class Derived(Base1, Base2):
__metaclass__ = Metaclass3
ping([], 1)
self.assertTrue(isinstance(Derived, list))
Derived, = Derived
self.assertTrue(isinstance(Derived, Metaclass3))
@_skip_under_py3k
def test_meta_no_bases(self):
from .advisory_testing import ping
from types import ClassType
class Thing:
ping([], 1)
klass, = Thing # unpack list created by pong
self.assertEqual(type(klass), ClassType)
class Test_isClassAdvisor(unittest.TestCase):
def _callFUT(self, *args, **kw):
from zope.interface.advice import isClassAdvisor
return isClassAdvisor(*args, **kw)
def test_w_non_function(self):
self.assertEqual(self._callFUT(self), False)
def test_w_normal_function(self):
def foo():
raise NotImplementedError()
self.assertEqual(self._callFUT(foo), False)
def test_w_advisor_function(self):
def bar():
raise NotImplementedError()
bar.previousMetaclass = object()
self.assertEqual(self._callFUT(bar), True)
class Test_determineMetaclass(unittest.TestCase):
def _callFUT(self, *args, **kw):
from zope.interface.advice import determineMetaclass
return determineMetaclass(*args, **kw)
@_skip_under_py3k
def test_empty(self):
from types import ClassType
self.assertEqual(self._callFUT(()), ClassType)
def test_empty_w_explicit_metatype(self):
class Meta(type):
pass
self.assertEqual(self._callFUT((), Meta), Meta)
def test_single(self):
class Meta(type):
pass
self.assertEqual(self._callFUT((Meta,)), type)
@_skip_under_py3k
def test_meta_of_class(self):
class Metameta(type):
pass
class Meta(type):
__metaclass__ = Metameta
self.assertEqual(self._callFUT((Meta, type)), Metameta)
@_skip_under_py2
def test_meta_of_class_py3k(self):
# Work around SyntaxError under Python2.
EXEC = '\n'.join([
'class Metameta(type):',
' pass',
'class Meta(type, metaclass=Metameta):',
' pass',
])
globs = {}
exec(EXEC, globs)
Meta = globs['Meta']
Metameta = globs['Metameta']
self.assertEqual(self._callFUT((Meta, type)), Metameta)
@_skip_under_py3k
def test_multiple_in_hierarchy(self):
class Meta_A(type):
pass
class Meta_B(Meta_A):
pass
class A(type):
__metaclass__ = Meta_A
class B(type):
__metaclass__ = Meta_B
self.assertEqual(self._callFUT((A, B,)), Meta_B)
@_skip_under_py2
def test_multiple_in_hierarchy_py3k(self):
# Work around SyntaxError under Python2.
EXEC = '\n'.join([
'class Meta_A(type):',
' pass',
'class Meta_B(Meta_A):',
' pass',
'class A(type, metaclass=Meta_A):',
' pass',
'class B(type, metaclass=Meta_B):',
' pass',
])
globs = {}
exec(EXEC, globs)
Meta_A = globs['Meta_A']
Meta_B = globs['Meta_B']
A = globs['A']
B = globs['B']
self.assertEqual(self._callFUT((A, B)), Meta_B)
@_skip_under_py3k
def test_multiple_not_in_hierarchy(self):
class Meta_A(type):
pass
class Meta_B(type):
pass
class A(type):
__metaclass__ = Meta_A
class B(type):
__metaclass__ = Meta_B
self.assertRaises(TypeError, self._callFUT, (A, B,))
@_skip_under_py2
def test_multiple_not_in_hierarchy_py3k(self):
# Work around SyntaxError under Python2.
EXEC = '\n'.join([
'class Meta_A(type):',
' pass',
'class Meta_B(type):',
' pass',
'class A(type, metaclass=Meta_A):',
' pass',
'class B(type, metaclass=Meta_B):',
' pass',
])
globs = {}
exec(EXEC, globs)
Meta_A = globs['Meta_A']
Meta_B = globs['Meta_B']
A = globs['A']
B = globs['B']
self.assertRaises(TypeError, self._callFUT, (A, B))
class Test_minimalBases(unittest.TestCase):
def _callFUT(self, klasses):
from zope.interface.advice import minimalBases
return minimalBases(klasses)
def test_empty(self):
self.assertEqual(self._callFUT([]), [])
@_skip_under_py3k
def test_w_oldstyle_meta(self):
class C:
pass
self.assertEqual(self._callFUT([type(C)]), [])
@_skip_under_py3k
def test_w_oldstyle_class(self):
class C:
pass
self.assertEqual(self._callFUT([C]), [C])
def test_w_newstyle_meta(self):
self.assertEqual(self._callFUT([type]), [type])
def test_w_newstyle_class(self):
class C(object):
pass
self.assertEqual(self._callFUT([C]), [C])
def test_simple_hierarchy_skips_implied(self):
class A(object):
pass
class B(A):
pass
class C(B):
pass
class D(object):
pass
self.assertEqual(self._callFUT([A, B, C]), [C])
self.assertEqual(self._callFUT([A, C]), [C])
self.assertEqual(self._callFUT([B, C]), [C])
self.assertEqual(self._callFUT([A, B]), [B])
self.assertEqual(self._callFUT([D, B, D]), [B, D])
def test_repeats_kicked_to_end_of_queue(self):
class A(object):
pass
class B(object):
pass
self.assertEqual(self._callFUT([A, B, A]), [B, A])
|