U
    
W[?  ã                   @   sl   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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)ÚTestCase)ÚECONNABORTED)Ú_ErrorFormatterÚformatError)Úplatformc                   @   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dd„ Ze ¡ dkrVde_dd„ Ze ¡ dkrpde_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_errorTab7   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_formatMessageB   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   ÚwinErrorX   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_winErrorR   s    ýz!ErrorFormatingTests.test_winErrorc                 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-   r.   Úsocket)r   r   r+   r-   r.   r   r   r   Útest_fromEnvironmentc   s*    


þ

þ

þz(ErrorFormatingTests.test_fromEnvironmentZwin32zTest will run only on Windows.c                 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)r1   r.   r   r/   r+   r   r   ÚImportErrorr0   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)   r2   r   ZgetTypeÚskipr4   r   r   r   r   r      s   
r   )r   r1   r   Ztwisted.trial.unittestr   Ztwisted.internet.tcpr   Ztwisted.python.win32r   r   Ztwisted.python.runtimer   ÚOSErrorr   r   r   r   r   r   Ú<module>   s   