
    daf7                        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      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zy)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                   $    e Zd ZdZddZd Zd Zy)TemporaryPyFilez+Create a temporary python source code file.Nc                      || _         || _        y N)source_codedir)selfr   	directorys      -/root/Python-3.12.4/Lib/test/test_tabnanny.py__init__zTemporaryPyFile.__init__B   s    &    c                     t        j                  d| j                  dd      5 }|j                  | j                         d d d        j
                  | _        | j                  S # 1 sw Y   &xY w)Nwz.pyF)moder   suffixdelete)tempfileNamedTemporaryFiler   writer   name	file_path)r   fs     r   	__enter__zTemporaryPyFile.__enter__F   s^    (($((5
 	&GGD$$%	& ~~	& 	&s   A%%A.c                 .    t        | j                         y r   )r	   r$   )r   exc_type	exc_valueexc_tracebacks       r   __exit__zTemporaryPyFile.__exit__N   s    t~~r   ) N)__name__
__module____qualname____doc__r   r&   r+    r   r   r   r   ?   s    5r   r   c                       e Zd ZdZd Zy)TestFormatWitnessesz&Testing `tabnanny.format_witnesses()`.c                     g d}|D ]F  \  }}| j                  ||      5  | j                  t        j                  |      |       ddd       H y# 1 sw Y   SxY w)z;Asserting formatter result by giving various input samples.))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tabnannyformat_witnesses)r   testsr7   r8   s       r   test_format_witnessesz)TestFormatWitnesses.test_format_witnessesU   sj    
  % 	MOE8EH= M  !:!:5!A8LM M	MM Ms   &AA	N)r-   r.   r/   r0   r>   r1   r   r   r3   r3   R   s    0Mr   r3   c                       e Zd ZdZd Zy)TestErrPrintzTesting `tabnanny.errprint()`.c           	         ddgdfdgdfg ddfg dfg}|D ]  \  }}| j                  ||      5  | j                  t              5  t               5 }t	        j
                  |  d	d	d	       | j                  j                         |       d	d	d	       d	d	d	        y	# 1 sw Y   <xY w# 1 sw Y    xY w# 1 sw Y   xY w)
zBAsserting result of `tabnanny.errprint()` by giving sample inputs.firstsecondzfirst second
zfirst
)         z1 2 3

)	argumentsr8   N)r9   assertRaises
SystemExitr   r;   errprintr:   getvalue)r   r=   argsr8   stderrs        r   test_errprintzTestErrPrint.test_errprintf   s     x "23Y	"	"J	
 $ 	CND(x@ C&&z2 C(* 1f ))401$$V__%6BCC C	C1 1C CC Cs;   B6B*B	#(B*B6B'#B**B3/B66B?	N)r-   r.   r/   r0   rO   r1   r   r   r@   r@   c   s    (Cr   r@   c                       e Zd Zd Zy)TestNannyNagc                 l   t        j                  ddd      ddddft        j                  ddd      ddddfg}|D ]  \  }}|j                         }|j                         }|j	                         }| j                  ||d   	      5  | j                  |d   |       d
d
d
       | j                  ||d         5  | j                  |d   |       d
d
d
       | j                  ||d         5  | j                  |d   |       d
d
d
        y
