U
    ˆjâ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                   @   s   e Zd ZdZdd„ ZdS )ÚCompor   c                 C   s   | j d | _ | j S ©Né   ©Únum©Úself© r   úG/tmp/pip-unpacked-wheel-j5h_c8_q/twisted/python/test/test_components.pyÚinc   s    z	Compo.incN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r      s   r   c                   @   s   e Zd Zdd„ ZdS )ÚIAdeptc                   C   s
   t ƒ ‚d S ©N)ÚNotImplementedErrorr   r   r   r   ÚadaptorFunc   s    zIAdept.adaptorFuncN)r   r   r   r   r   r   r   r   r      s   r   c                   @   s   e Zd Zdd„ ZdS )ÚIElapsedc                   C   s   dS )z
        1!
        Nr   r   r   r   r   ÚelapsedFunc#   s    zIElapsed.elapsedFuncN©r   r   r   r    r   r   r   r   r   "   s   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__+   s    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                   @   s   e Zd Zdd„ ZdS )ÚElapsedc                 C   s   dS r   r   r   r   r   r   r    6   s    zElapsed.elapsedFuncNr!   r   r   r   r   r)   4   s   r)   c                   @   s   e Zd ZdS )ÚACompN©r   r   r   r   r   r   r   r*   :   s   r*   c                   @   s   e Zd ZdS )ÚBCompNr+   r   r   r   r   r,   >   s   r,   c                   @   s   e Zd ZdS )ÚCCompNr+   r   r   r   r   r-   B   s   r-   c                   @   s   e Zd ZdS )ÚITestNr+   r   r   r   r   r.   F   s   r.   c                   @   s   e Zd ZdS )ÚITest2Nr+   r   r   r   r   r/   J   s   r/   c                   @   s   e Zd ZdS )ÚITest3Nr+   r   r   r   r   r0   N   s   r0   c                   @   s   e Zd ZdS )ÚITest4Nr+   r   r   r   r   r1   R   s   r1   c                   @   s   e Zd Zdd„ ZdS )ÚTestc                 C   s   d S r   r   r&   r   r   r   r(   X   s    zTest.__init__N©r   r   r   r(   r   r   r   r   r2   V   s   r2   c                   @   s   e Zd ZdZdd„ ZdS )ÚTest2r   c                 C   s   d S r   r   r&   r   r   r   r(   `   s    zTest2.__init__N)r   r   r   ZtemporaryAdapterr(   r   r   r   r   r4   \   s   r4   c                   @   s   e Zd ZdZdd„ ZdS )Ú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.
        ZglobalRegistryN)r   Úpatchr   r
   Z
addCleanupr   )r   ZscratchRegistryÚhookr   r   r   ÚsetUpi   s    zRegistryUsingMixin.setUpN)r   r   r   Ú__doc__r8   r   r   r   r   r5   d   s   r5   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   )
r5   r8   r   ÚregisterAdapterr2   r*   r.   r0   r4   r/   r   r   r   r   r8   ~   s    
zComponentizedTests.setUpc                 C   sj   t  ttt¡ t  ttt¡ tƒ }| t¡ ¡ dks8t	‚| t¡ ¡ dksNt	‚tt|ƒƒ 
¡ dksft	‚d S )N)r   r   )é   r<   r   )r   r;   r"   r   r   r)   r   ÚgetComponentr   ÚAssertionErrorr    ©r   Úcr   r   r   ÚtestComponentized…   s    z$ComponentizedTests.testComponentizedc                 C   s€   t ƒ }| t¡}| t¡}| t¡}| t¡}||ks:t‚||k	sFt‚| |¡ | t¡}| t¡}||kspt‚||k	s|t‚d S r   )r-   r=   r.   r/   r>   ZremoveComponent)r   r@   Úco1Zco2Úco3Úco4Zco5Zco6r   r   r   ÚtestInheritanceAdaptationŽ   s    






