U
    ª€ljÕ  ã                   @  sŒ   d Z ddlmZ ddlmZ ddlmZmZmZ	 ddl
mZmZ ddlmZ ddlmZ G d	d
„ d
eƒZG dd„ deƒZG dd„ deƒZdS )zY
Tests for L{twisted.internet.abstract}, a collection of APIs for implementing
reactors.
é    )Úannotations)ÚUnion)ÚexampleÚgivenÚ
strategies)ÚFileDescriptorÚisIPv6Address)ÚSynchronousTestCaseé   )Ú_FakeFDSetReactorc                   @  sd   e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zddœd
d„Zddœdd„Zddœdd„Z	dS )ÚIPv6AddressTestszw
    Tests for L{isIPv6Address}, a function for determining if a particular
    string is an IPv6 address literal.
    ÚNone)Úreturnc                 C  s   |   tdƒ¡ dS )zB
        The empty string is not an IPv6 address literal.
        Ú N©ÚassertFalser   ©Úself© r   úG/tmp/pip-unpacked-wheel-aghyhbke/twisted/internet/test/test_abstract.pyÚ
test_empty   s    zIPv6AddressTests.test_emptyc                 C  s   |   tdƒ¡ dS )zA
        A single C{":"} is not an IPv6 address literal.
        ú:Nr   r   r   r   r   Ú
test_colon   s    zIPv6AddressTests.test_colonc                 C  s   |   tdƒ¡ dS )z@
        C{"::1"} is the IPv6 loopback address literal.
        z::1N©Ú
assertTruer   r   r   r   r   Útest_loopback%   s    zIPv6AddressTests.test_loopbackc                 C  s.   |   tdƒ¡ |   tdƒ¡ |   tdƒ¡ dS )z†
        An otherwise valid IPv6 address literal may also include a C{"%"}
        followed by an arbitrary scope identifier.
        zfe80::1%eth0ú	fe80::2%1zfe80::3%en2Nr   r   r   r   r   Útest_scopeID+   s    zIPv6AddressTests.test_scopeIDc                 C  s.   |   tdƒ¡ |   tdƒ¡ |   tdƒ¡ dS )zv
        An otherwise invalid IPv6 address literal is still invalid with a
        trailing scope identifier.
        z%eth0z:%eth0ú
hello%eth0Nr   r   r   r   r   Útest_invalidWithScopeID4   s    z(IPv6AddressTests.test_invalidWithScopeIDc                 C  sJ   |   tdƒ¡ |   tdƒ¡ |  tdƒ¡ |  tdƒ¡ |  tdƒ¡ dS )zQ
        L{isIPv6Address} evaluates ASCII-encoded bytes as well as text.
        s	   fe80::2%1r   u   äŒ¡r   s
   hello%eth0N)r   r   r   r   r   r   r   Útest_unicodeAndBytes=   s
    z%IPv6AddressTests.test_unicodeAndBytesN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r    r   r   r   r   r      s   		r   c                   @  s:   e Zd ZdZdZdZddddœdd	„Zd
ddœdd„ZdS )ÚTrackingFileDescriptorz>
    Write a limited amount, and track what gets written.
    TFzlist[Union[int, bytes]]zlist[bytes]Úint)Ú
operationsÚwrittenÚ
send_limitc                 C  s$   || _ || _|| _t | tƒ ¡ d S ©N)r'   r(   Ú
SEND_LIMITr   Ú__init__r   )r   r'   r(   r)   r   r   r   r,   S   s    zTrackingFileDescriptor.__init__Úbytes)Údatar   c                 C  s@   | j  d¡}t|tƒst‚t|t|ƒƒ}| j |d |… ¡ |S )Nr   )	r'   ÚpopÚ
isinstancer&   ÚAssertionErrorÚminÚlenr(   Úappend)r   r.   ZtoWriter   r   r   ÚwriteSomeData[   s
    z$TrackingFileDescriptor.writeSomeDataN)r!   r"   r#   r$   Ú	connectedZ_writeDisconnectedr,   r5   r   r   r   r   r%   J   s
   r%   c                	   @  sb   e Zd ZdZeeje ejddd�ej	ddd�¡ddd�d	�e
d
ddgd	�dddœdd„ƒƒZdS )ÚWriteBufferingTestszE
    Tests for the complex logic in the L{FileDescriptor} class.
    r
   é
   )Zmin_sizeÚmax_sizer   )Z	min_valueZ	max_valueé   é   )r'   s   abcdefó   gzlist[Union[bytes, int]]r   )r'   r   c                 C  sŽ   d  dd„ |D ƒ¡}g }d}t|||ƒ}||d gdt|ƒ|   7 }|rtt|d tƒrj| | d¡¡ qF| ¡  qFd  |¡}|  ||¡ dS )	a]  
        A sequence of C{write()} and C{doWrite()} will eventually write all the
        data correctly and in order.

        @param operations: A list of C{bytes} (indicating a C{write()}) or
            C{int} (indicating C{doWrite()} with the integer being how much
            C{writeSomeData()} writeSomeData will successfully write).
        ó    c                 s  s   | ]}t |tƒr|V  qd S r*   )r0   r-   )Ú.0Úopr   r   r   Ú	<genexpr>}   s     
 z:WriteBufferingTests.test_writeBuffering.<locals>.<genexpr>é   é   r
   r   N)	Újoinr%   r3   r0   r-   Úwriter/   ZdoWriteÚassertEqual)r   r'   Úexpectedr(   r+   ÚfdÚresultr   r   r   Útest_writeBufferingh   s    

z'WriteBufferingTests.test_writeBufferingN)r!   r"   r#   r$   r   ÚstÚlistsZone_ofÚbinaryZintegersr   rI   r   r   r   r   r7   c   s   þúÿr7   N)r$   Ú
__future__r   Útypingr   Z
hypothesisr   r   r   rJ   Ztwisted.internet.abstractr   r   Ztwisted.trial.unittestr	   Ztest_tcpr   r   r%   r7   r   r   r   r   Ú<module>   s   7