o
    b                     @   s  d dl 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
 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mZmZ d d
lmZ d dlmZ d dl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% G dd dZ&e
e$G dd dZ'G dd dZ(G dd de(ej)Z*G dd de(ej)Z+G dd dZ,G dd de,ej)Z-G dd de,ej)Z.G d d! d!e,ej)Z/G d"d# d#ej)Z0G d$d% d%ej)Z1G d&d' d'ej)Z2G d(d) d)e2Z3G d*d+ d+ej4Z5G d,d- d-ej)Z6G d.d/ d/ej)Z7G d0d1 d1Z8G d2d3 d3e8e7Z9G d4d5 d5e8e7Z:G d6d7 d7ej)Z;G d8d9 d9ej)Z<G d:d; d;ej)Z=G d<d= d=ej)Z>G d>d? d?ej)Z?dS )@    N)StringIO)List)implementer)verifyObject)plugin)defer)twisted_trial)failurelogreflect)FilePathnamedAny)trial)reporterrunnerunittestutil) _ForceGarbageCollectionDecorator)	IReporter	ITestCasec                   @   s   e Zd Zdd Zdd ZdS )CapturingDebuggerc                 C   
   g | _ d S N)_callsself r   @/usr/lib/python3/dist-packages/twisted/trial/test/test_runner.py__init__      
zCapturingDebugger.__init__c                 O   s*   | j d |d |dd  i | 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                   @   s   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d Zdd ZdddZd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   
shouldStop_stream	_tbformat	_rterrors
_publisher)r   streamtbformatrterrors	publisherr   r   r   r   3   s   
zCapturingReporter.__init__c                 C      | 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   r5   >      zCapturingReporter.startTestc                 C   r4   )z}
        Report the status of a single test method
        @param method: an object that is adaptable to ITestMethod
        stopTestNr#   r6   r   r   r   r9   E   r8   zCapturingReporter.stopTestc                 C   r4   )zcalled when the reactor has been left in a 'dirty' state
        @param errs: a list of L{twisted.python.failure.Failure}s
        cleanupErrorNr#   )r   errsr   r   r   cleanupErrorsL      zCapturingReporter.cleanupErrorsc                 C   s   | j d d S )N
addSuccessr#   r   testr   r   r   r>   R      zCapturingReporter.addSuccessc                 C      dS )z@
        Do nothing. These tests don't care about done.
        Nr   r   r   r   r   doneU       zCapturingReporter.donec                 C   rB   )zD
        Do nothing. These tests don't care about addError.
        Nr   r   r@   errorr   r   r   addErrorZ   rD   zCapturingReporter.addErrorc                 C   rB   )zF
        Do nothing. These tests don't care about addFailure.
        Nr   )r   r@   r	   r   r   r   
addFailure_   rD   zCapturingReporter.addFailurec                 C   rB   )zN
        Do nothing. These tests don't care about addExpectedFailure.
        Nr   )r   r@   r	   todor   r   r   addExpectedFailured   rD   z$CapturingReporter.addExpectedFailurec                 C   rB   )zP
        Do nothing. These tests don't care about addUnexpectedSuccess.
        Nr   )r   r@   rI   r   r   r   addUnexpectedSuccessi   rD   z&CapturingReporter.addUnexpectedSuccessc                 C   rB   )zC
        Do nothing. These tests don't care about addSkip.
        Nr   )r   r@   reasonr   r   r   addSkipn   rD   zCapturingReporter.addSkipc                 C   rB   )zI
        Do nothing. These tests don't care about wasSuccessful.
        Nr   r   r   r   r   wasSuccessfuls   rD   zCapturingReporter.wasSuccessful)NNNNr   )r'   r(   r)   __doc__r0   r1   r%   	separatortestsRunr   r5   r9   r<   r>   rC   rG   rH   rJ   rK   rM   rN   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   tearDown~      zTrialRunnerTestsMixin.tearDownc                 C   rB   )z=
        Empty test method, used by the other tests.
        Nr   r   r   r   r   
