o
    >hA                     @   sf  d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZ ddlmZ ddlmZ ddlmZm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eG d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#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 )"z%
Tests for L{twisted.python.compat}.
    N)skipIf)_PYPYbytesEnvironcmp
comparableexecfile
intToBytesioType	iterbyteslazyByteSlicenativeStringnetworkStringreraise)FilePath)platform)SynchronousTestCaseTestCasec                   @   H   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S )IOTypeTestsz?
    Test cases for determining a file-like object's type.
    c                 C      |  tt t dS )z=
        An L{io.StringIO} accepts and returns text.
        N)assertEqualr	   ioStringIOstrself r   x/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/test/test_compat.pytest_3StringIO(      zIOTypeTests.test_3StringIOc                 C   r   )z=
        An L{io.BytesIO} accepts and returns bytes.
        N)r   r	   r   BytesIObytesr   r   r   r   test_3BytesIO.   r   zIOTypeTests.test_3BytesIOc                 C   D   t |  d}| t|t W d   dS 1 sw   Y  dS )zT
        A file opened via 'io.open' in text mode accepts and returns text.
        wN)openmktempr   r	   r   r   fr   r   r   test_3openTextMode4      "zIOTypeTests.test_3openTextModec                 C   r#   )zW
        A file opened via 'io.open' in binary mode accepts and returns bytes.
        wbN)r%   r&   r   r	   r!   r'   r   r   r   test_3openBinaryMode;   r*   z IOTypeTests.test_3openBinaryModec                 C   sF   t |  d}| t|t W d   dS 1 sw   Y  dS )z
        The L{codecs} module, oddly, returns a file-like object which returns
        bytes when not passed an 'encoding' argument.
        r+   N)codecsr%   r&   r   r	   r!   r'   r   r   r   test_codecsOpenBytesB   s   "z IOTypeTests.test_codecsOpenBytesc                 C   sJ   t j|  ddd}| t|t W d   dS 1 sw   Y  dS )zY
        When passed an encoding, however, the L{codecs} module returns unicode.
        r+   utf-8)encodingN)r-   r%   r&   r   r	   r   r'   r   r   r   test_codecsOpenTextJ   s   "zIOTypeTests.test_codecsOpenTextc                 C   s   |  tt t dS )zy
        When passed an object about which no sensible decision can be made, err
        on the side of unicode.
        N)r   r	   objectr   r   r   r   r   test_defaultToTextQ   s   zIOTypeTests.test_defaultToTextN)__name__
__module____qualname____doc__r   r"   r)   r,   r.   r1   r3   r   r   r   r   r   #   s    r   c                   @       e Zd ZdZdd Zdd ZdS )CompatTestszy
    Various utility functions in C{twisted.python.compat} provide same
    functionality as modern Python variants.
    c                 C   s   t  }|d |d |d t|}|  | |g d |d t|}|  | |ddg |d ddh}||}t|}|  | |g d d	S )
zG
        L{set} should behave like the expected set interface.
        bca)r<   r:   r;   drs)r<   r;   r>   r?   N)setaddlistsortr   removediscardunionr   r<   r:   r=   r   r   r   test_set_   s"   





zCompatTests.test_setc                 C   sj   t ddg}| tt|d | t|ddg t ddg}||}t|}|  | |g d dS )zS
        L{frozenset} should behave like the expected frozenset interface.
        r<   r:   rA   r>   r?   )r<   r:   r>   r?   N)		frozensetassertRaisesAttributeErrorgetattrr   sortedrF   rB   rC   rG   r   r   r   test_frozensetw   s   
zCompatTests.test_frozensetN)r4   r5   r6   r7   rH   rN   r   r   r   r   r9   Y   s    r9   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )ExecfileCompatTestszE
    Tests for the Python 3-friendly L{execfile} implementation.
    c                 C   sP   |   }t|d}||d W d   n1 sw   Y  t|dS )zo
        Write L{content} to a new temporary file, returning the L{FilePath}
        for the new file.
        r+   asciiNr/   )r&   r%   writeencoder   )r   contentpathr(   r   r   r   writeScript   s
   zExecfileCompatTests.writeScriptc                 C   s2   |  d}ddi}t|j| | d|d  dS )zX
        L{execfile} executes the specified file in the given global namespace.
        	foo += 1
foo      NrU   r   rT   r   )r   scriptglobalNamespacer   r   r   test_execfileGlobals   s   
z(ExecfileCompatTests.test_execfileGlobalsc                 C   sL   |  d}ddi}ddi}t|j|| | d|d  | d|d  dS )zk
        L{execfile} executes the specified file in the given global and local
        namespaces.
        rV   rW   
         NrZ   )r   r[   r\   localNamespacer   r   r   test_execfileGlobalsAndLocals   s   
z1ExecfileCompatTests.test_execfileGlobalsAndLocalsc                 C   s@   dD ]}|  d| }ddi}t|j| | d|d  qdS )z
        L{execfile} reads in the specified file using universal newlines so
        that scripts written on one platform will work on another.
        )
