o
    ¯bâd  ã                   @   s  d Z ddlmZ ddlmZmZmZ ddlmZ ddl	m
Z
 ddlmZmZ ddlmZmZmZ ddlmZ G d	d
„ d
e
jƒZG dd„ deƒZG dd„ deƒZeeƒG dd„ de
jƒƒZeeƒG dd„ de
jƒƒZG dd„ de
jƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZ G dd „ d eƒZ!eee e!ƒG d!d"„ d"e
jƒƒZ"eeƒG d#d$„ d$ƒƒZ#G d%d&„ d&ƒZ$G d'd(„ d(ej%e$ƒZ&G d)d*„ d*ej%ƒZ'G d+d,„ d,eƒZ(ee(ƒG d-d.„ d.e
jƒƒZ)ee(ƒG d/d0„ d0e
jƒƒZ*G d1d2„ d2ƒZ+G d3d4„ d4e
jƒZ,ee(ƒG d5d6„ d6e
jƒƒZ-G d7d8„ d8eƒZ.G d9d:„ d:eƒZ/ee.ƒG d;d<„ d<ƒƒZ0eG d=d>„ d>ƒƒZ1G d?d@„ d@e$ej%ƒZ2G dAdB„ dBe$ej%ƒZ3G dCdD„ dDeƒZ4G dEdF„ dFe4ƒZ5ee4ƒG dGdH„ dHƒƒZ6ee5ƒG dIdJ„ dJƒƒZ7G dKdL„ dLeƒZ8G dMdN„ dNƒZ9G dOdP„ dPej%ƒZ:dQS )Rz0
Test cases for Twisted component architecture.
é    )Úwraps)Ú	AttributeÚ	InterfaceÚimplementer)ÚAdapterRegistry)Ú
components)ÚcmpÚ
comparable)Ú_addHookÚ_removeHookÚproxyForInterface)Úunittestc                   @   ó   e Zd ZdZdd„ ZdS )ÚCompor   c                 C   s   | j d | _ | j S ©Né   ©Únum©Úself© r   úE/usr/lib/python3/dist-packages/twisted/python/test/test_components.pyÚinc   s   z	Compo.incN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r      ó    r   c                   @   ó   e Zd Zdd„ ZdS )ÚIAdeptc                   C   s   t ƒ ‚©N)ÚNotImplementedErrorr   r   r   r   ÚadaptorFunc   s   zIAdept.adaptorFuncN)r   r   r   r!   r   r   r   r   r      ó    r   c                   @   r   )ÚIElapsedc                   C   ó   dS )z
        1!
        Nr   r   r   r   r   ÚelapsedFunc#   ó    zIElapsed.elapsedFuncN©r   r   r   r%   r   r   r   r   r#   "   r"   r#   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚAdeptc                 C   s   || _ d| _d S ©Nr   ©Úoriginalr   ©r   Úorigr   r   r   Ú__init__+   ó   
zAdept.__init__c                 C   s   | j d | _ | j | j ¡ fS r   )r   r+   r   r   r   r   r   r!   /   s   zAdept.adaptorFuncN)r   r   r   r.   r!   r   r   r   r   r(   )   s    r(   c                   @   r   )ÚElapsedc                 C   r$   r   r   r   r   r   r   r%   6   ó   zElapsed.elapsedFuncNr'   r   r   r   r   r0   4   ó    r0   c                   @   ó   e Zd ZdS )ÚACompN©r   r   r   r   r   r   r   r4   :   ó    r4   c                   @   r3   )ÚBCompNr5   r   r   r   r   r7   >   r6   r7   c                   @   r3   )ÚCCompNr5   r   r   r   r   r8   B   r6   r8   c                   @   r3   )ÚITestNr5   r   r   r   r   r9   F   r6   r9   c                   @   r3   )ÚITest2Nr5   r   r   r   r   r:   J   r6   r:   c                   @   r3   )ÚITest3Nr5   r   r   r   r   r;   N   r6   r;   c                   @   r3   )ÚITest4Nr5   r   r   r   r   r<   R   r6   r<   c                   @   r   )ÚTestc                 C   ó   d S r   r   r,   r   r   r   r.   X   r1   zTest.__init__N©r   r   r   r.   r   r   r   r   r=   V   r2   r=   c                   @   r   )ÚTest2r   c                 C   r>   r   r   r,   r   r   r   r.   `   r1   zTest2.__init__N)r   r   r   ÚtemporaryAdapterr.   r   r   r   r   r@   \   s    r@   c                   @   r   )ÚRegistryUsingMixinzH
    Mixin for test cases which modify the global registry somehow.
    c                 C   s,   t ƒ }|  td|¡ t|ƒ}|  t|¡ dS )z‹
        Configure L{twisted.python.components.registerAdapter} to mutate an
        alternate registry to improve test isolation.
        ÚglobalRegistryN)r   Úpatchr   r
   Ú