test_empty   rD   z TrialRunnerTestsMixin.test_emptyc                 C   s   t jjS r   )r
   theLogPublisher	observersr   r   r   r   _getObservers   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)lenrY   r   runr@   assertEqual)r   originalCountnewCountr   r   r   test_addObservers   s   z'TrialRunnerTestsMixin.test_addObserversc                    sl   j jg   fdd}|j _j j j j t d  d  d u d dS )z=
        Test that a new file is opened on each run.
        c                             jj d S r   )r$   r   _logFileObserverr   loldSetUpLogFiler   r   r   setUpLogFile      zDTrialRunnerTestsMixin.test_logFileAlwaysActive.<locals>.setUpLogFile   r   r"   z'Should have created a new file observerN)r   _setUpLogFiler[   r@   r\   rZ   assertFalser   re   r   rb   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                      r`   r   )r$   r   _logFileObjectr   rb   r   r   re      rf   zBTrialRunnerTestsMixin.test_logFileGetsClosed.<locals>.setUpLogFiler"   r   N)r   rh   r[   r@   r\   rZ   
assertTrueclosedrj   r   rb   r   test_logFileGetsClosed   s   z,TrialRunnerTestsMixin.test_logFileGetsClosedN)
r'   r(   r)   rO   rT   rV   rY   r_   rk   ro   r   r   r   r   rR   y   s    
rR   c                   @       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r0   rV   r   r0   r   TrialRunnerr*   rq   r@   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)   rO   ru   rz   r   r   r   r   rq      s    rq   c                   @      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r0   uncleanWarningsrV   rs   r   r   r   r   ru      s
   z1TrialRunnerWithUncleanWarningsReporterTests.setUpNr'   r(   r)   rO   ru   r   r   r   r   r|      s    r|   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 )Nrr   )r
   r   r0   r   rt   r*   DRY_RUNmakeTestFixturesr   r   r   r   ru      s   zDryRunMixin.setUpc                 C   rB   )zh
        Set C{self.test} and C{self.suite}, where C{self.suite} is an empty
        TestSuite.
        Nr   r   r   r   r   r      rD   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[   	TestSuiter\   r   rx   r   r   r   rV      s   zDryRunMixin.test_emptyc                 C   s$   | j | j}| |jg d dS )z
        If we are running a single test, check the reporter starts, passes and
        then stops the test during a dry run.
        )r5   r>   r9   N)r   r[   r@   r\   r   rx   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[   r@   r\   r
   r   r   r   r   test_testsNotRun   s   zDryRunMixin.test_testsNotRunN)	r'   r(   r)   rO   ru   r   rV   r   r   r   r   r   r   r      s    r   c                   @   r{   )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                   @      e Zd Zdd ZdS )z;SynchronousDryRunTests.makeTestFixtures.<locals>.PyunitCasec                 S      d S r   r   r   r   r   r   test_foo     zDSynchronousDryRunTests.makeTestFixtures.<locals>.PyunitCase.test_fooNr'   r(   r)   r   r   r   r   r   
PyunitCase      r   r   )r   SynchronousTestCaser@   pyunitr   suiter   r   r   r   r   r   
     
z'SynchronousDryRunTests.makeTestFixturesNr'   r(   r)   rO   r   r   r   r   r   r         r   c                   @   r{   )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                          e Zd Z fddZdS )z.DryRunTests.makeTestFixtures.<locals>.MockTestc                    s    j d d S )Nr   )r
   r$   r@   r   r   r   r     rA   z7DryRunTests.makeTestFixtures.<locals>.MockTest.test_fooNr   r   r   r   r   MockTest      r   r   )r   TestCaser@   r   r   r   )r   r   r   r   r   r     s   