# 1 sw Y   txY w# 1 sw Y   LxY w# 1 sw Y   xY w)z5Asserting behaviour of `tabnanny.NannyNag` exception.r   foobar)linenomsgline   testmsgtestlinerU   )line_numberr8   NrV   )rV   r8   rW   )rW   r8   )r;   NannyNag
get_linenoget_msgget_liner9   r:   )r   r=   nannyr8   r[   rV   rW   s          r   test_all_methodszTestNannyNag.test_all_methodsx   sW    !!!UE2UE:
 !!!Y
;Y
C	
  % 	9OE8**,K--/C>>#D'(82D   B   (!3[AB #@ 7  %#674(62BC 9  &!1489 9	9B B7 79 9s$   	D=D1D*D	D'	*D3	N)r-   r.   r/   ra   r1   r   r   rQ   rQ   w   s    9r   rQ   c                   T    e Zd ZdZd ZddZd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zy)	TestCheckzTesting tabnanny.check().c                 n    | j                  t        t        dt        j                         dt        _        y )Nverboser   )
addCleanupsetattrr;   re   )r   s    r   setUpzTestCheck.setUp   s"    9h6F6FGr   c                 *   t               5 }t               5 }t        j                  |       ddd       ddd       | j	                  j                         |       | j	                  j                         |       y# 1 sw Y   RxY w# 1 sw Y   VxY w)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   r;   checkr:   rL   )r   dir_or_fileouterrstdoutrN   s         r   verify_tabnanny_checkzTestCheck.verify_tabnanny_check   sw      	(&/*; 	(vNN;'	( 	(*C0*C0	( 	( 	( 	(s!   B	A=B	=B	B		Bc                 t    t        t        d         5 }| j                  |       ddd       y# 1 sw Y   yxY w)z-A python source code file without any errors.r   Nr   SOURCE_CODESro   r   r$   s     r   test_correct_filezTestCheck.test_correct_file   s4    \,78 	2I&&y1	2 	2 	2   .7c           
         t        j                         5 }|dg}t        t        d   |      }t        t        d   |      }|5 }|5 }||fD ]  }|j	                  |d        dt
        _        t               5 }t               5 }	t        j                  |       ddd       ddd       j                         }|D ]/  }
| j                  |
      5  | j                  |
|       ddd       1 | j                  	j                         d       ddd       ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   >xY w# 1 sw Y   BxY w# 1 sw Y   yxY w)	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.
rD   N)rW   r,   )r    TemporaryDirectoryr   rr   appendr;   re   r   r   rj   rL   r9   assertInr:   )r   tmp_dirlinesfile1file2
file1_path
file2_pathr$   rn   rN   rW   s              r   test_correct_directory_verbosez(TestCheck.test_correct_directory_verbose   st    ((* 	8g{"78:E#L$>'RE#L$>'RE 8*e 8z",j!9 LILLI=0I!JKL $% $& ,&/2C ,vNN7+, ,*! 4D40 4dF34 44   !2B78 8		8 	8, , , ,4 48 8 8 8		8 	8s   1E&E	3E<D6	D*D6	%/EE'*EEE&*D3/D6	6D?;EEEEEE#	E&&E/c                     t        j                         5 }t        t        d   |      5  | j	                  |       ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)zADirectory which contains few error free python source code files.r   rw   N)r    rx   r   rr   ro   )r   r{   s     r   test_correct_directoryz TestCheck.test_correct_directory   s\    ((* 	4g l!;wO 4**734	4 	44 4	4 	4s!   AAAA	AA"c                     t        t        d         5 }d}|d| }| j                  t              5  | j	                  ||       ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)zBA python source code file eligible for raising `IndentationError`.r   Iunindent does not match any outer indentation level (<tokenize>, line 3)
: Indentation Error: rm   Nr   rr   rI   rJ   ro   r   r$   rm   s      r   test_when_wrong_indentedz"TestCheck.test_when_wrong_indented   sz    \*:;< 	?	*CM!6se<C"":. ?**9#*>?		? 	?? ?		? 	?"   A#AA#A 	A##A,c                     t        t        d         5 }d}|d| }| j                  t              5  | j	                  ||       ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)zEA python source code file eligible for raising 'tokenize.TokenError'.r
   z(('EOF in multi-line statement', (7, 0))
: Token Error: r   Nr   r   s      r   test_when_tokenize_tokenerrorz'TestCheck.test_when_tokenize_tokenerror   sw    \*ABC 	?y=CM6C"":. ?**9#*>?	? 	?? ?	? 	?r   c                     t        t        d         5 }|d}|dz  }|dz  }dt        _        | j	                  ||       ddd       y# 1 sw Y   yxY w)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")'
z3inconsistent use of tabs and spaces in indentation
rD   rl   N)r   rr   r;   re   ro   r   r$   rl   s      r    test_when_nannynag_error_verbosez*TestCheck.test_when_nannynag_error_verbose   sg    
 \*<=> 	;)M!IJC<<CIIC H&&yc&:	; 	; 	;s   .A

Ac                     t        t        d         5 }| d}| j                  ||       ddd       y# 1 sw Y   yxY w)zCA python source code file eligible for raising `tabnanny.NannyNag`.r   z 3 '\tprint("world")'
r   Nrq   r   s      r   test_when_nannynag_errorz"TestCheck.test_when_nannynag_error   sG    \*<=> 	;)K9:C&&yc&:	; 	; 	;s   5>c                     d}|dt         j                   dt        j                  t         j                         d|d}| j	                  t
              5  | j                  ||       ddd       y# 1 sw Y   yxY w)z6A python file which does not exist actually in system.z
no_file.pyz: I/O Error: [Errno z] z: rG   r   N)errnoENOENTosstrerrorrI   rJ   ro   )r   pathrm   s      r   test_when_no_filezTestCheck.test_when_no_file   sq    -ell^2U\\*+2dXR9z* 	6&&t&5	6 	6 	6s   A22A;c           	         t        j                         5 }t        t        d   |      }t        t        d   |      }|5 }|5 }d}|d| }| j	                  t
              5  | j                  ||       ddd       ddd       ddd       ddd       y# 1 sw Y   "xY w# 1 sw Y   &xY w# 1 sw Y   *xY w# 1 sw Y   yxY w)z?Directory containing wrongly indented python source code files.r   rw   r   r   r   r   N)r    rx   r   rr   rI   rJ   ro   )r   r{   