addCleanupr   )r   ÚscratchRegistryÚhookr   r   r   ÚsetUpi   s   zRegistryUsingMixin.setUpN)r   r   r   Ú__doc__rH   r   r   r   r   rB   d   ó    rB   c                   @   s`   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZdS )ÚComponentizedTestsz8
    Simple test case for caching in Componentized.
    c                 C   s8   t  | ¡ t ttt¡ t ttt¡ t ttt	¡ d S r   )
rB   rH   r   ÚregisterAdapterr=   r4   r9   r;   r@   r:   r   r   r   r   rH   ~   s   
zComponentizedTests.setUpc                 C   sj   t  ttt¡ t  ttt¡ tƒ }| t¡ ¡ dksJ ‚| t¡ ¡ dks'J ‚tt|ƒƒ 	¡ dks3J ‚d S )N)r   r   )é   rM   r   )
r   rL   r(   r   r   r0   r#   ÚgetComponentr!   r%   ©r   Úcr   r   r   ÚtestComponentized…   s   z$ComponentizedTests.testComponentizedc                 C   s€   t ƒ }| t¡}| t¡}| t¡}| t¡}||u sJ ‚||us#J ‚| |¡ | t¡}| t¡}||u s8J ‚||us>J ‚d S r   )r8   rN   r9   r:   ÚremoveComponent)r   rP   Úco1Úco2Úco3Úco4Úco5Úco6r   r   r   ÚtestInheritanceAdaptationŽ   s   






z,ComponentizedTests.testInheritanceAdaptationc                 C   s>   t ƒ }| t¡}| t¡}| t¡}|  |¡ |  ||¡ d S r   )r8   rN   r9   r;   r<   ÚassertIsNoneÚassertIs)r   rP   rS   rU   rV   r   r   r   ÚtestMultiAdapterœ   s   



z#ComponentizedTests.testMultiAdapterc                 C   sN   t  ¡ }tƒ }|  | t|¡|¡ |  |jt|d�|¡ |  | t¡d¡ dS )z‘
        Test that a default value specified to Componentized.getComponent if
        there is no component for the requested interface.
        )ÚdefaultN)r   ÚComponentizedÚobjectr[   rN   r9   )r   Úcomponentizedr]   r   r   r   Útest_getComponentDefaults¤   s
   z,ComponentizedTests.test_getComponentDefaultsc                 C   s<   t  ¡ }| tt¡ | t¡}|  |j|¡ |  |t¡ dS )zŽ
        C{Componentized.setAdapter} sets a component for an interface by
        wrapping the instance with the given adapter class.
        N)	r   r^   Ú
setAdapterr   r(   rN   ÚassertEqualr+   ÚassertIsInstance©r   r`   Ú	componentr   r   r   Útest_setAdapter¯   s
   
z"ComponentizedTests.test_setAdapterc                 C   s>   t  ¡ }|jtdd� | t¡}|  |j|¡ |  |t¡ dS )z˜
        C{Componentized.setAdapter} adapts the instance by wrapping it with
        given adapter class, then stores it using C{addComponent}.
        T)ÚignoreClassN)	r   r^   Ú
