/usr/lib/python3/dist-packages/zope/component/tests/test__declaration.py is in python3-zope.component 4.3.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | ##############################################################################
#
# Copyright (c) 2012 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.
#
##############################################################################
"""Test adapter declaration helpers
"""
import unittest
class Test_adapter(unittest.TestCase):
def _getTargetClass(self):
from zope.component._declaration import adapter
return adapter
def _makeOne(self, *interfaces):
return self._getTargetClass()(*interfaces)
def test_ctor_no_interfaces(self):
deco = self._makeOne()
self.assertEqual(list(deco.interfaces), [])
def test_ctor_w_interfaces(self):
from zope.interface import Interface
class IFoo(Interface):
pass
class IBar(Interface):
pass
deco = self._makeOne(IFoo, IBar)
self.assertEqual(list(deco.interfaces), [IFoo, IBar])
def test__call___w_class(self):
from zope.interface import Interface
class IFoo(Interface):
pass
class IBar(Interface):
pass
@self._makeOne(IFoo, IBar)
class Baz(object):
pass
self.assertEqual(Baz.__component_adapts__, (IFoo, IBar))
def test__call___w_inst_of_decorated_class(self):
from zope.interface import Interface
class IFoo(Interface):
pass
class IBar(Interface):
pass
@self._makeOne(IFoo, IBar)
class Baz(object):
pass
baz = Baz()
self.assertRaises(AttributeError,
getattr, baz, '__component_adapts_')
def test__call___w_non_class(self):
from zope.interface import Interface
class IFoo(Interface):
pass
class IBar(Interface):
pass
class Baz(object):
pass
deco = self._makeOne(IFoo, IBar)
baz = deco(Baz())
self.assertEqual(baz.__component_adapts__, (IFoo, IBar))
class Test_adapts(unittest.TestCase):
def _run_generated_code(self, code, globs, locs,
fails_under_py3k=True,
):
import warnings
#from zope.component._compat import PYTHON3
PYTHON3 = False
with warnings.catch_warnings(record=True) as log:
warnings.resetwarnings()
if not PYTHON3:
exec(code, globs, locs)
self.assertEqual(len(log), 0) # no longer warn
return True
else:
try:
exec(code, globs, locs)
except TypeError:
return False
else:
if fails_under_py3k:
self.fail("Didn't raise TypeError")
def test_instances_not_affected(self):
from zope.component._declaration import adapts
class C(object):
adapts()
self.assertEqual(C.__component_adapts__, ())
def _try():
return C().__component_adapts__
self.assertRaises(AttributeError, _try)
def test_called_from_function(self):
import warnings
from zope.component._declaration import adapts
from zope.interface import Interface
class IFoo(Interface):
pass
globs = {'adapts': adapts, 'IFoo': IFoo}
locs = {}
CODE = "\n".join([
'def foo():',
' adapts(IFoo)'
])
if self._run_generated_code(CODE, globs, locs, False):
foo = locs['foo']
with warnings.catch_warnings(record=True) as log:
warnings.resetwarnings()
self.assertRaises(TypeError, foo)
self.assertEqual(len(log), 0) # no longer warn
def test_called_twice_from_class(self):
import warnings
from zope.component._declaration import adapts
from zope.interface import Interface
from zope.interface._compat import PYTHON3
class IFoo(Interface):
pass
class IBar(Interface):
pass
globs = {'adapts': adapts, 'IFoo': IFoo, 'IBar': IBar}
locs = {}
CODE = "\n".join([
'class Foo(object):',
' adapts(IFoo)',
' adapts(IBar)',
])
with warnings.catch_warnings(record=True) as log:
warnings.resetwarnings()
try:
exec(CODE, globs, locs)
except TypeError:
if not PYTHON3:
self.assertEqual(len(log), 0) # no longer warn
else:
self.fail("Didn't raise TypeError")
def test_called_once_from_class(self):
from zope.component._declaration import adapts
from zope.interface import Interface
class IFoo(Interface):
pass
globs = {'adapts': adapts, 'IFoo': IFoo}
locs = {}
CODE = "\n".join([
'class Foo(object):',
' adapts(IFoo)',
])
if self._run_generated_code(CODE, globs, locs):
Foo = locs['Foo']
spec = Foo.__component_adapts__
self.assertEqual(list(spec), [IFoo])
class Test_adaptedBy(unittest.TestCase):
def _callFUT(self, obj):
from zope.component._declaration import adaptedBy
return adaptedBy(obj)
def test_obj_w_no_attr(self):
self.assertEqual(self._callFUT(object()), None)
def test__call___w_class(self):
from zope.interface import Interface
class IFoo(Interface):
pass
class IBar(Interface):
pass
class Baz(object):
__component_adapts__ = (IFoo, IBar)
self.assertEqual(self._callFUT(Baz), (IFoo, IBar))
def test__call___w_inst_of_decorated_class(self):
from zope.interface import Interface
from zope.component._declaration import _adapts_descr
class IFoo(Interface):
pass
class IBar(Interface):
pass
class Baz(object):
__component_adapts__ = _adapts_descr((IFoo, IBar))
baz = Baz()
self.assertEqual(self._callFUT(baz), None)
def test__call___w_non_class(self):
from zope.interface import Interface
class IFoo(Interface):
pass
class IBar(Interface):
pass
class Baz(object):
pass
baz = Baz()
baz.__component_adapts__ = (IFoo, IBar)
self.assertEqual(self._callFUT(baz), (IFoo, IBar))
def test_suite():
return unittest.TestSuite((
unittest.makeSuite(Test_adapter),
unittest.makeSuite(Test_adapts),
unittest.makeSuite(Test_adaptedBy),
))
|