o
    ¯bÉ
  ã                   @   sh   d Z ddlmZ ddl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 )zA
Whitebox tests for L{twisted.internet.abstract.FileDescriptor}.
é    )ÚverifyClass)ÚFileDescriptor)ÚIPushProducer)ÚSynchronousTestCasec                   @   s4   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ ZdS )Ú
MemoryFilea-  
    A L{FileDescriptor} customization which writes to a Python list in memory
    with certain limitations.

    @ivar _written: A C{list} of C{bytes} which have been accepted as written.

    @ivar _freeSpace: A C{int} giving the number of bytes which will be accepted
        by future writes.
    Tc                 C   s    t j| tƒ d� g | _d| _d S )N©Úreactorr   )r   Ú__init__ÚobjectÚ_writtenÚ
_freeSpace©Úself© r   úK/usr/lib/python3/dist-packages/twisted/internet/test/test_filedescriptor.pyr	      s   
zMemoryFile.__init__c                 C   ó   d S ©Nr   r   r   r   r   ÚstartWriting"   ó   zMemoryFile.startWritingc                 C   r   r   r   r   r   r   r   ÚstopWriting%   r   zMemoryFile.stopWritingc                 C   s:   t | jt|ƒƒ}|r|  j|8  _| j |d|… ¡ |S )zª
        Copy at most C{self._freeSpace} bytes from C{data} into C{self._written}.

        @return: A C{int} indicating how many bytes were copied from C{data}.
        N)Úminr   Úlenr   Úappend)r   ÚdataÚacceptLengthr   r   r   ÚwriteSomeData(   s
   zMemoryFile.writeSomeDataN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	connectedr	   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d„ ZdS )	ÚFileDescriptorTestsz&
    Tests for L{FileDescriptor}.
    c                 C   s    t tƒ d�}|  t|jd¡ dS )zF
        L{FileDescriptor.write} doesn't accept unicode data.
        r   ÚfooN)r   r
   ÚassertRaisesÚ	TypeErrorÚwrite©r   ÚfileDescriptorr   r   r   Ú$test_writeWithUnicodeRaisesException:   s   z8FileDescriptorTests.test_writeWithUnicodeRaisesExceptionc                 C   s$   t tƒ d�}|  t|jg d¢¡ dS )zN
        L{FileDescriptor.writeSequence} doesn't accept unicode data.
        r   )s   fooÚbars   bazN)r   r
   r#   r$   ÚwriteSequencer&   r   r   r   Ú,test_writeSequenceWithUnicodeRaisesExceptionA   s   ÿz@FileDescriptorTests.test_writeSequenceWithUnicodeRaisesExceptionc                 C   s   |   tttƒ¡ dS )zF
        L{FileDescriptor} should implement L{IPushProducer}.
        N)Ú
assertTruer   r   r   r   r   r   r   Ú$test_implementInterfaceIPushProducerJ   s   z8FileDescriptorTests.test_implementInterfaceIPushProducerN)r   r   r   r   r(   r+   r-   r   r   r   r   r!   5   s
    	r!   c                   @   s   e Zd ZdZdd„ ZdS )ÚWriteDescriptorTestszN
    Tests for L{FileDescriptor}'s implementation of L{IWriteDescriptor}.
    c                 C   s"   t ƒ }| d¡ |  | ¡ ¡ dS )z³
        When L{FileDescriptor.writeSomeData} returns C{0} to indicate no more
        data can be written immediately, L{FileDescriptor.doWrite} returns
        L{None}.
        s   hello, worldN)r   r%   ÚassertIsNoneÚdoWrite)r   Ú
descriptorr   r   r   Útest_kernelBufferFullV   s   
z*WriteDescriptorTests.test_kernelBufferFullN)r   r   r   r   r2   r   r   r   r   r.   Q   s    r.   N)r   Úzope.interface.verifyr   Útwisted.internet.abstractr   Útwisted.internet.interfacesr   Útwisted.trial.unittestr   r   r!   r.   r   r   r   r   Ú<module>   s   %