zDryRunTests.makeTestFixturesNr   r   r   r   r   r     r   r   c                   @   r{   )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                   @   r   )z6PyUnitDryRunTests.makeTestFixtures.<locals>.PyunitCasec                 S   r   r   r   r   r   r   r   r   (  r   z?PyUnitDryRunTests.makeTestFixtures.<locals>.PyunitCase.test_fooNr   r   r   r   r   r   '  r   r   r   )r   r   r@   r   r   r   r   r   r   r   &  r   z"PyUnitDryRunTests.makeTestFixturesNr   r   r   r   r   r   !  r   r   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                    sv   t  _ttd}d|d d }|d }tj	d|ddd |dg  fdd}t
j_|t
_g d	_d S )
N.zTest Helper ReporterzUtility for unit testing.	capturing)descriptionlongOptshortOptklassc                    s,    | t  tj| g|R i | S r   )r\   r   listoriginal)ifaceakwpluginsr   r   r   
getPluginsE  s    z%RunnerTests.setUp.<locals>.getPlugins)r5   r>   r9   r5   r>   r9   r5   r>   r9   r5   r>   r9   r5   r>   r9   r5   r>   r9   r5   r>   r9   r5   r>   r9   r5   r>   r9   r5   r>   r9   )r   Optionsconfigr   qualr*   splitjoinr   	_Reporterr   r   r   standardReport)r   partspackager   r   r   r   r   ru   0  s"   
zRunnerTests.setUpc                 C   s   | j t_d S r   )r   r   r   r   r   r   r   rT   m  s   zRunnerTests.tearDownc                 C   s   | j | d S r   )r   parseOptionsr   r%   r   r   r   r   p  rA   zRunnerTests.parseOptionsc                 C   s.   t | j}t |_| |j t |_	|S r   )
r   _makeRunnerr   r   r0   
addCleanuprS   r
   LogPublisher_log)r   rr   r   r   	getRunners  s
   
zRunnerTests.getRunnerc                 C   s2   |  g  | jd }|  }| || j d S Nr   )r   r   r   r\   rv   	__class__)r   ry   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   rv   r\   r   r   r   r   ry   r   r   r   test_runner_get_result  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   assertNotIsInstancerv   r   UncleanWarningsReporterWrapperr   r   r   r   r    test_uncleanWarningsOffByDefault  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   assertIsInstancerv   r   r   r   r   r   r   test_getsUncleanWarnings  s
   
z$RunnerTests.test_getsUncleanWarningsc                 C   s(   |  ddg |  }| |jd d S )N--temp-directory	some_path)r   r   r\   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 }|rN| |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   subsequentDirectoryConcurrentCase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     r=   zWRunnerTests.test_concurrentImplicitWorkingDirectory.<locals>.ConcurrentCase.test_secondN)r'   r(   r)   r   r   r   r   r   r   r     s    r   r   r   r"   _trial_tempz_trial_temp-1)r   r   N)r   r   r   r   r   r   mktempmakedirsr   r   r   r   r[   errorsfailuresfailr\   child)r   firstRunnerry   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 }|rF| |d d  dS 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)assertRaisesr   _WorkingDirectoryBusyr[   r   r   r   r   r   test_concurrent  s
   z[RunnerTests.test_concurrentExplicitWorkingDirectory.<locals>.ConcurrentCase.test_concurrentc                 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        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   r   r[   r   r   r   )r   initialDirectoryr   ry   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   
--reporterr   twisted.trial.test.sampleT)
r   r   r   r   
TestLoader
loadByNamer[   r\   r   r   )r   	my_runnerloaderr   ry   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   r   r   r   r   runSampleSuite  s   
zRunnerTests.runSampleSuitec                 C   sP   |  g d |  }t  }|_| |}| | j|j | dg|j dS )zL
        Trial uses its debugger if the `--debug` option is passed.
        )r   r   --debugr   r!   N)r   r   r   debuggerr   r\   r   r   )r   r   r   ry   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   pdbrcFc                    
   d _ d S NTruncall_calledpdbr   ry   r   r   r   r!   5  r    z=RunnerTests.test_runnerDebuggerDefaultsToPdb.<locals>.runcallr!   N)
r   r   touchr   patchr   Pdbr   r   rm   )r   	pdbrcFiler!   r   r   r    test_runnerDebuggerDefaultsToPdb+  s   z,RunnerTests.test_runnerDebuggerDefaultsToPdbc                    sN     g d d _ fdd} tjd|       j dS )zU
        Trial uses pdb if pdb is passed explicitly to the `--debugger` arg.
        )r   r   
