a
    ze5                     @   s   d dl mZmZ d dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
mZmZmZmZ dddddd	d
ZG dd dZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZdS )    )TestCasemockN)captured_stderrcaptured_stdoutscript_helperfindfileunlinkzCfruits = [
    "Apple",
    "Orange",
    "Banana",

print(fruits)
zLif True:
    print("hello")
  print("world")
else:
    print("else called")
zIif True:
 	print("hello")
	print("world")
else:
    print("else called")
zNif True:
    print("hello")
    print("world")
else:
    print("else called")
zGdef my_func():
	  print("hello world")
	  if True:
		print("If called")zLdef my_func():
		print("Hello world")
		if True:
	        print("If called"))incomplete_expressionwrong_indentednannynag_errored
error_freetab_space_errored_1tab_space_errored_2c                   @   s&   e Zd Zd	ddZdd Zdd ZdS )
TemporaryPyFile Nc                 C   s   || _ || _d S N)source_codedir)selfr   	directory r   (/usr/lib/python3.9/test/test_tabnanny.py__init__A   s    zTemporaryPyFile.__init__c                 C   sN   t jd| jddd}|| j W d    n1 s60    Y  |j| _| jS )Nwz.pyF)moder   suffixdelete)tempfileNamedTemporaryFiler   writer   name	file_path)r   fr   r   r   	__enter__E   s    
*zTemporaryPyFile.__enter__c                 C   s   t | j d S r   )r   r!   )r   exc_type	exc_valueexc_tracebackr   r   r   __exit__M   s    zTemporaryPyFile.__exit__)r   N)__name__
__module____qualname__r   r#   r'   r   r   r   r   r   >   s   
r   c                   @   s   e Zd Zdd ZdS )TestFormatWitnessesc              	   C   sZ   g d}|D ]H\}}| j ||d" | t|| W d    q1 sJ0    Y  qd S )N))ZTestzat tab sizes T, e, s, t)r   zat tab size )tzat tab size t)z  t  zat tab sizes  ,  , t,  ,  )wordsexpected)subTestassertEqualtabnannyZformat_witnesses)r   testsr-   r.   r   r   r   test_format_witnessesT   s    z)TestFormatWitnesses.test_format_witnessesN)r(   r)   r*   r3   r   r   r   r   r+   Q   s   r+   c                   @   s   e Zd Zdd ZdS )TestErrPrintc              
   C   s   ddgdfdgdfg ddfg dfg}|D ]v\}}| j ||dP t }tj|  W d    n1 sh0    Y  | | | W d    q*1 s0    Y  q*d S )	Nfirstsecondzfirst second
zfirst
)         z1 2 3

)Z	argumentsr.   )r/   r   r1   Zerrprintr0   getvalue)r   r2   argsr.   stderrr   r   r   test_errprinte   s    

(zTestErrPrint.test_errprintN)r(   r)   r*   r>   r   r   r   r   r4   b   s   r4   c                   @   s   e Zd Zdd ZdS )TestNannyNagc              	   C   s&  t dddddddft dddddddfg}|D ]\}}| }| }| }| j||d d	  | |d | W d    n1 s0    Y  | j||d
 d  | |d
 | W d    n1 s0    Y  | j||d d  | |d | W d    q81 s0    Y  q8d S )Nr   ZfooZbar)linenomsgline   ZtestmsgZtestliner@   )line_numberr.   rA   )rA   r.   rB   )rB   r.   )r1   NannyNagZ
get_linenoZget_msgZget_liner/   r0   )r   r2   Znannyr.   rD   rA   rB   r   r   r   test_all_methodsv   s&    


..zTestNannyNag.test_all_methodsN)r(   r)   r*   rF   r   r   r   r   r?   u   s   r?   c                   @   sf   e Zd Zdd Zd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 )	TestCheckc                 C   s   |  ttdtj dt_d S )Nverboser   )Z
addCleanupsetattrr1   rH   )r   r   r   r   setUp   s    zTestCheck.setUpr   c              	   C   sz   t  @}t }t| W d    n1 s.0    Y  W d    n1 sL0    Y  | | | | | | d S r   )r   r   r1   checkr0   r;   )r   Zdir_or_fileouterrstdoutr=   r   r   r   verify_tabnanny_check   s    FzTestCheck.verify_tabnanny_checkc                 C   s:   t td }| | W d    n1 s,0    Y  d S Nr   r   SOURCE_CODESrO   r   r!   r   r   r   test_correct_file   s    zTestCheck.test_correct_filec                 C   s~  t  ^}|dg}ttd |d}ttd |d}|
}|}||fD ]}||d qNdt_t @}t }	t	| W d    n1 s0    Y  W d    n1 s0    Y  |
 }|D ]<}
| j|
d | |
| W d    q1 s0    Y  q| |	
 d W d    n1 s00    Y  W d    n1 sP0    Y  W d    n1 sp0    Y  d S )Nz: listing directory
r   r   z: Clean bill of health.
r7   )rB   r   )r   TemporaryDirectoryr   rR   appendr1   rH   r   r   rK   r;   r/   assertInr0   )r   tmp_dirlinesZfile1Zfile2Z
file1_pathZ
file2_pathr!   rN   r=   rB   r   r   r   test_correct_directory_verbose   s    F,z(TestCheck.test_correct_directory_verbosec              	   C   sf   t  J}ttd |d | | W d    n1 s:0    Y  W d    n1 sX0    Y  d S )Nr   rU   r   rV   r   rR   rO   )r   rY   r   r   r   test_correct_directory   s    
z TestCheck.test_correct_directoryc                 C   sP   t td 0}d}|d| }| j||d W d    n1 sB0    Y  d S )Nr
   Iunindent does not match any outer indentation level (<tokenize>, line 3)
