U
    
W[ve                     @   s:  d Z ddlmZm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mZ ddlmZ ddlmZ dd	lmZmZ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%e&Z'G d&d' d'e&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 d0d1 d1ejZ.G d2d3 d3e&Z/G d4d5 d5ejZ0ee,G d6d7 d7ejZ1G d8d9 d9eZ2G d:d; d;eZ3ee2G d<d= d=e&Z4eG d>d? d?e&Z5G d@dA dAe(ej)Z6G dBdC dCe(ej)Z7G dDdE dEeZ8G dFdG dGe8Z9ee8G dHdI dIe&Z:ee9G dJdK dKe&Z;G dLdM dMeZ<G dNdO dOe&Z=G dPdQ dQej)Z>dRS )Sz0
Test cases for Twisted component architecture.
    )divisionabsolute_import)wraps)	Interfaceimplementer	Attribute)AdapterRegistry)
comparablecmp)unittest)
components)_addHook_removeHookproxyForInterfacec                   @   s   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      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   Z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"   1   s    zElapsed.elapsedFuncNr#   r   r   r   r   r*   /   s   r*   c                   @   s   e Zd ZdS )ACompNr   r   r   r   r   r   r   r+   4   s   r+   c                   @   s   e Zd ZdS )BCompNr,   r   r   r   r   r-   6   s   r-   c                   @   s   e Zd ZdS )CCompNr,   r   r   r   r   r.   8   s   r.   c                   @   s   e Zd ZdS )ITestNr,   r   r   r   r   r/   ;   s   r/   c                   @   s   e Zd ZdS )ITest2Nr,   r   r   r   r   r0   ?   s   r0   c                   @   s   e Zd ZdS )ITest3Nr,   r   r   r   r   r1   C   s   r1   c                   @   s   e Zd ZdS )ITest4Nr,   r   r   r   r   r2   G   s   r2   c                   @   s   e Zd Zdd ZdS )Testc                 C   s   d S r   r   r(   r   r   r   r)   M   s    zTest.__init__Nr   r   r   r)   r   r   r   r   r3   K   s   r3   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)   T   s    zTest2.__init__N)r   r   r   ZtemporaryAdapterr)   r   r   r   r   r5   Q   s   r5   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   Zpatchr   r   Z
addCleanupr   )r   ZscratchRegistryhookr   r   r   setUp]   s    zRegistryUsingMixin.setUpN)r   r   r   __doc__r8   r   r   r   r   r6   Y   s   r6   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   )
r6   r8   r   registerAdapterr3   r+   r/   r1   r5   r0   r   r   r   r   r8   r   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   testComponentizedz   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/   r0   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/   r1   r2   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   Z	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   rS   r/   ZunsetComponentrF   r=   rT   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ReprableComponentizedrS   r/   reprZassertIn)r   Zrcresultr   r   r   test_reprableComponentized   s
    z-ComponentizedTests.test_reprableComponentizedN)r   r   r   r9   r8   rA   rE   rH   rM   rQ   rR   rV   rW   r[   r   r   r   r   r:   n   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   r`   r   r   r   r   r\      s   r\   c                   @   s   e Zd ZdS )IMetaNr,   r   r   r   r   ra      s   ra   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   rd   r   r   r   r   rb      s   rb   c                   @   s   e Zd Zdd ZdS )BackwardsAdderc                 C   s   | j j| S r   r&   rc   r   r   r   rd      s    zBackwardsAdder.addNre   r   r   r   r   rf      s   rf   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   rc   r   r   r   r)   	  s    zMetaNumber.__init__N)r   r   r   r9   r)   r   r   r   r   rg     s   rg   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__Nr4   r   r   r   r   rh     s   rh   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   rc   r   r   r   rd     s    zComponentAdder.addN)r   r   r   r9   r)   rd   r   r   r   r   ri     s   ri   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   rm   r   r   r   r   rl   %  s   rl   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   xx4  s    z
IAttrXX.xxN)r   r   r   r9   rp   r   r   r   r   ro   0  s   ro   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   rm   @  s    z
Xcellent.xNrn   r   r   r   r   rq   ;  s   rq   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'   rk   r   r   r   r)   P  s    zDoubleXAdapter.__init__c                 C   s   | j  | j  fS r   )r'   rm   r   r   r   r   rp   T  s    zDoubleXAdapter.xxc                 C   s   t | j|jS r   )r
   r   )r   otherr   r   r   __cmp__X  s    zDoubleXAdapter.__cmp__N)r   r   r   r9   r   r)   rp   rv   r   r   r   r   rs   J  s
   rs   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;   rb   rg   ra   rN   rd   )r   nr   r   r   
test_basic^  s    zMetaInterfaceTests.test_basicc                 C   sJ   t ttt t }t|d t|d | t|dd d S )Nr      )r   r;   ri   rh   ra   rd   rN   r?   r   r   r   testComponentizedInteractionf  s
    z/MetaInterfaceTests.testComponentizedInteractionc                 C   s,   t ttt tt }| d|  d S )N)rr   rr   )r   r;   rs   rl   ro   rq   rN   rp   )r   rp   r   r   r   testAdapterWithCmpm  s    
z%MetaInterfaceTests.testAdapterWithCmpN)r   r   r   ry   r{   r|   r   r   r   r   rw   ]  s   rw   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>~      zGRegistrationTests._registerAdapterForClassOrInterface.<locals>.<lambda>Nr   r;   r/   rG   getAdapterFactoryr   r'   Zadapterr   r   r   #_registerAdapterForClassOrInterfacex  s    z5RegistrationTests._registerAdapterForClassOrInterfacec                 C   s   G dd dt }| |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   )rK   r   r   r   r   r   r   test_registerAdapterForClass  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   r0   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t }| |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   )rK   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   r0   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t }| |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   )rK   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/   r0   rG   r   r   r   r   r   &_multipleInterfacesForClassOrInterface  s      z8RegistrationTests._multipleInterfacesForClassOrInterfacec                 C   s   G dd dt }| |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   )rK   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   r1   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t }| |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   )rK   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   r0   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}   t  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   )r_   kwr   r   r   yay?  s    zIProxiedInterface.yayN)r   r   r   r9   r   ifaceAttributer   r   r   r   r   r   7  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   booI  s    zIProxiedSubInterface.booNr   r   r   r9   r   r   r   r   r   r   D  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)   X  s    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   \  s    zYayable.yayN)r   r   r   r9   r)   r   r   r   r   r   r   O  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                 C   s
   d| _ dS )z;
        Mark the fact that 'yay' has been called.
        TN)yayedr   r   r   r   r   m  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   t  s    zBooable.booN)r   r   r   r9   r   r   r   r   r   r   r   r   r   f  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   	methodOne  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   |  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   Z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   sd   G dd dt }| }| }||_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   rX   9  s    zDProxyForInterfaceTests.test_proxyDescriptorGet.<locals>.Sample.helloN)r   r   r   r   rX   r   r   r   r   Sample7  s   r   rX   r'   N)	rK   r'   r   Z_ProxyDescriptorrN   __get__rX   r   r   )r   r   Z	fakeProxyZ
testObjectZ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   YayableWrapperK  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   r_   kr   r   r   r   U  s    zIProxyForInterfaceTests.test_proxyInheritance.<locals>.EnhancedWrapper.yayN)r   r   r   r9   r   r   r   r   r   r   EnhancedWrapperP  s   r   rz            )rm   y   )rz   r   N)r   r   r   rN   r   r   dict)r   r   r   wrapperr   r   r   test_proxyInheritanceE  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_interfaceInheritance`  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_attributeCustomizationn  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   Z
__future__r   r   	functoolsr   Zzope.interfacer   r   r   Zzope.interface.adapterr   Ztwisted.python.compatr	   r
   Ztwisted.trialr   Ztwisted.pythonr   Ztwisted.python.componentsr   r   r   rJ   r   r   r!   rj   r$   r*   r+   r-   r.   r/   r0   r1   r2   r3   rK   r5   r6   ZSynchronousTestCaser:   r\   ra   rb   rf   rg   rh   ri   rl   ro   rq   rs   rw   r}   r   r   r   r   r   r   r   r   r   r   r   <module>   sp   
y	 D