a ze5@sdZddlmZmZddlmZddlZddlZddlZddlZddlZddl Z ddl m Z m Z m Z mZmZdddd d d d ZGd ddZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZdS)zfTesting `tabnanny` module. Glossary: * errored : Whitespace related problems present in file. )TestCasemock)rN)captured_stderrcaptured_stdout script_helperfindfileunlinkzCfruits = [ "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_expressionwrong_indentednannynag_errored error_freetab_space_errored_1tab_space_errored_2c@s*eZdZdZd ddZddZdd ZdS) TemporaryPyFilez+Create a temporary python source code file.NcCs||_||_dSN) source_codedir)selfr directoryrF/opt/bitninja-python-dojo/embedded/lib/python3.9/test/test_tabnanny.py__init__BszTemporaryPyFile.__init__cCsNtjd|jddd}||jWdn1s60Y|j|_|jS)Nwz.pyF)modersuffixdelete)tempfileNamedTemporaryFilerwritername file_path)rfrrr __enter__Fs *zTemporaryPyFile.__enter__cCst|jdSr)rr!)rexc_type exc_value exc_tracebackrrr__exit__NszTemporaryPyFile.__exit__)rN)__name__ __module__ __qualname____doc__rr#r'rrrrr?s rc@seZdZdZddZdS)TestFormatWitnessesz&Testing `tabnanny.format_witnesses()`.c CsZgd}|D]H\}}|j||d"|t||Wdq 1sJ0Yq dS)z;Asserting formatter result by giving various input samples.))ZTestzat tab sizes T, e, s, t)rz at tab size )tz at tab size t)z t zat tab sizes , , t, , )wordsexpectedN)subTest assertEqualtabnannyZformat_witnesses)rtestsr.r/rrrtest_format_witnessesUs z)TestFormatWitnesses.test_format_witnessesN)r(r)r*r+r4rrrrr,Rsr,c@seZdZdZddZdS) TestErrPrintzTesting `tabnanny.errprint()`.c Csddgdfdgdfgddfgdfg}|D]v\}}|j||dPt}tj|Wd n1sh0Y|||Wd q*1s0Yq*d S) zBAsserting result of `tabnanny.errprint()` by giving sample inputs.firstsecondz first second zfirst )z1 2 3  )Z argumentsr/N)r0rr2Zerrprintr1getvalue)rr3argsr/stderrrrr test_errprintfs   (zTestErrPrint.test_errprintN)r(r)r*r+r?rrrrr5csr5c@seZdZddZdS) TestNannyNagc Cs&tdddddddftdddddddfg}|D]\}}|}|}|}|j||dd  ||d|Wd n1s0Y|j||d d  ||d |Wd n1s0Y|j||d d ||d |Wd q81s0Yq8d S)z5Asserting behaviour of `tabnanny.NannyNag` exception.rZfooZbar)linenomsglineZtestmsgZtestlinerA) line_numberr/NrB)rBr/rC)rCr/)r2NannyNagZ get_linenoZget_msgZget_liner0r1)rr3Znannyr/rErBrCrrrtest_all_methodsws&     ..zTestNannyNag.test_all_methodsN)r(r)r*rGrrrrr@vsr@c@sjeZdZdZddZdddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZdS) TestCheckzTesting tabnanny.check().cCs|ttdtjdt_dS)Nverboser)Z addCleanupsetattrr2rI)rrrrsetUpszTestCheck.setUprc Cszt@}t}t|Wdn1s.0YWdn1sL0Y||||||dS)aRCommon 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)rrr2checkr1r<)rZ dir_or_fileouterrstdoutr>rrrverify_tabnanny_checksFzTestCheck.verify_tabnanny_checkcCs:ttd}||Wdn1s,0YdS)z-A python source code file without any errors.r Nr SOURCE_CODESrPrr!rrrtest_correct_fileszTestCheck.test_correct_filec Cs~t^}|dg}ttd|d}ttd|d}| }|}||fD]}||dqNdt_t@}t} t |Wdn1s0YWdn1s0Y| }|D]<} |j | d| | |Wdq1s0Yq| | dWdn1s00YWdn1sP0YWdn1sp0YdS) aDirectory 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 rz: Clean bill of health. r8N)rCr)rTemporaryDirectoryrrRappendr2rIrrrLr<r0assertInr1) rtmp_dirlinesZfile1Zfile2Z file1_pathZ file2_pathr!rOr>rCrrrtest_correct_directory_verboses   F,z(TestCheck.test_correct_directory_verbosec CsftJ}ttd|d||Wdn1s:0YWdn1sX0YdS)zADirectory which contains few error free python source code files.r rUNrrVrrRrP)rrYrrrtest_correct_directorys z TestCheck.test_correct_directorycCsPttd0}d}|d|}|j||dWdn1sB0YdS)zBA python source code file eligible for raising `IndentationError`.r Iunindent does not match any outer indentation level (, line 3) : Indentation Error: rNNrQrr!rNrrrtest_when_wrong_indentedsz"TestCheck.test_when_wrong_indentedcCsPttd0}d}|d|}|j||dWdn1sB0YdS)zEA python source code file eligible for raising 'tokenize.TokenError'.r z(('EOF in multi-line statement', (7, 0)) z: Token Error: r`NrQrarrrtest_when_tokenize_tokenerrorsz'TestCheck.test_when_tokenize_tokenerrorcCs^ttd>}|d}|d7}|d7}dt_|j||dWdn1sP0YdS)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 r8rMN)rrRr2rIrPrr!rMrrr test_when_nannynag_error_verboses  z*TestCheck.test_when_nannynag_error_verbosecCsHttd(}|d}|j||dWdn1s:0YdS)zCA python source code file eligible for raising `tabnanny.NannyNag`.r z 3 '\tprint("world")\n' rdNrQrerrrtest_when_nannynag_errors z"TestCheck.test_when_nannynag_errorcCs<d}|dtjdttjd|d}|j||ddS)z6A python file which does not exist actually in system.z no_file.pyz: I/O Error: [Errno z] z: r;r`N)errnoENOENTosstrerrorrP)rpathrNrrrtest_when_no_files zTestCheck.test_when_no_filec Cst}ttd|d}ttd|d}|T}|0}d}|d|}|j||dWdn1sj0YWdn1s0YWdn1s0YdS)z?Directory containing wrongly indented python source code files.r rUr r^r_r`Nr\)rrYZ error_fileZ code_fileZe_fileZc_filerNrrrtest_errored_directorys  z TestCheck.test_errored_directoryN)rr)r(r)r*r+rKrPrTr[r]rbrcrfrgrmrnrrrrrHs  rHc@s*eZdZdZedddZddZdS)TestProcessTokensz$Testing `tabnanny.process_tokens()`.ztabnanny.NannyNagc CsvttdV}t|"}tt|jWdn1s>0Y||j Wdn1sh0YdS)z=A python source code without any whitespace related problems.r N) rrRopenr2process_tokenstokenizegenerate_tokensreadlineZ assertFalseZcalled)rZ MockNannyNagr!r"rrrtest_with_correct_codes 0z(TestProcessTokens.test_with_correct_codec CsdD]}|j|dtt|z}t|R}t|j}|tj t |Wdn1sf0YWdn1s0YWdn1s0YWdq1s0YqdS)z>A python source code with whitespace related sampled problems.)r r)keyN) r0rrRrprrrsrtZ assertRaisesr2rFrq)rrvr!r"tokensrrrtest_with_errored_codes_samples s   z1TestProcessTokens.test_with_errored_codes_samplesN)r(r)r*r+rZpatchrurxrrrrros roc@sReZdZdZddddddZddZd d Zd d Zd dZddZ ddZ dS)TestCommandLinez+Tests command line interface of `tabnanny`.rF)rOr>partialc Gstjddg|R\}}}|d}|d}|r||f||ffD]X\}} | } |D]>} |j| | d|| | WdqZ1s0YqZqBn(||||||dS)zBCommon function to assert the behaviour of command line interface.z-mr2ascii)stdoutputN)rZassert_python_okdecode splitlinesr0rXZassertListEqual) rrOr>rzr=_rMrNr|r}Z_outputZ_stdrrr validate_cmd"s   0zTestCommandLine.validate_cmdcCsPttd0}|d}|d7}|j||dWdn1sB0YdS)z8Should displays error when errored python file is given.r r_zHunindent does not match any outer indentation level (, line 3)r>NrrRr)rr!r>rrrtest_with_errored_file3s z&TestCommandLine.test_with_errored_filecCs:ttd}||Wdn1s,0YdS)zAShould not display anything if python file is correctly indented.r NrrSrrrtest_with_error_free_file;sz)TestCommandLine.test_with_error_free_filecCs$td}d|d}|j|ddS)z%Should display usage on no arguments.z tabnanny.pyzUsage: z [-v] file_or_directory ...rN)rr)rrlr>rrrtest_command_usage@s z"TestCommandLine.test_command_usagecCsJttd*}|d}|jd||dWdn1s<0YdS)z*Should display less when quite mode is on.r r;z-q)rONr)rr!rOrrrtest_quiet_flagFs zTestCommandLine.test_quiet_flagcCsPttd0}td}|jd||ddWdn1sB0YdS)zs. -p