o
    ¯b  ã                   @   sx   d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
mZ ddlmZ G dd	„ d	eƒZG d
d„ deƒZdS )z
Test strerror
é    N)ÚskipIf)ÚECONNABORTED)Úplatform)Ú_ErrorFormatterÚformatError)ÚTestCasec                   @   s   e Zd ZdZdS )Ú_MyWindowsExceptionzT
    An exception type like L{ctypes.WinError}, but available on all platforms.
    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   ú</usr/lib/python3/dist-packages/twisted/test/test_strerror.pyr      s    r   c                   @   st   e Zd ZdZeZdZdd„ Zdd„ Zdd„ Z	d	d
„ Z
dd„ Zee ¡ dkdƒdd„ ƒZee ¡ dkdƒdd„ ƒZdS )ÚErrorFormatingTestsz3
    Tests for C{_ErrorFormatter.formatError}.
    zcorrect message valuec                 C   s0   t dddƒ}| | j¡}|  |t | j¡¡ dS )zž
        L{_ErrorFormatter.formatError} should use L{os.strerror} to format
        error messages if it is constructed without any better mechanism.
        N)r   r   ÚprobeErrorCodeÚassertEqualÚosÚstrerror©ÚselfÚ	formatterÚmessager   r   r   Útest_strerrorFormatting    s   z+ErrorFormatingTests.test_strerrorFormattingc                 C   sF   d}|   | j|¡ tdd|diƒ}| | j¡}|  |t | j¡¡ dS )zØ
        L{_ErrorFormatter.formatError} should use L{os.strerror} to format
        error messages if it is constructed with only an error tab which does
        not contain the error code it is called with.
        é   Núwrong message)ÚassertNotEqualr   r   r   r   r   r   )r   Úerrorr   r   r   r   r   Útest_emptyErrorTab)   s
   z&ErrorFormatingTests.test_emptyErrorTabc                 C   s2   t dd| j| jiƒ}| | j¡}|  || j¡ dS )zˆ
        L{_ErrorFormatter.formatError} should use C{errorTab} if it is supplied
        and contains the requested error code.
        N)r   r   ÚprobeMessager   r   r   r   r   r   Útest_errorTab6   s
   ÿz!ErrorFormatingTests.test_errorTabc                    sR   g ‰ ‡ ‡fdd„}t d|ˆjdiƒ}| ˆj¡}ˆ |ˆj¡ ˆ ˆ ˆjg¡ dS )z~
        L{_ErrorFormatter.formatError} should return the return value of
        C{formatMessage} if it is supplied.
        c                    s   ˆ   | ¡ ˆjS ©N)Úappendr   ©Ú	errorCode©ÚformatCallsr   r   r   ÚformatMessageH   s   
z=ErrorFormatingTests.test_formatMessage.<locals>.formatMessageNr   ©r   r   r   r   r   )r   r&   r   r   r   r$   r   Útest_formatMessageA   s   ÿz&ErrorFormatingTests.test_formatMessagec                    sF   g ‰‡ ‡fdd„}t |dd„ ˆ jdiƒ}| ˆ j¡}ˆ  |ˆ j¡ dS )zŸ
        L{_ErrorFormatter.formatError} should return the message argument from
        the exception L{winError} returns, if L{winError} is supplied.
        c                    s   ˆ  | ¡ t| ˆ jƒS r    )r!   r   r   r"   ©r   ÚwinCallsr   r   ÚwinErrorZ   s   
z3ErrorFormatingTests.test_winError.<locals>.winErrorc                 S   s   dS )NzformatMessage: wrong messager   )r   r   r   r   Ú<lambda>`   s    z3ErrorFormatingTests.test_winError.<locals>.<lambda>zerrorTab: wrong messageNr'   )r   r+   r   r   r   r)   r   Útest_winErrorS   s   ýz!ErrorFormatingTests.test_winErrorÚwin32zTest will run only on Windows.c                 C   s®   t  ¡ }|jdur ddlm} |  | | j¡|| jƒj¡ d|_|j	dur;ddl
m} |  | | j¡|| jƒ¡ d|_	|jdurUddlm} |  | | j¡|| j ¡ dS dS )z›
        L{_ErrorFormatter.fromEnvironment} should create an L{_ErrorFormatter}
        instance with attributes populated from available modules.
        Nr   ©ÚWinError©ÚFormatMessage)ÚerrorTab)r   ÚfromEnvironmentr+   Úctypesr0   r   r   r   r   r&   Úwin32apir2   r3   Úsocket)r   r   r0   r2   r3   r   r   r   Útest_fromEnvironmentf   s,   


þ

þ

þýz(ErrorFormatingTests.test_fromEnvironmentc                 C   s†   t jt g}zddlm} | |tƒj¡ W n	 ty   Y nw zddlm	} | |tƒ¡ W n	 ty8   Y nw |  
ttƒ|¡ dS )z¼
        Given a known-good errno, make sure that formatMessage gives results
        matching either C{socket.errorTab}, C{ctypes.WinError}, or
        C{win32api.FormatMessage}.
        r   r/   r1   N)r7   r3   r   r5   r0   r!   r   ÚImportErrorr6   r2   ÚassertInr   )r   Ú
acceptabler0   r2   r   r   r   Útest_correctLookupsˆ   s   ÿÿz'ErrorFormatingTests.test_correctLookupsN)r	   r
   r   r   r   r   r   r   r   r   r(   r-   r   r   ÚgetTyper8   r<   r   r   r   r   r      s    	
!r   )r   r   r7   Úunittestr   Útwisted.internet.tcpr   Útwisted.python.runtimer   Útwisted.python.win32r   r   Útwisted.trial.unittestr   ÚOSErrorr   r   r   r   r   r   Ú<module>   s   