--debuggerr   r   r   Fc                    r   r   r   r   r   r   r   r!   O  r    zGRunnerTests.test_runnerDebuggerWithExplicitlyPassedPdb.<locals>.runcallr!   N)r   r   r  r   r  r   r   rm   )r   r!   r   r   r   *test_runnerDebuggerWithExplicitlyPassedPdb>  s   z6RunnerTests.test_runnerDebuggerWithExplicitlyPassedPdbc                 C   sF   |  g d |  }| |}| | j|j | dg|jj dS )zM
        Trial uses specified debugger if the debugger is available.
        )r   r   r  z4twisted.trial.test.test_runner.RunnerTests.cdebuggerr   r   r!   N)r   r   r   r\   r   r   r   )r   r   ry   r   r   r   test_runnerDebuggerZ  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   rv   r   r   _ExitWrapperr   r   r   r   test_exitfirstm  s   zRunnerTests.test_exitfirstN)r'   r(   r)   ru   rT   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r   	cdebuggerr  r	  r   r   r   r   r   /  s(    =9$r   c                   @   r   )TrialSuiteTestsc                 C   s   t d d S )Nztwisted.trial.runner.TrialSuiter   r   r   r   r   test_importsy  s   zTrialSuiteTests.test_importsN)r'   r(   r)   r  r   r   r   r   r  x  r   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U dZg Zed ed< dd ZdS )zUntilFailureTests.FailAfterzC
        A test case that fails when run 3 times in a row.
        Ncountc                 C   s,   | j d  t| j dkr| d d S d S )N   zCount reached 3)r  r$   rZ   r   r   r   r   r   r     s   z$UntilFailureTests.FailAfter.test_foo)r'   r(   r)   rO   r  r   __annotations__r   r   r   r   r   	FailAfter  s   
 r  c                 C   s4   g t j_t d| _t | _tjtj	| jd| _d S )Nr   rr   )
r  r  r  r@   r   r0   r   rt   r   Reporterr   r   r   r   ru     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   runUntilFailurer@   r\   rQ   ri   rN   _getFailuresrx   r   r   r   test_runUntilFailure  s   z&UntilFailureTests.test_runUntilFailurec                 C   s
   t |jS )zL
        Get the number of failures that were reported to a result.
        )rZ   r   rx   r   r   r   r    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                         | |f | S r   r$   r@   	interface	decoratedr   r   decorate     z@UntilFailureTests.test_runUntilFailureDecorate.<locals>.decorater  r"   N)	r  r   r   r  r@   r\   rQ   rZ   r   r   r  ry   r   r  r   test_runUntilFailureDecorate  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                    r  r   r  r  r  r   r   r    r  zGUntilFailureTests.test_runUntilFailureForceGCDecorate.<locals>.decorater  Tr"   rg   N)r  r   r   _forceGarbageCollectionr  r@   r\   rQ   rZ   r   r   r  r   r  r   #test_runUntilFailureForceGCDecorate  s   z5UntilFailureTests.test_runUntilFailureForceGCDecorateN)r'   r(   r)   r   r   r  ru   r  r  r  r!  r   r   r   r   r  ~  s    
r  c                   @   rp   )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 )NTr}   )r  ru   r   rt   r   r  r0   r   r   r   r   ru     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.
        )rZ   _originalReporterr   rx   r   r   r   r       z%UncleanUntilFailureTests._getFailuresN)r'   r(   r)   rO   ru   r  r   r   r   r   r"    s    r"  c                   @   r{   )BreakingSuitez;
    A L{TestSuite} that logs an error when it is run.
    c                 C   s,   zt d t y   tt  Y d S w )Nz#error that occurs outside of a test)RuntimeErrorr
   errr	   Failurerx   r   r   r   r[     s
   zBreakingSuite.runN)r'   r(   r)   rO   r[   r   r   r   r   r%    r   r%  c                   @   (   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   )flushLoggedErrorsr&  r   r   r   r   rT     rU   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   LoggedSuiter\   countTestCases)r   r   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[   r\   rZ   r   idNOT_IN_TESTrm   checkr&  )r   ry   r   r   r   r   test_capturesError  s   
z$LoggedErrorsTests.test_capturesErrorN)r'   r(   r)   rO   rT   r.  r3  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 Nr   )r   r   
TestHolderholderr   r   r   r   ru   
  rf   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)r\   r7  r0  r   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)rw   r7  r   rm   r   r   r   r   r   test_holderImplementsITestCase  s   
