
    <e5                     B   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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                   &    e Zd ZdZddZd Zd ZdS )TemporaryPyFilez+Create a temporary python source code file. Nc                 "    || _         || _        d S N)source_codedir)selfr   	directorys      )/usr/lib/python3.11/test/test_tabnanny.py__init__zTemporaryPyFile.__init__B   s    &    c                     t          j        d| j        dd          5 }|                    | j                   d d d            n# 1 swxY w Y   |j        | _        | j        S )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	A	c                 .    t          | j                   d S r   )r	   r%   )r   exc_type	exc_valueexc_tracebacks       r   __exit__zTemporaryPyFile.__exit__N   s    t~r   )r   N)__name__
__module____qualname____doc__r   r'   r,    r   r   r   r   ?   sL        55         r   r   c                       e Zd ZdZd ZdS )TestFormatWitnessesz&Testing `tabnanny.format_witnesses()`.c                     g d}|D ]\\  }}|                      ||          5  |                     t          j        |          |           ddd           n# 1 swxY w Y   ]dS )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   s    
 
 
  % 	M 	MOE8EH== M M  !:5!A!A8LLLM M M M M M M M M M M M M M M	M 	Ms   )AA	A	N)r-   r.   r/   r0   r>   r1   r   r   r3   r3   R   s.        00M M M M Mr   r3   c                       e Zd ZdZd ZdS )TestErrPrintzTesting `tabnanny.errprint()`.c                 R   ddgdfdgdfg ddfg dfg}|D ]\  }}|                      ||          5  t                      5 }t          j        |  d	d	d	           n# 1 swxY w Y   |                     |                                |           d	d	d	           n# 1 swxY w Y   d	S )
zBAsserting result of `tabnanny.errprint()` by giving sample inputs.firstsecondzfirst second
zfirst
)         z1 2 3

)	argumentsr8   N)r9   r   r;   errprintr:   getvalue)r   r=   argsr8   stderrs        r   test_errprintzTestErrPrint.test_errprintf   sM    x "23Y	"YY	"J	
 $ 	? 	?ND(x@@ ? ?$&& -&%t,,- - - - - - - - - - - - - - -  !2!2X>>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ?	? 	?s5   BABA  B#A $+BB	"B	N)r-   r.   r/   r0   rM   r1   r   r   r@   r@   c   s)        ((? ? ? ? ?r   r@   c                       e Zd Zd ZdS )TestNannyNagc                    t          j        ddd          ddddft          j        ddd          ddddfg}|D ]5\  }}|                                }|                                }|                                }|                     ||d         	          5  |                     |d         |           d
d
d
           n# 1 swxY w Y   |                     ||d                   5  |                     |d         |           d
d
d
           n# 1 swxY w Y   |                     ||d                   5  |                     |d         |           d
d
d
           n# 1 swxY w Y   7d
S )z5Asserting behaviour of `tabnanny.NannyNag` exception.r   foobar)linenomsgline   testmsgtestlinerS   )line_numberr8   NrT   )rT   r8   rU   )rU   r8   )r;   NannyNag
get_linenoget_msgget_liner9   r:   )r   r=   nannyr8   rY   rT   rU   s          r   test_all_methodszTestNannyNag.test_all_methodsw   s^    !!UE22UE::
 !!Y
;;Y
CC	
  % 	9 	9OE8**,,K--//C>>##D'(82D    B B   (!3[AAAB B B B B B B B B B B B B B B #@@ 7 7  %#6667 7 7 7 7 7 7 7 7 7 7 7 7 7 74(62BCC 9 9  &!148889 9 9 9 9 9 9 9 9 9 9 9 9 9 9	9 	9s6   CC	
C	+DD	D	<E%%E)	,E)	N)r-   r.   r/   r_   r1   r   r   rO   rO   v   s#        9 9 9 9 9r   rO   c                   V    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dS )	TestCheckzTesting tabnanny.check().c                 v    |                      t          t          dt          j                   dt          _        d S )Nverboser   )
addCleanupsetattrr;   rc   )r   s    r   setUpzTestCheck.setUp   s+    9h6FGGGr   r   c                 f   t                      5 }t                      5 }t          j        |           ddd           n# 1 swxY w Y   ddd           n# 1 swxY 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   r;   checkr:   rJ   )r   dir_or_fileouterrstdoutrL   s         r   verify_tabnanny_checkzTestCheck.verify_tabnanny_check   s     	(&/*;*; 	(vN;'''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(**C000**C00000s1   A?AA	AA	AAAc                     t          t          d                   5 }|                     |           ddd           dS # 1 swxY w Y   dS )z-A python source code file without any errors.r   Nr   SOURCE_CODESrm   r   r%   s     r   test_correct_filezTestCheck.test_correct_file   s    \,788 	2I&&y111	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2   >AAc           
      b   t          j                    5 }|dg}t          t          d         |          }t          t          d         |          }|5 }|5 }||fD ]}|                    |d           dt
          _        t                      5 }t                      5 }	t          j	        |           ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   |
                                }|D ]F}
