/usr/share/pyshared/zope/component/nexttesting.py is in python-zope.component 3.10.0-2.
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 | ##############################################################################
#
# Copyright (c) 2009 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.
#
##############################################################################
"""Helper functions for testing utilities that use get/queryNextUtility.
"""
import zope.interface
from zope.component.interfaces import IComponentLookup, IComponents
class SiteManagerStub(object):
zope.interface.implements(IComponents)
__bases__ = ()
def __init__(self):
self._utils = {}
def setNext(self, next):
self.__bases__ = (next, )
def provideUtility(self, iface, util, name=''):
self._utils[(iface, name)] = util
def queryUtility(self, iface, name='', default=None):
return self._utils.get((iface, name), default)
def testingNextUtility(utility, nextutility, interface, name='',
sitemanager=None, nextsitemanager=None):
"""Provide a next utility for testing.
This function sets up two utilities, so the get/queryNextUtility functions
will see the second one as the "next" to the first one.
To test it, we need to create a utility interface and implementation:
>>> from zope.interface import Interface, implements
>>> class IAnyUtility(Interface):
... pass
>>> class AnyUtility(object):
... implements(IAnyUtility)
... def __init__(self, id):
... self.id = id
>>> any1 = AnyUtility(1)
>>> any1next = AnyUtility(2)
Now, we can make the "any1next" be next to "any1".
>>> testingNextUtility(any1, any1next, IAnyUtility)
>>> from zope.component import getNextUtility
>>> getNextUtility(any1, IAnyUtility) is any1next
True
It will work for named utilities as well.
>>> testingNextUtility(any1, any1next, IAnyUtility, 'any')
>>> getNextUtility(any1, IAnyUtility, 'any') is any1next
True
We can also provide our custom component registries:
>>> sm = SiteManagerStub()
>>> nextsm = SiteManagerStub()
>>> testingNextUtility(any1, any1next, IAnyUtility,
... sitemanager=sm, nextsitemanager=nextsm)
>>> IComponentLookup(any1) is sm
True
>>> IComponentLookup(any1next) is nextsm
True
>>> getNextUtility(any1, IAnyUtility) is any1next
True
"""
if sitemanager is None:
sitemanager = SiteManagerStub()
if nextsitemanager is None:
nextsitemanager = SiteManagerStub()
sitemanager.setNext(nextsitemanager)
sitemanager.provideUtility(interface, utility, name)
utility.__conform__ = (
lambda iface:
iface.isOrExtends(IComponentLookup) and sitemanager or None
)
nextsitemanager.provideUtility(interface, nextutility, name)
nextutility.__conform__ = (
lambda iface:
iface.isOrExtends(IComponentLookup) and nextsitemanager or None
)
|