U
    2ÜŸjé7  ã                   @   sÔ   d dl Z d dlmZ d dlm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 d d	lmZ d
ZG dd„ deƒZdd„ Zdd„ 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dS )é    N)Úpartial)ÚBytesIO)ÚmainÚprotocol)ÚStringTransport)Úfailure)Ú	iterbytes)Úbanana)ÚTestCasel   ÿÿÿÿ c                   @   s   e Zd Zdd„ ZdS )Ú	MathTestsc                 C   sp   t tddƒƒt tddƒƒ t tddƒƒ dg }|D ]6}tƒ }t ||j¡ | ¡ }t |¡}|  ||¡ q4d S )Nr   éd   iè  iL  i@B i¤B l                )	ÚlistÚranger   r	   Zint2b128ÚwriteÚgetvalueZb1282intÚassertEqual)ÚselfZ	funkylistÚiÚxÚvÚy© r   úC/tmp/pip-unpacked-wheel-4b529toj/twisted/spread/test/test_banana.pyÚtest_int2b128   s    ÿþýÿ
zMathTests.test_int2b128N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r      s   r   c                 C   s   |   |¡ dS )zÜ
    Dictate a Banana dialect to use.

    @param protocol: A L{banana.Banana} instance which has not yet had a
        dialect negotiated.

    @param dialect: A L{bytes} instance naming a Banana dialect to select.
    N)Z_selectDialect)r   Údialectr   r   r   ÚselectDialect#   s    r   c                 C   s0   t ƒ }| ƒ }| |¡ | ¡  | |¡ | ¡ S )a«  
    Banana encode an object using L{banana.Banana.sendEncoded}.

    @param bananaFactory: A no-argument callable which will return a new,
        unconnected protocol instance to use to do the encoding (this should
        most likely be a L{banana.Banana} instance).

    @param obj: The object to encode.
    @type obj: Any type supported by Banana.

    @return: A L{bytes} instance giving the encoded form of C{obj}.
    )r   ÚmakeConnectionÚclearÚsendEncodedÚvalue)ZbananaFactoryÚobjÚ	transportr	   r   r   r   Úencode2   s    

r%   c                   @   s.   e Zd ZdZejZdd„ Zdd„ Zdd„ Z	dS )	ÚBananaTestBasezn
    The base for test classes. It defines commonly used things and sets up a
    connection for testing.
    c                 C   sN   t ƒ | _|  ¡ | _| j t | j¡¡ t| jdƒ | j| j_	t
t| jƒ| _d S )Ns   none)r   ÚioÚencClassÚencr   r   ZFileWrapperr   Ú	putResultZexpressionReceivedr   r%   ©r   r   r   r   ÚsetUpP   s    