error_file	code_filee_filec_filerm   s          r   test_errored_directoryz TestCheck.test_errored_directory   s    ((* 	Ag(-.'J (\*gI  Avy AF6
"7u=&&z2 A..wC.@A	A A	A 	AA A	A A A A	A 	AsS   +B; B/B#"B	6B#>B/B;B B##B,(B//B8	4B;;CN)r,   r,   )r-   r.   r/   r0   rh   ro   rt   r   r   r   r   r   r   r   r   r1   r   r   rc   rc      s=    #12
804??;;6Ar   rc   c                   F    e Zd ZdZ ej
                  d      d        Zd Zy)TestProcessTokensz$Testing `tabnanny.process_tokens()`.ztabnanny.NannyNagc                 ,   t        t        d         5 }t        |      5 }t        j                  t        j                  |j                               ddd       | j                  |j                         ddd       y# 1 sw Y   -xY w# 1 sw Y   yxY w)z=A python source code without any whitespace related problems.r   N)
r   rr   openr;   process_tokenstokenizegenerate_tokensreadlineassertFalsecalled)r   MockNannyNagr$   r%   s       r   test_with_correct_codez(TestProcessTokens.test_with_correct_code	  s     \,78 	2Ii NA''(@(@(LMN\001	2 	2N N	2 	2s"   B
3A>#B
>B	B

Bc           
         dD ]  }| j                  |      5  t        t        |         5 }t        |      5 }t	        j
                  |j                        }| j                  t        j                        5  t        j                  |       ddd       ddd       ddd       ddd        y# 1 sw Y   $xY w# 1 sw Y   (xY w# 1 sw Y   ,xY w# 1 sw Y   xY w)z>A python source code with whitespace related sampled problems.)r   r   )keyN)r9   r   rr   r   r   r   r   rI   r;   r\   r   )r   r   r$   r%   tokenss        r   test_with_errored_codes_samplesz1TestProcessTokens.test_with_errored_codes_samples  s     B 	<C#& <$\#%67 <9i <A!)!9!9!**!E!..x/@/@A <$33F;<<<< <	<