|                     |
          5  |                     |
|           ddd           n# 1 swxY w Y   G|                     |	
                                d           ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w 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.
rD   N)rU   r   )r!   TemporaryDirectoryr   rp   appendr;   rc   r   r   rh   rJ   r9   assertInr:   )r   tmp_dirlinesfile1file2
file1_path
file2_pathr%   rl   rL   rU   s              r   test_correct_directory_verbosez(TestCheck.test_correct_directory_verbose   s%    (** 	8g888:E#L$>'RRRE#L$>'RRRE 8*e 8z",j!9 L LILLI!J!J!JKKKK#$ $&& ,&/2C2C ,vN7+++, , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,**! 4 4D400 4 4dF3334 4 4 4 4 4 4 4 4 4 4 4 4 4 4  !2!2B7778 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8		8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8s   AF$F:E5C	!C6C	CC		C
C	E5CE5 C!1E5D5)E55D99E5<D9=,E5)F5E99F<E9=F F$F	F$F	F$$F(+F(c                     t          j                    5 }t          t          d         |          5  |                     |           ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS )zADirectory which contains few error free python source code files.r   ru   Nr!   rv   r   rp   rm   )r   ry   s     r   test_correct_directoryz TestCheck.test_correct_directory   s    (** 	4g l!;wOOO 4 4**73334 4 4 4 4 4 4 4 4 4 4 4 4 4 4	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4s4   A+AA+A	A+A	A++A/2A/c                     t          t          d                   5 }d}|d| }|                     ||           ddd           dS # 1 swxY 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: rk   Nro   r   r%   rk   s      r   test_when_wrong_indentedz"TestCheck.test_when_wrong_indented   s    \*:;<< 	;	*C<<s<<C&&yc&:::		; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;   !A		AAc                     t          t          d                   5 }d}|d| }|                     ||           ddd           dS # 1 swxY w 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   Nro   r   s      r   test_when_tokenize_tokenerrorz'TestCheck.test_when_tokenize_tokenerror   s    \*ABCC 	;y=C6666C&&yc&:::	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;r   c                     t          t          d                   5 }|d}|dz  }|dz  }dt          _        |                     ||           ddd           dS # 1 swxY 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
rD   rj   N)r   rp   r;   rc   rm   r   r%   rj   s      r    test_when_nannynag_error_verbosez*TestCheck.test_when_nannynag_error_verbose   s    
 \*<=>> 	;)JJJC??C::C H&&yc&:::	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;s   3AA"Ac                     t          t          d                   5 }| d}|                     ||           ddd           dS # 1 swxY w Y   dS )zCA python source code file eligible for raising `tabnanny.NannyNag`.r   z 3 '\tprint("world")\n'
r   Nro   r   s      r   test_when_nannynag_errorz"TestCheck.test_when_nannynag_error   s    \*<=>> 	;)===C&&yc&:::	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;s   AA	A	c                     d}|dt           j         dt          j        t           j                   d|d}|                     ||           dS )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strerrorrm   )r   pathrk   s      r   test_when_no_filezTestCheck.test_when_no_file   sn     9 9el 9 9U\**9 9.29 9 9""4S"11111r   c                 v   t          j                    5 }t          t          d         |          }t          t          d         |          }|5 }|5 }d}|d| }|                     ||           ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS )z?Directory containing wrongly indented python source code files.r   ru   r   r   r   r   Nr   )r   ry   
error_file	code_filee_filec_filerk   s          r   test_errored_directoryz TestCheck.test_errored_directory   s   (** 	=g(-.'  J (\*g  I  =vy =F6====**7*<<<	= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	=sY   ;B.B!A?3B?BBBB
B.B	B.B	B..B25B2N)r   r   )r-   r.   r/   r0   rf   rm   rr   r   r   r   r   r   r   r   r   r1   r   r   ra   ra      s        ##  1 1 1 12 2 2
8 8 804 4 4; ; ;; ; ;; ; ;; ; ;2 2 2= = = = =r   ra   c                   J    e Zd ZdZ ej        d          d             Zd ZdS )TestProcessTokensz$Testing `tabnanny.process_tokens()`.ztabnanny.NannyNagc                 D   t          t          d                   5 }t          |          5 }t          j        t          j        |j                             ddd           n# 1 swxY w Y   |                     |j	                   ddd           dS # 1 swxY w Y   dS )z=A python source code without any whitespace related problems.r   N)
r   rp   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8 	2Ii NA'(@(L(LMMMN N N N N N N N N N N N N N N\0111	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2s4   B,A#B#A'	'B*A'	+BBBc           
         dD ]}|                      |          5  t          t          |                   5 }t          |          5 }t	          j        |j                  }|                     t          j	                  5  t          j
        |           ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   dS )z>A python source code with whitespace related sampled problems.)r   r   )keyN)r9   r   rp   r   r   r   r   assertRaisesr;   rZ   r   )r   r   r%   r&   tokenss        r   test_with_errored_codes_samplesz1TestProcessTokens.test_with_errored_codes_samples  s    B 	< 	<C#&& < <$\#%677 <9i <A!)!9!*!E!E!..x/@AA < <$3F;;;< < < < < < < < < < < < < < << < < < < < < < < < < < < < << < < < < < < < < < < < < < << < < < < < < < < < < < < < <	< 	<s|   C%C9B7	?B B7	 B$$B7	'B$(B7	+C7B;;C>B;?CC%CC%CC%%C)	,C)	N)r-   r.   r/   r0   r   patchr   r   r1   r   r   r   r     sP        ..TZ#$$2 2 %$2< < < < <r   r   c                   F    e Zd ZdZdddddZd Zd Zd Zd	 Zd
 Z	d Z