z,ComponentizedTests.testInheritanceAdaptationc                 C   s>   t ƒ }| t¡}| t¡}| t¡}|  |¡ |  ||¡ d S r   )r-   r=   r.   r0   r1   ÚassertIsNoneÚassertIs)r   r@   rB   rC   rD   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ÚobjectrG   r=   r.   )r   ÚcomponentizedrI   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   rJ   Z
setAdapterr   r"   r=   ÚassertEqualr%   ÚassertIsInstance©r   rL   Ú	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)ZignoreClassN)	r   rJ   Z
addAdapterr"   r=   r   rN   r%   rO   rP   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   rJ   rK   ÚsetComponentr.   rG   r=   ©r   rL   Ú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   rJ   rK   rT   r.   ZunsetComponentrF   r=   rU   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.
        Úhellor.   N)r   ZReprableComponentizedrT   r.   ÚreprZassertIn)r   ÚrcÚresultr   r   r   Útest_reprableComponentizedÚ   s
    z-ComponentizedTests.test_reprableComponentizedN)r   r   r   r9   r8   rA   rE   rH   rM   rR   rS   rW   rX   r]   r   r   r   r   r:   y   s   	
r:   c                   @   s   e Zd ZdZdd„ ZdS )ÚAdapterTestszTest adapters.c                 C   s2   t ƒ }t|ƒ}|  tjt|¡ |  t|d ƒ¡ d S r   )rK   r"   ÚassertRaisesr   ZCannotAdaptr.   rF   )r   ÚoÚar   r   r   ÚtestAdapterGetComponentè   s    z$AdapterTests.testAdapterGetComponentN)r   r   r   r9   rb   r   r   r   r   r^   å   s   r^   c                   @   s   e Zd ZdS )ÚIMetaNr+   r   r   r   r   rc   ï   s   rc   c                   @   s   e Zd Zdd„ ZdS )Ú	MetaAdderc                 C   s   | j j| S r   r$   ©r   r   r   r   r   Úaddõ   s    zMetaAdder.addN©r   r   r   rf   r   r   r   r   rd   ó   s   rd   c                   @   s   e Zd Zdd„ ZdS )ÚBackwardsAdderc                 C   s   | j j| S r   r$   re   r   r   r   rf   û   s    zBackwardsAdder.addNrg   r   r   r   r   rh   ù   s   rh   c                   @   s   e Zd ZdZdd„ ZdS )Ú
MetaNumberz9
    Integer wrapper for Interface adaptation tests.
    c                 C   s
   || _ d S r   r   re   r   r   r   r(     s    zMetaNumber.__init__N)r   r   r   r9   r(   r   r   r   r   ri   ÿ   s   ri   c                   @   s   e Zd Zdd„ ZdS )ÚComponentNumberc                 C   s   d| _ tj | ¡ d S r#   )r   r   rJ   r(   r   r   r   r   r(   	  s    zComponentNumber.__init__Nr3   r   r   r   r   rj     s   rj   c                   @   s    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   re   r   r   r   rf     s    zComponentAdder.addN)r   r   r   r9   r(   rf   r   r   r   r   rk     s   rk   c                   @   s   e Zd ZdZdd„ ZdS )ÚIAttrXz=
    Base interface for test of adapter with C{__cmp__}.
    c                   C   s   dS )z!
        Return a value.
        Nr   r   r   r   r   Úx"  s    zIAttrX.xN©r   r   r   r9   ro   r   r   r   r   rn     s   rn   c                   @   s   e Zd ZdZdd„ ZdS )ÚIAttrXXz@
    Adapted interface for test of adapter with C{__cmp__}.
    c                   C   s   dS )z+
        Return a tuple of values.
        Nr   r   r   r   r   Úxx-  s    z
IAttrXX.xxN)r   r   r   r9   rr   r   r   r   r   rq   (  s   rq   c                   @   s   e Zd ZdZdd„ ZdS )ÚXcellentzG
    L{IAttrX} implementation for test of adapter with C{__cmp__}.
    c                 C   s   dS )z;
        Return a value.

        @return: a value
        úx!r   r   r   r   r   ro   9  s    z
