Ϫf_dZddlmZddlZddlZddlZddlmZddl m Z ddl m Z ddl mZmZmZddlmZmZmZdd lmZdd lmZmZdd lmZmZmZdd lmZdd l m!Z!m"Z"mZddl#m$Z$ddl%m&Z&ddl'm(Z(ddl)m*Z*erddl+m,Z,d dZ-Gdde(j\Z/d!dZ0Gdde(j\Z1Gdde(j\Z2Gdde2Z3Gdde(j\Z4y)"z" Tests for loading tests by name. ) annotationsN)md5) attrgetter) ModuleType) TYPE_CHECKINGCallable Generator) assert_thatequal_tohas_properties)Matcher)filepathutil)PythonAttribute PythonModule getModule)ModuleNotFound)reporterrunnerunittest) _iterateTests) ITestCase)packages)after)SupportsRichComparisonchg}t|D]!}|j|j#|S)zI Return the id of each test within the given test suite or case. )rappendid)testsnamestests @/usr/lib/python3/dist-packages/twisted/trial/test/test_loader.py testNamesr$!s4 Ee$  TWWY Lc@eZdZdfd ZddZddZddZddZxZS)FinderPy3TestscTt|tj|_yN)supersetUpr TestLoaderloader)self __class__s r#r+zFinderPy3Tests.setUp,s  '') r%cZ|jt|jjdy)z findByName, if given something findable up until the last entry, will raise AttributeError (as it cannot tell if 'nonexistent' here is supposed to be a module or a class). ztwisted.trial.test.nonexistentN) assertRaisesAttributeErrorr- findByNamer.s r#test_findNonModulez!FinderPy3Tests.test_findNonModule0s$  DKK224T r%cZ|jt|jjdy)N nonextant)r1rr-r3r4s r#test_findNonPackagez"FinderPy3Tests.test_findNonPackage:s .$++*@*@+Nr%ctjtd}|jt|j j |y)z findByName, given a file path that doesn't exist, will raise a ValueError saying that it is not a Python file. znonexistent.pyN)rsibpath__file__r1 ValueErrorr-r3)r.paths r#test_findNonFilezFinderPy3Tests.test_findNonFile=s1 ||H&67 *dkk&<&rA __classcell__)r/s@r#r'r'+s* OD r%r'c  |j}|Jtt|jt t j tt j |jd}t td|S)a Match a module with a L{ModuleSpec} like that of the given module. @return: A matcher for a module spec that has the same name and origin as the given module spec, though the origin may be structurally inequal as long as it is semantically equal. )nameorigin__spec__) rLr r rJrrFilePathrKr)moduleexpected match_specs r#looselyResemblesrQOsrH   X]]+!!**8??;< J J' 44r%cXeZdZdZd dZd dZd dZd dZd dZd dZ d dZ d d Z d d Z y ) FileTestsz/ Tests for L{runner.filenameToModule}. c|jttjd}|j t |dy)zo L{runner.filenameToModule} raises a C{ValueError} when a non-existing file is passed. itz'it' doesn't existN)r1r<rfilenameToModule assertEqualstr)r.errs r# test_notFilezFileTests.test_notFilels3  F,C,CTJ S#78r%ctjtjtd}ddlm}|j||y)z If the file in question is a module on the Python path, then it should properly import and return that module. z sample.pyrsampleN)rrVrr:r;twisted.trial.testr]rWr.sample1sample2s r#test_moduleInPathzFileTests.test_moduleInPathts2 ))$,,x*MN8 '*r%c|j|jtjtj j |jdd}|j|jd|j|j|jddl m }|j||t|t!|y)a If passed the path to a file containing the implementation of a module within a package which is not on the import path, L{runner.filenameToModule} returns a module object loosely resembling the module defined by that file anyway. goodpackageztest_sample.pyzgoodpackage.test_sampler) test_sampleN) mangleSysPatholdPathrrVosr=joinparentrWrEcleanUpModulesnewPathrdre assertIsNotr rQr_s r#test_moduleNotInPathzFileTests.test_moduleNotInPath~s 4<<()) GGLLm5E F  ))+DE  4<<(6 '*G-g67r%ctjtjj |j d}|j |tjdy)z If the file in question is a package on the Python path, then it should properly import and return that package. rdN) rrVrhr=rirjassertIssysmodules)r.package1s r#test_packageInPathzFileTests.test_packageInPaths> **277<< ]+ST h M :;r%c|j|jtjtj j |jd}|j|jd|j|j|jddl }|j||t|t|y)z If passed the path to a directory which represents a package which is not on the import path, L{runner.filenameToModule} returns a module object loosely resembling the package defined by that directory anyway. rdrN)rfrgrrVrhr=rirjrWrErkrlrdrmr rQ)r.rsrds r#test_packageNotInPathzFileTests.test_packageNotInPaths 4<<(**277<< ]+ST **M:  4<<( ;/H.{;'> Nr%ctj|jjd}|j t j jd tj|j}|j|j|j|jy#|jwxYw)zX The C{__file__} attribute of the module should match the package name. zgoodpackage.pyutf8N)rrMrjryrr testModuleencoderrVr=rWr;remove)r.rrNs r#test_filenameMatchesPackagez%FileTests.test_filenameMatchesPackages$$T[[1778HIH//66v>? ,,X]];F   X]]FOO < OO HOO s AB44Ccftj|jjd}|j |jdj d t j|j}|j|jjdt j|jtjjz}|j|jjd|jy#|jwxYw)z Test loader against a filesystem directory containing an empty C{__init__.py} file. It should handle 'path' and 'path/' the same way. goodDirectory __init__.pyr%N)rrMrjryrzrrrVr= assertTruerEendswithrhsepr)r.goodDirrNs r#test_directoryzFileTests.test_directorys ##DKK066G! m$//4 ,,W\\:F OOFOO44_E F,,W\\BGGKK-GHF OOFOO44_E F NN GNN s B-DD0NrB) rErFrG__doc__rZrbrnrtrvr|rrrr%r#rSrSgs69+8*<=$ TO r%rSceZdZdZd"dZd"dZd"dZd"dZd"dZd"dZ d"dZ d"d Z d"d Z d"d Z d"d Zd"d Zd"dZd"dZd"dZd"dZd"dZd"dZd"dZd"dZd"dZd"dZd"dZd"dZd"dZd"dZ d#dZd"dZd"dZ d"dZ!d"d Z"y!)$ LoaderTestsz( Tests for L{trial.TestLoader}. cttj|_tjj |yr))rr,r-rSysPathManglingTestr+r4s r#r+zLoaderTests.setUps&'') $$**40r%cddlm}|jj|j}|j gd|j Dcgc]}|jc}gd}tt|tdfd|j_ |jj|j}|j ||j Dcgc]}|jc}ycc}wcc}w)Nrr\)test_atest_btest_c)rrrcDj|jdS)N)getshortDescription)xsortDicts r#z,LoaderTests.test_sortCases..sx||A4F4F4H"'Mr%) r^r]r- loadClass AlphabetTestrW_tests_testMethodNamedictziprangesorter)r.r]suiter"newOrderrs @r#test_sortCaseszLoaderTests.test_sortCasess- %%f&9&9:  *.3ll ;dT ! ! ; 2HeAh/0M  %%f&9&9: U\\#RTD$8$8#RS < $Ss C3 C8 c|jjd}tj}|j ||j |j d|j t|jdy)z{ Loading a test that fails and getting the result of it ends up with one test ran and one failure. z6twisted.trial.test.erroneous.TestRegularFail.test_failrN) r- loadByNamer TestResultrunrWtestsRunlenfailures)r.rresults r#test_loadFailurezLoaderTests.test_loadFailuresd  && D $$& & !, V__-q1r%cddlm}|jj|jj |jgd}|j d|j|j d|jy)z A decorated test method for which the decorator has failed to set the method's __name__ correctly is loaded and its name in the class scope discovered. rr\)r]DecorationTesttest_badDecoratorrjqualNamerrN) r^r]r- loadAnythingrrrWcountTestCasesrr.r]rs r#test_loadBadDecoratorz!LoaderTests.test_loadBadDecoratorsm . ((  ! ! 3 3((F)  E0023 ,e.C.CDr%cddlm}|jj|jj |jgd}|j d|j|j d|jy)z A decorated test method for which the decorator has set the method's __name__ correctly is loaded and the only name by which it goes is used. rr\)r]rtest_goodDecoratorrrrN) r^r]r-rrrrWrrrs r#test_loadGoodDecoratorz"LoaderTests.test_loadGoodDecoratorsm . ((  ! ! 4 4((G)  E0023 -u/D/DEr%cddlm}|jj|jj |jgd}|j d|j|j d|jy)z Load a decorated method which has been copied to a new name inside the class. Thus its __name__ and its key in the class's __dict__ no longer match. rr\)r]rtest_renamedDecoratorrrrN) r^r]r-rrrrWrrrs r#test_loadRenamedDecoratorz%LoaderTests.test_loadRenamedDecorator%sm . ((  ! ! 7 7((J)  E0023 0%2G2GHr%cddlm}|jj|j}|j d|j |j ddg|jDcgc]}|jc}ycc}w)Nrr\test_bartest_foo) r^r]r-rFooTestrWrrr)r.r]rr"s r#test_loadClasszLoaderTests.test_loadClass5sf- %%fnn5 E0023   $ &Ut';';&U &Us(B cddlm}|jt|jj ||jt|jj |j j|jt|jj d|jt|jj dyNrr\string)foobar)r^r]r1 TypeErrorr-rrrr.r]s r#test_loadNonClasszLoaderTests.test_loadNonClass>s- )T[[%:%:FC )T[[%:%:FNNU>UV )T[[%<%""$ &  4 4 67 C .//5}}= e$'')==   .0LM >sCcGddtj}|jj|}t j }|j ||j|jy)z L{TestLoader.loadClass} returns a suite with each test method represented by a different instances of the L{TestCase} they are defined on. ceZdZddZddZy)>LoaderTests.test_differentInstances..DistinctInstancescd|_y)Ntest1Run)firstr4s r#test_1zELoaderTests.test_differentInstances..DistinctInstances.test_1s ' r%c:|jt|dy)Nr ) assertFalsehasattrr4s r#test_2zELoaderTests.test_differentInstances..DistinctInstances.test_2s  w!78r%NrB)rErFrGr rrr%r#DistinctInstancesrs  ( 9r%rN) pyunitTestCaser-rrrrrr)r.rrrs r#test_differentInstancesz#LoaderTests.test_differentInstancessX 9 9 %%&78""$ & ,,./r%cddlm}|jj|}|j d|j |j dt |ddy)zp Check that C{test_suite} is used when present and other L{TestCase}s are not included. r)mockcustomsuite MyCustomSuiterJN)r^rr-rrWrgetattr)r.rrs r#test_loadModuleWith_test_suitez*LoaderTests.test_loadModuleWith_test_suitesM 7 &&7 E0023 '%*FGr%cddlm}|jj|}|j d|j |j dt |ddy)zo Check that C{testSuite} is used when present and other L{TestCase}s are not included. rmockcustomsuite2rrJN)r^rr-rrWrr)r.rrs r#test_loadModuleWith_testSuitez)LoaderTests.test_loadModuleWith_testSuitesN 8 &&'78 E0023 '%*FGr%cddlm}|jj|}|j dt |ddy)z Check that if C{testSuite} and C{test_suite} are both present in a module then C{testSuite} gets priority. r)mockcustomsuite3 testSuiterJN)r^rr-rrWr)r.rrs r#test_loadModuleWithBothCustomz)LoaderTests.test_loadModuleWithBothCustoms5 8 &&'78 geVT&BCr%cdd}ddlm}||jc|_} |jt|j j |||_y#||_wxYw)zz Make sure that any C{AttributeError}s raised by C{testSuite} are not swallowed by L{TestLoader}. ctd)Nzshould be reraised)r2rr%r#rzBLoaderTests.test_customLoadRaisesAttributeError..testSuites !56 6r%rrNrB)r^rrr1r2r-r)r.rroriginals r##test_customLoadRaisesAttributeErrorz/LoaderTests.test_customLoadRaisesAttributeErrorsX  7 809;K;U;U,"H 2   ndkk.D.DFV W)1  &  &s +A Act|}t|}|j|j|j||yr))r$rrW)r.test1test2names1names2s r#assertSuitesEqualzLoaderTests.assertSuitesEquals: 5!5!   (r%cd}|jj||gd}|jj|d}|j||y)z@ Check that loadByNames ignores duplicate names twisted.trial.test.test_logTN)r- loadByNamesrr))r.rNsuite1suite2s r#test_loadByNamesDuplicatez%LoaderTests.test_loadByNamesDuplicatesJ/((&&)94@''5 vv.r%cgd}|jj|}tjt |jj |}|j t|t|y)zX L{TestLoader.loadByNames} preserves the order of tests provided to it. )$inheritancepackage.test_x.A.test_fooztwisted.trial.test.samplerdr+z!twisted.trial.test.sample.FooTestpackage.test_moduleN)r-r,r TestSuitemaprrWr$r.rrr-r.s r#test_loadByNamesPreservesOrderz*LoaderTests.test_loadByNamesPreservesOrdersZ ((1!!#dkk&<&r%cddg}|jj|}tjt |jj |}|j ||y)zM Check that loadByNames loads all the names that it is given rdr2Nr-r,rr3r4rr)r5s r#test_loadDifferentNamesz#LoaderTests.test_loadDifferentNames sT!"78((1!!#dkk&<&>vF'..t45%+;F$CM,,, - -"-s&AE A5E A'E /%E E E c ddl}||j_|jj|d}t j |t }tt|}t|j|}tt||D]Q\}\}}|j|j|jd||j|jfzS|jt|t|y)zG Verify that packages are loaded in the correct order. rNTrz #%d: %s != %s)rFr-rrrdecoraterlistrrc enumeraterrWrJrr) r.rrFrresultingTestsmanifestnumber manifestTest actualTests r#loadSortedPackagesz'PackageOrderingTests.loadSortedPackagescs #  '' T'B!!%3mE2300892; . )3   .F.\:   !! 6<+<+.sillySorters2  AAAHHV$%//1AHr%)rtzrunner._LoadablerCrXro)r.rws r#test_sortPackagesSillyOrderz0PackageOrderingTests.test_sortPackagesSillyOrders   ,r%NrB)rzBCallable[[PythonModule | PythonAttribute], SupportsRichComparison]rCz5Generator[PythonModule | PythonAttribute, None, None])rz4Callable[[runner._Loadable], SupportsRichComparison]rCrD) rErFrGr+rcrrJrmrprxrr%r#rDrD,sG9+-X+- >+-\NT[[=J= =2" -r%rD)r r=rCz list[str])rNrrCzMatcher[ModuleType])5r __future__rrhrqrrhashlibroperatorrtypesrtypingrrr hamcrestr r r hamcrest.core.matcherr twisted.pythonrrtwisted.python.modulesrrrtwisted.python.reflectr twisted.trialrrtwisted.trial._asyncrunnerrtwisted.trial.itrialrr^rmatchersr _typeshedrr$rr'rQrSrr?rDrr%r#rs# 55::))KK1444*'0! X11! H50x,,xv}/(..}/@ 9k9]-877]-r%