addAdapterr(   rN   r   rc   r+   rd   re   r   r   r   Útest_addAdapterº   s
   
z"ComponentizedTests.test_addAdapterc                 C   s0   t  ¡ }tƒ }| t|¡ |  | t¡|¡ dS )zx
        C{Componentized.setComponent} stores the given component using the
        given interface as the key.
        N)r   r^   r_   ÚsetComponentr9   r[   rN   ©r   r`   Úobjr   r   r   Útest_setComponentÅ   s   z$ComponentizedTests.test_setComponentc                 C   s8   t  ¡ }tƒ }| t|¡ | t¡ |  | t¡¡ dS )zm
        C{Componentized.setComponent} removes the cached component for the
        given interface.
        N)r   r^   r_   rk   r9   ÚunsetComponentrZ   rN   rl   r   r   r   Útest_unsetComponentÏ   s
   
z&ComponentizedTests.test_unsetComponentc                 C   s8   t  ¡ }| td¡ t|ƒ}|  d|¡ |  d|¡ dS )zR
        C{ReprableComponentized} has a C{__repr__} that lists its cache.
        Úhellor9   N)r   ÚReprableComponentizedrk   r9   ÚreprÚassertIn)r   ÚrcÚresultr   r   r   Útest_reprableComponentizedÚ   s
   z-ComponentizedTests.test_reprableComponentizedN)r   r   r   rI   rH   rQ   rY   r\   ra   rg   rj   rn   rp   rw   r   r   r   r   rK   y   s    	
rK   c                   @   r   )ÚAdapterTestszTest adapters.c                 C   s2   t ƒ }t|ƒ}|  tjt|¡ |  t|d ƒ¡ d S r   )r_   r(   ÚassertRaisesr   ÚCannotAdaptr9   rZ   )r   ÚoÚar   r   r   ÚtestAdapterGetComponentè   s   z$AdapterTests.testAdapterGetComponentN)r   r   r   rI   r}   r   r   r   r   rx   å   r   rx   c                   @   r3   )ÚIMetaNr5   r   r   r   r   r~   ï   r6   r~   c                   @   r   )Ú	MetaAdderc                 C   s   | j j| S r   r*   ©r   r   r   r   r   Úaddõ   ó   zMetaAdder.addN©r   r   r   r�   r   r   r   r   r   ó   r2   r   c                   @   r   )ÚBackwardsAdderc                 C   s   | j j| S r   r*   r€   r   r   r   r�   û   r‚   zBackwardsAdder.addNrƒ   r   r   r   r   r„   ù   r2   r„   c                   @   r   )Ú
MetaNumberz9
    Integer wrapper for Interface adaptation tests.
    c                 C   ó
   || _ d S r   r   r€   r   r   r   r.     ó   
zMetaNumber.__init__N)r   r   r   rI   r.   r   r   r   r   r…   ÿ   rJ   r…   c                   @   r   )ÚComponentNumberc                 C   s   d| _ tj | ¡ d S r)   )r   r   r^   r.   r   r   r   r   r.   	  s   zComponentNumber.__init__Nr?   r   r   r   r   rˆ     r"   rˆ   c                   @   ó    e Zd ZdZdd„ Zdd„ ZdS )ÚComponentAdderz0
    Adder for componentized adapter tests.
    c                 C   s   t j | |¡ | jj| _d S r   )r   ÚAdapterr.   r+   r   ©r   r+   r   r   r   r.     s   zComponentAdder.__init__c                 C   s   |  j |7  _ | j S r   r   r€   r   r   r   r�     s   zComponentAdder.addN)r   r   r   rI   r.   r�   r   r   r   r   rŠ     s    rŠ   c                   @   r   )ÚIAttrXz=
    Base interface for test of adapter with C{__cmp__}.
    c                   C   r$   )z!
        Return a value.
        Nr   r   r   r   r   Úx"  r&   zIAttrX.xN©r   r   r   rI   rŽ   r   r   r   r   r�     rJ   r�   c                   @   r   )ÚIAttrXXz@
    Adapted interface for test of adapter with C{__cmp__}.
    c                   C   r$   )z+
        Return a tuple of values.
        Nr   r   r   r   r   Úxx-  r&   z
IAttrXX.xxN)r   r   r   rI   r‘   r   r   r   r   r�   (  rJ   r�   c                   @   r   )ÚXcellentzG
    L{IAttrX} implementation for test of adapter with C{__cmp__}.
    c                 C   r$   )z;
        Return a value.

        @return: a value
        úx!r   r   r   r   r   rŽ   9  s   z
Xcellent.xNr�   r   r   r   r   r’   3  s    r’   c                   @   s,   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	S )
ÚDoubleXAdapterz
    Adapter with __cmp__.
    é*   c                 C   r†   r   )r+   rŒ   r   r   r   r.   J  r‡   zDoubleXAdapter.__init__c                 C   s   | j  ¡ | j  ¡ fS r   )r+   rŽ   r   r   r   r   r‘   M  s   zDoubleXAdapter.xxc                 C   s   t | j|jƒS r   )r   r   )r   Úotherr   r   r   Ú__cmp__P  s   zDoubleXAdapter.__cmp__N)r   r   r   rI   r   r.   r‘   r—   r   r   r   r   r”   B  s    r”   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚMetaInterfaceTestsc                 C   s0   t  ttt¡ tdƒ}|  t|ƒ d¡d¡ dS )zS
        Registered adapters can be used to adapt classes to an interface.
        r   rM   N)r   rL   r   r…   r~   rc   r�   )r   Únr   r   r   Ú
test_basicU  s   zMetaInterfaceTests.test_basicc                 C   sJ   t  ttt¡ tƒ }t|ƒ d¡ t|ƒ d¡ |  t|ƒ d¡d¡ d S )Nr   é   )r   rL   rŠ   rˆ   r~   r�   rc   rO   r   r   r   ÚtestComponentizedInteraction]  s
   z/MetaInterfaceTests.testComponentizedInteractionc                 C   s,   t  ttt¡ ttƒ ƒ}|  d| ¡ ¡ d S )N)r“   r“   )r   rL   r”   r�   r�   r’   rc   r‘   )r   r‘   r   r   r   ÚtestAdapterWithCmpd  s   
z%MetaInterfaceTests.testAdapterWithCmpN)r   r   r   rš   rœ   r�   r   r   r   r   r˜   T  s    r˜   c                   @   sˆ   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd S )!ÚRegistrationTestsz)
    Tests for adapter registration.
    c                 C   s0   dd„ }t  ||t¡ |  t  |td¡|¡ dS )zÌ
        Register an adapter with L{components.registerAdapter} for the given
        class or interface and verify that the adapter can be looked up with
        L{components.getAdapterFactory}.
        c                 S   r>   r   r   ©r{   r   r   r   Ú<lambda>v  r&   zGRegistrationTests._registerAdapterForClassOrInterface.<locals>.<lambda>N©r   rL   r9   r[   ÚgetAdapterFactory©r   r+   Úadapterr   r   r   Ú#_registerAdapterForClassOrInterfacep  s   z5RegistrationTests._registerAdapterForClassOrInterfacec                 C   ó   G dd„ dƒ}|   |¡S )za
        Test that an adapter from a class can be registered and then looked
        up.
        c                   @   r3   )zCRegistrationTests.test_registerAdapterForClass.<locals>.TheOriginalNr5   r   r   r   r   ÚTheOriginal€  r6   r§   )r¥   ©r   r§   r   r   r   Útest_registerAdapterForClassz  ó   
z.RegistrationTests.test_registerAdapterForClassc                 C   ó
   |   t¡S )zf
        Test that an adapter from an interface can be registered and then
        looked up.
        )r¥   r:   r   r   r   r   Ú test_registerAdapterForInterface…  ó   
z2RegistrationTests.test_registerAdapterForInterfacec                 C   sL   dd„ }dd„ }t  ||t¡ |  tt j||t¡ |  t  |td¡|¡ dS )z˜
        Verify that L{components.registerAdapter} raises L{ValueError} if the
        from-type/interface and to-interface pair is not unique.
        c                 S   r$   ©NFr   rŸ   r   r   r   r    ‘  r&   zHRegistrationTests._duplicateAdapterForClassOrInterface.<locals>.<lambda>c                 S   r$   ©NTr   rŸ   r   r   r   r    ’  r&   N)r   rL   r9   ry   Ú
ValueErrorr[   r¢   )r   r+   ÚfirstAdapterÚsecondAdapterr   r   r   Ú$_duplicateAdapterForClassOrInterfaceŒ  s   ÿz6RegistrationTests._duplicateAdapterForClassOrInterfacec                 C   r¦   )zz
        Test that attempting to register a second adapter from a class
        raises the appropriate exception.
        c                   @   r3   )zDRegistrationTests.test_duplicateAdapterForClass.<locals>.TheOriginalNr5   r   r   r   r   r§      r6   r§   )r³   r¨   r   r   r   Útest_duplicateAdapterForClassš  rª   z/RegistrationTests.test_duplicateAdapterForClassc                 C   r«   )z
        Test that attempting to register a second adapter from an interface
        raises the appropriate exception.
        )r³   r:   r   r   r   r   Ú!test_duplicateAdapterForInterface¥  r­   z3RegistrationTests.test_duplicateAdapterForInterfacec                 C   sš   dd„ }dd„ }G dd„ dt ƒ}t |||¡ dt_zt |||¡ |  t ||d¡|¡ W dt_ndt_w |  ttj|||¡ |  t ||d¡|¡ dS )	zÖ
        Verify that when C{components.ALLOW_DUPLICATES} is set to C{True}, new
        adapter registrations for a particular from-type/interface and
        to-interface pair replace older registrations.
        c                 S   r$   r®   r   rŸ   r   r   r   r    ²  r&   zORegistrationTests._duplicateAdapterForClassOrInterfaceAllowed.<locals>.<lambda>c                 S   r$   r¯   r   rŸ   r   r   r   r    ³  r&   c                   @   r3   )zSRegistrationTests._duplicateAdapterForClassOrInterfaceAllowed.<locals>.TheInterfaceNr5   r   r   r   r   ÚTheInterfaceµ  r6   r¶   TNF)r   r   rL   ÚALLOW_DUPLICATESr[   r¢   ry   r°   )r   r+   r±   r²   r¶   r   r   r   Ú+_duplicateAdapterForClassOrInterfaceAllowed¬  s$   þÿÿz=RegistrationTests._duplicateAdapterForClassOrInterfaceAllowedc                 C   r¦   )z¿
        Test that when L{components.ALLOW_DUPLICATES} is set to a true
        value, duplicate registrations from classes are allowed to override
        the original registration.
        c                   @   r3   )zKRegistrationTests.test_duplicateAdapterForClassAllowed.<locals>.TheOriginalNr5   r   r   r   r   r§   Ó  r6   r§   )r¸   r¨   r   r   r   Ú$test_duplicateAdapterForClassAllowedÌ  s   
z6RegistrationTests.test_duplicateAdapterForClassAllowedc                 C   s   G dd„ dt ƒ}|  |¡S )zÂ
        Test that when L{components.ALLOW_DUPLICATES} is set to a true
        value, duplicate registrations from interfaces are allowed to
        override the original registration.
        c                   @   r3   )zORegistrationTests.test_duplicateAdapterForInterfaceAllowed.<locals>.TheOriginalNr5   r   r   r   r   r§   ß  r6   r§   )r   r¸   r¨   r   r   r   Ú(test_duplicateAdapterForInterfaceAllowedØ  s   
z:RegistrationTests.test_duplicateAdapterForInterfaceAllowedc                 C   sH   dd„ }t  ||tt¡ |  t  |td¡|¡ |  t  |td¡|¡ dS )zh
        Verify that an adapter can be registered for multiple to-interfaces at a
        time.
        c                 S   r>   r   r   rŸ   r   r   r   r    é  r&   zJRegistrationTests._multipleInterfacesForClassOrInterface.<locals>.<lambda>N)r   rL   r9   r:   r[   r¢   r£   r   r   r   Ú&_multipleInterfacesForClassOrInterfaceä  s   z8RegistrationTests._multipleInterfacesForClassOrInterfacec                 C   r¦   )zi
        Test the registration of an adapter from a class to several
        interfaces at once.
        c                   @   r3   )zFRegistrationTests.test_multipleInterfacesForClass.<locals>.TheOriginalNr5   r   r   r   r   r§   ô  r6   r§   )r»   r¨   r   r   r   Útest_multipleInterfacesForClassî  rª   z1RegistrationTests.test_multipleInterfacesForClassc                 C   r«   )zn
        Test the registration of an adapter from an interface to several
        interfaces at once.
        )r»   r;   r   r   r   r   Ú#test_multipleInterfacesForInterfaceù  r­   z5RegistrationTests.test_multipleInterfacesForInterfacec                 C   sl   dd„ }dd„ }G dd„ d|ƒ}t  ||t¡ t  ||t¡ |  t  |td¡|¡ |  t  |td¡|¡ dS )a   
        Verify that a new adapter can be registered for a particular
        to-interface from a subclass of a type or interface which already has an
        adapter registered to that interface and that the subclass adapter takes
        precedence over the base class adapter.
        c                 S   r$   r¯   r   rŸ   r   r   r   r      r&   zSRegistrationTests._subclassAdapterRegistrationForClassOrInterface.<locals>.<lambda>c                 S   r$   r®   r   rŸ   r   r   r   r      r&   c                   @   r3   )zVRegistrationTests._subclassAdapterRegistrationForClassOrInterface.<locals>.TheSubclassNr5   r   r   r   r   ÚTheSubclass
  r6   r¾   Nr¡   )r   r+   r±   r²   r¾   r   r   r   Ú/_subclassAdapterRegistrationForClassOrInterface   s   ÿzARegistrationTests._subclassAdapterRegistrationForClassOrInterfacec                 C   r¦   )z~
        Test that an adapter to a particular interface can be registered
        from both a class and its subclass.
        c                   @   r3   )zORegistrationTests.test_subclassAdapterRegistrationForClass.<locals>.TheOriginalNr5   r   r   r   r   r§     r6   r§   )r¿   r¨   r   r   r   Ú(test_subclassAdapterRegistrationForClass  rª   z:RegistrationTests.test_subclassAdapterRegistrationForClassc                 C   r«   )zƒ
        Test that an adapter to a particular interface can be registered
        from both an interface and its subclass.
        )r¿   r:   r   r   r   r   Ú,test_subclassAdapterRegistrationForInterface  r­   z>RegistrationTests.test_subclassAdapterRegistrationForInterfaceN)r   r   r   rI   r¥   r©   r¬   r³   r´   rµ   r¸   r¹   rº   r»   r¼   r½   r¿   rÀ   rÁ   r   r   r   r   rž   k  s"    
 
rž   c                   @   s    e Zd ZdZedƒZdd„ ZdS )ÚIProxiedInterfacez=
    An interface class for use by L{proxyForInterface}.
    z@
        An example declared attribute, which should be proxied.c                  O   r$   )z:
        A sample method which should be proxied.
        Nr   )r|   Úkwr   r   r   Úyay1  r&   zIProxiedInterface.yayN)r   r   r   rI   r   ÚifaceAttributerÄ   r   r   r   r   rÂ   '  s    ÿrÂ   c                   @   r   )ÚIProxiedSubInterfacezS
    An interface that derives from another for use with L{proxyForInterface}.
    c                   C   r$   )zD
        A different sample method which should be proxied.
        Nr   r   r   r   r   Úboo<  r&   zIProxiedSubInterface.booN©r   r   r   rI   rÇ   r   r   r   r   rÆ   7  rJ   rÆ   c                   @   r‰   )ÚYayablez¢
    A provider of L{IProxiedInterface} which increments a counter for
    every call to C{yay}.

    @ivar yays: The number of times C{yay} has been called.
    c                 C   s   d| _ g | _d S r)   )ÚyaysÚyayArgsr   r   r   r   r.   M  r/   zYayable.__init__c                 O   s$   |  j d7  _ | j ||f¡ | j S )z)
        Increment C{self.yays}.
        r   )rÊ   rË   Úappend©r   r|   rÃ   r   r   r   rÄ   Q  s   zYayable.yayN)r   r   r   rI   r.   rÄ   r   r   r   r   rÉ   B  s    rÉ   c                   @   s(   e Zd ZdZdZdZdd„ Zdd„ ZdS )ÚBooablez3
    An implementation of IProxiedSubInterface
    Fc                 O   ó
   d| _ dS )z;
        Mark the fact that 'yay' has been called.
        TN)ÚyayedrÍ   r   r   r   rÄ   e  ó   
zBooable.yayc                 C   rÏ   )z<
        Mark the fact that 'boo' has been called.1
        TN)Úbooedr   r   r   r   rÇ   k  rÑ   zBooable.booN)r   r   r   rI   rÐ   rÒ   rÄ   rÇ   r   r   r   r   rÎ   Z  s    rÎ   c                   @   r‰   )ÚIMultipleMethodsz-
    An interface with multiple methods.
    c                   C   r$   )z4
        The first method. Should return 1.
        Nr   r   r   r   r   Ú	methodOnew  r&   zIMultipleMethods.methodOnec                   C   r$   )z5
        The second method. Should return 2.
        Nr   r   r   r   r   Ú	methodTwo|  r&   zIMultipleMethods.methodTwoN©r   r   r   rI   rÔ   rÕ   r   r   r   r   rÓ   r  s    rÓ   c                   @   r‰   )ÚMultipleMethodImplementorz:
    A precise implementation of L{IMultipleMethods}.
    c                 C   r$   )z
        @return: 1
        r   r   r   r   r   r   rÔ   ‡  ó   z#MultipleMethodImplementor.methodOnec                 C   r$   )z
        @return: 2
        rM   r   r   r   r   r   rÕ   �  rØ   z#MultipleMethodImplementor.methodTwoNrÖ   r   r   r   r   r×   ‚  s    r×   c                   @   s€   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚProxyForInterfaceTestsz)
    Tests for L{proxyForInterface}.
    c                 C   s$   t ƒ }ttƒ|ƒ}|  |j|¡ dS )z‹
        Proxy objects should have an C{original} attribute which refers to the
        original object passed to the constructor.
        N)r_   r   rÂ   r[   r+   )r   r+   Úproxyr   r   r   Útest_original™  s   z$ProxyForInterfaceTests.test_originalc                 C   s@   t tƒ}tƒ }||ƒ}| ¡  |  | ¡ d¡ |  |jd¡ dS )z–
        The class created from L{proxyForInterface} passes methods on an
        interface to the object which is passed to its constructor.
        rM   N©r   rÂ   rÉ   rÄ   rc   rÊ   )r   ÚklassÚyayablerÚ   r   r   r   Útest_proxyMethod¢  s   z'ProxyForInterfaceTests.test_proxyMethodc                    sD   t tƒ‰ G ‡ fdd„dˆ ƒ}tƒ }||ƒ}| ¡  |  d|j¡ dS )zŠ
        Methods of the class created from L{proxyForInterface} can be used with
        the decorator-helper L{functools.wraps}.
        c                       s"   e Zd Ze” jƒ‡ fdd„ƒZdS )z?ProxyForInterfaceTests.test_decoratedProxyMethod.<locals>.klassc                    s   | j  jd7  _ˆ  | ¡S r   )r+   rÊ   rÄ   r   ©Úbaser   r   rÄ   ¶  s   
zCProxyForInterfaceTests.test_decoratedProxyMethod.<locals>.klass.yayN)r   r   r   r   rÄ   r   rà   r   r   rÝ   µ  s    rÝ   rM   NrÜ   )r   rÝ   r+   rÞ   r   rà   r   Útest_decoratedProxyMethod®  s   z0ProxyForInterfaceTests.test_decoratedProxyMethodc                    sB   t ƒ }tƒ |_ttƒ|ƒ‰ |  ˆ j|j¡ |  t‡ fdd„¡ dS )zc
        Proxy objects should proxy declared attributes, but not other
        attributes.
        c                      s   ˆ j S r   )rÊ   r   ©rÚ   r   r   r    É  s    z<ProxyForInterfaceTests.test_proxyAttribute.<locals>.<lambda>N)rÉ   r_   rÅ   r   rÂ   r[   ry   ÚAttributeError)r   rÞ   r   rã   r   Útest_proxyAttributeÀ  s
   z*ProxyForInterfaceTests.test_proxyAttributec                 C   s0   t ƒ }ttƒ|ƒ}tƒ }||_|  |j|¡ dS )zv
        The attributes that proxy objects proxy should be assignable and affect
        the original object.
        N)rÉ   r   rÂ   r_   rÅ   r[   ©r   rÞ   rÚ   Úthingyr   r   r   Útest_proxySetAttributeË  s
   z-ProxyForInterfaceTests.test_proxySetAttributec                 C   s0   t ƒ }d|_ttƒ|ƒ}|`|  t|dƒ¡ dS )zu
        The attributes that proxy objects proxy should be deletable and affect
        the original object.
        NrÅ   )rÉ   rÅ   r   rÂ   ÚassertFalseÚhasattr)r   rÞ   rÚ   r   r   r   Útest_proxyDeleteAttributeÖ  s
   z0ProxyForInterfaceTests.test_proxyDeleteAttributec                 C   s6   t ƒ }ttƒ|ƒ}|  | ¡ d¡ |  | ¡ d¡ dS )z€
        [Regression test] The proxy should send its method calls to the correct
        method, not the incorrect one.
        r   rM   N)r×   r   rÓ   rc   rÔ   rÕ   )r   ÚmultirÚ   r   r   r   Útest_multipleMethodsá  s   z+ProxyForInterfaceTests.test_multipleMethodsc                 C   sJ   G dd„ dt tƒƒ}tƒ }||ƒ}|  |jd¡ | ¡  |  |jd¡ dS )zP
        It is possible to subclass the result of L{proxyForInterface}.
        c                   @   r   )zAProxyForInterfaceTests.test_subclassing.<locals>.SpecializedProxyzU
            A specialized proxy which can decrement the number of yays.
            c                 S   s   | j  jd8  _dS )z?
                Decrement the number of yays.
                r   N)r+   rÊ   r   r   r   r   rÇ   õ  s   zEProxyForInterfaceTests.test_subclassing.<locals>.SpecializedProxy.booNrÈ   r   r   r   r   ÚSpecializedProxyð  rJ   rî   r   éÿÿÿÿN)r   rÂ   rÉ   rc   rÊ   rÇ   )r   rî   rÞ   Úspecialr   r   r   Útest_subclassingë  s   z'ProxyForInterfaceTests.test_subclassingc                 C   s   t tƒ}|  |jd¡ dS )zQ
        The name of a proxy class indicates which interface it proxies.
        zA(Proxy for twisted.python.test.test_components.IProxiedInterface)N)r   rÂ   rc   r   ©r   rÚ   r   r   r   Útest_proxyName  s
   þz%ProxyForInterfaceTests.test_proxyNamec                 C   s   t tƒ}|  t |¡¡ dS )zO
        The resulting proxy implements the interface that it proxies.
        N)r   rÂ   Ú