z
zfoo = 'okay'rW   NokayrZ   )r   
lineEndingr[   r\   r   r   r   test_execfileUniversalNewlines   s   z2ExecfileCompatTests.test_execfileUniversalNewlinesN)r4   r5   r6   r7   rU   r]   rb   rg   r   r   r   r   rO      s    
	rO   c                   @   s   e Zd ZdZdd ZdS )PYPYTestz!
    Identification of PyPy.
    c                 C   s&   dt jv r| t dS | t dS )z,
        On PyPy, L{_PYPY} is True.
        PyPyN)sysversion
assertTruer   assertFalser   r   r   r   	test_PYPY   s   
zPYPYTest.test_PYPYN)r4   r5   r6   r7   rn   r   r   r   r   rh      s    rh   c                   @   r8   )
ComparablezE
    Objects that can be compared to each other, but not others.
    c                 C   s
   || _ d S N)value)r   rq   r   r   r   __init__   s   
zComparable.__init__c                 C   s   t |tstS t| j|jS rp   )
isinstancero   NotImplementedr   rq   )r   otherr   r   r   __cmp__   s   
zComparable.__cmp__N)r4   r5   r6   r7   rr   rv   r   r   r   r   ro      s    ro   c                   @   @   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S )ComparableTestszR
    L{comparable} decorated classes emulate Python 2's C{__cmp__} semantics.
    c                 C   s0   |  tdtdk | tdtdk dS )zo
        Instances of a class that is decorated by C{comparable} support
        equality comparisons.
        rX   rY   Nrl   ro   rm   r   r   r   r   test_equality      zComparableTests.test_equalityc                 C   s0   |  tdtdk | tdtdk dS )zq
        Instances of a class that is decorated by C{comparable} support
        inequality comparisons.
        rX   rY   N)rm   ro   rl   r   r   r   r   test_nonEquality   r{   z ComparableTests.test_nonEqualityc                 C   s0   |  tdtdk | tdtdk dS )zs
        Instances of a class that is decorated by C{comparable} support
        greater-than comparisons.
        rY   rX   r      Nry   r   r   r   r   test_greaterThan      z ComparableTests.test_greaterThanc                 C   sF   |  tdtdk |  tdtdk | tdtdk dS )z|
        Instances of a class that is decorated by C{comparable} support
        greater-than-or-equal comparisons.
        rX   rY   r   r}   Nry   r   r   r   r   test_greaterThanOrEqual      z'ComparableTests.test_greaterThanOrEqualc                 C   s0   |  tdtdk  | tdtdk  dS )zp
        Instances of a class that is decorated by C{comparable} support
        less-than comparisons.
        r   r}   rY   Nry   r   r   r   r   test_lessThan   r   zComparableTests.test_lessThanc                 C   sF   |  tdtdk |  tdtdk | tdtdk dS )zy
        Instances of a class that is decorated by C{comparable} support
        less-than-or-equal comparisons.
        r}   r   rY   Nry   r   r   r   r   test_lessThanOrEqual  r   z$ComparableTests.test_lessThanOrEqualN)
r4   r5   r6   r7   rz   r|   r~   r   r   r   r   r   r   r   rx      s    			rx   c                   @   rw   )Python3ComparableTestsz;
    Python 3-specific functionality of C{comparable}.
    c                 C      |  tdt t dS )z
        Instances of a class that is decorated by C{comparable} support
        returning C{NotImplemented} from C{__eq__} if it is returned by the
        underlying C{__cmp__} call.
        rX   N)r   ro   __eq__r2   rt   r   r   r   r   test_notImplementedEquals     z0Python3ComparableTests.test_notImplementedEqualsc                 C   r   )z
        Instances of a class that is decorated by C{comparable} support
        returning C{NotImplemented} from C{__ne__} if it is returned by the
        underlying C{__cmp__} call.
        rX   N)r   ro   __ne__r2   rt   r   r   r   r   test_notImplementedNotEquals  r   z3Python3ComparableTests.test_notImplementedNotEqualsc                 C   r   )z
        Instances of a class that is decorated by C{comparable} support
        returning C{NotImplemented} from C{__gt__} if it is returned by the
        underlying C{__cmp__} call.
        rX   N)r   ro   __gt__r2   rt   r   r   r   r   test_notImplementedGreaterThan#  r   z5Python3ComparableTests.test_notImplementedGreaterThanc                 C   r   )z
        Instances of a class that is decorated by C{comparable} support
        returning C{NotImplemented} from C{__lt__} if it is returned by the
        underlying C{__cmp__} call.
        rX   N)r   ro   __lt__r2   rt   r   r   r   r   test_notImplementedLessThan+  r   z2Python3ComparableTests.test_notImplementedLessThanc                 C   r   )z
        Instances of a class that is decorated by C{comparable} support
        returning C{NotImplemented} from C{__ge__} if it is returned by the
        underlying C{__cmp__} call.
        rX   N)r   ro   __ge__r2   rt   r   r   r   r   $test_notImplementedGreaterThanEquals3  r   z;Python3ComparableTests.test_notImplementedGreaterThanEqualsc                 C   r   )z
        Instances of a class that is decorated by C{comparable} support
        returning C{NotImplemented} from C{__le__} if it is returned by the
        underlying C{__cmp__} call.
        rX   N)r   ro   __le__r2   rt   r   r   r   r   !test_notImplementedLessThanEquals;  r   z8Python3ComparableTests.test_notImplementedLessThanEqualsN)
