o
    Óó·ié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 ]}t }t ||j” | ” }t |”}|  ||” qd 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   śeC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-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)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}.
    N)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J d S )Ns   hello)r)   r!   ŚdataReceivedr'   r   r-   r+   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``.
        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   ó    |   ” }|  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Æ   ó   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.
        rC   r   N)rJ   r)   ŚsetPrefixLimitrE   r!   r'   r   Śtruncater;   r	   r<   r3   )r   rI   Śencodedr   r   r   Śtest_decodeTooLargeLong·   ó   
z#BananaTests.test_decodeTooLargeLongc                 C   s
   |   ”  S r.   )rJ   r+   r   r   r   Ś_getLargestÅ   s   
zBananaTests._getLargestc                 C   rK   )z
        Test that a negative long below the implementation-specific limit is
        rejected as too small to be encoded.
        N)rT   r;   r	   r<   r)   r!   )r   rH   r   r   r   Śtest_encodeTooSmallLongČ   rM   z#BananaTests.test_encodeTooSmallLongc                 C   rN   )z
        Test that a negative long below the implementation specific limit is
        rejected as too small to be decoded.
        rC   r   N)rT   r)   rO   rE   r!   r'   r   rP   r;   r	   r<   r3   )r   rH   rQ   r   r   r   Śtest_decodeTooSmallLongŠ   rS   z#BananaTests.test_decodeTooSmallLongc                 C   ó0   | j  d” | j  | j ” ” |  | jd” d S )Ni÷  ©r)   r!   r3   r'   r   r   r-   r+   r   r   r   Śtest_integerŽ   r5   zBananaTests.test_integerc                 C   rW   )Ni	ü’’rX   r+   r   r   r   Śtest_negativeć   r5   zBananaTests.test_negativec                 C   rW   )Ng     ø@rX   r+   r   r   r   Ś
test_floatč   r5   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é	   é
   rX   ©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   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   éž’’’N)	ŚsysŚmaxsizer)   r!   Śfeedr'   r   r   r-   rf   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   rk     s   ’zBananaTests.feedc                 C   ó   d}|   tj| j|” d S )Ns   ©r;   r	   r<   rk   ©r   rm   r   r   r   Śtest_oversizedList  ó   zBananaTests.test_oversizedListc                 C   ro   )Ns   rp   rq   r   r   r   Śtest_oversizedString!  rs   z BananaTests.test_oversizedStringc                 C   s.   d}z	| j  |” W d S  tjy   Y d S w )Ns       )r)   r3   r	   r<   )r   ZcrashStringr   r   r   Śtest_crashString&  s   ’zBananaTests.test_crashStringc                 C   rW   )Né   rX   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’’’rv   s   rC   ó   }rD   ó   ~r   ó   s      ó    ó   r\   ó   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   r4   r7   r9   r:   r6   rA   rJ   rL   rR   rT   rU   rV   rY   rZ   r[   rg   rl   rk   rr   rt   ru   rw   r   r   r   r   r   r2   f   s2    	
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   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   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   )ri   Ś	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   r   r   r   r   r   r   Ś<module>   s$    ~*