a
    ze5                     @   s   d Z 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 )zfTesting `tabnanny` module.

Glossary:
    * errored    : Whitespace related problems present in file.
    )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Zd
ddZdd Zdd	 ZdS )TemporaryPyFilez+Create a temporary python source code file. 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____doc__r   r#   r'   r   r   r   r   r   >   s   
r   c                   @   s   e Zd ZdZdd ZdS )TestFormatWitnessesz&Testing `tabnanny.format_witnesses()`.c              	   C   sZ   g d}|D ]H\}}| j ||d" | t|| W d   q1 sJ0    Y  qdS )z;Asserting formatter result by giving various input samples.))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expectedN)subTestassertEqualtabnannyZformat_witnesses)r   testsr.   r/   r   r   r   test_format_witnessesT   s    z)TestFormatWitnesses.test_format_witnessesN)r(   r)   r*   r+   r4   r   r   r   r   r,   Q   s   r,   c                   @   s   e Zd ZdZdd ZdS )TestErrPrintzTesting `tabnanny.errprint()`.c              
   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 )
zBAsserting result of `tabnanny.errprint()` by giving sample inputs.firstsecondzfirst second
zfirst
)         z1 2 3

)Z	argumentsr/   N)r0   r   r2   Zerrprintr1   getvalue)r   r3   argsr/   stderrr   r   r   test_errprinte   s    

(zTestErrPrint.test_errprintN)r(   r)   r*   r+   r?   r   r   r   r   r5   b   s   r5   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 )z5Asserting behaviour of `tabnanny.NannyNag` exception.r   ZfooZbar)linenomsgline   ZtestmsgZtestlinerA   )line_numberr/   NrB   )rB   r/   rC   )rC   r/   )r2   NannyNagZ
get_linenoZget_msgZget_liner0   r1   )r   r3   Znannyr/   rE   rB   rC   r   r   r   test_all_methodsv   s&    


..zTestNannyNag.test_all_methodsN)r(   r)   r*   rG   r   r   r   r   r@   u   s   r@   c                   @   sj   e Zd 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 )	TestCheckzTesting tabnanny.check().c                 C   s   |  ttdtj dt_d S )Nverboser   )Z
addCleanupsetattrr2   rI   )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 )aR  Common verification for tabnanny.check().

        Use this method to assert expected values of `stdout` and `stderr` after
        running tabnanny.check() on given `dir` or `file` path. Because
        tabnanny.check() captures exceptions and writes to `stdout` and
        `stderr`, asserting standard outputs is the only way.
        N)r   r   r2   checkr1   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 )z-A python source code file without any errors.r   Nr   SOURCE_CODESrP   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 )	a  Directory containing few error free python source code files.

        Because order of files returned by `os.lsdir()` is not fixed, verify the
        existence of each output lines at `stdout` using `in` operator.
        `verbose` mode of `tabnanny.verbose` asserts `stdout`.
        z: listing directory
r   r   z: Clean bill of health.
r8   N)rC   r   )r   TemporaryDirectoryr   rR   appendr2   rI   r   r   rL   r<   r0   assertInr1   )r   tmp_dirlinesZfile1Zfile2Z
file1_pathZ
file2_pathr!   rO   r>   rC   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 )zADirectory which contains few error free python source code files.r   rU   Nr   rV   r   rR   rP   )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 )zBA python source code file eligible for raising `IndentationError`.r
   Iunindent does not match any outer indentation level (<tokenize>, line 3)
: Indentation Error: rN   NrQ   r   r!   rN   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 )zEA python source code file eligible for raising 'tokenize.TokenError'.r	   z(('EOF in multi-line statement', (7, 0))
z: Token Error: r`   NrQ   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 )zA python source code file eligible for raising `tabnanny.NannyNag`.

        Tests will assert `stdout` after activating `tabnanny.verbose` mode.
        r   z': *** Line 3: trouble in tab city! ***
z%offending line: '\tprint("world")\n'
z$indent not equal e.g. at tab size 1
r8   rM   N)r   rR   r2   rI   rP   r   r!   rM   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 )zCA python source code file eligible for raising `tabnanny.NannyNag`.r   z 3 '\tprint("world")\n'
rd   NrQ   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 )z6A python file which does not exist actually in system.z
no_file.pyz: I/O Error: [Errno z] z: r;   r`   N)errnoENOENTosstrerrorrP   )r   pathrN   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 )z?Directory containing wrongly indented python source code files.r
   rU   r   r^   r_   r`   Nr\   )r   rY   Z
error_fileZ	code_fileZe_fileZc_filerN   r   r   r   test_errored_directory   s    
z TestCheck.test_errored_directoryN)r   r   )r(   r)   r*   r+   rK   rP   rT   r[   r]   rb   rc   rf   rg   rm   rn   r   r   r   r   rH      s   
rH   c                   @   s*   e Zd ZdZeddd Zdd ZdS )TestProcessTokensz$Testing `tabnanny.process_tokens()`.z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 )z=A python source code without any whitespace related problems.r   N)
r   rR   openr2   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 )z>A python source code with whitespace related sampled problems.)r   r   )keyN)r0   r   rR   rp   rr   rs   rt   ZassertRaisesr2   rF   rq   )r   rv   r!   r"   tokensr   r   r   test_with_errored_codes_samples  s    	
z1TestProcessTokens.test_with_errored_codes_samplesN)r(   r)   r*   r+   r   Zpatchru   rx   r   r   r   r   ro      s   
ro   c                   @   sR   e Zd 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 )TestCommandLinez+Tests command line interface of `tabnanny`.r   F)rO   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 )zBCommon function to assert the behaviour of command line interface.z-mr2   )stdoutputN)r   Zassert_python_okrj   fsdecode
splitlinesr0   rX   ZassertListEqual)r   rO   r>   rz   r=   _rM   rN   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 )z8Should displays error when errored python file is given.r
   r_   zHunindent does not match any outer indentation level (<tokenize>, line 3)r>   N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 )zAShould not display anything if python file is correctly indented.r   Nr   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 )z%Should display usage on no arguments.ztabnanny.pyzUsage: z [-v] file_or_directory ...r   N)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 )z*Should display less when quite mode is on.r   r;   z-q)rO   Nr   )r   r!   rO   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 )z<Should display more error information if verbose mode is on.r   $offending line: '\tprint("world")\n'z-vTrO   rz   Nr   rR   textwrapdedentstripr   r   rl   rO   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 )zBShould display detailed error information if double verbose is on.r   r   z-vvTr   Nr   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   r   ry     s   ry   )r+   Zunittestr   r   rh   rj   r2   rr   r   r   Ztest.supportr   r   r   r   r   rR   r   r,   r5   r@   rH   ro   ry   r   r   r   r   <module>   s,   -p