Xcellent.xNrp   r   r   r   r   rs   3  s   rs   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   s
   || _ d S r   )r%   rm   r   r   r   r(   J  s    zDoubleXAdapter.__init__c                 C   s   | j  ¡ | j  ¡ fS r   )r%   ro   r   r   r   r   rr   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   r9   r   r(   rr   rx   r   r   r   r   ru   B  s
   ru   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   r<   N)r   r;   rd   ri   rc   rN   rf   )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   r;   rk   rj   rc   rf   rN   r?   r   r   r   ÚtestComponentizedInteraction]  s
    z/MetaInterfaceTests.testComponentizedInteractionc                 C   s,   t  ttt¡ ttƒ ƒ}|  d| ¡ ¡ d S )N)rt   rt   )r   r;   ru   rn   rq   rs   rN   rr   )r   rr   r   r   r   ÚtestAdapterWithCmpd  s    
z%MetaInterfaceTests.testAdapterWithCmpN)r   r   r   r{   r}   r~   r   r   r   r   ry   T  s   ry   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   s   d S r   r   ©r`   r   r   r   Ú<lambda>v  ó    zGRegistrationTests._registerAdapterForClassOrInterface.<locals>.<lambda>N©r   r;   r.   rG   ÚgetAdapterFactory©r   r%   Úadapterr   r   r   Ú#_registerAdapterForClassOrInterfacep  s    z5RegistrationTests._registerAdapterForClassOrInterfacec                 C   s   G dd„ dƒ}|   |¡S )za
        Test that an adapter from a class can be registered and then looked
        up.
        c                   @   s   e Zd ZdS )zCRegistrationTests.test_registerAdapterForClass.<locals>.TheOriginalNr+   r   r   r   r   ÚTheOriginal€  s   rˆ   )r‡   ©r   rˆ   r   r   r   Útest_registerAdapterForClassz  s    z.RegistrationTests.test_registerAdapterForClassc                 C   s
   |   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…  s    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   s   dS ©NFr   r€   r   r   r   r�   ‘  r‚   zHRegistrationTests._duplicateAdapterForClassOrInterface.<locals>.<lambda>c                 S   s   dS ©NTr   r€   r   r   r   r�   ’  r‚   N)r   r;   r.   r_   Ú
ValueErrorrG   r„   )r   r%   ÚfirstAdapterÚsecondAdapterr   r   r   Ú$_duplicateAdapterForClassOrInterfaceŒ  s        ÿz6RegistrationTests._duplicateAdapterForClassOrInterfacec                 C   s   G dd„ dƒ}|   |¡S )zz
        Test that attempting to register a second adapter from a class
        raises the appropriate exception.
        c                   @   s   e Zd ZdS )zDRegistrationTests.test_duplicateAdapterForClass.<locals>.TheOriginalNr+   r   r   r   r   rˆ      s   rˆ   )r‘   r‰   r   r   r   Útest_duplicateAdapterForClassš  s    z/RegistrationTests.test_duplicateAdapterForClassc                 C   s
   |   t¡S )z
        Test that attempting to register a second adapter from an interface
        raises the appropriate exception.
        )r‘   r/   r   r   r   r   Ú!test_duplicateAdapterForInterface¥  s    z3RegistrationTests.test_duplicateAdapterForInterfacec                 C   s”   dd„ }dd„ }G dd„ dt ƒ}t |||¡ dt_z(t |||¡ |  t ||d¡|¡ W 5 dt_X |  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   s   dS rŒ   r   r€   r   r   r   r�   ²  r‚   zORegistrationTests._duplicateAdapterForClassOrInterfaceAllowed.<locals>.<lambda>c                 S   s   dS r�   r   r€   r   r   r   r�   ³  r‚   c                   @   s   e Zd ZdS )zSRegistrationTests._duplicateAdapterForClassOrInterfaceAllowed.<locals>.TheInterfaceNr+   r   r   r   r   ÚTheInterfaceµ  s   r”   TFN)r   r   r;   ZALLOW_DUPLICATESrG   r„   r_   rŽ   )r   r%   r�   r�   r”   r   r   r   Ú+_duplicateAdapterForClassOrInterfaceAllowed¬  s.    þ    ÿ ÿz=RegistrationTests._duplicateAdapterForClassOrInterfaceAllowedc                 C   s   G dd„ dƒ}|   |¡S )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                   @   s   e Zd ZdS )zKRegistrationTests.test_duplicateAdapterForClassAllowed.<locals>.TheOriginalNr+   r   r   r   r   rˆ   Ó  s   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                   @   s   e Zd ZdS )zORegistrationTests.test_duplicateAdapterForInterfaceAllowed.<locals>.TheOriginalNr+   r   r   r   r   rˆ   ß  s   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   s   d S r   r   r€   r   r   r   r�   é  r‚   zJRegistrationTests._multipleInterfacesForClassOrInterface.<locals>.<lambda>N)r   r;   r.   r/   rG   r„   r…   r   r   r   Ú&_multipleInterfacesForClassOrInterfaceä  s    z8RegistrationTests._multipleInterfacesForClassOrInterfacec                 C   s   G dd„ dƒ}|   |¡S )zi
        Test the registration of an adapter from a class to several
        interfaces at once.
        c                   @   s   e Zd ZdS )zFRegistrationTests.test_multipleInterfacesForClass.<locals>.TheOriginalNr+   r   r   r   r   rˆ   ô  s   rˆ   )r˜   r‰   r   r   r   Útest_multipleInterfacesForClassî  s    z1RegistrationTests.test_multipleInterfacesForClassc                 C   s
   |   t¡S )zn
        Test the registration of an adapter from an interface to several
        interfaces at once.
        )r˜   r0   r   r   r   r   Ú#test_multipleInterfacesForInterfaceù  s    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   s   dS r�   r   r€   r   r   r   r�     r‚   zSRegistrationTests._subclassAdapterRegistrationForClassOrInterface.<locals>.<lambda>c                 S   s   dS rŒ   r   r€   r   r   r   r�     r‚   c                   @   s   e Zd ZdS )zVRegistrationTests._subclassAdapterRegistrationForClassOrInterface.<locals>.TheSubclassNr+   r   r   r   r   ÚTheSubclass
  s   r›   Nrƒ   )r   r%   r�   r�   r›   r   r   r   Ú/_subclassAdapterRegistrationForClassOrInterface   s     ÿzARegistrationTests._subclassAdapterRegistrationForClassOrInterfacec                 C   s   G dd„ dƒ}|   |¡S )z~
        Test that an adapter to a particular interface can be registered
        from both a class and its subclass.
        c                   @   s   e Zd ZdS )zORegistrationTests.test_subclassAdapterRegistrationForClass.<locals>.TheOriginalNr+   r   r   r   r   rˆ     s   rˆ   )rœ   r‰   r   r   r   Ú(test_subclassAdapterRegistrationForClass  s    z:RegistrationTests.test_subclassAdapterRegistrationForClassc                 C   s
   |   t¡S )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  s    z>RegistrationTests.test_subclassAdapterRegistrationForInterfaceN)r   r   r   r9   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   s   dS )z:
        A sample method which should be proxied.
        Nr   )ra   Úkwr   r   r   Úyay1  s    zIProxiedInterface.yayN)r   r   r   r9   r   ÚifaceAttributer¡   r   r   r   r   rŸ   '  s
   ÿrŸ   c                   @   s   e Zd ZdZdd„ ZdS )ÚIProxiedSubInterfacezS
    An interface that derives from another for use with L{proxyForInterface}.
    c                   C   s   dS )zD
        A different sample method which should be proxied.
        Nr   r   r   r   r   Úboo<  s    zIProxiedSubInterface.booN©r   r   r   r9   r¤   r   r   r   r   r£   7  s   r£   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú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  s    zYayable.__init__c                 O   s$   |  j d7  _ | j ||f¡ | j S )z)
        Increment C{self.yays}.
        r   )r§   r¨   Úappend©r   ra   r    r   r   r   r¡   Q  s    zYayable.yayN)r   r   r   r9   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   s
   d| _ dS )z;
        Mark the fact that 'yay' has been called.
        TN)Úyayedrª   r   r   r   r¡   e  s    zBooable.yayc                 C   s
   d| _ dS )z<
        Mark the fact that 'boo' has been called.1
        TN)Úbooedr   r   r   r   r¤   k  s    zBooable.booN)r   r   r   r9   r¬   r­   r¡   r¤   r   r   r   r   r«   Z  s
   r«   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚIMultipleMethodsz-
    An interface with multiple methods.
    c                   C   s   dS )z4
        The first method. Should return 1.
        Nr   r   r   r   r   Ú	methodOnew  s    zIMultipleMethods.methodOnec                   C   s   dS )z5
        The second method. Should return 2.
        Nr   r   r   r   r   Ú	methodTwo|  s    zIMultipleMethods.methodTwoN©r   r   r   r9   r¯   r°   r   r   r   r   r®   r  s   r®   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚMultipleMethodImplementorz:
    A precise implementation of L{IMultipleMethods}.
    c                 C   s   dS )z
        @return: 1
        r   r   r   r   r   r   r¯   ‡  s    z#MultipleMethodImplementor.methodOnec                 C   s   dS )z
        @return: 2
        r<   r   r   r   r   r   r°   �  s    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)rK   r   rŸ   rG   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.
        r<   N©r   rŸ   r¦   r¡   rN   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·   r<   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�   É  r‚   z<ProxyForInterfaceTests.test_proxyAttribute.<locals>.<lambda>N)r¦   rK   r¢   r   rŸ   rG   r_   Ú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Ÿ   rK   r¢   rG   ©r   r¸   r´   Z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   r<   N)r²   r   r®   rN   r¯   r°   )r   Z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                   @   s   e Zd ZdZdd„ ZdS )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ð  s   rÆ   r   éÿÿÿÿN)r   rŸ   r¦   rN   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Ÿ   rN   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Ÿ   Ú
assertTrueZ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                   @   s   e Zd ZdZdd„ ZdS )z>ProxyForInterfaceTests.test_proxyDescriptorGet.<locals>.SampleFc                 S   s
   d| _ d S r�   )Úcalledr   r   r   r   rY   &  s    zDProxyForInterfaceTests.test_proxyDescriptorGet.<locals>.Sample.helloN)r   r   r   rÎ   rY   r   r   r   r   ÚSample#  s   rÏ   rY   r%   N)r%   r   Z_ProxyDescriptorrN   Ú__get__rY   rÌ   rÎ   )r   rÏ   Z	fakeProxyZ
testObjectÚpdZ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   r9   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| f|ž|Žd S )Nr   é   )ÚwrappedYaysr¡   )r   ra   Úk©rÓ   r   r   r¡   E  s    zIProxyForInterfaceTests.test_proxyInheritance.<locals>.EnhancedWrapper.yayN)r   r   r   r9   rÕ   r¡   r   r×   r   r   ÚEnhancedWrapper>  s   rØ   r|   é   é   é   )ro   Úyé   )r|   rÙ   N)r   rŸ   r¦   rN   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   Z
proxyClassZ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)ZoriginalAttributer   r¢   N)r¦   rK   r¢   r   rŸ   rG   râ   rN   r¡   rÂ   rÃ   rÀ   r   r   r   Útest_attributeCustomization[  s    z2ProxyForInterfaceTests.test_attributeCustomizationN)r   r   r   r9   rµ   r¹   r¼   r¿   rÁ   rÄ   rÅ   rÉ   rË   rÍ   rÒ   rà   rá   rã   r   r   r   r   r³   ”  s   	

 r³   N);r9   Ú	functoolsr   Zzope.interfacer   r   r   Zzope.interface.adapterr   Ztwisted.pythonr   Ztwisted.python.compatr   r	   Ztwisted.python.componentsr
   r   r   Ztwisted.trialr   rJ   r   r   r   rl   r"   r)   r*   r,   r-   r.   r/   r0   r1   r2   r4   r5   ZSynchronousTestCaser:   r^   rc   rd   rh   ri   rj   rk   rn   rq   rs   ru   ry   r   rŸ   r£   r¦   r«   r®   r²   r³   r   r   r   r   Ú<module>   sn   

l
	 =