U
     T‚g  ã                   @   s˜   d Z ddlZddlmZ ddlmZ ddlmZ G dd„ dƒZ	dd	„ Z
G d
d„ dƒZG dd„ dƒZG dd„ dƒZG dd„ dƒZdd„ Zeƒ Zdd„ ZdS )a¼  
I{Private} test utilities for use throughout Twisted's test suite.  Unlike
C{proto_helpers}, this is no exception to the
don't-use-it-outside-Twisted-we-won't-maintain-compatibility rule!

@note: Maintainers be aware: things in this module should be gradually promoted
    to more full-featured test helpers and exposed as public API as your
    maintenance time permits.  In order to be public API though, they need
    their own test cases.
é    N)ÚBytesIO)Úminidom)ÚFileWrapperc                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚIOPumpzŠUtility to pump data between clients and servers for protocol testing.

    Perhaps this is a utility worthy of being in protocol.py?
    c                 C   s   || _ || _|| _|| _d S )N)ÚclientÚserverÚclientIOÚserverIO)Úselfr   r   r   r	   © r   ú:/tmp/pip-unpacked-wheel-1_7di8ic/twisted/test/testutils.pyÚ__init__   s    zIOPump.__init__c                 C   s   |   ¡ r
q dS )z,Pump until there is no more input or output.N)Úpump)r
   r   r   r   Úflush"   s    zIOPump.flushc                 C   s˜   | j  d¡ | j d¡ | j  ¡ }| j ¡ }| j  d¡ | j d¡ | j  ¡  | j ¡  |D ]}| j |¡ q\|D ]}| j |¡ qr|sŒ|r�dS dS dS )zOMove data back and forth.

        Returns whether any data was moved.
        r   é   N)r   Úseekr	   ÚreadÚtruncater   ZdataReceivedr   )r
   ZcDataZsDataÚbyter   r   r   r   '   s    



zIOPump.pumpN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r      s   r   c                 C   sJ   t ƒ }t ƒ }| t|ƒ¡ |  t|ƒ¡ t|| ||ƒ}| ¡  | ¡  |S )z-Take two Protocol instances and connect them.)r   ZmakeConnectionr   r   r   )r   r   ZcioÚsior   r   r   r   ÚreturnConnected>   s    r   c                   @   s   e Zd ZdZdd„ ZdS )ÚXMLAssertionMixinzŠ
    Test mixin defining a method for comparing serialized XML documents.

    Must be mixed in to a L{test case<unittest.TestCase>}.
    c                 C   s$   |   t |¡ ¡ t |¡ ¡ ¡ dS )zï
        Verify that two strings represent the same XML document.

        @param first: An XML string.
        @type first: L{bytes}

        @param second: An XML string that should match C{first}.
        @type second: L{bytes}
        N)ÚassertEqualÚdomÚparseStringZtoxml)r
   ÚfirstÚsecondr   r   r   ÚassertXMLEqualS   s    
 ÿz XMLAssertionMixin.assertXMLEqualN)r   r   r   r   r!   r   r   r   r   r   L   s   r   c                   @   s    e Zd ZdZeedœdd„ZdS )Ú_EqualzN
    A class the instances of which are equal to anything and everything.
    ©ÚotherÚreturnc                 C   s   dS )NTr   ©r
   r$   r   r   r   Ú__eq__g   s    z_Equal.__eq__N©r   r   r   r   ÚobjectÚboolr'   r   r   r   r   r"   b   s   r"   c                   @   s    e Zd ZdZeedœdd„ZdS )Ú	_NotEqualz>
    A class the instances of which are equal to nothing.
    r#   c                 C   s   dS )NFr   r&   r   r   r   r'   p   s    z_NotEqual.__eq__Nr(   r   r   r   r   r+   k   s   r+   c                   @   s   e Zd ZdZdd„ ZdS )ÚComparisonTestsMixina¾  
    A mixin which defines a method for making assertions about the correctness
    of an implementation of C{==} and C{!=}.

    Use this to unit test objects which follow the common convention for C{==}
    and C{!=}:

        - The object compares equal to itself
        - The object cooperates with unrecognized types to allow them to
          implement the comparison
        - The object implements not-equal as the opposite of equal
    c                 C   s˜   |   ||k¡ |   ||k¡ |  ||k¡ |  ||k¡ |  ||k¡ |   ||k¡ |   |tƒ k¡ |  |tƒ k¡ |  |tƒ k¡ |   |tƒ k¡ dS )aU  
        Assert that C{firstValueOne} is equal to C{secondValueOne} but not
        equal to C{valueOne} and that it defines equality cooperatively with
        other types it doesn't know about.

        @param firstValueOne: An object which is expected to compare as equal
            to C{secondValueOne} and not equal to C{valueTwo}.

        @param secondValueOne: A different object than C{firstValueOne} but
            which is expected to compare equal to that object.

        @param valueTwo: An object which is expected to compare as not equal to
            C{firstValueOne}.
        N)Ú
assertTrueZassertFalser"   r+   )r
   ZfirstValueOneZsecondValueOneZvalueTwor   r   r   Ú"assertNormalEqualityImplementation‚   s    z7ComparisonTestsMixin.assertNormalEqualityImplementationN)r   r   r   r   r.   r   r   r   r   r,   t   s   r,   c                  C   sL   d} d}zt   t j¡} |  d¡ d}W n tk
r:   Y nX | rH|  ¡  |S )z4Returns True if the system can bind an IPv6 address.NF)z::1r   T)ÚsocketÚAF_INET6ÚbindÚOSErrorÚclose)ÚsockÚhas_ipv6r   r   r   Ú	_has_ipv6¥   s    
r6   c                 C   s   t s
d| _| S )Nz.Does not work on systems without IPv6 support.)ÚHAS_IPV6Úskip)Úfr   r   r   ÚskipWithoutIPv6¹   s    r:   )r   r/   Úior   Úxml.domr   r   Ztwisted.internet.protocolr   r   r   r   r"   r+   r,   r6   r7   r:   r   r   r   r   Ú<module>   s   (		1