zBananaTestBase.setUpc                 C   s
   || _ dS )z¨
        Store an expression received by C{self.enc}.

        @param result: The object that was received.
        @type result: Any type supported by Banana.
        N)Úresult)r   r-   r   r   r   r*   X   s    zBananaTestBase.putResultc                 C   s   | j  t tj¡¡ | ` d S ©N)r)   ZconnectionLostr   ZFailurer   ZCONNECTION_DONEr+   r   r   r   ÚtearDowna   s    zBananaTestBase.tearDownN)
r   r   r   Ú__doc__r	   ÚBananar(   r,   r*   r/   r   r   r   r   r&   H   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 d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0S )1ÚBananaTestsz
    General banana tests.
    c                 C   s0   | j  d¡ | j  | j ¡ ¡ | jdks,t‚d S )Ns   hello)r)   r!   ÚdataReceivedr'   r   r-   ÚAssertionErrorr+   r   r   r   Útest_stringk   s    zBananaTests.test_stringc                 C   s   |   dd¡ dS )z”
        Banana does not support unicode.  ``Banana.sendEncoded`` raises
        ``BananaError`` if called with an instance of ``unicode``.
        Zhellozbuiltins.strN)Ú_unsupportedTypeTestr+   r   r   r   Útest_unsupportedUnicodep   s    z#BananaTests.test_unsupportedUnicodec                 C   s   |   td¡ dS )zÀ
        Banana does not support arbitrary builtin types like L{type}.
        L{banana.Banana.sendEncoded} raises L{banana.BananaError} if called
        with an instance of L{type}.
        zbuiltins.typeN)r6   Útyper+   r   r   r   Útest_unsupportedBuiltinTypew   s    z'BananaTests.test_unsupportedBuiltinTypec                 C   s   |   tƒ td ¡ dS )zæ
        Banana does not support arbitrary user-defined types (such as those
        defined with the ``class`` statement).  ``Banana.sendEncoded`` raises
        ``BananaError`` if called with an instance of such a type.
        z
.MathTestsN)r6   r   r   r+   r   r   r   Útest_unsupportedUserType€   s    z$BananaTests.test_unsupportedUserTypec                 C   s0   |   tj| jj|¡}|  d|› d�t|ƒ¡ dS )aû  
        Assert that L{banana.Banana.sendEncoded} raises L{banana.BananaError}
        if called with the given object.

        @param obj: Some object that Banana does not support.
        @param name: The name of the type of the object.

        @raise: The failure exception is raised if L{Banana.sendEncoded} does
            not raise L{banana.BananaError} or if the message associated with the
            exception is not formatted to include the type of the unsupported
            object.
        zBanana cannot send z objectsN)ÚassertRaisesr	   ÚBananaErrorr)   r!   ZassertInÚstr)r   r#   ÚnameÚexcr   r   r   r6   ˆ   s    z BananaTests._unsupportedTypeTestc                 C   s>   | j  d¡ | j  | j ¡ ¡ |  | jd¡ |  | jt¡ dS )z×
        A positive integer less than 2 ** 32 should round-trip through
        banana without changing value and should come out represented
        as an C{int} (regardless of the type which was encoded).
        i§'  N)	r)   r!   r3   r'   r   r   r-   ZassertIsInstanceÚintr+   r   r   r   Útest_int˜   s    zBananaTests.test_intc                 C   s(   | j j}|d }d| d }|d }|S )Né   é   é   )r)   ÚprefixLimit)r   ÚbytesÚbitsÚlargestÚsmallestr   r   r   Ú_getSmallest£   s
    zBananaTests._getSmallestc                 C   s    |   ¡ }|  tj| jj|¡ dS )zz
        Test that a long above the implementation-specific limit is rejected
        as too large to be encoded.
        N)rJ   r;   r	   r<   r)   r!   )r   rI   r   r   r   Útest_encodeTooLargeLong¯   s    z#BananaTests.test_encodeTooLargeLongc                 C   sj   |   ¡ }| j | jjd ¡ | j |¡ | j ¡ }| j d¡ | j | jjd ¡ |  t	j
| jj|¡ dS )zz
        Test that a long above the implementation specific limit is rejected
        as too large to be decoded.
        rC   r   N)rJ   r)   ÚsetPrefixLimitrE   r!   r'   r   Útruncater;   r	   r<   r3   )r   rI   Úencodedr   r   r   Útest_decodeTooLargeLong·   s    
z#BananaTests.test_decodeTooLargeLongc                 C   s
   |   ¡  S r.   )rJ   r+   r   r   r   Ú_getLargestÅ   s    zBananaTests._getLargestc                 C   s    |   ¡ }|  tj| jj|¡ dS )zƒ
        Test that a negative long below the implementation-specific limit is
        rejected as too small to be encoded.
        N)rP   r;   r	   r<   r)   r!   )r   rH   r   r   r   Útest_encodeTooSmallLongÈ   s    z#BananaTests.test_encodeTooSmallLongc                 C   sj   |   ¡ }| j | jjd ¡ | j |¡ | j ¡ }| j d¡ | j | jjd ¡ |  t	j
| jj|¡ dS )zƒ
        Test that a negative long below the implementation specific limit is
        rejected as too small to be decoded.
        rC   r   N)rP   r)   rL   rE   r!   r'   r   rM   r;   r	   r<   r3   )r   rH   rN   r   r   r   Útest_decodeTooSmallLongÐ   s    
z#BananaTests.test_decodeTooSmallLongc                 C   s0   | j  d¡ | j  | j ¡ ¡ |  | jd¡ d S )Ni÷  ©r)   r!   r3   r'   r   r   r-   r+   r   r   r   Útest_integerÞ   s    zBananaTests.test_integerc                 C   s0   | j  d¡ | j  | j ¡ ¡ |  | jd¡ d S )Ni	üÿÿrS   r+   r   r   r   Útest_negativeã   s    zBananaTests.test_negativec                 C   s0   | j  d¡ | j  | j ¡ ¡ |  | jd¡ d S )Ng     ¸�@rS   r+   r   r   r   Ú
test_floatè   s    zBananaTests.test_floatc              	   C   sX   ddddgddgddd	d
dggdgg g}| j  |¡ | j  | j ¡ ¡ |  | j|¡ d S )NrD   rC   é   é   ç     €>@çš™™™™D@é   ó   sixó   sevenó   eighté	   é
   rS   ©r   Zfoor   r   r   Ú	test_listí   s    ø
zBananaTests.test_listc                 C   sl   ddddgddgddd	d
dggdgt jd t jd t jd g
}| j |¡ |  | j ¡ ¡ |  | j|¡ dS )zj
        Test feeding the data byte per byte to the receiver. Normally
        data is not split.
        rD   rC   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   éþÿÿÿN)	ÚsysÚmaxsizer)   r!   Úfeedr'   r   r   r-   ra   r   r   r   Útest_partialü   s    õzBananaTests.test_partialc                 C   s   t |ƒD ]}| j |¡ qdS )z‡
        Feed the data byte per byte to the receiver.

        @param data: The bytes to deliver.
        @type data: L{bytes}
        N)r   r)   r3   )r   ÚdataÚbyter   r   r   rf     s    zBananaTests.feedc                 C   s   d}|   tj| j|¡ d S )Ns   €©r;   r	   r<   rf   ©r   rh   r   r   r   Útest_oversizedList  s    zBananaTests.test_oversizedListc                 C   s   d}|   tj| j|¡ d S )Ns   ‚rj   rk   r   r   r   Útest_oversizedString!  s    z BananaTests.test_oversizedStringc                 C   s0   d}z| j  |¡ W n tjk
r*   Y nX d S )Ns       €)r)   r3   r	   r<   )r   ZcrashStringr   r   r   Útest_crashString&  s
    zBananaTests.test_crashStringc                 C   s0   | j  d¡ | j  | j ¡ ¡ |  | jd¡ d S )Né   €rS   r+   r   r   r   Útest_crashNegativeLong9  s    z"BananaTests.test_crashNegativeLongc                 C   sP  d}d}d}|   |  |d ¡d| ¡ |   |  |d ¡d| ¡ |   |  |d ¡d	| ¡ d
}|   |  |d ¡d| ¡ |   |  |d ¡d| ¡ |   |  |d ¡d| ¡ d}|   |  |d ¡d| ¡ |   |  |d ¡d	| ¡ |   |  |d ¡d¡ d}|   |  |d ¡d| ¡ |   |  |d ¡d| ¡ |   |  |d ¡d| ¡ dS )zÇ
        Test that integers below the maximum C{INT} token size cutoff are
        serialized as C{INT} or C{NEG} and that larger integers are
        serialized as C{LONGINT} or C{LONGNEG}.
        iÿÿÿro   s   �rC   ó   }rD   ó   ~r   ó   s      …ó    ó   rW   ó   s   ƒs       ƒs      †ó   N)r   r%   )r   Z	baseIntInZ	baseNegInZ
baseIntOutZbaseLongIntOutZ
baseNegOutZbaseLongNegOutr   r   r   Útest_sizedIntegerTypesE  s$    z"BananaTests.test_sizedIntegerTypesN)r   r   r   r0   r5   r7   r9   r:   r6   rA   rJ   rK   rO   rP   rQ   rR   rT   rU   rV   rb   rg   rf   rl   rm   rn   rp   rx   r   r   r   r   r2   f   s0   	
r2   c                   @   sZ   e Zd ZdZdZeejje fƒej	 Z
edƒej	 Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚDialectTestsz2
    Tests for Banana's handling of dialects.
    s   remote)éz   c                 C   s   |   t| jj| j¡ dS )z|
        If no dialect has been selected and a PB VOCAB item is received,
        L{NotImplementedError} is raised.
        N)r;   ÚNotImplementedErrorr)   r3   ÚlegalPbItemr+   r   r   r   Útest_dialectNotSetl  s    zDialectTests.test_dialectNotSetc                 C   s.   t | jdƒ | j | j¡ |  | j| j¡ dS )zS
        If the PB dialect has been selected, a PB VOCAB item is accepted.
        ó   pbN)r   r)   r3   r|   r   r-   Úvocabr+   r   r   r   Útest_receivePbs  s    zDialectTests.test_receivePbc                 C   s$   t | jdƒ |  t| jj| j¡ dS )zž
        If the PB dialect has been selected and an unrecognized PB VOCAB item
        is received, L{banana.Banana.dataReceived} raises L{KeyError}.
        r~   N)r   r)   r;   ÚKeyErrorr3   ÚillegalPbItemr+   r   r   r   Útest_receiveIllegalPb{  s    z"DialectTests.test_receiveIllegalPbc                 C   s2   t | jdƒ | j | j¡ |  | j| j ¡ ¡ dS )zl
        if pb dialect is selected, the sender must be able to send things in
        that dialect.
        r~   N)r   r)   r!   r   r   r|   r'   r   r+   r   r   r   Útest_sendPbƒ  s    zDialectTests.test_sendPbN)r   r   r   r0   r   rF   r	   r1   ZoutgoingVocabularyZVOCABr|   r‚   r}   r€   rƒ   r„   r   r   r   r   ry   c  s   ry   c                   @   s   e Zd ZdZdd„ ZdS )ÚGlobalCoderTestszM
    Tests for the free functions L{banana.encode} and L{banana.decode}.
    c                 C   s0   d}|   tjtj|¡ d}|  t |¡d¡ dS )zJ
        Calls to L{banana.decode} are independent of each other.
        sB   …s   �rD   N)r;   r	   r<   Údecoder   )r   ZundecodableZ	decodabler   r   r   Útest_statelessDecode’  s    z%GlobalCoderTests.test_statelessDecodeN)r   r   r   r0   r‡   r   r   r   r   r…   �  s   r…   )rd   Ú	functoolsr   r'   r   Ztwisted.internetr   r   Ztwisted.internet.testingr   Ztwisted.pythonr   Ztwisted.python.compatr   Ztwisted.spreadr	   Ztwisted.trial.unittestr
   Z_maxintr   r   r%   r&   r2   ry   r…   r   r   r   r   Ú<module>   s"    ~*