assertTrueÚimplementedByrò   r   r   r   Útest_implements  s   z&ProxyForInterfaceTests.test_implementsc                 C   sb   G dd„ dƒ}|ƒ }|ƒ }||_ t dd¡}|  | |¡|j¡ | d¡}||ƒ |  |j¡ dS )a\  
        _ProxyDescriptor's __get__ method should return the appropriate
        attribute of its argument's 'original' attribute if it is invoked with
        an object.  If it is invoked with None, it should return a false
        class-method emulator instead.

        For some reason, Python's documentation recommends to define
        descriptors' __get__ methods with the 'type' parameter as optional,
        despite the fact that Python itself never actually calls the descriptor
        that way.  This is probably do to support 'foo.__get__(bar)' as an
        idiom.  Let's make sure that the behavior is correct.  Since we don't
        actually use the 'type' argument at all, this test calls it the
        idiomatic way to ensure that signature works; test_proxyInheritance
        verifies the how-Python-actually-calls-it signature.
        c                   @   r   )z>ProxyForInterfaceTests.test_proxyDescriptorGet.<locals>.SampleFc                 S   s
   d| _ d S r¯   )Úcalledr   r   r   r   rq   &  r‡   zDProxyForInterfaceTests.test_proxyDescriptorGet.<locals>.Sample.helloN)r   r   r   r÷   rq   r   r   r   r   ÚSample#  r   rø   rq   r+   N)r+   r   Ú_ProxyDescriptorrc   Ú__get__rq   rô   r÷   )r   rø   Ú	fakeProxyÚ
