B
    u9a  ã               @   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.7/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k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   sR   ddt dƒg| j_| j ¡  x0t| jjƒD ]}dd |d ¡kr*P q*W 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   sL   dt dƒg| j_| j ¡  x,| jjD ]}dd |d ¡kr$P q$W 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   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&   Ú
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#   )r/   r2   r$   z"now exiting InteractiveConsole...
r1   )r/   Zexitmsgu	   bye! Î¶Ð¶Ú
)
r   r   r   r   r   r    r3   r   r&   r4   )r
   Z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   s    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   ÚassertIn)r
   Úoutputr7   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$   N)r'   )r:   r)   r   r   r   r;   Š   s    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
        )
r   r   r   r   r   r'   r   r&   r   r<   )r
   r=   r7   r   r   r   Útest_context_tb†   s    

z&TestInteractiveConsole.test_context_tbN)Ú__name__Ú
__module__Ú__qualname__r   r	   r!   r"   r*   r,   r.   r5   r9   r>   r?   r   r   r   r   r      s   		r   Ú__main__)Ú__doc__r   ZunittestÚtextwrapr   Ú
contextlibr   r   Ztestr   Úimport_moduler   ZTestCaser   r@   Úmainr   r   r   r   Ú<module>   s   
 