12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658 |
- ##############################################################################
- #
- # 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.
- #
- ##############################################################################
- """Test the new API for making and checking interface declarations
- """
- import unittest
- from zope.interface._compat import _skip_under_py3k
- class _Py3ClassAdvice(object):
- def _run_generated_code(self, code, globs, locs,
- fails_under_py3k=True,
- ):
- import warnings
- from zope.interface._compat import PYTHON3
- 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")
- class NamedTests(unittest.TestCase):
- def test_class(self):
- from zope.interface.declarations import named
- @named(u'foo')
- class Foo(object):
- pass
- self.assertEqual(Foo.__component_name__, u'foo')
- def test_function(self):
- from zope.interface.declarations import named
- @named(u'foo')
- def doFoo(o):
- raise NotImplementedError()
- self.assertEqual(doFoo.__component_name__, u'foo')
- def test_instance(self):
- from zope.interface.declarations import named
- class Foo(object):
- pass
- foo = Foo()
- named(u'foo')(foo)
- self.assertEqual(foo.__component_name__, u'foo')
- class DeclarationTests(unittest.TestCase):
- def _getTargetClass(self):
- from zope.interface.declarations import Declaration
- return Declaration
- def _makeOne(self, *args, **kw):
- return self._getTargetClass()(*args, **kw)
- def test_ctor_no_bases(self):
- decl = self._makeOne()
- self.assertEqual(list(decl.__bases__), [])
- def test_ctor_w_interface_in_bases(self):
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- decl = self._makeOne(IFoo)
- self.assertEqual(list(decl.__bases__), [IFoo])
- def test_ctor_w_implements_in_bases(self):
- from zope.interface.declarations import Implements
- impl = Implements()
- decl = self._makeOne(impl)
- self.assertEqual(list(decl.__bases__), [impl])
- def test_changed_wo_existing__v_attrs(self):
- decl = self._makeOne()
- decl.changed(decl) # doesn't raise
- self.assertFalse('_v_attrs' in decl.__dict__)
- def test_changed_w_existing__v_attrs(self):
- decl = self._makeOne()
- decl._v_attrs = object()
- decl.changed(decl)
- self.assertFalse('_v_attrs' in decl.__dict__)
- def test___contains__w_self(self):
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- decl = self._makeOne()
- self.assertFalse(decl in decl)
- def test___contains__w_unrelated_iface(self):
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- decl = self._makeOne()
- self.assertFalse(IFoo in decl)
- def test___contains__w_base_interface(self):
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- decl = self._makeOne(IFoo)
- self.assertTrue(IFoo in decl)
- def test___iter___empty(self):
- decl = self._makeOne()
- self.assertEqual(list(decl), [])
- def test___iter___single_base(self):
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- decl = self._makeOne(IFoo)
- self.assertEqual(list(decl), [IFoo])
- def test___iter___multiple_bases(self):
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- IBar = InterfaceClass('IBar')
- decl = self._makeOne(IFoo, IBar)
- self.assertEqual(list(decl), [IFoo, IBar])
- def test___iter___inheritance(self):
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- IBar = InterfaceClass('IBar', (IFoo,))
- decl = self._makeOne(IBar)
- self.assertEqual(list(decl), [IBar]) #IBar.interfaces() omits bases
- def test___iter___w_nested_sequence_overlap(self):
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- IBar = InterfaceClass('IBar')
- decl = self._makeOne(IBar, (IFoo, IBar))
- self.assertEqual(list(decl), [IBar, IFoo])
- def test_flattened_empty(self):
- from zope.interface.interface import Interface
- decl = self._makeOne()
- self.assertEqual(list(decl.flattened()), [Interface])
- def test_flattened_single_base(self):
- from zope.interface.interface import Interface
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- decl = self._makeOne(IFoo)
- self.assertEqual(list(decl.flattened()), [IFoo, Interface])
- def test_flattened_multiple_bases(self):
- from zope.interface.interface import Interface
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- IBar = InterfaceClass('IBar')
- decl = self._makeOne(IFoo, IBar)
- self.assertEqual(list(decl.flattened()), [IFoo, IBar, Interface])
- def test_flattened_inheritance(self):
- from zope.interface.interface import Interface
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- IBar = InterfaceClass('IBar', (IFoo,))
- decl = self._makeOne(IBar)
- self.assertEqual(list(decl.flattened()), [IBar, IFoo, Interface])
- def test_flattened_w_nested_sequence_overlap(self):
- from zope.interface.interface import Interface
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- IBar = InterfaceClass('IBar')
- decl = self._makeOne(IBar, (IFoo, IBar))
- # Note that decl.__iro__ has IFoo first.
- self.assertEqual(list(decl.flattened()), [IFoo, IBar, Interface])
- def test___sub___unrelated_interface(self):
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- IBar = InterfaceClass('IBar')
- before = self._makeOne(IFoo)
- after = before - IBar
- self.assertTrue(isinstance(after, self._getTargetClass()))
- self.assertEqual(list(after), [IFoo])
- def test___sub___related_interface(self):
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- before = self._makeOne(IFoo)
- after = before - IFoo
- self.assertEqual(list(after), [])
- def test___sub___related_interface_by_inheritance(self):
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- IBar = InterfaceClass('IBar', (IFoo,))
- before = self._makeOne(IBar)
- after = before - IBar
- self.assertEqual(list(after), [])
- def test___add___unrelated_interface(self):
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- IBar = InterfaceClass('IBar')
- before = self._makeOne(IFoo)
- after = before + IBar
- self.assertTrue(isinstance(after, self._getTargetClass()))
- self.assertEqual(list(after), [IFoo, IBar])
- def test___add___related_interface(self):
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- IBar = InterfaceClass('IBar')
- IBaz = InterfaceClass('IBaz')
- before = self._makeOne(IFoo, IBar)
- other = self._makeOne(IBar, IBaz)
- after = before + other
- self.assertEqual(list(after), [IFoo, IBar, IBaz])
- class TestImplements(unittest.TestCase):
- def _getTargetClass(self):
- from zope.interface.declarations import Implements
- return Implements
- def _makeOne(self, *args, **kw):
- return self._getTargetClass()(*args, **kw)
- def test_ctor_no_bases(self):
- impl = self._makeOne()
- self.assertEqual(impl.inherit, None)
- self.assertEqual(impl.declared, ())
- self.assertEqual(impl.__name__, '?')
- self.assertEqual(list(impl.__bases__), [])
- def test___repr__(self):
- impl = self._makeOne()
- impl.__name__ = 'Testing'
- self.assertEqual(repr(impl), '<implementedBy Testing>')
- def test___reduce__(self):
- from zope.interface.declarations import implementedBy
- impl = self._makeOne()
- self.assertEqual(impl.__reduce__(), (implementedBy, (None,)))
- def test_sort(self):
- from zope.interface.declarations import implementedBy
- class A(object):
- pass
- class B(object):
- pass
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- self.assertEqual(implementedBy(A), implementedBy(A))
- self.assertEqual(hash(implementedBy(A)), hash(implementedBy(A)))
- self.assertTrue(implementedBy(A) < None)
- self.assertTrue(None > implementedBy(A))
- self.assertTrue(implementedBy(A) < implementedBy(B))
- self.assertTrue(implementedBy(A) > IFoo)
- self.assertTrue(implementedBy(A) <= implementedBy(B))
- self.assertTrue(implementedBy(A) >= IFoo)
- self.assertTrue(implementedBy(A) != IFoo)
- def test_proxy_equality(self):
- # https://github.com/zopefoundation/zope.interface/issues/55
- class Proxy(object):
- def __init__(self, wrapped):
- self._wrapped = wrapped
- def __getattr__(self, name):
- raise NotImplementedError()
- def __eq__(self, other):
- return self._wrapped == other
- def __ne__(self, other):
- return self._wrapped != other
- from zope.interface.declarations import implementedBy
- class A(object):
- pass
- class B(object):
- pass
- implementedByA = implementedBy(A)
- implementedByB = implementedBy(B)
- proxy = Proxy(implementedByA)
- # The order of arguments to the operators matters,
- # test both
- self.assertTrue(implementedByA == implementedByA)
- self.assertTrue(implementedByA != implementedByB)
- self.assertTrue(implementedByB != implementedByA)
- self.assertTrue(proxy == implementedByA)
- self.assertTrue(implementedByA == proxy)
- self.assertFalse(proxy != implementedByA)
- self.assertFalse(implementedByA != proxy)
- self.assertTrue(proxy != implementedByB)
- self.assertTrue(implementedByB != proxy)
- class Test_implementedByFallback(unittest.TestCase):
- def _callFUT(self, *args, **kw):
- from zope.interface.declarations import implementedByFallback
- return implementedByFallback(*args, **kw)
- def test_dictless_wo_existing_Implements_wo_registrations(self):
- class Foo(object):
- __slots__ = ('__implemented__',)
- foo = Foo()
- foo.__implemented__ = None
- self.assertEqual(list(self._callFUT(foo)), [])
- def test_dictless_wo_existing_Implements_cant_assign___implemented__(self):
- class Foo(object):
- def _get_impl(self):
- raise NotImplementedError()
- def _set_impl(self, val):
- raise TypeError
- __implemented__ = property(_get_impl, _set_impl)
- def __call__(self):
- # act like a factory
- raise NotImplementedError()
- foo = Foo()
- self.assertRaises(TypeError, self._callFUT, foo)
- def test_dictless_wo_existing_Implements_w_registrations(self):
- from zope.interface import declarations
- class Foo(object):
- __slots__ = ('__implemented__',)
- foo = Foo()
- foo.__implemented__ = None
- reg = object()
- with _MonkeyDict(declarations,
- 'BuiltinImplementationSpecifications') as specs:
- specs[foo] = reg
- self.assertTrue(self._callFUT(foo) is reg)
- def test_dictless_w_existing_Implements(self):
- from zope.interface.declarations import Implements
- impl = Implements()
- class Foo(object):
- __slots__ = ('__implemented__',)
- foo = Foo()
- foo.__implemented__ = impl
- self.assertTrue(self._callFUT(foo) is impl)
- def test_dictless_w_existing_not_Implements(self):
- from zope.interface.interface import InterfaceClass
- class Foo(object):
- __slots__ = ('__implemented__',)
- foo = Foo()
- IFoo = InterfaceClass('IFoo')
- foo.__implemented__ = (IFoo,)
- self.assertEqual(list(self._callFUT(foo)), [IFoo])
- def test_w_existing_attr_as_Implements(self):
- from zope.interface.declarations import Implements
- impl = Implements()
- class Foo(object):
- __implemented__ = impl
- self.assertTrue(self._callFUT(Foo) is impl)
- def test_builtins_added_to_cache(self):
- from zope.interface import declarations
- from zope.interface.declarations import Implements
- from zope.interface._compat import _BUILTINS
- with _MonkeyDict(declarations,
- 'BuiltinImplementationSpecifications') as specs:
- self.assertEqual(list(self._callFUT(tuple)), [])
- self.assertEqual(list(self._callFUT(list)), [])
- self.assertEqual(list(self._callFUT(dict)), [])
- for typ in (tuple, list, dict):
- spec = specs[typ]
- self.assertTrue(isinstance(spec, Implements))
- self.assertEqual(repr(spec),
- '<implementedBy %s.%s>'
- % (_BUILTINS, typ.__name__))
- def test_builtins_w_existing_cache(self):
- from zope.interface import declarations
- t_spec, l_spec, d_spec = object(), object(), object()
- with _MonkeyDict(declarations,
- 'BuiltinImplementationSpecifications') as specs:
- specs[tuple] = t_spec
- specs[list] = l_spec
- specs[dict] = d_spec
- self.assertTrue(self._callFUT(tuple) is t_spec)
- self.assertTrue(self._callFUT(list) is l_spec)
- self.assertTrue(self._callFUT(dict) is d_spec)
- def test_oldstyle_class_no_assertions(self):
- # TODO: Figure out P3 story
- class Foo:
- pass
- self.assertEqual(list(self._callFUT(Foo)), [])
- def test_no_assertions(self):
- # TODO: Figure out P3 story
- class Foo(object):
- pass
- self.assertEqual(list(self._callFUT(Foo)), [])
- def test_w_None_no_bases_not_factory(self):
- class Foo(object):
- __implemented__ = None
- foo = Foo()
- self.assertRaises(TypeError, self._callFUT, foo)
- def test_w_None_no_bases_w_factory(self):
- from zope.interface.declarations import objectSpecificationDescriptor
- class Foo(object):
- __implemented__ = None
- def __call__(self):
- raise NotImplementedError()
- foo = Foo()
- foo.__name__ = 'foo'
- spec = self._callFUT(foo)
- self.assertEqual(spec.__name__,
- 'zope.interface.tests.test_declarations.foo')
- self.assertTrue(spec.inherit is foo)
- self.assertTrue(foo.__implemented__ is spec)
- self.assertTrue(foo.__providedBy__ is objectSpecificationDescriptor)
- self.assertFalse('__provides__' in foo.__dict__)
- def test_w_None_no_bases_w_class(self):
- from zope.interface.declarations import ClassProvides
- class Foo(object):
- __implemented__ = None
- spec = self._callFUT(Foo)
- self.assertEqual(spec.__name__,
- 'zope.interface.tests.test_declarations.Foo')
- self.assertTrue(spec.inherit is Foo)
- self.assertTrue(Foo.__implemented__ is spec)
- self.assertTrue(isinstance(Foo.__providedBy__, ClassProvides))
- self.assertTrue(isinstance(Foo.__provides__, ClassProvides))
- self.assertEqual(Foo.__provides__, Foo.__providedBy__)
- def test_w_existing_Implements(self):
- from zope.interface.declarations import Implements
- impl = Implements()
- class Foo(object):
- __implemented__ = impl
- self.assertTrue(self._callFUT(Foo) is impl)
- class Test_implementedBy(Test_implementedByFallback):
- # Repeat tests for C optimizations
- def _callFUT(self, *args, **kw):
- from zope.interface.declarations import implementedBy
- return implementedBy(*args, **kw)
- def test_optimizations(self):
- from zope.interface.declarations import implementedByFallback
- from zope.interface.declarations import implementedBy
- try:
- import zope.interface._zope_interface_coptimizations
- except ImportError:
- self.assertIs(implementedBy, implementedByFallback)
- else:
- self.assertIsNot(implementedBy, implementedByFallback)
- class Test_classImplementsOnly(unittest.TestCase):
- def _callFUT(self, *args, **kw):
- from zope.interface.declarations import classImplementsOnly
- return classImplementsOnly(*args, **kw)
- def test_no_existing(self):
- from zope.interface.declarations import ClassProvides
- from zope.interface.interface import InterfaceClass
- class Foo(object):
- pass
- ifoo = InterfaceClass('IFoo')
- self._callFUT(Foo, ifoo)
- spec = Foo.__implemented__
- self.assertEqual(spec.__name__,
- 'zope.interface.tests.test_declarations.Foo')
- self.assertTrue(spec.inherit is None)
- self.assertTrue(Foo.__implemented__ is spec)
- self.assertTrue(isinstance(Foo.__providedBy__, ClassProvides))
- self.assertTrue(isinstance(Foo.__provides__, ClassProvides))
- self.assertEqual(Foo.__provides__, Foo.__providedBy__)
- def test_w_existing_Implements(self):
- from zope.interface.declarations import Implements
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- IBar = InterfaceClass('IBar')
- impl = Implements(IFoo)
- impl.declared = (IFoo,)
- class Foo(object):
- __implemented__ = impl
- impl.inherit = Foo
- self._callFUT(Foo, IBar)
- # Same spec, now different values
- self.assertTrue(Foo.__implemented__ is impl)
- self.assertEqual(impl.inherit, None)
- self.assertEqual(impl.declared, (IBar,))
- class Test_classImplements(unittest.TestCase):
- def _callFUT(self, *args, **kw):
- from zope.interface.declarations import classImplements
- return classImplements(*args, **kw)
- def test_no_existing(self):
- from zope.interface.declarations import ClassProvides
- from zope.interface.interface import InterfaceClass
- class Foo(object):
- pass
- IFoo = InterfaceClass('IFoo')
- self._callFUT(Foo, IFoo)
- spec = Foo.__implemented__
- self.assertEqual(spec.__name__,
- 'zope.interface.tests.test_declarations.Foo')
- self.assertTrue(spec.inherit is Foo)
- self.assertTrue(Foo.__implemented__ is spec)
- self.assertTrue(isinstance(Foo.__providedBy__, ClassProvides))
- self.assertTrue(isinstance(Foo.__provides__, ClassProvides))
- self.assertEqual(Foo.__provides__, Foo.__providedBy__)
- def test_w_existing_Implements(self):
- from zope.interface.declarations import Implements
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- IBar = InterfaceClass('IBar')
- impl = Implements(IFoo)
- impl.declared = (IFoo,)
- class Foo(object):
- __implemented__ = impl
- impl.inherit = Foo
- self._callFUT(Foo, IBar)
- # Same spec, now different values
- self.assertTrue(Foo.__implemented__ is impl)
- self.assertEqual(impl.inherit, Foo)
- self.assertEqual(impl.declared, (IFoo, IBar,))
- def test_w_existing_Implements_w_bases(self):
- from zope.interface.declarations import Implements
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- IBar = InterfaceClass('IBar')
- IBaz = InterfaceClass('IBaz', IFoo)
- b_impl = Implements(IBaz)
- impl = Implements(IFoo)
- impl.declared = (IFoo,)
- class Base1(object):
- __implemented__ = b_impl
- class Base2(object):
- __implemented__ = b_impl
- class Foo(Base1, Base2):
- __implemented__ = impl
- impl.inherit = Foo
- self._callFUT(Foo, IBar)
- # Same spec, now different values
- self.assertTrue(Foo.__implemented__ is impl)
- self.assertEqual(impl.inherit, Foo)
- self.assertEqual(impl.declared, (IFoo, IBar,))
- self.assertEqual(impl.__bases__, (IFoo, IBar, b_impl))
- class Test__implements_advice(unittest.TestCase):
- def _callFUT(self, *args, **kw):
- from zope.interface.declarations import _implements_advice
- return _implements_advice(*args, **kw)
- def test_no_existing_implements(self):
- from zope.interface.declarations import classImplements
- from zope.interface.declarations import Implements
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- class Foo(object):
- __implements_advice_data__ = ((IFoo,), classImplements)
- self._callFUT(Foo)
- self.assertFalse('__implements_advice_data__' in Foo.__dict__)
- self.assertTrue(isinstance(Foo.__implemented__, Implements))
- self.assertEqual(list(Foo.__implemented__), [IFoo])
- class Test_implementer(unittest.TestCase):
- def _getTargetClass(self):
- from zope.interface.declarations import implementer
- return implementer
- def _makeOne(self, *args, **kw):
- return self._getTargetClass()(*args, **kw)
- def test_oldstyle_class(self):
- # TODO Py3 story
- from zope.interface.declarations import ClassProvides
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- class Foo:
- pass
- decorator = self._makeOne(IFoo)
- returned = decorator(Foo)
- self.assertTrue(returned is Foo)
- spec = Foo.__implemented__
- self.assertEqual(spec.__name__,
- 'zope.interface.tests.test_declarations.Foo')
- self.assertTrue(spec.inherit is Foo)
- self.assertTrue(Foo.__implemented__ is spec)
- self.assertTrue(isinstance(Foo.__providedBy__, ClassProvides))
- self.assertTrue(isinstance(Foo.__provides__, ClassProvides))
- self.assertEqual(Foo.__provides__, Foo.__providedBy__)
- def test_newstyle_class(self):
- from zope.interface.declarations import ClassProvides
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- class Foo(object):
- pass
- decorator = self._makeOne(IFoo)
- returned = decorator(Foo)
- self.assertTrue(returned is Foo)
- spec = Foo.__implemented__
- self.assertEqual(spec.__name__,
- 'zope.interface.tests.test_declarations.Foo')
- self.assertTrue(spec.inherit is Foo)
- self.assertTrue(Foo.__implemented__ is spec)
- self.assertTrue(isinstance(Foo.__providedBy__, ClassProvides))
- self.assertTrue(isinstance(Foo.__provides__, ClassProvides))
- self.assertEqual(Foo.__provides__, Foo.__providedBy__)
- def test_nonclass_cannot_assign_attr(self):
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- decorator = self._makeOne(IFoo)
- self.assertRaises(TypeError, decorator, object())
- def test_nonclass_can_assign_attr(self):
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- class Foo(object):
- pass
- foo = Foo()
- decorator = self._makeOne(IFoo)
- returned = decorator(foo)
- self.assertTrue(returned is foo)
- spec = foo.__implemented__
- self.assertEqual(spec.__name__, 'zope.interface.tests.test_declarations.?')
- self.assertTrue(spec.inherit is None)
- self.assertTrue(foo.__implemented__ is spec)
- class Test_implementer_only(unittest.TestCase):
- def _getTargetClass(self):
- from zope.interface.declarations import implementer_only
- return implementer_only
- def _makeOne(self, *args, **kw):
- return self._getTargetClass()(*args, **kw)
- def test_function(self):
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- decorator = self._makeOne(IFoo)
- def _function():
- raise NotImplementedError()
- self.assertRaises(ValueError, decorator, _function)
- def test_method(self):
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- decorator = self._makeOne(IFoo)
- class Bar:
- def _method():
- raise NotImplementedError()
- self.assertRaises(ValueError, decorator, Bar._method)
- def test_oldstyle_class(self):
- # TODO Py3 story
- from zope.interface.declarations import Implements
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- IBar = InterfaceClass('IBar')
- old_spec = Implements(IBar)
- class Foo:
- __implemented__ = old_spec
- decorator = self._makeOne(IFoo)
- returned = decorator(Foo)
- self.assertTrue(returned is Foo)
- spec = Foo.__implemented__
- self.assertEqual(spec.__name__, '?')
- self.assertTrue(spec.inherit is None)
- self.assertTrue(Foo.__implemented__ is spec)
- def test_newstyle_class(self):
- from zope.interface.declarations import Implements
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass('IFoo')
- IBar = InterfaceClass('IBar')
- old_spec = Implements(IBar)
- class Foo(object):
- __implemented__ = old_spec
- decorator = self._makeOne(IFoo)
- returned = decorator(Foo)
- self.assertTrue(returned is Foo)
- spec = Foo.__implemented__
- self.assertEqual(spec.__name__, '?')
- self.assertTrue(spec.inherit is None)
- self.assertTrue(Foo.__implemented__ is spec)
- # Test '_implements' by way of 'implements{,Only}', its only callers.
- class Test_implementsOnly(unittest.TestCase, _Py3ClassAdvice):
- def test_simple(self):
- import warnings
- from zope.interface.declarations import implementsOnly
- from zope.interface._compat import PYTHON3
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- globs = {'implementsOnly': implementsOnly,
- 'IFoo': IFoo,
- }
- locs = {}
- CODE = "\n".join([
- 'class Foo(object):'
- ' implementsOnly(IFoo)',
- ])
- with warnings.catch_warnings(record=True) as log:
- warnings.resetwarnings()
- try:
- exec(CODE, globs, locs)
- except TypeError:
- self.assertTrue(PYTHON3, "Must be Python 3")
- else:
- if PYTHON3:
- self.fail("Didn't raise TypeError")
- Foo = locs['Foo']
- spec = Foo.__implemented__
- self.assertEqual(list(spec), [IFoo])
- self.assertEqual(len(log), 0) # no longer warn
- def test_called_once_from_class_w_bases(self):
- from zope.interface.declarations import implements
- from zope.interface.declarations import implementsOnly
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- IBar = InterfaceClass("IBar")
- globs = {'implements': implements,
- 'implementsOnly': implementsOnly,
- 'IFoo': IFoo,
- 'IBar': IBar,
- }
- locs = {}
- CODE = "\n".join([
- 'class Foo(object):',
- ' implements(IFoo)',
- 'class Bar(Foo):'
- ' implementsOnly(IBar)',
- ])
- if self._run_generated_code(CODE, globs, locs):
- Bar = locs['Bar']
- spec = Bar.__implemented__
- self.assertEqual(list(spec), [IBar])
- class Test_implements(unittest.TestCase, _Py3ClassAdvice):
- def test_called_from_function(self):
- import warnings
- from zope.interface.declarations import implements
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- globs = {'implements': implements, 'IFoo': IFoo}
- locs = {}
- CODE = "\n".join([
- 'def foo():',
- ' implements(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.interface.declarations import implements
- from zope.interface.interface import InterfaceClass
- from zope.interface._compat import PYTHON3
- IFoo = InterfaceClass("IFoo")
- IBar = InterfaceClass("IBar")
- globs = {'implements': implements, 'IFoo': IFoo, 'IBar': IBar}
- locs = {}
- CODE = "\n".join([
- 'class Foo(object):',
- ' implements(IFoo)',
- ' implements(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.interface.declarations import implements
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- globs = {'implements': implements, 'IFoo': IFoo}
- locs = {}
- CODE = "\n".join([
- 'class Foo(object):',
- ' implements(IFoo)',
- ])
- if self._run_generated_code(CODE, globs, locs):
- Foo = locs['Foo']
- spec = Foo.__implemented__
- self.assertEqual(list(spec), [IFoo])
- class ProvidesClassTests(unittest.TestCase):
- def _getTargetClass(self):
- from zope.interface.declarations import ProvidesClass
- return ProvidesClass
- def _makeOne(self, *args, **kw):
- return self._getTargetClass()(*args, **kw)
- def test_simple_class_one_interface(self):
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- class Foo(object):
- pass
- spec = self._makeOne(Foo, IFoo)
- self.assertEqual(list(spec), [IFoo])
- def test___reduce__(self):
- from zope.interface.declarations import Provides # the function
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- class Foo(object):
- pass
- spec = self._makeOne(Foo, IFoo)
- klass, args = spec.__reduce__()
- self.assertTrue(klass is Provides)
- self.assertEqual(args, (Foo, IFoo))
- def test___get___class(self):
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- class Foo(object):
- pass
- spec = self._makeOne(Foo, IFoo)
- Foo.__provides__ = spec
- self.assertTrue(Foo.__provides__ is spec)
- def test___get___instance(self):
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- class Foo(object):
- pass
- spec = self._makeOne(Foo, IFoo)
- Foo.__provides__ = spec
- def _test():
- foo = Foo()
- return foo.__provides__
- self.assertRaises(AttributeError, _test)
- class Test_Provides(unittest.TestCase):
- def _callFUT(self, *args, **kw):
- from zope.interface.declarations import Provides
- return Provides(*args, **kw)
- def test_no_cached_spec(self):
- from zope.interface import declarations
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- cache = {}
- class Foo(object):
- pass
- with _Monkey(declarations, InstanceDeclarations=cache):
- spec = self._callFUT(Foo, IFoo)
- self.assertEqual(list(spec), [IFoo])
- self.assertTrue(cache[(Foo, IFoo)] is spec)
- def test_w_cached_spec(self):
- from zope.interface import declarations
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- prior = object()
- class Foo(object):
- pass
- cache = {(Foo, IFoo): prior}
- with _Monkey(declarations, InstanceDeclarations=cache):
- spec = self._callFUT(Foo, IFoo)
- self.assertTrue(spec is prior)
- class Test_directlyProvides(unittest.TestCase):
- def _callFUT(self, *args, **kw):
- from zope.interface.declarations import directlyProvides
- return directlyProvides(*args, **kw)
- def test_w_normal_object(self):
- from zope.interface.declarations import ProvidesClass
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- class Foo(object):
- pass
- obj = Foo()
- self._callFUT(obj, IFoo)
- self.assertTrue(isinstance(obj.__provides__, ProvidesClass))
- self.assertEqual(list(obj.__provides__), [IFoo])
- def test_w_class(self):
- from zope.interface.declarations import ClassProvides
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- class Foo(object):
- pass
- self._callFUT(Foo, IFoo)
- self.assertTrue(isinstance(Foo.__provides__, ClassProvides))
- self.assertEqual(list(Foo.__provides__), [IFoo])
- @_skip_under_py3k
- def test_w_non_descriptor_aware_metaclass(self):
- # There are no non-descriptor-aware types in Py3k
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- class MetaClass(type):
- def __getattribute__(cls, name):
- # Emulate metaclass whose base is not the type object.
- if name == '__class__':
- return cls
- # Under certain circumstances, the implementedByFallback
- # can get here for __dict__
- return type.__getattribute__(cls, name) # pragma: no cover
- class Foo(object):
- __metaclass__ = MetaClass
- obj = Foo()
- self.assertRaises(TypeError, self._callFUT, obj, IFoo)
- def test_w_classless_object(self):
- from zope.interface.declarations import ProvidesClass
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- the_dict = {}
- class Foo(object):
- def __getattribute__(self, name):
- # Emulate object w/o any class
- if name == '__class__':
- return None
- raise NotImplementedError(name)
- def __setattr__(self, name, value):
- the_dict[name] = value
- obj = Foo()
- self._callFUT(obj, IFoo)
- self.assertTrue(isinstance(the_dict['__provides__'], ProvidesClass))
- self.assertEqual(list(the_dict['__provides__']), [IFoo])
- class Test_alsoProvides(unittest.TestCase):
- def _callFUT(self, *args, **kw):
- from zope.interface.declarations import alsoProvides
- return alsoProvides(*args, **kw)
- def test_wo_existing_provides(self):
- from zope.interface.declarations import ProvidesClass
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- class Foo(object):
- pass
- obj = Foo()
- self._callFUT(obj, IFoo)
- self.assertTrue(isinstance(obj.__provides__, ProvidesClass))
- self.assertEqual(list(obj.__provides__), [IFoo])
- def test_w_existing_provides(self):
- from zope.interface.declarations import directlyProvides
- from zope.interface.declarations import ProvidesClass
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- IBar = InterfaceClass("IBar")
- class Foo(object):
- pass
- obj = Foo()
- directlyProvides(obj, IFoo)
- self._callFUT(obj, IBar)
- self.assertTrue(isinstance(obj.__provides__, ProvidesClass))
- self.assertEqual(list(obj.__provides__), [IFoo, IBar])
- class Test_noLongerProvides(unittest.TestCase):
- def _callFUT(self, *args, **kw):
- from zope.interface.declarations import noLongerProvides
- return noLongerProvides(*args, **kw)
- def test_wo_existing_provides(self):
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- class Foo(object):
- pass
- obj = Foo()
- self._callFUT(obj, IFoo)
- self.assertEqual(list(obj.__provides__), [])
- def test_w_existing_provides_hit(self):
- from zope.interface.declarations import directlyProvides
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- class Foo(object):
- pass
- obj = Foo()
- directlyProvides(obj, IFoo)
- self._callFUT(obj, IFoo)
- self.assertEqual(list(obj.__provides__), [])
- def test_w_existing_provides_miss(self):
- from zope.interface.declarations import directlyProvides
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- IBar = InterfaceClass("IBar")
- class Foo(object):
- pass
- obj = Foo()
- directlyProvides(obj, IFoo)
- self._callFUT(obj, IBar)
- self.assertEqual(list(obj.__provides__), [IFoo])
- def test_w_iface_implemented_by_class(self):
- from zope.interface.declarations import implementer
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- @implementer(IFoo)
- class Foo(object):
- pass
- obj = Foo()
- self.assertRaises(ValueError, self._callFUT, obj, IFoo)
- class ClassProvidesBaseFallbackTests(unittest.TestCase):
- def _getTargetClass(self):
- from zope.interface.declarations import ClassProvidesBaseFallback
- return ClassProvidesBaseFallback
- def _makeOne(self, klass, implements):
- # Don't instantiate directly: the C version can't have attributes
- # assigned.
- class Derived(self._getTargetClass()):
- def __init__(self, k, i):
- self._cls = k
- self._implements = i
- return Derived(klass, implements)
- def test_w_same_class_via_class(self):
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- class Foo(object):
- pass
- cpbp = Foo.__provides__ = self._makeOne(Foo, IFoo)
- self.assertTrue(Foo.__provides__ is cpbp)
- def test_w_same_class_via_instance(self):
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- class Foo(object):
- pass
- foo = Foo()
- cpbp = Foo.__provides__ = self._makeOne(Foo, IFoo)
- self.assertTrue(foo.__provides__ is IFoo)
- def test_w_different_class(self):
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- class Foo(object):
- pass
- class Bar(Foo):
- pass
- bar = Bar()
- cpbp = Foo.__provides__ = self._makeOne(Foo, IFoo)
- self.assertRaises(AttributeError, getattr, Bar, '__provides__')
- self.assertRaises(AttributeError, getattr, bar, '__provides__')
- class ClassProvidesBaseTests(ClassProvidesBaseFallbackTests):
- # Repeat tests for C optimizations
- def _getTargetClass(self):
- from zope.interface.declarations import ClassProvidesBase
- return ClassProvidesBase
- def test_optimizations(self):
- from zope.interface.declarations import ClassProvidesBaseFallback
- try:
- import zope.interface._zope_interface_coptimizations
- except ImportError:
- self.assertIs(self._getTargetClass(), ClassProvidesBaseFallback)
- else:
- self.assertIsNot(self._getTargetClass(), ClassProvidesBaseFallback)
- class ClassProvidesTests(unittest.TestCase):
- def _getTargetClass(self):
- from zope.interface.declarations import ClassProvides
- return ClassProvides
- def _makeOne(self, *args, **kw):
- return self._getTargetClass()(*args, **kw)
- def test_w_simple_metaclass(self):
- from zope.interface.declarations import implementer
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- IBar = InterfaceClass("IBar")
- @implementer(IFoo)
- class Foo(object):
- pass
- cp = Foo.__provides__ = self._makeOne(Foo, type(Foo), IBar)
- self.assertTrue(Foo.__provides__ is cp)
- self.assertEqual(list(Foo().__provides__), [IFoo])
- def test___reduce__(self):
- from zope.interface.declarations import implementer
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- IBar = InterfaceClass("IBar")
- @implementer(IFoo)
- class Foo(object):
- pass
- cp = Foo.__provides__ = self._makeOne(Foo, type(Foo), IBar)
- self.assertEqual(cp.__reduce__(),
- (self._getTargetClass(), (Foo, type(Foo), IBar)))
- class Test_directlyProvidedBy(unittest.TestCase):
- def _callFUT(self, *args, **kw):
- from zope.interface.declarations import directlyProvidedBy
- return directlyProvidedBy(*args, **kw)
- def test_wo_declarations_in_class_or_instance(self):
- class Foo(object):
- pass
- foo = Foo()
- self.assertEqual(list(self._callFUT(foo)), [])
- def test_w_declarations_in_class_but_not_instance(self):
- from zope.interface.declarations import implementer
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- @implementer(IFoo)
- class Foo(object):
- pass
- foo = Foo()
- self.assertEqual(list(self._callFUT(foo)), [])
- def test_w_declarations_in_instance_but_not_class(self):
- from zope.interface.declarations import directlyProvides
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- class Foo(object):
- pass
- foo = Foo()
- directlyProvides(foo, IFoo)
- self.assertEqual(list(self._callFUT(foo)), [IFoo])
- def test_w_declarations_in_instance_and_class(self):
- from zope.interface.declarations import directlyProvides
- from zope.interface.declarations import implementer
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- IBar = InterfaceClass("IBar")
- @implementer(IFoo)
- class Foo(object):
- pass
- foo = Foo()
- directlyProvides(foo, IBar)
- self.assertEqual(list(self._callFUT(foo)), [IBar])
- class Test_classProvides(unittest.TestCase, _Py3ClassAdvice):
- def test_called_from_function(self):
- import warnings
- from zope.interface.declarations import classProvides
- from zope.interface.interface import InterfaceClass
- from zope.interface._compat import PYTHON3
- IFoo = InterfaceClass("IFoo")
- globs = {'classProvides': classProvides, 'IFoo': IFoo}
- locs = {}
- CODE = "\n".join([
- 'def foo():',
- ' classProvides(IFoo)'
- ])
- exec(CODE, globs, locs)
- foo = locs['foo']
- with warnings.catch_warnings(record=True) as log:
- warnings.resetwarnings()
- self.assertRaises(TypeError, foo)
- if not PYTHON3:
- self.assertEqual(len(log), 0) # no longer warn
- def test_called_twice_from_class(self):
- import warnings
- from zope.interface.declarations import classProvides
- from zope.interface.interface import InterfaceClass
- from zope.interface._compat import PYTHON3
- IFoo = InterfaceClass("IFoo")
- IBar = InterfaceClass("IBar")
- globs = {'classProvides': classProvides, 'IFoo': IFoo, 'IBar': IBar}
- locs = {}
- CODE = "\n".join([
- 'class Foo(object):',
- ' classProvides(IFoo)',
- ' classProvides(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.interface.declarations import classProvides
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- globs = {'classProvides': classProvides, 'IFoo': IFoo}
- locs = {}
- CODE = "\n".join([
- 'class Foo(object):',
- ' classProvides(IFoo)',
- ])
- if self._run_generated_code(CODE, globs, locs):
- Foo = locs['Foo']
- spec = Foo.__providedBy__
- self.assertEqual(list(spec), [IFoo])
- # Test _classProvides_advice through classProvides, its only caller.
- class Test_provider(unittest.TestCase):
- def _getTargetClass(self):
- from zope.interface.declarations import provider
- return provider
- def _makeOne(self, *args, **kw):
- return self._getTargetClass()(*args, **kw)
- def test_w_class(self):
- from zope.interface.declarations import ClassProvides
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- @self._makeOne(IFoo)
- class Foo(object):
- pass
- self.assertTrue(isinstance(Foo.__provides__, ClassProvides))
- self.assertEqual(list(Foo.__provides__), [IFoo])
- class Test_moduleProvides(unittest.TestCase):
- def test_called_from_function(self):
- from zope.interface.declarations import moduleProvides
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- globs = {'__name__': 'zope.interface.tests.foo',
- 'moduleProvides': moduleProvides, 'IFoo': IFoo}
- locs = {}
- CODE = "\n".join([
- 'def foo():',
- ' moduleProvides(IFoo)'
- ])
- exec(CODE, globs, locs)
- foo = locs['foo']
- self.assertRaises(TypeError, foo)
- def test_called_from_class(self):
- from zope.interface.declarations import moduleProvides
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- globs = {'__name__': 'zope.interface.tests.foo',
- 'moduleProvides': moduleProvides, 'IFoo': IFoo}
- locs = {}
- CODE = "\n".join([
- 'class Foo(object):',
- ' moduleProvides(IFoo)',
- ])
- with self.assertRaises(TypeError):
- exec(CODE, globs, locs)
- def test_called_once_from_module_scope(self):
- from zope.interface.declarations import moduleProvides
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- globs = {'__name__': 'zope.interface.tests.foo',
- 'moduleProvides': moduleProvides, 'IFoo': IFoo}
- CODE = "\n".join([
- 'moduleProvides(IFoo)',
- ])
- exec(CODE, globs)
- spec = globs['__provides__']
- self.assertEqual(list(spec), [IFoo])
- def test_called_twice_from_module_scope(self):
- from zope.interface.declarations import moduleProvides
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- globs = {'__name__': 'zope.interface.tests.foo',
- 'moduleProvides': moduleProvides, 'IFoo': IFoo}
- locs = {}
- CODE = "\n".join([
- 'moduleProvides(IFoo)',
- 'moduleProvides(IFoo)',
- ])
- with self.assertRaises(TypeError):
- exec(CODE, globs)
- class Test_getObjectSpecificationFallback(unittest.TestCase):
- def _callFUT(self, *args, **kw):
- from zope.interface.declarations import getObjectSpecificationFallback
- return getObjectSpecificationFallback(*args, **kw)
- def test_wo_existing_provides_classless(self):
- the_dict = {}
- class Foo(object):
- def __getattribute__(self, name):
- # Emulate object w/o any class
- if name == '__class__':
- raise AttributeError(name)
- try:
- return the_dict[name]
- except KeyError:
- raise AttributeError(name)
- def __setattr__(self, name, value):
- raise NotImplementedError()
- foo = Foo()
- spec = self._callFUT(foo)
- self.assertEqual(list(spec), [])
- def test_existing_provides_is_spec(self):
- from zope.interface.declarations import directlyProvides
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- def foo():
- raise NotImplementedError()
- directlyProvides(foo, IFoo)
- spec = self._callFUT(foo)
- self.assertTrue(spec is foo.__provides__)
- def test_existing_provides_is_not_spec(self):
- def foo():
- raise NotImplementedError()
- foo.__provides__ = object() # not a valid spec
- spec = self._callFUT(foo)
- self.assertEqual(list(spec), [])
- def test_existing_provides(self):
- from zope.interface.declarations import directlyProvides
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- class Foo(object):
- pass
- foo = Foo()
- directlyProvides(foo, IFoo)
- spec = self._callFUT(foo)
- self.assertEqual(list(spec), [IFoo])
- def test_wo_provides_on_class_w_implements(self):
- from zope.interface.declarations import implementer
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- @implementer(IFoo)
- class Foo(object):
- pass
- foo = Foo()
- spec = self._callFUT(foo)
- self.assertEqual(list(spec), [IFoo])
- def test_wo_provides_on_class_wo_implements(self):
- class Foo(object):
- pass
- foo = Foo()
- spec = self._callFUT(foo)
- self.assertEqual(list(spec), [])
- class Test_getObjectSpecification(Test_getObjectSpecificationFallback):
- # Repeat tests for C optimizations
- def _callFUT(self, *args, **kw):
- from zope.interface.declarations import getObjectSpecification
- return getObjectSpecification(*args, **kw)
- def test_optimizations(self):
- from zope.interface.declarations import getObjectSpecificationFallback
- from zope.interface.declarations import getObjectSpecification
- try:
- import zope.interface._zope_interface_coptimizations
- except ImportError:
- self.assertIs(getObjectSpecification,
- getObjectSpecificationFallback)
- else:
- self.assertIsNot(getObjectSpecification,
- getObjectSpecificationFallback)
- class Test_providedByFallback(unittest.TestCase):
- def _callFUT(self, *args, **kw):
- from zope.interface.declarations import providedByFallback
- return providedByFallback(*args, **kw)
- def test_wo_providedBy_on_class_wo_implements(self):
- class Foo(object):
- pass
- foo = Foo()
- spec = self._callFUT(foo)
- self.assertEqual(list(spec), [])
- def test_w_providedBy_valid_spec(self):
- from zope.interface.declarations import Provides
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- class Foo(object):
- pass
- foo = Foo()
- foo.__providedBy__ = Provides(Foo, IFoo)
- spec = self._callFUT(foo)
- self.assertEqual(list(spec), [IFoo])
- def test_w_providedBy_invalid_spec(self):
- class Foo(object):
- pass
- foo = Foo()
- foo.__providedBy__ = object()
- spec = self._callFUT(foo)
- self.assertEqual(list(spec), [])
- def test_w_providedBy_invalid_spec_class_w_implements(self):
- from zope.interface.declarations import implementer
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- @implementer(IFoo)
- class Foo(object):
- pass
- foo = Foo()
- foo.__providedBy__ = object()
- spec = self._callFUT(foo)
- self.assertEqual(list(spec), [IFoo])
- def test_w_providedBy_invalid_spec_w_provides_no_provides_on_class(self):
- class Foo(object):
- pass
- foo = Foo()
- foo.__providedBy__ = object()
- expected = foo.__provides__ = object()
- spec = self._callFUT(foo)
- self.assertTrue(spec is expected)
- def test_w_providedBy_invalid_spec_w_provides_diff_provides_on_class(self):
- class Foo(object):
- pass
- foo = Foo()
- foo.__providedBy__ = object()
- expected = foo.__provides__ = object()
- Foo.__provides__ = object()
- spec = self._callFUT(foo)
- self.assertTrue(spec is expected)
- def test_w_providedBy_invalid_spec_w_provides_same_provides_on_class(self):
- from zope.interface.declarations import implementer
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- @implementer(IFoo)
- class Foo(object):
- pass
- foo = Foo()
- foo.__providedBy__ = object()
- foo.__provides__ = Foo.__provides__ = object()
- spec = self._callFUT(foo)
- self.assertEqual(list(spec), [IFoo])
- class Test_providedBy(Test_providedByFallback):
- # Repeat tests for C optimizations
- def _callFUT(self, *args, **kw):
- from zope.interface.declarations import providedBy
- return providedBy(*args, **kw)
- def test_optimizations(self):
- from zope.interface.declarations import providedByFallback
- from zope.interface.declarations import providedBy
- try:
- import zope.interface._zope_interface_coptimizations
- except ImportError:
- self.assertIs(providedBy, providedByFallback)
- else:
- self.assertIsNot(providedBy, providedByFallback)
- class ObjectSpecificationDescriptorFallbackTests(unittest.TestCase):
- def _getTargetClass(self):
- from zope.interface.declarations \
- import ObjectSpecificationDescriptorFallback
- return ObjectSpecificationDescriptorFallback
- def _makeOne(self, *args, **kw):
- return self._getTargetClass()(*args, **kw)
- def test_accessed_via_class(self):
- from zope.interface.declarations import Provides
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- class Foo(object):
- pass
- Foo.__provides__ = Provides(Foo, IFoo)
- Foo.__providedBy__ = self._makeOne()
- self.assertEqual(list(Foo.__providedBy__), [IFoo])
- def test_accessed_via_inst_wo_provides(self):
- from zope.interface.declarations import implementer
- from zope.interface.declarations import Provides
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- IBar = InterfaceClass("IBar")
- @implementer(IFoo)
- class Foo(object):
- pass
- Foo.__provides__ = Provides(Foo, IBar)
- Foo.__providedBy__ = self._makeOne()
- foo = Foo()
- self.assertEqual(list(foo.__providedBy__), [IFoo])
- def test_accessed_via_inst_w_provides(self):
- from zope.interface.declarations import directlyProvides
- from zope.interface.declarations import implementer
- from zope.interface.declarations import Provides
- from zope.interface.interface import InterfaceClass
- IFoo = InterfaceClass("IFoo")
- IBar = InterfaceClass("IBar")
- IBaz = InterfaceClass("IBaz")
- @implementer(IFoo)
- class Foo(object):
- pass
- Foo.__provides__ = Provides(Foo, IBar)
- Foo.__providedBy__ = self._makeOne()
- foo = Foo()
- directlyProvides(foo, IBaz)
- self.assertEqual(list(foo.__providedBy__), [IBaz, IFoo])
- class ObjectSpecificationDescriptorTests(
- ObjectSpecificationDescriptorFallbackTests):
- # Repeat tests for C optimizations
- def _getTargetClass(self):
- from zope.interface.declarations import ObjectSpecificationDescriptor
- return ObjectSpecificationDescriptor
- def test_optimizations(self):
- from zope.interface.declarations import (
- ObjectSpecificationDescriptorFallback)
- try:
- import zope.interface._zope_interface_coptimizations
- except ImportError:
- self.assertIs(self._getTargetClass(),
- ObjectSpecificationDescriptorFallback)
- else:
- self.assertIsNot(self._getTargetClass(),
- ObjectSpecificationDescriptorFallback)
- # Test _normalizeargs through its callers.
- class _Monkey(object):
- # context-manager for replacing module names in the scope of a test.
- def __init__(self, module, **kw):
- self.module = module
- self.to_restore = dict([(key, getattr(module, key)) for key in kw])
- for key, value in kw.items():
- setattr(module, key, value)
- def __enter__(self):
- return self
- def __exit__(self, exc_type, exc_val, exc_tb):
- for key, value in self.to_restore.items():
- setattr(self.module, key, value)
- class _MonkeyDict(object):
- # context-manager for restoring a dict w/in a module in the scope of a test.
- def __init__(self, module, attrname, **kw):
- self.module = module
- self.target = getattr(module, attrname)
- self.to_restore = self.target.copy()
- self.target.clear()
- self.target.update(kw)
- def __enter__(self):
- return self.target
- def __exit__(self, exc_type, exc_val, exc_tb):
- self.target.clear()
- self.target.update(self.to_restore)
|