< << << << <sQ   CC?B;	6B/B;	CC/B84B;	;C CCCC	N)r-   r.   r/   r0   r   patchr   r   r1   r   r   r   r     s(    .TZZ#$2 %2<r   r   c                   F    e Zd ZdZddddddZd Zd Zd Zd	 Zd
 Z	d Z
y)TestCommandLinez+Tests command line interface of `tabnanny`.r,   F)rn   rN   partialexpect_failurec                f   |rt        j                  ddg| \  }}}nt        j                  ddg| \  }}}t        j                  |      }t        j                  |      }|rd||f||ffD ]X  \  }	}
|
j                         }|	j                         D ]0  }| j                  ||      5  | j                  ||       ddd       2 Z y| j                  |j                         |j                                | j                  |j                         |j                                y# 1 sw Y   xY w)zBCommon function to assert the behaviour of command line interface.z-mr;   )stdoutputN)	r   assert_python_failureassert_python_okr   fsdecode
splitlinesr9   rz   assertListEqual)r   rn   rN   r   r   rM   _rl   rm   r   r   _output_stds                r   validate_cmdzTestCommandLine.validate_cmd'  s   '==dJVQUVKAsC'88zQDQKAsC kk#kk#!'}= 5V ++-NN, 5D$w? 5dG45 555   !163D3D3FG  !163D3D3FG	5 5s   *D''D0c                     t        t        d         5 }|d}|dz  }| j                  ||d       ddd       y# 1 sw Y   yxY w)z8Should displays error when errored python file is given.r   r   zFunindent does not match any outer indentation level (<string>, line 3)TrN   r   Nr   rr   r   )r   r$   rN   s      r   test_with_errored_filez&TestCommandLine.test_with_errored_file;  s[    \*:;< 	M	"_5F - .FitL		M 	M 	Ms	   ;Ac                 t    t        t        d         5 }| j                  |       ddd       y# 1 sw Y   yxY w)zAShould not display anything if python file is correctly indented.r   Nr   rs   s     r   test_with_error_free_filez)TestCommandLine.test_with_error_free_fileC  s4    \,78 	)Ii(	) 	) 	)ru   c                 L    t        d      }d| d}| j                  |d       y)z%Should display usage on no arguments.ztabnanny.pyzUsage: z [-v] file_or_directory ...Tr   N)r   r   )r   r   rN   s      r   test_command_usagez"TestCommandLine.test_command_usageH  s.    &4& ;<=r   c                     t        t        d         5 }| d}| j                  d||       ddd       y# 1 sw Y   yxY w)z*Should display less when quite mode is on.r   rG   z-q)rn   Nr   )r   r$   rn   s      r   test_quiet_flagzTestCommandLine.test_quiet_flagN  sH    \*<=> 	>)!{"%FdIf=	> 	> 	>s   6?c                     t        t        d         5 }t        j                  d      j	                         }| j                  d||d       ddd       y# 1 sw Y   yxY w)z<Should display more error information if verbose mode is on.r   "offending line: '\tprint("world")'z-vTrn   r   Nr   rr   textwrapdedentstripr   r   r   rn   s      r   test_verbose_modez!TestCommandLine.test_verbose_modeT  s_    \*<=> 	G$__7eg  dDF		G 	G 	G   9AAc                     t        t        d         5 }t        j                  d      j	                         }| j                  d||d       ddd       y# 1 sw Y   yxY w)zBShould display detailed error information if double verbose is on.r   r   z-vvTr   Nr   r   s      r   test_double_verbose_modez(TestCommandLine.test_double_verbose_mode\  s_    \*<=> 	H$__7eg  eT&$G		H 	H 	Hr   N)r-   r.   r/   r0   r   r   r   r   r   r   r   r1   r   r   r   r   $  s6    5)+BV[ H(M)
>>GHr   r   )r0   unittestr   r   r   r   r;   r   r    r   test.supportr   r   r   r   test.support.os_helperr	   rr   r   r3   r@   rQ   rc   r   r   r1   r   r   <module>r      s   
 $  	    $ $ )
		%	%	%	!	'K*Z &M( M"C8 C(98 96qA qAh< <<>Hh >Hr   