o
    GXîh#  ã                   @   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	 e	 
d¡ZG dd	„ d	ejƒZed
kr:e ¡  dS dS )zCTest InteractiveConsole and InteractiveInterpreter from code moduleé    N)Údedent)Ú	ExitStack)Úmock)Úimport_helperÚ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   ú?/opt/python-3.10.19/usr/lib/python3.10/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 rB| 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#   ó   

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+   r#   zTestInteractiveConsole.test_ps2c                 C   sN   ddt dƒg| j_| j ¡  t| jjƒD ]}dd |d ¡v r" d S qt	dƒ‚)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_stderr3   s   
ÿz*TestInteractiveConsole.test_console_stderrc                 C   sH   dt dƒg| j_| j ¡  | jjD ]}dd |d ¡v r d S qtdƒ‚)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_sysexcepthookE   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_bannerM   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%   r2   r6   r&   z"now exiting InteractiveConsole...
r5   )r3   Zexitmsgu	   bye! Î¶Ð¶Ú
r7   )r   Úerr_msgÚexpectedÚmessager   r   r   Útest_exit_msg[   s&   

ÿz$TestInteractiveConsole.test_exit_msgc                 C   óJ   dt dƒg| j_| j ¡  d dd„ | jjD ƒ¡}tdƒ}|  	||¡ d S )Nz(raise ValueError('') from AttributeErrorr   r%   c                 s   ó    | ]
}d   |d ¡V  qdS ©r%   r&   N©r)   ©Ú.0r,   r   r   r   Ú	<genexpr>{   ó   € 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_tbw   s   ÿ
	z$TestInteractiveConsole.test_cause_tbc                 C   r?   )Nztry: ham
except: eggs
r   r%   c                 s   r@   rA   rB   rC   r   r   r   rE   ‹   rF   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
        rG   rH   r   r   r   Útest_context_tb‡   s   ÿ
z&TestInteractiveConsole.test_context_tbN)Ú__name__Ú
__module__Ú__qualname__r   r	   r"   r$   r-   r/   r1   r9   r>   rJ   rK   r   r   r   r   r      s    		r   Ú__main__)Ú__doc__r   ZunittestÚtextwrapr   Ú
contextlibr   r   Ztest.supportr   Úimport_moduler   ZTestCaser   rL   Úmainr   r   r   r   Ú<module>   s    
 ÿ