o
    ¯bï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)Úfailure)Ú	iterbytes)Úbanana)ÚStringTransport)Ú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 ]}tƒ }t ||j¡ | ¡ }t |¡}|  ||¡ qd S )Nr   éd   iè  iL  i@B i¤B l                )	ÚlistÚranger   r   Úint2b128ÚwriteÚgetvalueÚb1282intÚassertEqual)ÚselfÚ	funkylistÚiÚxÚvÚy© r   úA/usr/lib/python3/dist-packages/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)Ú_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)Ú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   ÚFileWrapperr"   Ú	putResultÚ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   r4   r   r   r   r0   X   s   
zBananaTestBase.putResultc                 C   s   | j  t tj¡¡ | ` d S ©N)r.   ÚconnectionLostr   ÚFailurer   ÚCONNECTION_DONEr2   r   r   r   ÚtearDowna   s   zBananaTestBase.tearDownN)
r   r   r   Ú__doc__r   ÚBananar-   r3   r0   r9   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J ‚d S )Ns   hello)r.   r%   ÚdataReceivedr,   r   r4   r2   r   r   r   Útest_stringk   ó   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``.
        Úhellozbuiltins.strN)Ú_unsupportedTypeTestr2   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)rA   Útyper2   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)rA   r   r   r2   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%   ÚassertInÚstr)r   r(   ÚnameÚexcr   r   r   rA   ˆ   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%   r=   r,   r   r   r4   ÚassertIsInstanceÚintr2   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   ó    |   ¡ }|  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)rW   rF   r   rG   r.   r%   )r   rV   r   r   r   Útest_encodeTooLargeLong¯   ó   z#BananaTests.test_encodeTooLargeLongc                 C   ój   |   ¡ }| 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.
        rP   r   N)rW   r.   ÚsetPrefixLimitrR   r%   r,   r   ÚtruncaterF   r   rG   r=   )r   rV   Úencodedr   r   r   Útest_decodeTooLargeLong·   ó   
z#BananaTests.test_decodeTooLargeLongc                 C   s
   |   ¡  S r5   )rW   r2   r   r   r   Ú_getLargestÅ   s   
zBananaTests._getLargestc                 C   rX   )zƒ
        Test that a negative long below the implementation-specific limit is
        rejected as too small to be encoded.
        N)ra   rF   r   rG   r.   r%   )r   rU   r   r   r   Útest_encodeTooSmallLongÈ   rZ   z#BananaTests.test_encodeTooSmallLongc                 C   r[   )zƒ
        Test that a negative long below the implementation specific limit is
        rejected as too small to be decoded.
        rP   r   N)ra   r.   r\   rR   r%   r,   r   r]   rF   r   rG   r=   )r   rU   r^   r   r   r   Útest_decodeTooSmallLongÐ   r`   z#BananaTests.test_decodeTooSmallLongc                 C   ó0   | j  d¡ | j  | j ¡ ¡ |  | jd¡ d S )Ni÷  ©r.   r%   r=   r,   r   r   r4   r2   r   r   r   Útest_integerÞ   r?   zBananaTests.test_integerc                 C   rd   )Ni	üÿÿre   r2   r   r   r   Útest_negativeã   r?   zBananaTests.test_negativec                 C   rd   )Ng     ¸�@re   r2   r   r   r   Ú
test_floatè   r?   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 )NrQ   rP   é   é   ç     €>@çš™™™™D@é   ó   sixó   sevenó   eighté	   é
   re   ©r   Ú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.
        rQ   rP   ri   rj   rk   rl   rm   rn   ro   rp   rq   rr   éþÿÿÿN)	ÚsysÚmaxsizer.   r%   Úfeedr,   r   r   r4   rs   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.   r=   )r   ÚdataÚbyter   r   r   ry     s   ÿzBananaTests.feedc                 C   ó   d}|   tj| j|¡ d S )Ns   €©rF   r   rG   ry   ©r   r{   r   r   r   Útest_oversizedList  ó   zBananaTests.test_oversizedListc                 C   r}   )Ns   ‚r~   r   r   r   r   Útest_oversizedString!  r�   z BananaTests.test_oversizedStringc                 C   s.   d}z	| j  |¡ W d S  tjy   Y d S w )Ns       €)r.   r=   r   rG   )r   ÚcrashStringr   r   r   Útest_crashString&  s   ÿzBananaTests.test_crashStringc                 C   rd   )Né   €re   r2   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ÿÿÿr…   s   �rP   ó   }rQ   ó   ~r   ó   s      …ó    ó   ri   ó   s   ƒs       ƒs      †ó   N)r   r*   )r   Ú	baseIntInÚ	baseNegInÚ
baseIntOutÚbaseLongIntOutÚ
baseNegOutÚbaseLongNegOutr   r   r   Útest_sizedIntegerTypesE  s$   z"BananaTests.test_sizedIntegerTypesN)r   r   r   r:   r>   rB   rD   rE   rA   rN   rW   rY   r_   ra   rb   rc   rf   rg   rh   ru   rz   ry   r€   r‚   r„   r†   r”   r   r   r   r   r<   f   s2    	
r<   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)rF   ÚNotImplementedErrorr.   r=   ÚlegalPbItemr2   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.   r=   r˜   r   r4   Úvocabr2   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.   rF   ÚKeyErrorr=   ÚillegalPbItemr2   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   r2   r   r   r   Útest_sendPbƒ  s   zDialectTests.test_sendPbN)r   r   r   r:   r›   rS   r   r;   ÚoutgoingVocabularyÚVOCABr˜   rž   r™   rœ   rŸ   r    r   r   r   r   r•   c  s    r•   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   �rQ   N)rF   r   rG   Údecoder   )r   ÚundecodableÚ	decodabler   r   r   Útest_statelessDecode’  s   z%GlobalCoderTests.test_statelessDecodeN)r   r   r   r:   r§   r   r   r   r   r£   �  s    r£   )rw   Ú	functoolsr   r,   r   Útwisted.internetr   r   Útwisted.pythonr   Útwisted.python.compatr   Útwisted.spreadr   Útwisted.test.proto_helpersr	   Útwisted.trial.unittestr
   Ú_maxintr   r"   r*   r+   r<   r•   r£   r   r   r   r   Ú<module>   s$    ~*