U
    ª€ljg  ã                   @   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   ú>/tmp/pip-unpacked-wheel-aghyhbke/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)Z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   ©Z	errorCode©Z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   ZwinCallsr   r   ÚwinErrorZ   s    
z3ErrorFormatingTests.test_winError.<locals>.winErrorc                 S   s   dS )NzformatMessage: wrong messager   )r   r   r   r   Ú<lambda>`   ó    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k	r@ddlm} |  | | j¡|| jƒj¡ d|_|j	dk	rvddl
m} |  | | j¡|| jƒ¡ d|_	|jdk	r¦ddlm} |  | | j¡|| j ¡ dS )z›
        L{_ErrorFormatter.fromEnvironment} should create an L{_ErrorFormatter}
        instance with attributes populated from available modules.
        Nr   ©ÚWinError©ÚFormatMessage)ÚerrorTab)r   ZfromEnvironmentr'   Úctypesr-   r   r   r   r   r#   Úwin32apir/   r0   Úsocket)r   r   r-   r/   r0   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k
r@   Y nX zddlm	} | |tƒ¡ W n tk
rt   Y nX |  
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,   r.   N)r3   r0   r   r1   r-   r    r   ÚImportErrorr2   r/   ZassertInr   )r   Z
acceptabler-   r/   r   r   r   Útest_correctLookupsˆ   s    z'ErrorFormatingTests.test_correctLookupsN)r	   r
   r   r   r   r   r   r   r   r   r%   r*   r   r   ÚgetTyper4   r6   r   r   r   r   r      s   	
!r   )r   r   r3   Zunittestr   Ztwisted.internet.tcpr   Ztwisted.python.runtimer   Ztwisted.python.win32r   r   Ztwisted.trial.unittestr   ÚOSErrorr   r   r   r   r   r   Ú<module>   s   