o
    bd                     @   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
	 =