o
    HXh5                     @   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 ddl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   </opt/python-3.10.19/usr/lib/python3.10/test/test_tabnanny.py__init__B   s   
zTemporaryPyFile.__init__c                 C   sN   t jd| jddd}|| j W d    n1 sw   Y  |j| _| jS )Nwz.pyF)moder   suffixdelete)tempfileNamedTemporaryFiler   writer   name	file_path)r   fr   r   r   	__enter__F   s   
zTemporaryPyFile.__enter__c                 C   s   t | j d S r   )r   r!   )r   exc_type	exc_valueexc_tracebackr   r   r   __exit__N   s   zTemporaryPyFile.__exit__)r   N)__name__
__module____qualname____doc__r   r#   r'   r   r   r   r   r   ?   s
    
r   c                   @      e Zd ZdZdd ZdS )TestFormatWitnessesz&Testing `tabnanny.format_witnesses()`.c              	   C   sZ   g d}|D ]$\}}| j ||d | t|| W d   n1 s%w   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/   r0   r   r   r   test_format_witnessesU   s   z)TestFormatWitnesses.test_format_witnessesN)r(   r)   r*   r+   r5   r   r   r   r   r-   R       r-   c                   @   r,   )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 ];\}}| j ||d( t }tj|  W d	   n1 s4w   Y  | | | W d	   n1 sKw   Y  qd	S )
zBAsserting result of `tabnanny.errprint()` by giving sample inputs.firstsecondzfirst second
zfirst
)         z1 2 3

)Z	argumentsr0   N)r1   r   r3   Zerrprintr2   getvalue)r   r4   argsr0   stderrr   r   r   test_errprintf   s   

zTestErrPrint.test_errprintN)r(   r)   r*   r+   rA   r   r   r   r   r7   c   r6   r7   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 ]s\}}| }| }| }| j||d d	 | |d | W d
   n1 sHw   Y  | j||d d | |d | W d
   n1 siw   Y  | j||d d | |d | W d
   n1 sw   Y  qd
S )z5Asserting behaviour of `tabnanny.NannyNag` exception.r   ZfooZbar)linenomsgline   ZtestmsgZtestlinerC   )line_numberr0   NrD   )rD   r0   rE   )rE   r0   )r3   NannyNagZ
get_linenoZget_msgZget_liner1   r2   )r   r4   Znannyr0   rG   rD   rE   r   r   r   test_all_methodsw   s0   


zTestNannyNag.test_all_methodsN)r(   r)   r*   rI   r   r   r   r   rB   v   s    rB   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setattrr3   rK   )r   r   r   r   setUp   s   
zTestCheck.setUpr   c              	   C   sz   t   }t }t| W d   n1 sw   Y  W d   n1 s&w   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   r3   checkr2   r>   )r   Zdir_or_fileouterrstdoutr@   r   r   r   verify_tabnanny_check   s    zTestCheck.verify_tabnanny_checkc                 C   <   t td }| | W d   dS 1 sw   Y  dS )z-A python source code file without any errors.r   Nr   SOURCE_CODESrR   r   r!   r   r   r   test_correct_file      "zTestCheck.test_correct_filec                 C   s  t  }|dg}ttd |d}ttd |d}|}|r}||fD ]
}||d q%dt_t  }t }	t	| W d   n1 sJw   Y  W d   n1 sYw   Y  |
 }|D ]}
| j|
d | |
| W d   n1 s}w   Y  qd| |	
 d W d   n1 sw   Y  W d   n1 sw   Y  W d   dS W d   dS 1 sw   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.
r:   N)rE   r   )r   TemporaryDirectoryr   rU   appendr3   rK   r   r   rN   r>   r1   assertInr2   )r   tmp_dirlinesZfile1Zfile2Z
file1_pathZ
file2_pathr!   rQ   r@   rE   r   r   r   test_correct_directory_verbose   s4   
  "z(TestCheck.test_correct_directory_verbosec              	   C   sx   t  .}ttd |d | | W d   n1 sw   Y  W d   dS W d   dS 1 s5w   Y  dS )zADirectory which contains few error free python source code files.r   rY   Nr   rZ   r   rU   rR   )r   r]   r   r   r   test_correct_directory   s   