: Indentation Error: rM   rQ   r   r!   rM   r   r   r   test_when_wrong_indented   s    z"TestCheck.test_when_wrong_indentedc                 C   sP   t td 0}d}|d| }| j||d W d    n1 sB0    Y  d S )Nr	   z(('EOF in multi-line statement', (7, 0))
z: Token Error: r`   rQ   ra   r   r   r   test_when_tokenize_tokenerror   s    z'TestCheck.test_when_tokenize_tokenerrorc                 C   s^   t td >}|d}|d7 }|d7 }dt_| j||d W d    n1 sP0    Y  d S )Nr   z': *** Line 3: trouble in tab city! ***
z%offending line: '\tprint("world")\n'
z$indent not equal e.g. at tab size 1
r7   rL   )r   rR   r1   rH   rO   r   r!   rL   r   r   r    test_when_nannynag_error_verbose   s    
z*TestCheck.test_when_nannynag_error_verbosec                 C   sH   t td (}| d}| j||d W d    n1 s:0    Y  d S )Nr   z 3 '\tprint("world")\n'
rd   rQ   re   r   r   r   test_when_nannynag_error   s    
z"TestCheck.test_when_nannynag_errorc                 C   s<   d}|dt j dtt j d|d}| j||d d S )Nz
no_file.pyz: I/O Error: [Errno z] z: r:   r`   )errnoENOENTosstrerrorrO   )r   pathrM   r   r   r   test_when_no_file   s    
zTestCheck.test_when_no_filec              
   C   s   t  }ttd |d}ttd |d}|T}|0}d}|d| }| j||d W d    n1 sj0    Y  W d    n1 s0    Y  W d    n1 s0    Y  d S )Nr
   rU   r   r^   r_   r`   r\   )r   rY   Z
error_fileZ	code_fileZe_fileZc_filerM   r   r   r   test_errored_directory   s    
z TestCheck.test_errored_directoryN)r   r   )r(   r)   r*   rJ   rO   rT   r[   r]   rb   rc   rf   rg   rm   rn   r   r   r   r   rG      s   
rG   c                   @   s&   e Zd Zeddd Zdd ZdS )TestProcessTokensztabnanny.NannyNagc              	   C   sv   t td V}t|"}tt|j W d    n1 s>0    Y  | |j	 W d    n1 sh0    Y  d S rP   )
r   rR   openr1   process_tokenstokenizegenerate_tokensreadlineZassertFalseZcalled)r   ZMockNannyNagr!   r"   r   r   r   test_with_correct_code  s    
0z(TestProcessTokens.test_with_correct_codec                 C   s   dD ]}| j |d tt| z}t|R}t|j}| tj	 t
| W d    n1 sf0    Y  W d    n1 s0    Y  W d    n1 s0    Y  W d    q1 s0    Y  qd S )N)r   r   )key)r/   r   rR   rp   rr   rs   rt   ZassertRaisesr1   rE   rq   )r   rv   r!   r"   tokensr   r   r   test_with_errored_codes_samples  s    	
z1TestProcessTokens.test_with_errored_codes_samplesN)r(   r)   r*   r   Zpatchru   rx   r   r   r   r   ro      s   
ro   c                   @   sN   e Zd ZddddddZdd Zdd	 Zd
d Zdd Zdd Zdd Z	dS )TestCommandLiner   F)rN   r=   partialc             
   G   s   t jddg|R  \}}}t|}t|}|r||f||ffD ]X\}}	|	 }
| D ]>}| j||
d | ||
 W d    qZ1 s0    Y  qZqBn(| | |  | | |  d S )Nz-mr1   )stdoutput)r   Zassert_python_okrj   fsdecode
splitlinesr/   rX   ZassertListEqual)r   rN   r=   rz   r<   _rL   rM   r{   r|   Z_outputZ_stdr   r   r   validate_cmd!  s    

0zTestCommandLine.validate_cmdc                 C   sP   t td 0}|d}|d7 }| j||d W d    n1 sB0    Y  d S )Nr
   r_   zHunindent does not match any outer indentation level (<tokenize>, line 3)r=   r   rR   r   )r   r!   r=   r   r   r   test_with_errored_file2  s    
z&TestCommandLine.test_with_errored_filec                 C   s:   t td }| | W d    n1 s,0    Y  d S rP   r   rS   r   r   r   test_with_error_free_file:  s    z)TestCommandLine.test_with_error_free_filec                 C   s$   t d}d| d}| j|d d S )Nztabnanny.pyzUsage: z [-v] file_or_directory ...r   )r   r   )r   rl   r=   r   r   r   test_command_usage?  s    z"TestCommandLine.test_command_usagec                 C   sJ   t td *}| d}| jd||d W d    n1 s<0    Y  d S )Nr   r:   z-q)rN   r   )r   r!   rN   r   r   r   test_quiet_flagE  s    
zTestCommandLine.test_quiet_flagc                 C   sP   t td 0}td }| jd||dd W d    n1 sB0    Y  d S )Nr   $offending line: '\tprint("world")\n'z-vTrN   rz   r   rR   textwrapdedentstripr   r   rl   rN   r   r   r   test_verbose_modeK  s
    z!TestCommandLine.test_verbose_modec                 C   sP   t td 0}td }| jd||dd W d    n1 sB0    Y  d S )Nr   r   z-vvTr   r   r   r   r   r   test_double_verbose_modeS  s
    z(TestCommandLine.test_double_verbose_modeN)
r(   r)   r*   r   r   r   r   r   r   r   r   r   r   r   ry     s   ry   )Zunittestr   r   rh   rj   r1   rr   r   r   Ztest.supportr   r   r   r   r   rR   r   r+   r4   r?   rG   ro   ry   r   r   r   r   <module>   s*   -p