dS )TestCommandLinez+Tests command line interface of `tabnanny`.r   F)rl   rL   partialc                p   t          j        ddg|R  \  }}}t          j        |          }t          j        |          }|r||f||ffD ]u\  }}	|	                                }
|                                D ]G}|                     ||
          5  |                     ||
           ddd           n# 1 swxY w Y   HvdS |                     |                                |                                           |                     |                                |                                           dS )zBCommon function to assert the behaviour of command line interface.z-mr;   )stdoutputN)r   assert_python_okr   fsdecode
splitlinesr9   rx   assertListEqual)r   rl   rL   r   rK   _rj   rk   r   r   _output_stds               r   validate_cmdzTestCommandLine.validate_cmd"  s   #4T:MMMM3 k#k# 	H!'}= 5 5V ++--NN,, 5 5D$w?? 5 5dG4445 5 5 5 5 5 5 5 5 5 5 5 5 5 555 5   !1!163D3D3F3FGGG  !1!163D3D3F3FGGGGGs   B33B7:B7c                     t          t          d                   5 }|d}|dz  }|                     ||           ddd           dS # 1 swxY w 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)rL   Nr   rp   r   )r   r%   rL   s      r   test_with_errored_filez&TestCommandLine.test_with_errored_file3  s    \*:;<< 	8	";;;F , -Fi777		8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8s   "A

AAc                     t          t          d                   5 }|                     |           ddd           dS # 1 swxY w Y   dS )zAShould not display anything if python file is correctly indented.r   Nr   rq   s     r   test_with_error_free_filez)TestCommandLine.test_with_error_free_file;  s    \,788 	)Ii(((	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)rs   c                 \    t          d          }d| d}|                     |           dS )z%Should display usage on no arguments.ztabnanny.pyzUsage: z [-v] file_or_directory ...r   N)r   r   )r   r   rL   s      r   test_command_usagez"TestCommandLine.test_command_usage@  s<    &&<4<<<(((((r   c                     t          t          d                   5 }| d}|                     d||           ddd           dS # 1 swxY w Y   dS )z*Should display less when quite mode is on.r   rG   z-q)rl   Nr   )r   r%   rl   s      r   test_quiet_flagzTestCommandLine.test_quiet_flagF  s    \*<=>> 	>)!%%%FdIf===	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	>s   AA
A
c                     t          t          d                   5 }t          j        d                                          }|                     d||d           ddd           dS # 1 swxY w Y   dS )z<Should display more error information if verbose mode is on.r   $offending line: '\tprint("world")\n'z-vTrl   r   Nr   rp   textwrapdedentstripr   r   r   rl   s      r   test_verbose_modez!TestCommandLine.test_verbose_modeL  s    \*<=>> 	G$_: egg  dDFFF		G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G   A A((A,/A,c                     t          t          d                   5 }t          j        d                                          }|                     d||d           ddd           dS # 1 swxY w Y   dS )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_modeT  s    \*<=>> 	H$_: egg  eT&$GGG		H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	Hr   N)r-   r.   r/   r0   r   r   r   r   r   r   r   r1   r   r   r   r     s        55)+B H H H H H"8 8 8) ) )
) ) )> > >G G GH H H H Hr   r   )r0   unittestr   r   r   r   r;   r   r!   r   test.supportr   r   r   r   test.support.os_helperr	   rp   r   r3   r@   rO   ra   r   r   r1   r   r   <module>r      s"   
 $ # # # # # # #  				    $ $ $ $ $ $ $ $ $ $ $ $ ) ) ) ) ) )
		%	%	%	!	'K* *Z       &M M M M M( M M M"? ? ? ? ?8 ? ? ?&9 9 9 9 98 9 9 96m= m= m= m= m= m= m= m=`< < < < < < < <<;H ;H ;H ;H ;Hh ;H ;H ;H ;H ;Hr   