U
    
W[
                     @   sx   d Z ddlmZm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}.
    )divisionabsolute_import)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Z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   s   d S Nr   r   r   r   r   startWriting$   s    zMemoryFile.startWritingc                 C   s   d S r   r   r   r   r   r   stopWriting(   s    zMemoryFile.stopWritingc                 C   s:   t | jt|}|r6|  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   dataZacceptLengthr   r   r   writeSomeData,   s
    zMemoryFile.writeSomeDataN)	__name__
__module____qualname____doc__Z	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	   ZfooN)r   r   assertRaises	TypeErrorwriter   ZfileDescriptorr   r   r   $test_writeWithUnicodeRaisesException>   s    z8FileDescriptorTests.test_writeWithUnicodeRaisesExceptionc                 C   s&   t t d}| t|jdddg dS )zN
        L{FileDescriptor.writeSequence} doesn't accept unicode data.
        r	   s   fooZbars   bazN)r   r   r   r    ZwriteSequencer"   r   r   r   ,test_writeSequenceWithUnicodeRaisesExceptionF   s      z@FileDescriptorTests.test_writeSequenceWithUnicodeRaisesExceptionc                 C   s   |  ttt dS )zF
        L{FileDescriptor} should implement L{IPushProducer}.
        N)Z
assertTruer   r   r   r   r   r   r   $test_implementInterfaceIPushProducerO   s    z8FileDescriptorTests.test_implementInterfaceIPushProducerN)r   r   r   r   r#   r$   r%   r   r   r   r   r   :   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!   ZassertIsNoneZdoWrite)r   Z
descriptorr   r   r   test_kernelBufferFull[   s    
z*WriteDescriptorTests.test_kernelBufferFullN)r   r   r   r   r'   r   r   r   r   r&   W   s   r&   N)r   Z
__future__r   r   Zzope.interface.verifyr   Ztwisted.internet.abstractr   Ztwisted.internet.interfacesr   Ztwisted.trial.unittestr   r   r   r&   r   r   r   r   <module>   s   (