z.TestHolderTests.test_holderImplementsITestCasec                 C   4   t  }| j| | |  | d|j dS )zc
        A L{runner.TestHolder} can run against the standard Python
        C{TestResult}.
        r"   N)r   r/  r7  r[   rm   rN   r\   rQ   rx   r   r   r   test_runsWithStandardResult"     z+TestHolderTests.test_runsWithStandardResultN)r'   r(   r)   ru   r9  r:  r<  r   r   r   r   r4  	  s
    	r4  c                   @   sN   e Zd ZdZedZG dd d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   r   r   )r   r   r   r   r   r   ?  r    z-ErrorHolderTestsMixin.TestResultStub.__init__c                 C   r   r   r   r?   r   r   r   r5   B  r   z.ErrorHolderTestsMixin.TestResultStub.startTestc                 C   r   r   r   r?   r   r   r   r9   E  r   z-ErrorHolderTestsMixin.TestResultStub.stopTestc                 C   s   | j ||f d S r   )r   r$   rE   r   r   r   rG   H  s   z-ErrorHolderTestsMixin.TestResultStub.addErrorN)r'   r(   r)   rO   r   r5   r9   rG   r   r   r   r   TestResultStub:  s    r?  c                 C   r;  )zd
        A L{runner.ErrorHolder} can run against the standard Python
        C{TestResult}.
        r"   N)r   r/  r7  r[   ri   rN   r\   rQ   rx   r   r   r   r<  K  r=  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)	r7  r[   ry   r\   r   rF   typevaluetbr   r   r   r   test_runU  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)r7  ry   r\   r   rF   r@  rA  rB  r   r   r   r   	test_call_  s
   zErrorHolderTestsMixin.test_callc                 C   s   |  | j d dS )zH
        L{runner.ErrorHolder.countTestCases} always returns 0.
        r   N)r\   r7  r-  r   r   r   r   test_countTestCasesi  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reprr7  rF   r\   )r   expectedr   r   r   	test_repro  s   zErrorHolderTestsMixin.test_reprN)r'   r(   r)   rO   ZeroDivisionErrorexceptionForTestsr?  r<  rC  rD  rE  rI  r   r   r   r   r>  -  s    



r>  c                   @   r{   )FailureHoldingErrorHolderTestsz
    Tests for L{runner.ErrorHolder} behaving similarly to L{runner.TestHolder}
    when constructed with a L{Failure} representing its error.
    c                 C   sJ   d| _ z| j ty   t | _Y nw t| j | j| _| 	 | _
d S r5  )r   rK  rJ  r	   r(  rF   r   ErrorHolderr7  r?  ry   r   r   r   r   ru     s   z$FailureHoldingErrorHolderTests.setUpNr   r   r   r   r   rL  {      rL  c                   @   r{   )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   sP   d| _ z| j ty   t }t | _Y nw t	| j || _
|  | _d S r5  )r   rK  rJ  sysexc_infor	   r(  rF   r   rM  r7  r?  ry   )r   exceptionInfor   r   r   ru     s   z$ExcInfoHoldingErrorHolderTests.setUpNr   r   r   r   r   rO    rN  rO  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   r   r   r   )r   blahr   r   r   r     r   z.MalformedMethodTests.ContainMalformed.test_fooc                   C   r   r   r   r   r   r   r   test_bar  r   z.MalformedMethodTests.ContainMalformed.test_barN)	r'   r(   r)   rO   r   rU  r   inlineCallbacks	test_spamr   r   r   r   ContainMalformed  s
    rX  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}.
        rr   r"   N)r   r   rt   r   r  rS  rX  r[   r\   rQ   ri   rN   rZ   r   )r   r7   r0   trialRunnerr@   ry   r   r   r   _test  s   