"z TestCheck.test_correct_directoryc                 C   R   t td }d}|d| }| j||d W d   dS 1 s"w   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: rP   NrT   r   r!   rP   r   r   r   test_when_wrong_indented   s
   "z"TestCheck.test_when_wrong_indentedc                 C   rb   )zEA python source code file eligible for raising 'tokenize.TokenError'.r	   z(('EOF in multi-line statement', (7, 0))
z: Token Error: re   NrT   rf   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   dS 1 s)w   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
r:   rO   N)r   rU   r3   rK   rR   r   r!   rO   r   r   r    test_when_nannynag_error_verbose   s   
"z*TestCheck.test_when_nannynag_error_verbosec                 C   sJ   t td }| d}| j||d W d   dS 1 sw   Y  dS )zCA python source code file eligible for raising `tabnanny.NannyNag`.r   z 3 '\tprint("world")\n'
ri   NrT   rj   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=   re   N)errnoENOENTosstrerrorrR   )r   pathrP   r   r   r   test_when_no_file   s   
zTestCheck.test_when_no_filec              
   C   s   t  U}ttd |d}ttd |d}|*}|}d}|d| }| j||d W d   n1 s5w   Y  W d   n1 sDw   Y  W d   dS W d   dS 1 s\w   Y  dS )z?Directory containing wrongly indented python source code files.r
   rY   r   rc   rd   re   Nr`   )r   r]   Z
error_fileZ	code_fileZe_fileZc_filerP   r   r   r   test_errored_directory   s"   
 "z TestCheck.test_errored_directoryN)r   r   )r(   r)   r*   r+   rM   rR   rW   r_   ra   rg   rh   rk   rl   rr   rs   r   r   r   r   rJ      s    
rJ   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   sx   t td ,}t|}tt|j W d   n1 sw   Y  | |j	 W d   dS 1 s5w   Y  dS )z=A python source code without any whitespace related problems.r   N)
r   rU   openr3   process_tokenstokenizegenerate_tokensreadlineZassertFalseZcalled)r   ZMockNannyNagr!   r"   r   r   r   test_with_correct_code  s   
"z(TestProcessTokens.test_with_correct_codec                 C   s   dD ]c}| j |dS tt| =}t|)}t|j}| tj	 t
| W d   n1 s3w   Y  W d   n1 sBw   Y  W d   n1 sQw   Y  W d   n1 s`w   Y  qdS )z>A python source code with whitespace related sampled problems.)r   r   )keyN)r1   r   rU   ru   rw   rx   ry   ZassertRaisesr3   rH   rv   )r   r{   r!   r"   tokensr   r   r   test_with_errored_codes_samples  s    	
z1TestProcessTokens.test_with_errored_codes_samplesN)r(   r)   r*   r+   r   Zpatchrz   r}   r   r   r   r   rt     s
    
rt   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)rQ   r@   partialc             
   G   s   t jddg|R  \}}}t|}t|}|rP||f||ffD ],\}}	|	 }
| D ]}| j||
d | ||
 W d   n1 sGw   Y  q-q!dS | | |  | | |  dS )zBCommon function to assert the behaviour of command line interface.z-mr3   )stdoutputN)r   Zassert_python_okro   fsdecode
splitlinesr1   r\   ZassertListEqual)r   rQ   r@   r   r?   _rO   rP   r   r   Z_outputZ_stdr   r   r   validate_cmd"  s   

zTestCommandLine.validate_cmdc                 C   sR   t td }|d}|d7 }| j||d W d   dS 1 s"w   Y  dS )z8Should displays error when errored python file is given.r
   rd   zHunindent does not match any outer indentation level (<tokenize>, line 3)r@   Nr   rU   r   )r   r!   r@   r   r   r   test_with_errored_file3  s
   
"z&TestCommandLine.test_with_errored_filec                 C   rS   )zAShould not display anything if python file is correctly indented.r   Nr   rV   r   r   r   test_with_error_free_file;  rX   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   rq   r@   r   r   r   test_command_usage@  s   z"TestCommandLine.test_command_usagec                 C   sL   t td }| d}| jd||d W d   dS 1 sw   Y  dS )z*Should display less when quite mode is on.r   r=   z-q)rQ   Nr   )r   r!   rQ   r   r   r   test_quiet_flagF  s   
"zTestCommandLine.test_quiet_flagc                 C   R   t td }td }| jd||dd W d   dS 1 s"w   Y  dS )z<Should display more error information if verbose mode is on.r   $offending line: '\tprint("world")\n'z-vTrQ   r   Nr   rU   textwrapdedentstripr   r   rq   rQ   r   r   r   test_verbose_modeL     "z!TestCommandLine.test_verbose_modec                 C   r   )zBShould display detailed error information if double verbose is on.r   r   z-vvTr   Nr   r   r   r   r   test_double_verbose_modeT  r   z(TestCommandLine.test_double_verbose_modeN)r(   r)   r*   r+   r   r   r   r   r   r   r   r   r   r   r   r~     s    r~   )r+   Zunittestr   r   rm   ro   r3   rw   r   r   Ztest.supportr   r   r   r   Ztest.support.os_helperr   rU   r   r-   r7   rB   rJ   rt   r~   r   r   r   r   <module>   s0    -p