/usr/lib/python3/dist-packages/zope/location/tests/test_location.py is in python3-zope.location 4.0.3-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 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 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 | ##############################################################################
#
# 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.
#
##############################################################################
import unittest
class ConformsToILocation(object):
def test_class_conforms_to_ILocation(self):
from zope.interface.verify import verifyClass
from zope.location.interfaces import ILocation
verifyClass(ILocation, self._getTargetClass())
def test_instance_conforms_to_ILocation(self):
from zope.interface.verify import verifyObject
from zope.location.interfaces import ILocation
verifyObject(ILocation, self._makeOne())
class LocationTests(unittest.TestCase, ConformsToILocation):
def _getTargetClass(self):
from zope.location.location import Location
return Location
def _makeOne(self):
return self._getTargetClass()()
def test_ctor(self):
loc = self._makeOne()
self.assertEqual(loc.__parent__, None)
self.assertEqual(loc.__name__, None)
class Test_locate(unittest.TestCase):
def _callFUT(self, obj, *args, **kw):
from zope.location.location import locate
return locate(obj, *args, **kw)
def test_wo_name(self):
class Dummy(object):
pass
parent = Dummy()
dummy = Dummy()
self._callFUT(dummy, parent)
self.assertTrue(dummy.__parent__ is parent)
self.assertEqual(dummy.__name__, None)
def test_w_name(self):
class Dummy(object):
pass
parent = Dummy()
dummy = Dummy()
self._callFUT(dummy, parent, 'name')
self.assertTrue(dummy.__parent__ is parent)
self.assertEqual(dummy.__name__, 'name')
class Test_located(unittest.TestCase):
def _callFUT(self, obj, *args, **kw):
from zope.location.location import located
return located(obj, *args, **kw)
def test_wo_name_obj_implements_ILocation(self):
from zope.interface import implementer
from zope.location.interfaces import ILocation
@implementer(ILocation)
class Dummy(object):
__parent__ = None
__name__ = object()
parent = Dummy()
dummy = Dummy()
self._callFUT(dummy, parent)
self.assertTrue(dummy.__parent__ is parent)
self.assertEqual(dummy.__name__, None)
def test_w_name_adaptable_to_ILocation(self):
from zope.interface.interface import adapter_hooks
from zope.location.interfaces import ILocation
_hooked = []
def _hook(iface, obj):
_hooked.append((iface, obj))
return obj
class Dummy(object):
pass
parent = Dummy()
dummy = Dummy()
before = adapter_hooks[:]
adapter_hooks.insert(0, _hook)
try:
self._callFUT(dummy, parent, 'name')
finally:
adapter_hooks[:] = before
self.assertTrue(dummy.__parent__ is parent)
self.assertEqual(dummy.__name__, 'name')
self.assertEqual(len(_hooked), 1)
self.assertEqual(_hooked[0], (ILocation, dummy))
def test_wo_name_not_adaptable_to_ILocation(self):
class Dummy(object):
__parent__ = None
__name__ = 'before'
parent = Dummy()
dummy = Dummy()
self.assertRaises(TypeError, self._callFUT, dummy, parent, 'name')
self.assertEqual(dummy.__parent__, None)
self.assertEqual(dummy.__name__, 'before')
class Test_LocationIterator(unittest.TestCase):
def _callFUT(self, obj):
from zope.location.location import LocationIterator
return LocationIterator(obj)
def test_w_None(self):
self.assertEqual(list(self._callFUT(None)), [])
def test_w_non_location_object(self):
island = object()
self.assertEqual(list(self._callFUT(island)), [island])
def test_w_isolated_location_object(self):
class Dummy(object):
__parent__ = None
__name__ = 'before'
island = Dummy()
self.assertEqual(list(self._callFUT(island)), [island])
def test_w_nested_location_object(self):
class Dummy(object):
__parent__ = None
__name__ = 'before'
parent = Dummy()
child = Dummy()
child.__parent__ = parent
grand = Dummy()
grand.__parent__ = child
self.assertEqual(list(self._callFUT(grand)), [grand, child, parent])
class Test_inside(unittest.TestCase):
def _callFUT(self, i1, i2):
from zope.location.location import inside
return inside(i1, i2)
def test_w_non_location_objects(self):
island = object()
atoll = object()
self.assertTrue(self._callFUT(island, island))
self.assertFalse(self._callFUT(island, atoll))
self.assertFalse(self._callFUT(atoll, island))
self.assertTrue(self._callFUT(atoll, atoll))
def test_w_isolated_location_objects(self):
class Dummy(object):
__parent__ = None
__name__ = 'before'
island = Dummy()
atoll = Dummy()
self.assertTrue(self._callFUT(island, island))
self.assertFalse(self._callFUT(island, atoll))
self.assertFalse(self._callFUT(atoll, island))
self.assertTrue(self._callFUT(atoll, atoll))
def test_w_nested_location_object(self):
class Dummy(object):
__parent__ = None
__name__ = 'before'
parent = Dummy()
child = Dummy()
child.__parent__ = parent
grand = Dummy()
grand.__parent__ = child
self.assertTrue(self._callFUT(child, parent))
self.assertFalse(self._callFUT(parent, child))
self.assertTrue(self._callFUT(child, child))
self.assertTrue(self._callFUT(grand, parent))
self.assertFalse(self._callFUT(parent, grand))
self.assertTrue(self._callFUT(grand, child))
self.assertFalse(self._callFUT(child, grand))
self.assertTrue(self._callFUT(grand, grand))
class ClassAndInstanceDescrTests(unittest.TestCase):
def _getTargetClass(self):
from zope.location.location import ClassAndInstanceDescr
return ClassAndInstanceDescr
def _makeOne(self, _inst, _class):
return self._getTargetClass()(_inst, _class)
def _makeScaffold(self):
_inst_called = []
def _inst(*args, **kw):
_inst_called.append((args, kw))
return 'INST'
_class_called = []
def _class(*args, **kw):
_class_called.append((args, kw))
return 'CLASS'
class Foo(object):
descr = self._makeOne(_inst, _class)
return Foo, _class_called, _inst_called
def test_fetched_from_class(self):
Foo, _class_called, _inst_called = self._makeScaffold()
self.assertEqual(Foo.descr, 'CLASS')
self.assertEqual(_class_called, [((Foo,),{})])
self.assertEqual(_inst_called, [])
def test_fetched_from_instance(self):
Foo, _class_called, _inst_called = self._makeScaffold()
foo = Foo()
self.assertEqual(foo.descr, 'INST')
self.assertEqual(_class_called, [])
self.assertEqual(_inst_called, [((foo,),{})])
_MARKER = object()
class LocationProxyTests(unittest.TestCase, ConformsToILocation):
def _getTargetClass(self):
from zope.location.location import LocationProxy
return LocationProxy
def _makeOne(self, obj=None, container=_MARKER, name=_MARKER):
if obj is None:
obj = object()
if container is _MARKER:
if name is _MARKER:
return self._getTargetClass()(obj)
return self._getTargetClass()(obj, name=name)
if name is _MARKER:
return self._getTargetClass()(obj, container)
return self._getTargetClass()(obj, container, name)
def test_ctor_defaults(self):
dummy = object() # can't setattr
proxy = self._makeOne(dummy)
self.assertEqual(proxy.__parent__, None)
self.assertEqual(proxy.__name__, None)
def test_ctor_explicit(self):
dummy = object() # can't setattr
parent = object()
proxy = self._makeOne(dummy, parent, 'name')
self.assertTrue(proxy.__parent__ is parent)
self.assertEqual(proxy.__name__, 'name')
def test___getattribute___wrapped(self):
class Context(object):
attr = 'ATTR'
context = Context()
proxy = self._makeOne(context)
self.assertEqual(proxy.attr, 'ATTR')
def test___setattr___wrapped(self):
class Context(object):
attr = 'BEFORE'
context = Context()
proxy = self._makeOne(context)
proxy.attr = 'AFTER'
self.assertEqual(context.attr, 'AFTER')
def test___doc___from_derived_class(self):
klass = self._getTargetClass()
class Derived(klass):
"""DERIVED"""
self.assertEqual(Derived.__doc__, 'DERIVED')
def test___doc___from_target_class(self):
klass = self._getTargetClass()
class Derived(klass):
"""DERIVED"""
class Context(object):
"""CONTEXT"""
proxy = self._makeOne(Context())
self.assertEqual(proxy.__doc__, 'CONTEXT')
def test___doc___from_target_instance(self):
klass = self._getTargetClass()
class Derived(klass):
"""DERIVED"""
class Context(object):
"""CONTEXT"""
context = Context()
context.__doc__ = 'INSTANCE'
proxy = self._makeOne(context)
self.assertEqual(proxy.__doc__, 'INSTANCE')
def test___reduce__(self):
proxy = self._makeOne()
self.assertRaises(TypeError, proxy.__reduce__)
def test___reduce_ex__(self):
proxy = self._makeOne()
self.assertRaises(TypeError, proxy.__reduce_ex__, 1)
def test___reduce___via_pickling(self):
import pickle
class Context(object):
def __reduce__(self):
return {'a': 1}
proxy = self._makeOne(Context())
# XXX: this TypeError is not due to LocationProxy.__reduce__:
# it's descriptor (under pure Python) isn't begin triggered
# properly
self.assertRaises(TypeError, pickle.dumps, proxy)
def test__providedBy___class(self):
from zope.interface import Interface
from zope.interface import implementer
from zope.interface import providedBy
from zope.interface import provider
class IProxyFactory(Interface):
pass
class IProxy(Interface):
pass
@provider(IProxyFactory)
@implementer(IProxy)
class Foo(self._getTargetClass()):
pass
self.assertEqual(list(providedBy(Foo)), [IProxyFactory])
def test__providedBy___instance(self):
from zope.interface import Interface
from zope.interface import implementer
from zope.interface import providedBy
from zope.interface import provider
from zope.location.interfaces import ILocation
class IProxyFactory(Interface):
pass
class IProxy(Interface):
pass
class IContextFactory(Interface):
pass
class IContext(Interface):
pass
@provider(IProxyFactory)
@implementer(IProxy)
class Proxy(self._getTargetClass()):
pass
@provider(IContextFactory)
@implementer(IContext)
class Context(object):
pass
context = Context()
proxy = Proxy(context)
self.assertEqual(list(providedBy(proxy)), [IContext, IProxy, ILocation])
class LocationPyProxyTests(LocationProxyTests):
def setUp(self):
import sys
for mod in ('zope.location.location',
'zope.proxy.decorator'):
try:
del sys.modules[mod]
except KeyError:
pass
import zope.proxy
self.orig = (zope.proxy.ProxyBase,
zope.proxy.getProxiedObject,
zope.proxy.setProxiedObject,
zope.proxy.isProxy,
zope.proxy.sameProxiedObjects,
zope.proxy.queryProxy,
zope.proxy.queryInnerProxy,
zope.proxy.removeAllProxies,
zope.proxy.non_overridable)
zope.proxy.ProxyBase = zope.proxy.PyProxyBase
zope.proxy.getProxiedObject = zope.proxy.py_getProxiedObject
zope.proxy.setProxiedObject = zope.proxy.py_setProxiedObject
zope.proxy.isProxy = zope.proxy.py_isProxy
zope.proxy.sameProxiedObjects = zope.proxy.py_sameProxiedObjects
zope.proxy.queryProxy = zope.proxy.py_queryProxy
zope.proxy.queryInnerProxy = zope.proxy.py_queryInnerProxy
zope.proxy.removeAllProxies = zope.proxy.py_removeAllProxies
zope.proxy.non_overridable = zope.proxy.PyNonOverridable
def tearDown(self):
import zope.proxy
(zope.proxy.ProxyBase,
zope.proxy.getProxiedObject,
zope.proxy.setProxiedObject,
zope.proxy.isProxy,
zope.proxy.sameProxiedObjects,
zope.proxy.queryProxy,
zope.proxy.queryInnerProxy,
zope.proxy.removeAllProxies,
zope.proxy.non_overridable) = self.orig
def test_suite():
return unittest.TestSuite((
unittest.makeSuite(LocationTests),
unittest.makeSuite(Test_locate),
unittest.makeSuite(Test_located),
unittest.makeSuite(Test_inside),
unittest.makeSuite(Test_LocationIterator),
unittest.makeSuite(ClassAndInstanceDescrTests),
# In case of Python-only version, tests are simply run twice.
unittest.makeSuite(LocationProxyTests),
unittest.makeSuite(LocationPyProxyTests),
))
|