a
    äze  ã                   @   st   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	 e	 
d¡ZG dd	„ d	ejƒZed
krpe ¡  dS )zCTest InteractiveConsole and InteractiveInterpreter from code moduleé    N)Údedent)Ú	ExitStack)Úmock)ÚsupportÚcodec                   @   sd   e 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S )ÚTestInteractiveConsolec                 C   s   t  ¡ | _|  ¡  d S )N)r   ZInteractiveConsoleÚconsoleÚmock_sys©Úself© r   ú+/usr/lib/python3.9/test/test_code_module.pyÚsetUp   s    
zTestInteractiveConsole.setUpc                 C   s”   t ƒ }|  |j¡ | tjddd¡| _| t d¡¡| _| t d¡¡| _tjdt	j
t	j
d}| |¡| _t
jt
ju r„| jj| j_| j`| j`dS )	z.Mock system environment for InteractiveConsolez
code.inputT)Zcreatezcode.sys.stdoutzcode.sys.stderrzcode.sys)ÚwrapsÚspecN)r   Z
addCleanupÚcloseÚenter_contextr   ZpatchÚinfuncÚstdoutÚstderrr   ÚsysÚsysmodÚ
excepthookÚ__excepthook__Úps1Úps2)r   ÚstackZprepatchr   r   r   r	      s    
ÿ
zTestInteractiveConsole.mock_sysc                 C   sL   t dƒ| j_| j ¡  |  | jjd¡ d| j_| j ¡  |  | jjd¡ d S )NÚFinishedz>>> z	custom1> )ÚEOFErrorr   Úside_effectr   ÚinteractÚassertEqualr   r   r
   r   r   r   Útest_ps1"   s    

zTestInteractiveConsole.test_ps1c                 C   sL   t dƒ| j_| j ¡  |  | jjd¡ d| j_| j ¡  |  | jjd¡ d S )Nr   z... z	custom2> )	r   r   r   r   r    r!   r   r   r   r
   r   r   r   Útest_ps2*   s    

zTestInteractiveConsole.test_ps2c                 C   sP   ddt dƒg| j_| j ¡  t| jjƒD ]}dd |d ¡v r( qLq(t	dƒ‚d S )Nz	'antioch'Ú r   Zantioché   zno console stdout)
r   r   r   r   r    Úlistr   Úmethod_callsÚjoinÚAssertionError©r   Úcallr   r   r   Útest_console_stderr2   s    
z*TestInteractiveConsole.test_console_stderrc                 C   sJ   dt dƒg| j_| j ¡  | jjD ]}dd |d ¡v r" qFq"tdƒ‚d S )NZ	undefinedr   Ú	NameErrorr$   r%   zNo syntax error from console)	r   r   r   r   r    r   r'   r(   r)   r*   r   r   r   Útest_syntax_error;   s    
z(TestInteractiveConsole.test_syntax_errorc                 C   s:   dt dƒg| j_t ¡ }|| j_| j ¡  |  	|j
¡ d S )Nzraise ValueError('')r   )r   r   r   r   ZMockr   r   r   r    Z
assertTrueZcalled)r   Úhookr   r   r   Útest_sysexcepthookD   s    ÿ
z)TestInteractiveConsole.test_sysexcepthookc                 C   sˆ   t dƒ| j_| jjdd |  t| jjƒd¡ | jjd }|  |ddi g¡ | j 	¡  t dƒ| j_| jjdd |  t| jjƒd	¡ d S )
Nr   ZFoo©Úbanneré   r   Úwrite)zFoo
r$   é   ©
r   r   r   r   r    r!   Úlenr   r'   Z
reset_mock)r   Zbanner_callr   r   r   Útest_bannerL   s    
z"TestInteractiveConsole.test_bannerc                 C   sö   t dƒ| j_| jjdd |  t| jjƒd¡ | jjd }d}|  |d|fi g¡ | j 	¡  t dƒ| j_| jjddd |  t| jjƒd¡ | j 	¡  d	}t dƒ| j_| jjd|d |  t| jjƒd¡ | jjd }|d
 }|  |d|fi g¡ d S )Nr   r$   r1   r5   r%   z"now exiting InteractiveConsole...
r4   )r2   Zexitmsgu	   bye! Î¶Ð¶Ú
r6   )r   Úerr_msgÚexpectedÚmessager   r   r   Útest_exit_msgZ   s&    

ÿz$TestInteractiveConsole.test_exit_msgc                 C   sJ   dt dƒg| j_| j ¡  d dd„ | jjD ƒ¡}tdƒ}|  	||¡ d S )Nz(raise ValueError('') from AttributeErrorr   r$   c                 s   s   | ]}d   |d ¡V  qdS ©r$   r%   N©r(   ©Ú.0r+   r   r   r   Ú	<genexpr>z   ó    z7TestInteractiveConsole.test_cause_tb.<locals>.<genexpr>zÝ
        AttributeError

        The above exception was the direct cause of the following exception:

        Traceback (most recent call last):
          File "<console>", line 1, in <module>
        ValueError
        ©
r   r   r   r   r    r(   r   r'   r   ZassertIn©r   Úoutputr;   r   r   r   Útest_cause_tbv   s    ÿ
	z$TestInteractiveConsole.test_cause_tbc                 C   sJ   dt dƒg| j_| j ¡  d dd„ | jjD ƒ¡}tdƒ}|  	||¡ d S )Nztry: ham
except: eggs
r   r$   c                 s   s   | ]}d   |d ¡V  qdS r>   r?   r@   r   r   r   rB   Š   rC   z9TestInteractiveConsole.test_context_tb.<locals>.<genexpr>ah  
        Traceback (most recent call last):
          File "<console>", line 1, in <module>
        NameError: name 'ham' is not defined

        During handling of the above exception, another exception occurred:

        Traceback (most recent call last):
          File "<console>", line 2, in <module>
        NameError: name 'eggs' is not defined
        rD   rE   r   r   r   Útest_context_tb†   s    ÿ
z&TestInteractiveConsole.test_context_tbN)Ú__name__Ú
__module__Ú__qualname__r   r	   r"   r#   r,   r.   r0   r8   r=   rG   rH   r   r   r   r   r      s   		r   Ú__main__)Ú__doc__r   ZunittestÚtextwrapr   Ú
contextlibr   r   Útestr   Úimport_moduler   ZTestCaser   rI   Úmainr   r   r   r   Ú<module>   s   
 