zMalformedMethodTests._testc                 C   r   )zE
        Test when the method has extra (useless) arguments.
        r   NrZ  r   r   r   r   test_extraArg  r   z"MalformedMethodTests.test_extraArgc                 C   r   )zJ
        Test when the method doesn't have even self as argument.
        rU  Nr[  r   r   r   r   
test_noArg  r   zMalformedMethodTests.test_noArgc                 C   r   )z5
        Test a decorated method also fails.
        rW  Nr[  r   r   r   r   test_decorated  r   z#MalformedMethodTests.test_decoratedN)r'   r(   r)   rO   r   r   rX  rZ  r\  r]  r^  r   r   r   r   rS    s    rS  c                   @   r)  )	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                       r   )z6DestructiveTestSuiteTests.test_basic.<locals>.MockTestc                         d d S r   r  r   calledr   r   r     rU   z?DestructiveTestSuiteTests.test_basic.<locals>.MockTest.test_fooNr   r   ra  r   r   r     r   r   r   Tr   N)	r   r   r   r/  r   DestructiveTestSuiter\   r[   r-  r   r   r@   ry   r   r   ra  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                    r`  )Nr"   r  r   ra  r   r   	test_foo1  rU   zEDestructiveTestSuiteTests.test_shouldStop.<locals>.MockTest.test_foo1c                 S   s   t  r   )KeyboardInterruptr   r   r   r   	test_foo2  s   zEDestructiveTestSuiteTests.test_shouldStop.<locals>.MockTest.test_foo2c                    r`  )Nrg   r  r   ra  r   r   	test_foo3  rU   zEDestructiveTestSuiteTests.test_shouldStop.<locals>.MockTest.test_foo3N)r'   r(   r)   rf  rh  ri  r   ra  r   r   r     s    r   r"   N)r   r   r   r/  r   r   rc  suiteFactory	loadClassr\   r[   r-  )r   r   ry   r   r   r   ra  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                   @   r   )z8DestructiveTestSuiteTests.test_cleanup.<locals>.MockTestc                 S   r   r   r   r   r   r   r   r     r   zADestructiveTestSuiteTests.test_cleanup.<locals>.MockTest.test_fooNr   r   r   r   r   r     r   r   r   r"   r   N)	r   r   r   r/  r   rc  r\   r-  r[   rd  r   r   r   test_cleanup  s   
z&DestructiveTestSuiteTests.test_cleanupN)r'   r(   r)   rO   re  rl  rm  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   r   r   r   r   r   r   r   printErrors  r   z/RunnerDeprecationTests.FakeReporter.printErrorsc                 G   r   r   r   r   r   r   r   printSummary"  r   z0RunnerDeprecationTests.FakeReporter.printSummaryc                 G   r   r   r   r   r   r   r   write%  r   z)RunnerDeprecationTests.FakeReporter.writec                 G   r   r   r   r   r   r   r   writeln(  r   z+RunnerDeprecationTests.FakeReporter.writeln)r'   r(   r)   rO   rC   rP   r0   ro  rp  rq  rr  r   r   r   r   FakeReporter  s    rs  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   )ry   r   r   <lambda>2  rD   zBRunnerDeprecationTests.test_reporterDeprecations.<locals>.<lambda>c                      s     td d S )Nr0  )r[   r   r   r   )rY  r   r   f4  s   z;RunnerDeprecationTests.test_reporterDeprecations.<locals>.fr   categorymessagezR%s should implement done() but doesn't. Falling back to printErrors() and friends.filenamer"   )r   rt   rs  rv   flushWarningstest_reporterDeprecationsr\   DeprecationWarningr   r   r   rm   __file__
startswithrZ   )r   ru  warningsr   )ry   rY  r   rz  +  s   


z0RunnerDeprecationTests.test_reporterDeprecationsN)r'   r(   r)   r   r  rs  rz  r   r   r   r   rn    s    rn  c                   @   r{   )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.texasrangerwalkertexasN)r   r   _qualNameWalkerr\   )r   walkerResultsr   r   r   test_walksDownPathK  s   
z+QualifiedNameWalkerTests.test_walksDownPathN)r'   r(   r)   rO   r  r   r   r   r   r  F  r   r  c                   @   r{   )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_importDoesNothingc  r$  z0TrialMainDoesNothingTests.test_importDoesNothingN)r'   r(   r)   rO   r  r   r   r   r   r  ]  rN  r  )@r   r   rP  r   r   ior   typingr   zope.interfacer   zope.interface.verifyr   twistedr   twisted.internetr   twisted.pluginsr   twisted.pythonr	   r
   r   twisted.python.filepathr   twisted.python.reflectr   twisted.scriptsr   twisted.trialr   r   r   twisted.trial._asyncrunnerr   twisted.trial.itrialr   r   r   r*   rR   r   rq   r|   r   r   r   r   r   r  r  r"  r   r%  r*  r4  r>  rL  rO  rS  r_  rn  r  r  r   r   r   r   <module>   s^   	Q<
,  KL$N1D3