testObjectÚpdÚfakeClassMethodr   r   r   Útest_proxyDescriptorGet  s   
z.ProxyForInterfaceTests.test_proxyDescriptorGetc                    sp   G dd„ dt tƒƒ‰ G ‡ fdd„dˆ ƒ}tƒ }||ƒ}|  |jddddd	�d
¡ |  |jdtddd	�fg¡ dS )z¿
        Subclasses of the class returned from L{proxyForInterface} should be
        able to upcall methods by reference to their superclass, as any normal
        Python class can.
        c                   @   s   e Zd ZdZdS )zDProxyForInterfaceTests.test_proxyInheritance.<locals>.YayableWrapperzI
            This class does not override any functionality.
            N)r   r   r   rI   r   r   r   r   ÚYayableWrapper9  s    r   c                       s    e Zd ZdZdZ‡ fdd„ZdS )zEProxyForInterfaceTests.test_proxyInheritance.<locals>.EnhancedWrapperz@
            This class overrides the 'yay' method.
            r   c                    s*   |  j d7  _ ˆ j| g|¢R i |¤Žd S )Nr   é   )ÚwrappedYaysrÄ   )r   r|   Úk©r   r   r   rÄ   E  s   zIProxyForInterfaceTests.test_proxyInheritance.<locals>.EnhancedWrapper.yayN)r   r   r   rI   r  rÄ   r   r  r   r   ÚEnhancedWrapper>  s    r  r›   é   é   é   )rŽ   Úyé   )r›   r  N)r   rÂ   rÉ   rc   rÄ   rË   Údict)r   r  rÞ   Úwrapperr   r  r   Útest_proxyInheritance2  s    z,ProxyForInterfaceTests.test_proxyInheritancec                 C   sB   t tƒ}tƒ }||ƒ}| ¡  | ¡  |  |j¡ |  |j¡ dS )z 
        Proxies of subinterfaces generated with proxyForInterface should allow
        access to attributes of both the child and the base interfaces.
        N)r   rÆ   rÎ   rÄ   rÇ   rô   rÐ   rÒ   )r   Ú
