U
    `[                     @   s  d dl mZmZ d dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
mZmZ d dlmZmZmZmZ d dlmZ d dlmZmZmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dl m!Z! d dl"m#Z# d dl$m%Z% e&dZ'G dd de(Z)eeG dd de(Z*G dd de(Z+G dd de+ej,Z-G dd de+ej,Z.G dd de(Z/G dd de/ej,Z0G dd  d e/ej,Z1G d!d" d"e/ej,Z2G d#d$ d$ej,Z3G d%d& d&ej,Z4G d'd( d(ej,Z5G d)d* d*e5Z6G d+d, d,ej7Z8G d-d. d.ej,Z9G d/d0 d0ej,Z:G d1d2 d2e(Z;G d3d4 d4e;e:Z<G d5d6 d6e;e:Z=G d7d8 d8ej,Z>G d9d: d:ej,Z?G d;d< d<ej,Z@G d=d> d>ej,ZAG d?d@ d@ej,ZBdS )A    )absolute_importdivisionN)implementer)verifyObject)	IReporter	ITestCase)unittestrunnerreporterutil) _ForceGarbageCollectionDecorator)failurelogreflect)FilePathnamedAny)NativeStringIO)trial)twisted_trial)plugin)deferr   c                   @   s   e Zd Zdd Zdd ZdS )CapturingDebuggerc                 C   s
   g | _ d S N)_callsself r   @/usr/lib/python3/dist-packages/twisted/trial/test/test_runner.py__init__"   s    zCapturingDebugger.__init__c                 O   s&   | j d |d |dd  | d S )Nruncallr      r   append)r   argskwargsr   r   r   r    %   s    zCapturingDebugger.runcallN)__name__
__module____qualname__r   r    r   r   r   r   r       s   r   c                   @   sV   e Zd ZdZdZdZdZdZdZdddZ	dd Z
dd Zd	d
 Zdd Zdd ZdS )CapturingReporterzC
    Reporter that keeps a log of all actions performed on it.
    Nc                 C   s(   g | _ d| _|| _|| _|| _|| _dS )z.
        Create a capturing reporter.
        FN)r   Z
shouldStop_streamZ	_tbformatZ	_rterrors
_publisher)r   streamtbformatZrterrorsZ	publisherr   r   r   r   7   s    zCapturingReporter.__init__c                 C   s   | j d dS )z
        Report the beginning of a run of a single test method
        @param method: an object that is adaptable to ITestMethod
        	startTestNr"   r   methodr   r   r   r.   D   s    zCapturingReporter.startTestc                 C   s   | j d dS )z}
        Report the status of a single test method
        @param method: an object that is adaptable to ITestMethod
        stopTestNr"   r/   r   r   r   r1   L   s    zCapturingReporter.stopTestc                 C   s   | j d dS )zcalled when the reactor has been left in a 'dirty' state
        @param errs: a list of L{twisted.python.failure.Failure}s
        ZcleanupErrorNr"   )r   Zerrsr   r   r   cleanupErrorsT   s    zCapturingReporter.cleanupErrorsc                 C   s   | j d d S )N
addSuccessr"   r   testr   r   r   r3   [   s    zCapturingReporter.addSuccessc                 C   s   dS )z@
        Do nothing. These tests don't care about done.
        Nr   r   r   r   r   done_   s    zCapturingReporter.done)NNNN)r&   r'   r(   __doc__r,   r-   r$   	separatortestsRunr   r.   r1   r2   r3   r6   r   r   r   r   r)   +   s     
r)   c                   @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )TrialRunnerTestsMixinz9
    Mixin defining tests for L{runner.TrialRunner}.
    c                 C   s   | j   d S r   )r	   _tearDownLogFiler   r   r   r   tearDownj   s    zTrialRunnerTestsMixin.tearDownc                 C   s   dS )z=
        Empty test method, used by the other tests.
        Nr   r   r   r   r   
test_emptyn   s    z TrialRunnerTestsMixin.test_emptyc                 C   s   t jjS r   )r   ZtheLogPublisherZ	observersr   r   r   r   _getObserverst   s    z#TrialRunnerTestsMixin._getObserversc                 C   s6   t |  }| j| j t |  }| || dS )zn
        Any log system observers L{TrialRunner.run} adds are removed by the
        time it returns.
        N)lenr>   r	   runr5   assertEqual)r   ZoriginalCountZnewCountr   r   r   test_addObserversx   s    z'TrialRunnerTestsMixin.test_addObserversc                    sl   j jg   fdd}|j _j j j j t d  d  d kd dS )z=
        Test that a new file is opened on each run.
        c                      s       jj d S r   )r#   r	   Z_logFileObserverr   lZoldSetUpLogFiler   r   r   setUpLogFile   s    zDTrialRunnerTestsMixin.test_logFileAlwaysActive.<locals>.setUpLogFile   r   r!   z'Should have created a new file observerN)r	   _setUpLogFiler@   r5   rA   r?   assertFalser   rE   r   rC   r   test_logFileAlwaysActive   s    z.TrialRunnerTestsMixin.test_logFileAlwaysActivec                    sV   j jg   fdd}|j _j j t d  d j dS )zB
        Test that file created is closed during the run.
        c                      s       jj d S r   )r#   r	   Z_logFileObjectr   rC   r   r   rE      s    zBTrialRunnerTestsMixin.test_logFileGetsClosed.<locals>.setUpLogFiler!   r   N)r	   rG   r@   r5   rA   r?   
assertTrueclosedrI   r   rC   r   test_logFileGetsClosed   s    z,TrialRunnerTestsMixin.test_logFileGetsClosedN)
r&   r'   r(   r7   r<   r=   r>   rB   rJ   rM   r   r   r   r   r:   f   s   r:   c                   @   s    e Zd ZdZdd Zdd ZdS )TrialRunnerTestszm
    Tests for L{runner.TrialRunner} with the feature to turn unclean errors
    into warnings disabled.
    c                 C   s(   t  | _tjt| jd| _td| _d S )Nr,   r=   r   r,   r	   TrialRunnerr)   rN   r5   r   r   r   r   setUp   s    zTrialRunnerTests.setUpc                 C   s   | j  }| |jt dS )z
        The reporter constructed by L{runner.TrialRunner} is passed
        L{twisted.python.log} as the value for the C{publisher} parameter.
        N)r	   _makeResultassertIdenticalr+   r   r   resultr   r   r   test_publisher   s    
zTrialRunnerTests.test_publisherN)r&   r'   r(   r7   rR   rW   r   r   r   r   rN      s   rN   c                   @   s   e Zd ZdZdd ZdS )+TrialRunnerWithUncleanWarningsReporterTestsza
    Tests for the TrialRunner's interaction with an unclean-error suppressing
    reporter.
    c                 C   s*   t  | _tjt| jdd| _td| _d S )NTr,   ZuncleanWarningsr=   rP   r   r   r   r   rR      s
    
z1TrialRunnerWithUncleanWarningsReporterTests.setUpNr&   r'   r(   r7   rR   r   r   r   r   rX      s   rX   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )DryRunMixinze
    Mixin for testing that 'dry run' mode works with various
    L{pyunit.TestCase} subclasses.
    c                 C   s2   g | _ t | _tjttjj| jd| _|   d S )NrO   )r   r   r,   r	   rQ   r)   ZDRY_RUNmakeTestFixturesr   r   r   r   rR      s    zDryRunMixin.setUpc                 C   s   dS )zh
        Set C{self.test} and C{self.suite}, where C{self.suite} is an empty
        TestSuite.
        Nr   r   r   r   r   r\      s    zDryRunMixin.makeTestFixturesc                 C   s"   | j t  }| |jg  dS )zf
        If there are no tests, the reporter should not receive any events to
        report.
        N)r	   r@   	TestSuiterA   r   rU   r   r   r   r=      s    zDryRunMixin.test_emptyc                 C   s&   | j | j}| |jdddg dS )z
        If we are running a single test, check the reporter starts, passes and
        then stops the test during a dry run.
        r.   r3   r1   N)r	   r@   r5   rA   r   rU   r   r   r   test_singleCaseReporting   s    z$DryRunMixin.test_singleCaseReportingc                 C   s    | j | j | | jg  dS )zT
        When we are doing a dry run, the tests should not actually be run.
        N)r	   r@   r5   rA   r   r   r   r   r   test_testsNotRun   s    zDryRunMixin.test_testsNotRunN)	r&   r'   r(   r7   rR   r\   r=   r^   r_   r   r   r   r   r[      s   			r[   c                   @   s   e Zd ZdZdd ZdS )SynchronousDryRunTestszS
    Check that 'dry run' mode works well with trial's L{SynchronousTestCase}.
    c                 C   s*   G dd dt j}|d| _t | _d S )Nc                   @   s   e Zd Zdd ZdS )z;SynchronousDryRunTests.makeTestFixtures.<locals>.PyunitCasec                 S   s   d S r   r   r   r   r   r   test_foo   s    zDSynchronousDryRunTests.makeTestFixtures.<locals>.PyunitCase.test_fooNr&   r'   r(   ra   r   r   r   r   
PyunitCase   s   rc   ra   )r   SynchronousTestCaser5   pyunitr]   suiter   rc   r   r   r   r\      s    
z'SynchronousDryRunTests.makeTestFixturesNr&   r'   r(   r7   r\   r   r   r   r   r`      s   r`   c                   @   s   e Zd ZdZdd ZdS )DryRunTestsz@
    Check that 'dry run' mode works well with Trial tests.
    c                    s.   G  fdddt j}|d _t  _d S )Nc                       s   e Zd Z fddZdS )z.DryRunTests.makeTestFixtures.<locals>.MockTestc                    s    j d d S )Nra   )r   r#   r5   r   r   r   ra     s    z7DryRunTests.makeTestFixtures.<locals>.MockTest.test_fooNrb   r   r   r   r   MockTest
  s   rk   ra   )r   TestCaser5   r	   r]   rf   )r   rk   r   r   r   r\   	  s    
zDryRunTests.makeTestFixturesNrh   r   r   r   r   ri     s   ri   c                   @   s   e Zd ZdZdd ZdS )PyUnitDryRunTestszJ
    Check that 'dry run' mode works well with stdlib unittest tests.
    c                 C   s*   G dd dt j}|d| _t  | _d S )Nc                   @   s   e Zd Zdd ZdS )z6PyUnitDryRunTests.makeTestFixtures.<locals>.PyunitCasec                 S   s   d S r   r   r   r   r   r   ra     s    z?PyUnitDryRunTests.makeTestFixtures.<locals>.PyunitCase.test_fooNrb   r   r   r   r   rc     s   rc   ra   )re   rl   r5   r]   rf   rg   r   r   r   r\     s    
z"PyUnitDryRunTests.makeTestFixturesNrh   r   r   r   r   rm     s   rm   c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Ze Zd!d" Zd#d$ Zd%S )&RunnerTestsc                    s   t  _ttd}d|d d }|d }tj	d|ddd |dg  fdd}t
j_|t
_d	d
dd	d
dd	d
dd	d
dd	d
dd	d
dd	d
dd	d
dd	d
dd	d
dg_d S )N.zTest Helper ReporterzUtility for unit testing.	capturing)descriptionZlongOptZshortOptklassc                    s&    | t  tj| f|| S r   )rA   r   listoriginal)ZifaceakwZpluginsr   r   r   
getPlugins3  s    z%RunnerTests.setUp.<locals>.getPluginsr.   r3   r1   )r   ZOptionsconfigr   qualr)   splitjoinr   Z	_Reporterr   ry   ru   standardReport)r   partspackagers   ry   r   rx   r   rR      sX    
                  zRunnerTests.setUpc                 C   s   | j t_d S r   )ru   r   ry   r   r   r   r   r<   F  s    zRunnerTests.tearDownc                 C   s   | j | d S r   )rz   parseOptionsr   r$   r   r   r   r   J  s    zRunnerTests.parseOptionsc                 C   s.   t | j}t |_| |j t |_	|S r   )
r   Z_makeRunnerrz   r   r,   
addCleanupr;   r   ZLogPublisherZ_log)r   rr   r   r   	getRunnerN  s
    
zRunnerTests.getRunnerc                 C   s2   |  g  | jd }|  }| || j d S Nr
   )r   rz   r   rA   rS   	__class__)r   rV   r	   r   r   r   test_runner_can_get_reporter_  s    

z(RunnerTests.test_runner_can_get_reporterc                 C   s2   |  g  |  }| }| |j| jd  d S r   )r   r   rS   rA   r   rz   r   r	   rV   r   r   r   test_runner_get_resultf  s    
z"RunnerTests.test_runner_get_resultc                 C   s(   |  g  |  }| | tj dS )z
        By default Trial sets the 'uncleanWarnings' option on the runner to
        False. This means that dirty reactor errors will be reported as
        errors. See L{test_reporter.DirtyReactorTests}.
        N)r   r   ZassertNotIsInstancerS   r
   UncleanWarningsReporterWrapperr   r	   r   r   r    test_uncleanWarningsOffByDefaultm  s
    

z,RunnerTests.test_uncleanWarningsOffByDefaultc                 C   s*   |  dg |  }| | tj dS )z
        Specifying '--unclean-warnings' on the trial command line will cause
        reporters to be wrapped in a device which converts unclean errors to
        warnings.  See L{test_reporter.DirtyReactorTests} for implications.
        z--unclean-warningsN)r   r   assertIsInstancerS   r
   r   r   r   r   r   test_getsUncleanWarningsy  s
    
z$RunnerTests.test_getsUncleanWarningsc                 C   s(   |  ddg |  }| |jd d S )N--temp-directoryZ	some_path)r   r   rA   ZworkingDirectoryr   r   r   r   test_runner_working_directory  s    z)RunnerTests.test_runner_working_directoryc                    s   |  g  | tjt  t|    tj | 	 }| 	 i G  fdddt
j | d}|j|j }|r| |d d  | djdjd d	S )
z
        If no working directory is explicitly specified and the default
        working directory is in use by another runner, L{TrialRunner.run}
        selects a different default working directory to use.
        c                       s*   e Zd Z fddZfddZdS )zKRunnerTests.test_concurrentImplicitWorkingDirectory.<locals>.ConcurrentCasec                    s<   t   d< }t j | t j|  d dS )z
                Start a second test run which will have a default working
                directory which is the same as the working directory of the
                test run already in progress.
                
concurrenttest_secondN)osgetcwdchdirpathr   r@   )r   ZsubsequentDirectoryConcurrentCaseZrunDirectorysecondRunnerwherer   r   
test_first  s    zVRunnerTests.test_concurrentImplicitWorkingDirectory.<locals>.ConcurrentCase.test_firstc                    s   t   d< dS )zR
                Record the working directory for later analysis.
                recordN)r   r   r   )r   r   r   r     s    zWRunnerTests.test_concurrentImplicitWorkingDirectory.<locals>.ConcurrentCase.test_secondN)r&   r'   r(   r   r   r   r   r   r   r     s   r   r   r   r!   Z_trial_tempz_trial_temp-1)r   r   N)r   r   r   r   r   r   mktempmakedirsr   r   r   rd   r@   errorsfailuresfailrA   Zchild)r   firstRunnerrV   badr   r   r   'test_concurrentImplicitWorkingDirectory  s&    


z3RunnerTests.test_concurrentImplicitWorkingDirectoryc                    s   |  dtj|  g t }| tj| |  }|  G  fdddt	j
 | d}|j|j }|r| |d d  dS )z
        If a working directory which is already in use is explicitly specified,
        L{TrialRunner.run} raises L{_WorkingDirectoryBusy}.
        r   c                       s"   e Zd Z fddZdd ZdS )zKRunnerTests.test_concurrentExplicitWorkingDirectory.<locals>.ConcurrentCasec                    s   |  tjj d dS )z
                Try to start another runner in the same working directory and
                assert that it raises L{_WorkingDirectoryBusy}.
                test_failureN)ZassertRaisesr   Z_WorkingDirectoryBusyr@   r   r   r   r   r   test_concurrent  s
     z[RunnerTests.test_concurrentExplicitWorkingDirectory.<locals>.ConcurrentCase.test_concurrentc                 S   s   |  d dS )zE
                Should not be called, always fails.
                z$test_failure should never be called.N)r   r   r   r   r   r     s    zXRunnerTests.test_concurrentExplicitWorkingDirectory.<locals>.ConcurrentCase.test_failureN)r&   r'   r(   r   r   r   r   r   r   r     s   	r   r   r   r!   N)r   r   r   abspathr   r   r   r   r   r   rd   r@   r   r   r   )r   ZinitialDirectoryr   rV   r   r   r   r   'test_concurrentExplicitWorkingDirectory  s    z3RunnerTests.test_concurrentExplicitWorkingDirectoryc                 C   sR   |  d|  dddg |  }t }|dd}||}| | j|j	 d S )Nr   
--reporterrq   twisted.trial.test.sampleT)
r   r   r   r	   
TestLoader
loadByNamer@   rA   r~   r   )r   	my_runnerloaderrf   rV   r   r   r   test_runner_normal  s     
zRunnerTests.test_runner_normalc                 C   s   t  }|dd}||S )Nr   T)r	   r   r   r@   )r   r   r   rf   r   r   r   runSampleSuite  s    zRunnerTests.runSampleSuitec                 C   sT   |  ddddg |  }t  }|_| |}| | j|j | dg|j dS )zL
        Trial uses its debugger if the `--debug` option is passed.
        r   rq   --debugr   r    N)r   r   r   debuggerr   rA   r~   r   )r   r   r   rV   r   r   r   test_runnerDebug  s     
zRunnerTests.test_runnerDebugc                    s^     ddg td}|  d _ fdd} tjd|      	 j dS )	zL
        Trial uses pdb if no debugger is specified by `--debugger`
        r   r   ZpdbrcFc                    s
   d _ d S NTruncall_calledpdbrf   rV   r   r   r   r      s    z=RunnerTests.test_runnerDebuggerDefaultsToPdb.<locals>.runcallr    N)
r   r   Ztouchr   patchr   Pdbr   r   rK   )r   Z	pdbrcFiler    r   r   r    test_runnerDebuggerDefaultsToPdb  s    z,RunnerTests.test_runnerDebuggerDefaultsToPdbc                    sV     ddddddg d _ fdd	} tjd
|       j dS )zU
        Trial uses pdb if pdb is passed explicitly to the `--debugger` arg.
        r   rq   
--debuggerr   r   r   Fc                    s
   d _ d S r   r   r   r   r   r   r       s    zGRunnerTests.test_runnerDebuggerWithExplicitlyPassedPdb.<locals>.runcallr    N)r   r   r   r   r   r   r   rK   )r   r    r   r   r   *test_runnerDebuggerWithExplicitlyPassedPdb  s       z6RunnerTests.test_runnerDebuggerWithExplicitlyPassedPdbc                 C   sN   |  ddddddg |  }| |}| | j|j | dg|jj dS )	zM
        Trial uses specified debugger if the debugger is available.
        r   rq   r   z4twisted.trial.test.test_runner.RunnerTests.cdebuggerr   r   r    N)r   r   r   rA   r~   r   r   )r   r   rV   r   r   r   test_runnerDebugger,  s     
zRunnerTests.test_runnerDebuggerc                 C   s.   |  dg |  }| }| |tj dS )z
        If trial was passed the C{--exitfirst} option, the constructed test
        result object is wrapped with L{reporter._ExitWrapper}.
        z--exitfirstN)r   r   rS   r   r
   Z_ExitWrapperr   r   r   r   test_exitfirst=  s    zRunnerTests.test_exitfirstN)r&   r'   r(   rR   r<   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Z	cdebuggerr   r   r   r   r   r   rn     s&   &7#rn   c                   @   s   e Zd Zdd ZdS )TrialSuiteTestsc                 C   s   t d d S )Nztwisted.trial.runner.TrialSuiter   r   r   r   r   test_importsK  s    zTrialSuiteTests.test_importsN)r&   r'   r(   r   r   r   r   r   r   I  s   r   c                   @   sF   e Zd ZG dd dejZdd Zdd Zdd Zd	d
 Z	dd Z
dS )UntilFailureTestsc                   @   s   e Zd ZdZg Zdd ZdS )zUntilFailureTests.FailAfterzC
        A test case that fails when run 3 times in a row.
        c                 C   s(   | j d  t| j dkr$| d d S )N   zCount reached 3)countr#   r?   r   r   r   r   r   ra   W  s    z$UntilFailureTests.FailAfter.test_fooN)r&   r'   r(   r7   r   ra   r   r   r   r   	FailAfterR  s   r   c                 C   s4   g t j_t d| _t | _tjtj	| jd| _d S )Nra   rO   )
r   r   r   r5   r   r,   r	   rQ   r
   Reporterr   r   r   r   rR   ]  s    zUntilFailureTests.setUpc                 C   s@   | j | j}| |jd | |  | | |d dS )zl
        Test that the runUntilFailure method of the runner actually fail after
        a few runs.
        r!   N)r	   runUntilFailurer5   rA   r9   rH   wasSuccessful_getFailuresrU   r   r   r   test_runUntilFailured  s    z&UntilFailureTests.test_runUntilFailurec                 C   s
   t |jS )zL
        Get the number of failures that were reported to a result.
        )r?   r   rU   r   r   r   r   o  s    zUntilFailureTests._getFailuresc                    sb   g   fdd}|  td| | j| j}| |jd | t d |  | jtfg dS )z
        C{runUntilFailure} doesn't decorate the tests uselessly: it does it one
        time when run starts, but not at each turn.
        c                    s     | |f | S r   r#   r5   Z	interfaceZ	decoratedr   r   decorate|  s    z@UntilFailureTests.test_runUntilFailureDecorate.<locals>.decorater   r!   N)	r   r   r	   r   r5   rA   r9   r?   r   r   r   rV   r   r   r   test_runUntilFailureDecoratev  s    z.UntilFailureTests.test_runUntilFailureDecoratec                    sr   g   fdd}|  td| d| j_| j| j}| |jd | t d |  | jt	f| jt
fg dS )z
        C{runUntilFailure} applies the force-gc decoration after the standard
        L{ITestCase} decoration, but only one time.
        c                    s     | |f | S r   r   r   r   r   r   r     s    zGUntilFailureTests.test_runUntilFailureForceGCDecorate.<locals>.decorater   Tr!   rF   N)r   r   r	   Z_forceGarbageCollectionr   r5   rA   r9   r?   r   r   r   r   r   r   #test_runUntilFailureForceGCDecorate  s    z5UntilFailureTests.test_runUntilFailureForceGCDecorateN)r&   r'   r(   re   rl   r   rR   r   r   r   r   r   r   r   r   r   Q  s   r   c                   @   s    e Zd ZdZdd Zdd ZdS )UncleanUntilFailureTestszh
    Test that the run-until-failure feature works correctly with the unclean
    error suppressor.
    c                 C   s$   t |  tjtj| jdd| _d S )NTrY   )r   rR   r	   rQ   r
   r   r,   r   r   r   r   rR     s    
zUncleanUntilFailureTests.setUpc                 C   s   t |jjS )z
        Get the number of failures that were reported to a result that
        is wrapped in an UncleanFailureWrapper.
        )r?   Z_originalReporterr   rU   r   r   r   r     s    z%UncleanUntilFailureTests._getFailuresN)r&   r'   r(   r7   rR   r   r   r   r   r   r     s   r   c                   @   s   e Zd ZdZdd ZdS )BreakingSuitez;
    A L{TestSuite} that logs an error when it is run.
    c                 C   s4   zt dW n" t k
r.   tt  Y nX d S )Nz#error that occurs outside of a test)RuntimeErrorr   errr   FailurerU   r   r   r   r@     s    zBreakingSuite.runN)r&   r'   r(   r7   r@   r   r   r   r   r     s   r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	LoggedErrorsTestsa  
    It is possible for an error generated by a test to be logged I{outside} of
    any test. The log observers constructed by L{TestCase} won't catch these
    errors. Here we try to generate such errors and ensure they are reported to
    a L{TestResult} object.
    c                 C   s   |  t d S r   )ZflushLoggedErrorsr   r   r   r   r   r<     s    zLoggedErrorsTests.tearDownc                 C   s   t  }| | d dS )zg
        Check that we can construct a L{runner.LoggedSuite} and that it
        starts empty.
        r   N)r	   LoggedSuiterA   countTestCases)r   rf   r   r   r   test_construct  s    z LoggedErrorsTests.test_constructc                 C   sl   t  }tt g}|| | t|jd | |jd d 	 tj
 | |jd d t dS )zU
        Chek that a L{LoggedSuite} reports any logged errors to its result.
        r!   r   N)r
   
TestResultr	   r   r   r@   rA   r?   r   idZNOT_IN_TESTrK   Zcheckr   )r   rV   rf   r   r   r   test_capturesError  s    
z$LoggedErrorsTests.test_capturesErrorN)r&   r'   r(   r7   r<   r   r   r   r   r   r   r     s   	r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestHolderTestsc                 C   s   d| _ t| j | _d S Nrr   )rr   r	   Z
TestHolderholderr   r   r   r   rR     s    zTestHolderTests.setUpc                 C   s,   |  | j | j |  | j | j dS )z
        Check that L{runner.TestHolder} takes a description as a parameter
        and that this description is returned by the C{id} and
        C{shortDescription} methods.
        N)rA   r   r   rr   ZshortDescriptionr   r   r   r   test_holder  s    zTestHolderTests.test_holderc                 C   s6   |  | jt| j | tt| jd| jtf  dS )z?
        L{runner.TestHolder} implements L{ITestCase}.
        z5%r claims to provide %r but does not do so correctly.N)rT   r   r   rK   r   r   r   r   r   test_holderImplementsITestCase  s    
z.TestHolderTests.test_holderImplementsITestCasec                 C   s4   t  }| j| | |  | d|j dS )zc
        A L{runner.TestHolder} can run against the standard Python
        C{TestResult}.
        r!   N)re   r   r   r@   rK   r   rA   r9   rU   r   r   r   test_runsWithStandardResult  s    z+TestHolderTests.test_runsWithStandardResultN)r&   r'   r(   rR   r   r   r   r   r   r   r   r     s   
r   c                   @   sP   e Zd ZdZedZG dd deZdd Zdd Z	d	d
 Z
dd Zdd ZdS )ErrorHolderTestsMixinaa  
    This mixin defines test methods which can be applied to a
    L{runner.ErrorHolder} constructed with either a L{Failure} or a
    C{exc_info}-style tuple.

    Subclass this and implement C{setUp} to create C{self.holder} referring to a
    L{runner.ErrorHolder} instance and C{self.error} referring to a L{Failure}
    which the holder holds.
    z"integer division or modulo by zeroc                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )z$ErrorHolderTestsMixin.TestResultStubz)
        Stub for L{TestResult}.
        c                 C   s
   g | _ d S r   )r   r   r   r   r   r     s    z-ErrorHolderTestsMixin.TestResultStub.__init__c                 C   s   d S r   r   r4   r   r   r   r.     s    z.ErrorHolderTestsMixin.TestResultStub.startTestc                 C   s   d S r   r   r4   r   r   r   r1     s    z-ErrorHolderTestsMixin.TestResultStub.stopTestc                 C   s   | j ||f d S r   )r   r#   )r   r5   errorr   r   r   addError   s    z-ErrorHolderTestsMixin.TestResultStub.addErrorN)r&   r'   r(   r7   r   r.   r1   r   r   r   r   r   TestResultStub  s
   r   c                 C   s4   t  }| j| | |  | d|j dS )zd
        A L{runner.ErrorHolder} can run against the standard Python
        C{TestResult}.
        r!   N)re   r   r   r@   rH   r   rA   r9   rU   r   r   r   r   $  s    z1ErrorHolderTestsMixin.test_runsWithStandardResultc                 C   s<   | j | j | | jj| j | jj| jj| jjffg dS )zM
        L{runner.ErrorHolder} adds an error to the result when run.
        N)	r   r@   rV   rA   r   r   typevaluetbr   r   r   r   test_run/  s
    zErrorHolderTestsMixin.test_runc                 C   s:   |  | j | | jj| j | jj| jj| jjffg dS )zP
        L{runner.ErrorHolder} adds an error to the result when called.
        N)r   rV   rA   r   r   r   r   r   r   r   r   r   	test_call9  s
    zErrorHolderTestsMixin.test_callc                 C   s   |  | j d dS )zH
        L{runner.ErrorHolder.countTestCases} always returns 0.
        r   N)rA   r   r   r   r   r   r   test_countTestCasesC  s    z)ErrorHolderTestsMixin.test_countTestCasesc                 C   s,   d t| jjd }| t| j| dS )zh
        L{runner.ErrorHolder.__repr__} returns a string describing the error it
        holds.
        z0<ErrorHolder description='description' error={}>r!   N)formatreprr   r   rA   )r   Zexpectedr   r   r   	test_reprJ  s
    zErrorHolderTestsMixin.test_reprN)r&   r'   r(   r7   ZeroDivisionErrorexceptionForTestsobjectr   r   r   r   r   r   r   r   r   r   r     s   	

r   c                   @   s   e Zd ZdZdd ZdS )FailureHoldingErrorHolderTestsz
    Tests for L{runner.ErrorHolder} behaving similarly to L{runner.TestHolder}
    when constructed with a L{Failure} representing its error.
    c                 C   sP   d| _ z
| jW n tk
r.   t | _Y nX t| j | j| _| 	 | _
d S r   )rr   r   r   r   r   r   r	   ErrorHolderr   r   rV   r   r   r   r   rR   ]  s    
z$FailureHoldingErrorHolderTests.setUpNrZ   r   r   r   r   r   X  s   r   c                   @   s   e Zd ZdZdd ZdS )ExcInfoHoldingErrorHolderTestsz
    Tests for L{runner.ErrorHolder} behaving similarly to L{runner.TestHolder}
    when constructed with a C{exc_info}-style tuple representing its error.
    c                 C   sV   d| _ z
| jW n& tk
r6   t }t | _Y nX t	| j || _
|  | _d S r   )rr   r   r   sysexc_infor   r   r   r	   r   r   r   rV   )r   ZexceptionInfor   r   r   rR   n  s    
z$ExcInfoHoldingErrorHolderTests.setUpNrZ   r   r   r   r   r   i  s   r   c                   @   sB   e Zd ZdZG dd dejZdd Zdd Zdd	 Z	d
d Z
dS )MalformedMethodTestszR
    Test that trial manages when test methods don't have correct signatures.
    c                   @   s*   e Zd ZdZdd Zdd ZeeZdS )z%MalformedMethodTests.ContainMalformedzV
        This TestCase holds malformed test methods that trial should handle.
        c                 C   s   d S r   r   )r   Zblahr   r   r   ra     s    z.MalformedMethodTests.ContainMalformed.test_fooc                   C   s   d S r   r   r   r   r   r   test_bar  s    z.MalformedMethodTests.ContainMalformed.test_barN)	r&   r'   r(   r7   ra   r   r   ZinlineCallbacks	test_spamr   r   r   r   ContainMalformed  s   r   c                 C   s\   t  }tjtj|d}t|}||}| |j	d | 
|  | t|jd dS )zL
        Wrapper for one of the test method of L{ContainMalformed}.
        rO   r!   N)r   r	   rQ   r
   r   r   r   r@   rA   r9   rH   r   r?   r   )r   r0   r,   trialRunnerr5   rV   r   r   r   _test  s    

zMalformedMethodTests._testc                 C   s   |  d dS )zE
        Test when the method has extra (useless) arguments.
        ra   Nr   r   r   r   r   test_extraArg  s    z"MalformedMethodTests.test_extraArgc                 C   s   |  d dS )zJ
        Test when the method doesn't have even self as argument.
        r   Nr  r   r   r   r   
test_noArg  s    zMalformedMethodTests.test_noArgc                 C   s   |  d dS )z5
        Test a decorated method also fails.
        r   Nr  r   r   r   r   test_decorated  s    z#MalformedMethodTests.test_decoratedN)r&   r'   r(   r7   re   rl   r   r   r  r  r  r   r   r   r   r   {  s   
r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	DestructiveTestSuiteTestsz2
    Test for L{runner.DestructiveTestSuite}.
    c                    sn   g  G  fdddt j}|d}t }t|g}|  g  || |  dg | | d dS )zL
        Thes destructive test suite should run the tests normally.
        c                       s   e Zd Z fddZdS )z6DestructiveTestSuiteTests.test_basic.<locals>.MockTestc                    s     d d S r   r   rj   Zcalledr   r   ra     s    z?DestructiveTestSuiteTests.test_basic.<locals>.MockTest.test_fooNrb   r   r  r   r   rk     s   rk   ra   Tr   N)	re   rl   r
   r   r	   DestructiveTestSuiterA   r@   r   r   rk   r5   rV   rf   r   r  r   
test_basic  s    
z$DestructiveTestSuiteTests.test_basicc                    st   g  G  fdddt j}t }t }tj|_||}| 	 g  |
| | 	 dg | 	| d dS )zu
        Test the C{shouldStop} management: raising a C{KeyboardInterrupt} must
        interrupt the suite.
        c                       s,   e Zd Z fddZdd Z fddZdS )z;DestructiveTestSuiteTests.test_shouldStop.<locals>.MockTestc                    s     d d S )Nr!   r   rj   r  r   r   	test_foo1  s    zEDestructiveTestSuiteTests.test_shouldStop.<locals>.MockTest.test_foo1c                 S   s
   t  d S r   )KeyboardInterruptrj   r   r   r   	test_foo2  s    zEDestructiveTestSuiteTests.test_shouldStop.<locals>.MockTest.test_foo2c                    s     d d S )NrF   r   rj   r  r   r   	test_foo3  s    zEDestructiveTestSuiteTests.test_shouldStop.<locals>.MockTest.test_foo3N)r&   r'   r(   r
  r  r  r   r  r   r   rk     s   rk   r!   N)r   rl   r
   r   r	   r   r  ZsuiteFactoryZ	loadClassrA   r@   r   )r   rk   rV   r   rf   r   r  r   test_shouldStop  s    

z)DestructiveTestSuiteTests.test_shouldStopc                 C   s\   G dd dt j}|d}t }t|g}| | d || | | d dS )zn
        Checks that the test suite cleanups its tests during the run, so that
        it ends empty.
        c                   @   s   e Zd Zdd ZdS )z8DestructiveTestSuiteTests.test_cleanup.<locals>.MockTestc                 S   s   d S r   r   rj   r   r   r   ra     s    zADestructiveTestSuiteTests.test_cleanup.<locals>.MockTest.test_fooNrb   r   r   r   r   rk     s   rk   ra   r!   r   N)	re   rl   r
   r   r	   r  rA   r   r@   r  r   r   r   test_cleanup  s    
z&DestructiveTestSuiteTests.test_cleanupN)r&   r'   r(   r7   r	  r  r  r   r   r   r   r    s   r  c                   @   s&   e Zd ZG dd dejZdd ZdS )RunnerDeprecationTestsc                   @   s<   e Zd ZdZdZdZdZdd Zdd Zdd Z	d	d
 Z
dS )z#RunnerDeprecationTests.FakeReporterz
        Fake reporter that does *not* implement done() but *does* implement
        printErrors, separator, printSummary, stream, write and writeln
        without deprecations.
        Nc                 G   s   d S r   r   r   r   r   r   printErrors  s    z/RunnerDeprecationTests.FakeReporter.printErrorsc                 G   s   d S r   r   r   r   r   r   printSummary  s    z0RunnerDeprecationTests.FakeReporter.printSummaryc                 G   s   d S r   r   r   r   r   r   write  s    z)RunnerDeprecationTests.FakeReporter.writec                 G   s   d S r   r   r   r   r   r   writeln  s    z+RunnerDeprecationTests.FakeReporter.writeln)r&   r'   r(   r7   r6   r8   r,   r  r  r  r  r   r   r   r   FakeReporter  s   r  c                    s   t d|    fdd_fdd}|  | | jg}| |d d t | |d d d	t	 j
  | t|d d
  | t|d dS )zk
        The runner emits a warning if it is using a result that doesn't
        implement 'done'.
        Nc                      s    S r   r   r   )rV   r   r   <lambda>	      zBRunnerDeprecationTests.test_reporterDeprecations.<locals>.<lambda>c                      s     td d S )Nr   )r@   re   rl   r   )r   r   r   f
  s    z;RunnerDeprecationTests.test_reporterDeprecations.<locals>.fr   categorymessagezR%s should implement done() but doesn't. Falling back to printErrors() and friends.filenamer!   )r	   rQ   r  rS   ZflushWarningstest_reporterDeprecationsrA   DeprecationWarningr   r{   r   rK   __file__
startswithr?   )r   r  warningsr   )rV   r   r   r    s    

z0RunnerDeprecationTests.test_reporterDeprecationsN)r&   r'   r(   r
   r   r  r  r   r   r   r   r    s   r  c                   @   s   e Zd ZdZdd ZdS )QualifiedNameWalkerTestsz<
    Tests for L{twisted.trial.runner._qualNameWalker}.
    c                 C   s6   t td}| |dg fddgfdddgfg dS )z
        C{_qualNameWalker} is a generator that, when given a Python qualified
        name, yields that name, and then the parent of that name, and so forth,
        along with a list of the tried components, in a 2-tuple.
        zwalker.texas.rangerzwalker.texasZrangerZwalkerZtexasN)rt   r	   Z_qualNameWalkerrA   )r   ZwalkerResultsr   r   r   test_walksDownPath   s    
z+QualifiedNameWalkerTests.test_walksDownPathN)r&   r'   r(   r7   r"  r   r   r   r   r!    s   r!  c                   @   s   e Zd ZdZdd ZdS )TrialMainDoesNothingTestszl
    Importing L{twisted.trial.__main__} will not run the script
    unless it is actually C{__main__}.
    c                 C   s   t d dS )zO
        If we import L{twisted.trial.__main__}, it should do nothing.
        ztwisted.trial.__main__N)
__import__r   r   r   r   test_importDoesNothing4  s    z0TrialMainDoesNothingTests.test_importDoesNothingN)r&   r'   r(   r7   r%  r   r   r   r   r#  /  s   r#  )CZ
__future__r   r   r   r   r   Zzope.interfacer   Zzope.interface.verifyr   Ztwisted.trial.itrialr   r   Ztwisted.trialr   r	   r
   r   Ztwisted.trial._asyncrunnerr   Ztwisted.pythonr   r   r   Ztwisted.python.filepathr   Ztwisted.python.reflectr   Ztwisted.python.compatr   Ztwisted.scriptsr   Ztwisted.pluginsr   Ztwistedr   Ztwisted.internetr   r$  re   r   r   r)   r:   rd   rN   rX   r[   r`   ri   rm   rn   r   r   r   r]   r   r   r   r   r   r   r   r  r  r!  r#  r   r   r   r   <module>   s\   :=1  ,K"(Q.?3