r4   r5   r6   r7   r   r   r   r   r   r   r   r   r   r   r     s    r   c                   @   (   e Zd ZdZdd Zdd Zdd ZdS )	CmpTestszA
    L{cmp} should behave like the built-in Python 2 C{cmp}.
    c                 C   s>   |  tddd |  tddd |  tdgdgd dS )z5
        L{cmp} returns 0 for equal objects.
        r<   r   rX   Nr   r   r   r   r   r   test_equalsI  s   zCmpTests.test_equalsc                 C   (   |  tddd |  tddd dS )zS
        L{cmp} returns 1 if its first argument is bigger than its second.
           r   rX      z   aNr   r   r   r   r   r~   Q     zCmpTests.test_greaterThanc                 C   r   )zU
        L{cmp} returns -1 if its first argument is smaller than its second.
        g?gffffff@r      dNr   r   r   r   r   r   X  r   zCmpTests.test_lessThanN)r4   r5   r6   r7   r   r~   r   r   r   r   r   r   D  s
    r   c                   @   r   )StringTestsz8
    Compatibility functions and types for strings.
    c                 C   s$   |  t|| | t|t dS )z
        Raise an exception indicating a failed test if the output of
        C{nativeString(original)} is unequal to the expected string, or is not
        a native string.
        N)r   r   assertIsInstancer   )r   originalexpectedr   r   r   assertNativeStringe  s   zStringTests.assertNativeStringc                 C      |  ttd dS )zj
        C{nativeString} raises a C{UnicodeError} if input bytes are not ASCII
        decodable.
           NrJ   UnicodeErrorr   r   r   r   r   test_nonASCIIBytesToStringn     z&StringTests.test_nonASCIIBytesToStringc                 C   r   )zk
        C{nativeString} raises a C{UnicodeError} if input Unicode is not ASCII
        encodable.
        u   ሴNr   r   r   r   r   test_nonASCIIUnicodeToStringu  r   z(StringTests.test_nonASCIIUnicodeToStringc                 C   s   |  dd dS )z
        C{nativeString} converts bytes to the native string format, assuming
        an ASCII encoding if applicable.
        s   hellohelloNr   r   r   r   r   test_bytesToString|     zStringTests.test_bytesToStringc                 C      |  dd dS )z
        C{nativeString} converts unicode to the native string format, assuming
        an ASCII encoding if applicable.
        zGood dayNr   r   r   r   r   test_unicodeToString  r   z StringTests.test_unicodeToStringc                 C   r   )zJ
        C{nativeString} leaves native strings as native strings.
        zHello!Nr   r   r   r   r   test_stringToString  s   zStringTests.test_stringToStringc                 C   r   )zu
        C{nativeString} raises a C{TypeError} if given an object that is not a
        string of some sort.
        rX   N)rJ   	TypeErrorr   r   r   r   r   test_unexpectedType  r   zStringTests.test_unexpectedTypeN)r4   r5   r6   r7   r   r   r   r   r   r   r   r   r   r   r   r   `  s    	r   c                   @   r   )	NetworkStringTestsz%
    Tests for L{networkString}.
    c                 C   s   |  dtd dS )zu
        L{networkString} returns a C{unicode} object passed to it encoded into
        a C{bytes} instance.
        s   foorW   N)r   r   r   r   r   r   test_str     zNetworkStringTests.test_strc                 C   r   )z
        L{networkString} raises L{UnicodeError} if passed a C{unicode} instance
        containing characters not encodable in ASCII.
        u   ☃N)rJ   r   r   r   r   r   r   test_unicodeOutOfRange  r   z)NetworkStringTests.test_unicodeOutOfRangec                 C   s"   |  ttt  |  ttd dS )z
        L{networkString} raises L{TypeError} if passed a non-string object or
        the wrong type of string object.
        s   bytesN)rJ   r   r   r2   r   r   r   r   test_nonString  s   z!NetworkStringTests.test_nonStringN)r4   r5   r6   r7   r   r   r   r   r   r   r   r     s
    r   c                   @   r8   )ReraiseTestszH
    L{reraise} re-raises exceptions on both Python 2 and Python 3.
    c              	   C   s   zdd  W n t y   t \}}}Y nw zt|d W n- t yK   t \}}}| |t | || | t	|d t	|d  Y dS w | 
d dS )z
        Calling L{reraise} with an exception instance and a traceback of
        L{None} re-raises it with a new traceback.
        rX   r   Nr   The exception was not raised.)BaseExceptionrj   exc_infor   r   ZeroDivisionErrorassertIsassertNotEqual	traceback	format_tbfailr   typrq   tbtyp2value2tb2r   r   r   test_reraiseWithNone  s    
z!ReraiseTests.test_reraiseWithNonec              	   C   s   zdd  W n t y   t \}}}Y nw zt|| W n- t yK   t \}}}| |t | || | t|d t|d  Y dS w | 	d dS )z
        Calling L{reraise} with an exception instance and a traceback
        re-raises the exception with the given traceback.
        rX   r   r   r   N)
r   rj   r   r   r   r   r   r   r   r   r   r   r   r   test_reraiseWithTraceback  s   &z&ReraiseTests.test_reraiseWithTracebackN)r4   r5   r6   r7   r   r   r   r   r   r   r     s    r   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )Python3BytesTestszB
    Tests for L{iterbytes}, L{intToBytes}, L{lazyByteSlice}.
    c                 C   s$   d}t t|}| |g d dS )z
        When L{iterbytes} is called with a bytestring, the returned object
        can be iterated over, resulting in the individual bytes of the
        bytestring.
        s   abcd)r      b   cr   N)rB   r
   r   )r   inputresultr   r   r   test_iteration  s   z Python3BytesTests.test_iterationc                 C   s   |  tdd dS )z
        When L{intToBytes} is called with an integer, the result is an
        ASCII-encoded string representation of the number.
           s   213N)r   r   r   r   r   r   test_intToBytes  r   z!Python3BytesTests.test_intToBytesc                 C   s   d}|  tt|| dS )zv
        L{lazyByteSlice} called with some bytes returns a semantically equal
        version of these bytes.
           123XYZNr   r!   r   r   datar   r   r   test_lazyByteSliceNoOffset  s   z,Python3BytesTests.test_lazyByteSliceNoOffsetc                 C   s&   d}|  tt|d|dd  dS )z
        L{lazyByteSlice} called with some bytes and an offset returns a
        semantically equal version of these bytes starting at the given offset.
        r   rY   Nr   r   r   r   r   test_lazyByteSliceOffset   s   "z*Python3BytesTests.test_lazyByteSliceOffsetc                 C   s(   d}|  tt|dd|dd  dS )z
        L{lazyByteSlice} called with some bytes, an offset and a length returns
        a semantically equal version of these bytes starting at the given
        offset, up to the given length.
        r   rY   r}      Nr   r   r   r   r   !test_lazyByteSliceOffsetAndLength  s   $z3Python3BytesTests.test_lazyByteSliceOffsetAndLengthN)	r4   r5   r6   r7   r   r   r   r   r   r   r   r   r   r     s    
r   c                   @   s&   e Zd ZdZee ddd ZdS )BytesEnvironTestsz$
    Tests for L{BytesEnviron}.
    z+Environment vars are always str on Windows.c                 C   sP   t  }t }| D ]\}}|t| |t| q
| t|tg dS )zz
        The output of L{BytesEnviron} should always be a L{dict} with L{bytes}
        values and L{bytes} keys.
        N)r   r@   itemsrA   typer   rB   r!   )r   r   typeskeyvalr   r   r   test_alwaysBytes  s   z"BytesEnvironTests.test_alwaysBytesN)r4   r5   r6   r7   r   r   	isWindowsr   r   r   r   r   r     s    r   )(r7   r-   r   rj   r   unittestr   twisted.python.compatr   r   r   r   r   r   r	   r
   r   r   r   r   twisted.python.filepathr   twisted.python.runtimer   twisted.trial.unittestr   r   r   r9   rO   rh   ro   rx   r   r   r   r   r   r   r   r   r   r   r   <module>   s0   86-0:68.0