proxyClassÚbooablerÚ   r   r   r   Útest_interfaceInheritanceN  s   z0ProxyForInterfaceTests.test_interfaceInheritancec                 C   s~   t ƒ }tƒ |_ttdd�|ƒ}|  |j|¡ |  | ¡ d¡ |  |j|j¡ tƒ }||_|  |j|¡ |`|  	t
|dƒ¡ dS )a  
        The original attribute name can be customized via the
        C{originalAttribute} argument of L{proxyForInterface}: the attribute
        should change, but the methods of the original object should still be
        callable, and the attributes still accessible.
        Úfoo)ÚoriginalAttributer   rÅ   N)rÉ   r_   rÅ   r   rÂ   r[   r  rc   rÄ   ré   rê   ræ   r   r   r   Útest_attributeCustomization[  s   z2ProxyForInterfaceTests.test_attributeCustomizationN)r   r   r   rI   rÛ   rß   râ   rå   rè   rë   rí   rñ   ró   rö   rÿ   r  r  r  r   r   r   r   rÙ   ”  s     	

 rÙ   N);rI   Ú	functoolsr   Úzope.interfacer   r   r   Úzope.interface.adapterr   Útwisted.pythonr   Útwisted.python.compatr   r	   Útwisted.python.componentsr
   r   r   Útwisted.trialr   r^   r   r   r#   r‹   r(   r0   r4   r7   r8   r9   r:   r;   r<   r=   r@   rB   ÚSynchronousTestCaserK   rx   r~   r   r„   r…   rˆ   rŠ   r�   r�   r’   r”   r˜   rž   rÂ   rÆ   rÉ   rÎ   rÓ   r×   rÙ   r   r   r   r   Ú<module>   sp   

l
	 =