
    7bh7                        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      </opt/python-3.12.12/usr/lib/python3.12/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   sY    (($((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   sS    
  %OE8EH=  !:!:5!A8L >=  %==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	
 $ND(x@&&z2(*f ))40 +$$V__%6B 3 A@ $ +* 32 A@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   s:    !!!UE2UE:
 !!!Y
;Y
C	
  %OE8**,K--/C>>#D'(82D     (!3[A #@  %#6 A4(62BC  &!148 DC  %  A@CCs$   	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   se     &/*;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   s+    \,78I&&y1 988   .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   s0    ((*g{"78:E#L$>'RE#L$>'RE*ez",j!9ILLI=0I!JK ": $% $&&/2CvNN7+ 3D&*!D40dF3 10 "   !2B7 ',	 +* 3D2C&& 10 ',e	 +*s   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   sK    ((*g l!;wO**73 P +*OO +*s!   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   sj    \*:;<	*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))
z: Token Error: r   Nr   r   s      r   test_when_tokenize_tokenerrorz'TestCheck.test_when_tokenize_tokenerror   si    \*ABCy=CM6C"":.**9#*> / DC /. DCr   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   s^    
 \*<=>)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   s>    \*<=>)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   sh    -ell^2U\\*+2dXR9z*&&t&5 +**s   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    ((*g(-.'J (\*gI vyF6
"7u=&&z2..wC.@ 3	 (1 +* 32	 (1y +*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	  si     \,78IiA''(@(@(LM !\001 98  98s"   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79iA!)!9!9!**!E!..x/@/@A$33F; B ) 8 '& B
 BA ) 87 '&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#!'}=V ++-NN,D$w?dG4 @? -  >   !163D3D3FG  !163D3D3FG	 @?s   *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;  sO    \*:;<	"%:;F - .FitL	 =<<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  s+    \,78Ii( 988ru   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  s?    \*<=>)!{"%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  sR    \*<=>$__7eg  dDF	 ?>>   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\  sR    \*<=>$__7eg  eT&$G	 ?>>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   