U
    
W[&                     @   sj  d Z ddlmZmZ ddlZddl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mZ ddlmZ dd	lmZ dd
lmZmZmZ G dd deeZG dd deZG dd dejZG dd dejZG dd dejZ G dd dejZ!G dd dejZ"G dd dejZ#G dd dejZ$G dd dejZ%ee%j&dd  Z'ee%j&d!d"d#d$ Z(dS )%a  
Tests for assertions provided by C{SynchronousTestCase} and C{TestCase},
provided by L{twisted.trial.unittest}.

L{TestFailureTests} demonstrates that L{SynchronousTestCase.fail} works, so that
is the only method on C{twisted.trial.unittest.SynchronousTestCase} that is
initially assumed to work.  The test classes are arranged so that the methods
demonstrated to work earlier in the file are used by those later in the file
(even though the runner will probably not run the tests in this order).
    )divisionabsolute_importN)FancyEqMixin)prefixedMethodsaccumulateMethodsfullyQualifiedName
deprecated)VersiongetVersionString)Failure)unittest)Deferredfailsucceedc                   @   s    e Zd ZdZdd Zdd ZdS )MockEqualitynamec                 C   s
   || _ d S Nr   )selfr    r   D/usr/lib/python3/dist-packages/twisted/trial/test/test_assertions.py__init__    s    zMockEquality.__init__c                 C   s   d| j f S )NzMockEquality(%s)r   r   r   r   r   __repr__$   s    zMockEquality.__repr__N)__name__
__module____qualname__ZcompareAttributesr   r   r   r   r   r   r      s   r   c                   @   s    e Zd ZdZdd Ze ZZdS )ComparisonErrorzH
    An object which raises exceptions from its comparison methods.
    c                 C   s   t dd S )NzComparison is broken)
ValueError)r   otherr   r   r   _error,   s    zComparisonError._errorN)r   r   r   __doc__r!   __eq____ne__r   r   r   r   r   (   s   r   c                   @   s:   e Zd ZdZG dd dejZdd Zdd Zdd	 Z	d
S )TestFailureTestsa  
    Tests for the most basic functionality of L{SynchronousTestCase}, for
    failing tests.

    This class contains tests to demonstrate that L{SynchronousTestCase.fail}
    can be used to fail a test, and that that failure is reflected in the test
    result object.  This should be sufficient functionality so that further
    tests can be built on L{SynchronousTestCase} instead of
    L{unittest.TestCase}.  This depends on L{unittest.TestCase} working.
    c                   @   s   e Zd Zdd ZdS )zTestFailureTests.FailingTestc                 C   s   |  d d S )NzThis test fails.r   r   r   r   r   
test_fails?   s    z'TestFailureTests.FailingTest.test_failsN)r   r   r   r'   r   r   r   r   FailingTest>   s   r(   c                 C   sD   t | jj | jj g}t | _| jd|| _t	| jd | _
dS )zo
        Load a suite of one test which can be used to exercise the failure
        handling behavior.
        .r   N)r   	__class__r(   pyunitZ
TestLoaderloaderZloadTestsFromNamejoinsuitelisttest)r   Z
componentsr   r   r   setUpC   s      
zTestFailureTests.setUpc              
   C   sV   z| j d W n6 | j jk
rF } z| dt| W 5 d}~X Y nX | d dS )z}
        L{SynchronousTestCase.fail} raises
        L{SynchronousTestCase.failureException} with the given argument.
        ZfailedNzRSynchronousTestCase.fail method did not raise SynchronousTestCase.failureException)r0   r   failureExceptionassertEqualstrr   resultr   r   r   	test_failO   s    "zTestFailureTests.test_failc                 C   s^   t  }| j| | |  | |jg  | t|j	d | |j	d d | j
 dS )z
        When a test method raises L{SynchronousTestCase.failureException}, the test is
        marked as having failed on the L{TestResult}.
           r   N)r+   Z
TestResultr.   runassertFalseZwasSuccessfulr3   errorslenZfailuresr0   r5   r   r   r   test_failingExceptionFails^   s    z+TestFailureTests.test_failingExceptionFailsN)
r   r   r   r"   r   SynchronousTestCaser(   r1   r7   r=   r   r   r   r   r%   3   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 )AssertFalseTestsa  
    Tests for L{SynchronousTestCase}'s C{assertFalse} and C{failIf} assertion
    methods.

    This is pretty paranoid.  Still, a certain paranoia is healthy if you
    are testing a unit testing framework.

    @note: As of 11.2, C{assertFalse} is preferred over C{failIf}.
    c                 C   sB   dddddg fD ],}||d|f }||kr|  d|f  qdS )z
        Perform the positive case test for C{failIf} or C{assertFalse}.

        @param method: The test method to test.
        r           FNr   failed on %rDid not return argument %rr&   )r   methodnotTruer6   r   r   r   _assertFalseFalsev   s    z"AssertFalseTests._assertFalseFalsec                 C   s   dddddgdfD ]r}z||d|f  W nD | j k
rn } z$| d|f t|d||f  W 5 d}~X Y qX | d	|j|f  qdS )
z
        Perform the negative case test for C{failIf} or C{assertFalse}.

        @param method: The test method to test.
        r8   Tcat         rA   $Raised incorrect exception on %r: %rNCall to %s(%r) didn't failr2   assertInr4   r   r   )r   rC   trueer   r   r   _assertFalseTrue   s     
z!AssertFalseTests._assertFalseTruec                 C   s   |  | j dS )zt
        L{SynchronousTestCase.failIf} returns its argument if its argument is
        not considered true.
        N)rE   failIfr   r   r   r   test_failIfFalse   s    z!AssertFalseTests.test_failIfFalsec                 C   s   |  | j dS )zy
        L{SynchronousTestCase.assertFalse} returns its argument if its argument
        is not considered true.
        N)rE   r:   r   r   r   r   test_assertFalseFalse   s    z&AssertFalseTests.test_assertFalseFalsec                 C   s   |  | j dS )z
        L{SynchronousTestCase.failIf} raises
        L{SynchronousTestCase.failureException} if its argument is considered
        true.
        N)rQ   rR   r   r   r   r   test_failIfTrue   s    z AssertFalseTests.test_failIfTruec                 C   s   |  | j dS )z
        L{SynchronousTestCase.assertFalse} raises
        L{SynchronousTestCase.failureException} if its argument is considered
        true.
        N)rQ   r:   r   r   r   r   test_assertFalseTrue   s    z%AssertFalseTests.test_assertFalseTrueN)
r   r   r   r"   rE   rQ   rS   rT   rU   rV   r   r   r   r   r?   l   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 )AssertTrueTestsa  
    Tests for L{SynchronousTestCase}'s C{assertTrue} and C{failUnless} assertion
    methods.

    This is pretty paranoid.  Still, a certain paranoia is healthy if you
    are testing a unit testing framework.

    @note: As of 11.2, C{assertTrue} is preferred over C{failUnless}.
    c                 C   s   dddddg fD ]r}z||d|f  W nD | j k
rl } z$| d|f t|d||f  W 5 d}~X Y qX | d|j|f  qdS )	z
        Perform the negative case test for C{assertTrue} and C{failUnless}.

        @param method: The test method to test.
        r   r@   FNr   rA   rK   rL   rM   )r   rC   rD   rP   r   r   r   _assertTrueFalse   s     
z AssertTrueTests._assertTrueFalsec                 C   sD   dddddgdfD ],}||d|f }||kr|  d|f  qdS )	z
        Perform the positive case test for C{assertTrue} and C{failUnless}.

        @param method: The test method to test.
        r8   TrF   rG   rH   rA   rB   Nr&   )r   rC   rO   r6   r   r   r   _assertTrueTrue   s    zAssertTrueTests._assertTrueTruec                 C   s   |  | j dS )z
        L{SynchronousTestCase.assertTrue} raises
        L{SynchronousTestCase.failureException} if its argument is not
        considered true.
        N)rX   
assertTruer   r   r   r   test_assertTrueFalse   s    z$AssertTrueTests.test_assertTrueFalsec                 C   s   |  | j dS )z
        L{SynchronousTestCase.failUnless} raises
        L{SynchronousTestCase.failureException} if its argument is not
        considered true.
        N)rX   
failUnlessr   r   r   r   test_failUnlessFalse   s    z$AssertTrueTests.test_failUnlessFalsec                 C   s   |  | j dS )zt
        L{SynchronousTestCase.assertTrue} returns its argument if its argument
        is considered true.
        N)rY   rZ   r   r   r   r   test_assertTrueTrue   s    z#AssertTrueTests.test_assertTrueTruec                 C   s   |  | j dS )zt
        L{SynchronousTestCase.failUnless} returns its argument if its argument
        is considered true.
        N)rY   r\   r   r   r   r   test_failUnlessTrue   s    z#AssertTrueTests.test_failUnlessTrueN)
r   r   r   r"   rX   rY   r[   r]   r^   r_   r   r   r   r   rW      s   			rW   c                   @   sj  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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d0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Z d<d= Z!d>d? Z"d@dA Z#dBdC Z$dDdE Z%dFdG Z&dHdI Z'dJdK Z(dLdM Z)dNdO Z*dPdQ Z+e,e-j.dRdSdSkrfdTe+_/dSS )USynchronousAssertionsTestsa  
    Tests for L{SynchronousTestCase}'s assertion methods.  That is, failUnless*,
    failIf*, assert* (not covered by other more specific test classes).

    Note: As of 11.2, assertEqual is preferred over the failUnlessEqual(s)
    variants.  Tests have been modified to reflect this preference.

    This is pretty paranoid.  Still, a certain paranoia is healthy if you are
    testing a unit testing framework.
    c                 C   s"   |  ||}||kr| d d S )Nz)assertEqual should return first parameter)r3   r   )r   firstsecondxr   r   r   _testEqualPair  s    z)SynchronousAssertionsTests._testEqualPairc           	      C   s   d}z|  || W n | jk
r } zrtd}z| || W nR |jk
r } z2d}t|}t|}||kr| d||f  W 5 d}~X Y nX W 5 d}~X Y nX |s| d||f  dS )z
        Assert that when called with unequal arguments, C{assertEqual} raises a
        failure exception with the same message as the standard library
        C{assertEqual} would have raised.
        Fr1   TzExpected: %r; Got: %rNz'Call to assertEqual(%r, %r) didn't fail)r3   r2   r+   TestCaser4   r   )	r   ra   rb   ZraisedZ
ourFailureZcaseZtheirFailureZgotexpectedr   r   r   _testUnequalPair  s"    
6
z+SynchronousAssertionsTests._testUnequalPairc                 C   s:   |  dd | dd |  dgdg | dgd d S )NrF   Zdogr8   orange)rd   rg   r   r   r   r   test_assertEqual_basic1  s    z1SynchronousAssertionsTests.test_assertEqual_basicc                 C   sL   t d}t d}t d}| || | || | || | || d S )Nra   rb   )r   rd   rg   )r   rc   yzr   r   r   test_assertEqual_custom8  s    z2SynchronousAssertionsTests.test_assertEqual_customc                 C   s.   d}|  | j| jdd|}| |t| dS )zi
        When a message is passed to L{assertEqual} it is included in the error
        message.
        messagefoobarN)assertRaisesr2   r3   rN   r4   )r   rm   	exceptionr   r   r   test_assertEqualMessageB  s       z2SynchronousAssertionsTests.test_assertEqualMessagec                 C   sB   |  | j| jddd}|  | j| jdd}| t|t| dS )zz
        If a message is specified as L{None}, it is not included in the error
        message of L{assertEqual}.
        rn   ro   N)rp   r2   r3   r4   )r   ZexceptionForNoneZexceptionWithoutr   r   r   test_assertEqualNoneMessageO  s           z6SynchronousAssertionsTests.test_assertEqualNoneMessagec                 C   sf   t  }dg}z| || W n2 | jk
r<   | d Y n& tk
rN   Y nX | d||f  d S )Nrh   z6Fail raised when ValueError ought to have been raised.z3Comparing %r and %r should have raised an exception)r   r3   r2   r   r   )r   Zapplerh   r   r   r   test_assertEqual_incomparable]  s    z8SynchronousAssertionsTests.test_assertEqual_incomparablec                 C   s   |d S r   r   )r   errorr   r   r   _raiseErrorl  s    z&SynchronousAssertionsTests._raiseErrorc                 C   s&   |  t| jt}| t|td d S )Nz?Expect failUnlessRaises to return instance of raised exception.)failUnlessRaisesr   rv   rZ   
isinstancer   rc   r   r   r   test_failUnlessRaises_expectedp  s    z9SynchronousAssertionsTests.test_failUnlessRaises_expectedc                 C   sV   z|  t| jt W n2 tk
r2   | d Y n  | jk
rF   Y nX | d d S )Nz9failUnlessRaises shouldn't re-raise unexpected exceptionsz4Expected exception wasn't raised. Should have failed)rw   r   rv   	TypeErrorr   r2   r   r   r   r    test_failUnlessRaises_unexpectedv  s    z;SynchronousAssertionsTests.test_failUnlessRaises_unexpectedc              
      s`   d z|  t fdd W n4 | jk
rP } z| t|d W 5 d }~X Y nX | d d S )NrI   c                      s    S r   r   r   ZreturnValuer   r   <lambda>      zNSynchronousAssertionsTests.test_failUnlessRaises_noException.<locals>.<lambda>z"ValueError not raised (3 returned)z(Exception not raised. Should have failed)rw   r   r2   r3   r4   r   r   rP   r   r}   r   !test_failUnlessRaises_noException  s    
z<SynchronousAssertionsTests.test_failUnlessRaises_noExceptionc                 C   sl   |  | j| j| j}| t|| jd| jf  z|  | j| jt}W n | jk
r\   Y nX | d d S )Nz#Expected %r instance to be returnedzShould have raised exception)rw   r2   rv   rZ   rx   r   r   ry   r   r   r   &test_failUnlessRaises_failureException  s    zASynchronousAssertionsTests.test_failUnlessRaises_failureExceptionc              	   C   s4   t d}| t 
}|W 5 Q R X | ||j dS )z
        If C{assertRaises} is used to create a context manager and an exception
        is raised from the body of the C{with} statement then the context
        manager's C{exception} attribute is set to the exception that was
        raised.
        markerN)r   rp   assertIsrq   )r   rq   contextr   r   r    test_assertRaisesContextExpected  s    z;SynchronousAssertionsTests.test_assertRaisesContextExpectedc              
   C   s   z"|  t tdW 5 Q R X W nT | jk
rv } z4t|}djttd}| ||d| W 5 d}~X Y nX | 	d dS )z
        If C{assertRaises} is used to create a context manager and the wrong
        exception type is raised from the body of the C{with} statement then
        the C{with} statement raises C{failureException} describing the
        mismatch.
        r   z/{type} raised instead of ValueError:
 Traceback)typez>Exception message did not begin with expected information: {0}N:Mismatched exception type should have caused test failure.)
rp   r   r{   r2   r4   formatr   rZ   
startswithr   )r   rq   rm   rf   r   r   r   "test_assertRaisesContextUnexpected  s$    z=SynchronousAssertionsTests.test_assertRaisesContextUnexpectedc              
   C   sb   z|  t W 5 Q R X W n8 | jk
rR } zt|}| |d W 5 d}~X Y nX | d dS )z
        If C{assertRaises} is used to create a context manager and no exception
        is raised from the body of the C{with} statement then the C{with}
        statement raises C{failureException} describing the lack of exception.
        z%ValueError not raised (None returned)Nz5Non-exception result should have caused test failure.)rp   r   r2   r4   r3   r   )r   rq   rm   r   r   r   #test_assertRaisesContextNoException  s    z>SynchronousAssertionsTests.test_assertRaisesContextNoExceptionc              
   C   s   z$|  ttf t W 5 Q R X W nx | jk
r } zXt|}d|k}d|k}g }|rb|d |rp|d |r| d|d	| W 5 d}~X Y nX | d dS )	z
        If the exception type passed to C{assertRaises} does not have a
        C{__name__} then the context manager still manages to construct a
        descriptive string for it.
        r   r{   z(expected ValueError in exception messagez'expected TypeError in exception messagez; zmessage = {0}Nr   )
rp   r   r{   AttributeErrorr2   r4   appendr   r-   r   )r   rq   rm   Z
valueErrorZ	typeErrorr;   r   r   r   test_brokenName  s"    

*z*SynchronousAssertionsTests.test_brokenNamec                 C   s\   dgdgdg  }}}|  ||}| ||d | | j| j || | | j| j || d S )Nr8   rG   )failIfEqual should return first parameter)failIfEqualr3   rw   r2   r   rc   rj   rk   retr   r   r   test_failIfEqual_basic  s        z1SynchronousAssertionsTests.test_failIfEqual_basicc                 C   sX   t d}t d}t d}| ||}| ||d | | j| j|| | ||d d S )Nra   rb   Zfecundr   z"__ne__ should make these not equal)r   r   r3   rw   r2   r   r   r   r   test_failIfEqual_customEq  s      z4SynchronousAssertionsTests.test_failIfEqual_customEqc                 C   s(   t  }t  }| ||}| || dS )z
        C{failIfIdentical} returns its first argument if its first and second
        arguments are not the same object.
        N)objectfailIfIdenticalr3   )r   rc   rj   r6   r   r   r   test_failIfIdenticalPositive	  s    z7SynchronousAssertionsTests.test_failIfIdenticalPositivec                 C   s   t  }| | j| j|| dS )z~
        C{failIfIdentical} raises C{failureException} if its first and second
        arguments are the same object.
        N)r   rw   r2   r   ry   r   r   r   test_failIfIdenticalNegative  s      z7SynchronousAssertionsTests.test_failIfIdenticalNegativec                 C   s\   dgdgdg  }}}|  ||}| ||d | | j| j || | | j| j || d S )Nr8   rG   z1failUnlessIdentical should return first parameter)ZfailUnlessIdenticalr3   rw   r2   r   r   r   r   test_failUnlessIdentical  s        z3SynchronousAssertionsTests.test_failUnlessIdenticalc                 C   sd   d\}}}|  ||d |  ||d}| ||d | | j| j ||d | | j| j ||d d S )N)g      ?g?g333333?g?z4failUnlessApproximates should return first parameterg?)ZfailUnlessApproximatesr3   rw   r2   r   r   r   r   test_failUnlessApproximates)  s     
      z6SynchronousAssertionsTests.test_failUnlessApproximatesc                 C   s\   d}d}d}d}|  ||| |  |||}| ||d||f  | | j| j ||| d S )N   A!   @XO  @C   @z<failUnlessAlmostEqual should return first parameter (%r, %r))ZfailUnlessAlmostEqualr3   rw   r2   r   Z	precisionrc   rj   rk   r   r   r   r   test_failUnlessAlmostEqual5  s    
   z5SynchronousAssertionsTests.test_failUnlessAlmostEqualc                 C   sd   d}d}d}d}|  |||}| ||d||f  | | j| j ||| | | j| j ||| d S )Nr   r   r   r   z8failIfAlmostEqual should return first parameter (%r, %r))ZfailIfAlmostEqualr3   rw   r2   r   r   r   r   test_failIfAlmostEqualB  s(    
      z1SynchronousAssertionsTests.test_failIfAlmostEqualc                 C   s^   d}d}d}|  || |  ||}| ||d | | j| j || | | j| j || d S NrF   zthe dog satzthe cat satzshould return first parameter)ZfailUnlessSubstringr3   rw   r2   r   r   r   r   test_failUnlessSubstringP  s         z3SynchronousAssertionsTests.test_failUnlessSubstringc                 C   s^   d}d}d}|  || |  ||}| ||d | | j| j || | | j| j || d S r   )ZfailIfSubstringr3   rw   r2   r   r   r   r   test_failIfSubstring]  s         z/SynchronousAssertionsTests.test_failIfSubstringc                 C   s   t d}| || dS )zB
        L{assertIs} passes if two objects are identical.
        ra   N)r   r   r   ar   r   r   test_assertIsj  s    z(SynchronousAssertionsTests.test_assertIsc                 C   s6   t dt d }}| || | | j| j|| dS )zE
        L{assertIs} fails if two objects are not identical.
        ra   N)r   r3   rp   r2   r   r   r   br   r   r   test_assertIsErrorr  s    z-SynchronousAssertionsTests.test_assertIsErrorc                 C   s.   t dt d }}| || | || dS )zI
        L{assertIsNot} passes if two objects are not identical.
        ra   N)r   r3   assertIsNotr   r   r   r   test_assertIsNot{  s    z+SynchronousAssertionsTests.test_assertIsNotc                 C   s    t d}| | j| j|| dS )zD
        L{assertIsNot} fails if two objects are identical.
        ra   N)r   rp   r2   r   r   r   r   r   test_assertIsNotError  s    z0SynchronousAssertionsTests.test_assertIsNotErrorc                 C   s$   t dtfi }| }| || dS )z<
        Test a true condition of assertIsInstance.
        ANr   r   assertIsInstance)r   r   r   r   r   r   test_assertIsInstance  s    z0SynchronousAssertionsTests.test_assertIsInstancec                 C   s6   t dtfi }t dtfi }| }| |||f dS )zR
        Test a true condition of assertIsInstance with multiple classes.
        r   BNr   r   r   r   r   r   r   r   $test_assertIsInstanceMultipleClasses  s    z?SynchronousAssertionsTests.test_assertIsInstanceMultipleClassesc                 C   s:   t dtfi }t dtfi }| }| | j| j|| dS )z6
        Test an error with assertIsInstance.
        r   r   Nr   r   rp   r2   r   r   r   r   r   test_assertIsInstanceError  s    z5SynchronousAssertionsTests.test_assertIsInstanceErrorc                 C   sL   t dtfi }t dtfi }t dtfi }| }| | j| j|||f dS )zK
        Test an error with assertIsInstance and multiple classes.
        r   r   CNr   r   r   r   r   r   r   r   r   )test_assertIsInstanceErrorMultipleClasses  s
    zDSynchronousAssertionsTests.test_assertIsInstanceErrorMultipleClassesc                 C   s*   |  | j| jdtd}| dt| dS )z
        If L{TestCase.assertIsInstance} is passed a custom message as its 3rd
        argument, the message is included in the failure exception raised when
        the assertion fails.
        rI   zSilly assertionN)rp   r2   r   r4   rN   )r   excr   r   r   "test_assertIsInstanceCustomMessage  s       z=SynchronousAssertionsTests.test_assertIsInstanceCustomMessagec                 C   s2   t dtfi }t dtfi }| }| || dS )z?
        Test a true condition of assertNotIsInstance.
        r   r   Nr   r   assertNotIsInstancer   r   r   r   test_assertNotIsInstance  s    z3SynchronousAssertionsTests.test_assertNotIsInstancec                 C   sD   t dtfi }t dtfi }t dtfi }| }| |||f dS )zT
        Test a true condition of assertNotIsInstance and multiple classes.
        r   r   r   Nr   r   r   r   r   'test_assertNotIsInstanceMultipleClasses  s
    zBSynchronousAssertionsTests.test_assertNotIsInstanceMultipleClassesc                 C   sD   t dtfi }| }| | j| j||}| t|d||f  dS )z9
        Test an error with assertNotIsInstance.
        r   z%r is an instance of %sN)r   r   rp   r2   r   r3   r4   )r   r   r   ru   r   r   r   test_assertNotIsInstanceError  s      z8SynchronousAssertionsTests.test_assertNotIsInstanceErrorc                 C   s>   t dtfi }t dtfi }| }| | j| j|||f dS )zN
        Test an error with assertNotIsInstance and multiple classes.
        r   r   N)r   r   rp   r2   r   r   r   r   r   ,test_assertNotIsInstanceErrorMultipleClasses  s    zGSynchronousAssertionsTests.test_assertNotIsInstanceErrorMultipleClassesc                 C   s   |  ddiddi dS )z
        L{twisted.trial.unittest.TestCase} supports the C{assertDictEqual}
        method inherited from the standard library in Python 2.7.
        r   r8   N)assertDictEqualr   r   r   r   test_assertDictEqual  s    z/SynchronousAssertionsTests.test_assertDictEqualr   Nz:assertDictEqual is not available on this version of Python)0r   r   r   r"   rd   rg   ri   rl   rr   rs   rt   rv   rz   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   getattrr   r>   skipr   r   r   r   r`     sX   


			



r`   c                   @   st   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S )WarningAssertionTestsc                 C   s*   dd }|  tdt|d}| |d dS )z0
        Test basic assertWarns report.
        c                 S   s   t jdtd | S NWoo deprecatedcategorywarningswarnDeprecationWarningr   r   r   r   r	     s    z:WarningAssertionTests.test_assertWarns.<locals>.deprecatedr   {   NassertWarnsr   __file__r3   r   r	   rr   r   r   test_assertWarns  s    
 z&WarningAssertionTests.test_assertWarnsc                 C   sH   dd }|  tdt|d}| |d |  tdt|d}| |d dS )z}
        Test that assertWarns cleans the warning registry, so the warning is
        not swallowed the second time.
        c                 S   s   t jdtd | S r   r   r   r   r   r   r	     s    zGWarningAssertionTests.test_assertWarnsRegistryClean.<locals>.deprecatedr   r   A  Nr   )r   r	   Zr1Zr2r   r   r   test_assertWarnsRegistryClean  s    
 
 z3WarningAssertionTests.test_assertWarnsRegistryCleanc              	   C   s&   dd }|  | j| jtdt|d dS )zH
        Test assertWarns failure when no warning is generated.
        c                 S   s   | S r   r   r   r   r   r   normal  s    z;WarningAssertionTests.test_assertWarnsError.<locals>.normalr   r   Nrp   r2   r   r   r   )r   r   r   r   r   test_assertWarnsError  s        z+WarningAssertionTests.test_assertWarnsErrorc              	   C   s&   dd }|  | j| jtdt|d dS )zF
        Test assertWarns failure when the category is wrong.
        c                 S   s   t jdtd | S NFoo deprecatedr   r   r   r   r   r   r	   #  s    zGWarningAssertionTests.test_assertWarnsWrongCategory.<locals>.deprecatedr   r   N)rp   r2   r   UserWarningr   r   r	   r   r   r   test_assertWarnsWrongCategory  s        z3WarningAssertionTests.test_assertWarnsWrongCategoryc              	   C   s&   dd }|  | j| jtdt|d dS )zE
        Test assertWarns failure when the message is wrong.
        c                 S   s   t jdtd | S r   r   r   r   r   r   r	   /  s    zFWarningAssertionTests.test_assertWarnsWrongMessage.<locals>.deprecatedBar deprecatedr   Nr   r   r   r   r   test_assertWarnsWrongMessage+  s        z2WarningAssertionTests.test_assertWarnsWrongMessagec              	   C   s&   dd }|  | j| jtdt|d dS )z
        If the warning emitted by a function refers to a different file than is
        passed to C{assertWarns}, C{failureException} is raised.
        c                 S   s   t jdtdd d S )Nr   rG   )r   
stacklevelr   r   r   r   r   r	   <  s
    
  zCWarningAssertionTests.test_assertWarnsWrongFile.<locals>.deprecatedr   r   Nr   r   r   r   r   test_assertWarnsWrongFile7  s        z/WarningAssertionTests.test_assertWarnsWrongFilec                 C   sR   G dd d}|  tdt|}| t|| |  tdt|}| t|| dS )zH
        Test assertWarns works when creating a class instance.
        c                   @   s   e Zd Zdd ZdS )z;WarningAssertionTests.test_assertWarnsOnClass.<locals>.Warnc                 S   s   t jdtd d S )NDo not call mer   )r   r   RuntimeWarningr   r   r   r   r   U  s    zDWarningAssertionTests.test_assertWarnsOnClass.<locals>.Warn.__init__N)r   r   r   r   r   r   r   r   WarnT  s   r   r   N)r   r   r   rZ   rx   )r   r   r   r   r   r   test_assertWarnsOnClassP  s    

z-WarningAssertionTests.test_assertWarnsOnClassc                 C   sX   G dd d}| }|  tdt|jd}| |d |  tdt|jd}| |d dS )zI
        Test assertWarns works when used on an instance method.
        c                   @   s   e Zd Zdd ZdS )z<WarningAssertionTests.test_assertWarnsOnMethod.<locals>.Warnc                 S   s   t jdtd |S )Nr   r   r   r   r   r   r   r	   d  s    zGWarningAssertionTests.test_assertWarnsOnMethod.<locals>.Warn.deprecatedN)r   r   r   r	   r   r   r   r   r   c  s   r   r   r   N)r   r   r   r	   r3   r   r   wr   r   r   r   test_assertWarnsOnMethod_  s    
 
 z.WarningAssertionTests.test_assertWarnsOnMethodc                 C   sT   G dd d}| }|  tdt|d}| |d |  tdt|d}| |d dS )zM
        Test assertWarns works on instance with C{__call__} method.
        c                   @   s   e Zd Zdd ZdS )z:WarningAssertionTests.test_assertWarnsOnCall.<locals>.Warnc                 S   s   t jdtd |S )NEgg deprecatedr   r   r   r   r   r   __call__u  s    zCWarningAssertionTests.test_assertWarnsOnCall.<locals>.Warn.__call__N)r   r   r   r   r   r   r   r   r   t  s   r   r   r   Nr   r   r   r   r   test_assertWarnsOnCallp  s    
 
 z,WarningAssertionTests.test_assertWarnsOnCallc                 C   s*   dd }|  tdt|d}| |d dS )zD
        Test assertWarns on a warning filtered by default.
        c                 S   s   t jdtd | S r   r   r   PendingDeprecationWarningr   r   r   r   r	     s    z@WarningAssertionTests.test_assertWarnsFilter.<locals>.deprecatedr   r   N)r   r   r   r3   r   r   r   r   test_assertWarnsFilter  s      z,WarningAssertionTests.test_assertWarnsFilterc                    s(   dd   fdd}|  tdt| dS )z
        C{assertWarns} does not raise an exception if the function it is passed
        triggers the same warning more than once.
        c                   S   s   t jdtd d S r   r   r   r   r   r   r	     s    zJWarningAssertionTests.test_assertWarnsMultipleWarnings.<locals>.deprecatedc                      s         d S r   r   r   r   r   r   f  s    zAWarningAssertionTests.test_assertWarnsMultipleWarnings.<locals>.fr   N)r   r   r   )r   r   r   r   r    test_assertWarnsMultipleWarnings  s       z6WarningAssertionTests.test_assertWarnsMultipleWarningsc              	   C   s6   dd }|  | j| jtdt|d}| t|d dS )z
        For now, assertWarns is unable to handle multiple different warnings,
        so it should raise an exception if it's the case.
        c                 S   s    t jdtd t jdtd d S )Nr   r   zAnother one)r   r   r   r   r   r   r   r   r	     s    zKWarningAssertionTests.test_assertWarnsDifferentWarnings.<locals>.deprecatedr   r   zCan't handle different warningsN)rp   r2   r   r   r   r3   r4   )r   r	   rP   r   r   r   !test_assertWarnsDifferentWarnings  s        z7WarningAssertionTests.test_assertWarnsDifferentWarningsc                    sX   G dd dt   fdd}|d |  dt|d | |g\}| |d d dS )	z
        Warnings emitted before L{TestCase.assertWarns} is called do not get
        flushed and do not alter the behavior of L{TestCase.assertWarns}.
        c                   @   s   e Zd ZdS )zPWarningAssertionTests.test_assertWarnsAfterUnassertedWarning.<locals>.TheWarningN)r   r   r   r   r   r   r   
TheWarning  s   r   c                    s   t j|  d d S )Nr   )r   r   )rm   r   r   r   r     s    zGWarningAssertionTests.test_assertWarnsAfterUnassertedWarning.<locals>.frn   ro   rm   N)Warningr   r   flushWarningsr3   )r   r   Zwarningr   r   r   &test_assertWarnsAfterUnassertedWarning  s    z<WarningAssertionTests.test_assertWarnsAfterUnassertedWarningN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s   r   c                   @   s   e Zd ZdZe Zee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d#d$ Zd%S )&ResultOfAssertionsTestsz
    Tests for L{SynchronousTestCase.successResultOf},
    L{SynchronousTestCase.failureResultOf}, and
    L{SynchronousTestCase.assertNoResult}.
    z	Bad timesc                 C   s   |  | j| jt  dS )z
        L{SynchronousTestCase.successResultOf} raises
        L{SynchronousTestCase.failureException} when called with a L{Deferred}
        with no current result.
        N)rp   r2   successResultOfr   r   r   r   r   test_withoutSuccessResult  s
      z1ResultOfAssertionsTests.test_withoutSuccessResultc                 C   s   |  | j| jt| j dS )z
        L{SynchronousTestCase.successResultOf} raises
        L{SynchronousTestCase.failureException} when called with a L{Deferred}
        with a failure result.
        N)rp   r2   r   r   failurer   r   r   r   test_successResultOfWithFailure  s
      z7ResultOfAssertionsTests.test_successResultOfWithFailurec              
   C   sT   z|  t| j W n: | jk
rN } z| | j t| W 5 d}~X Y nX dS )z
        L{SynchronousTestCase.successResultOf} raises a
        L{SynchronousTestCase.failureException} that has the original failure
        traceback when called with a L{Deferred} with a failure result.
        N)r   r   r   r2   rN   getTracebackr4   r   r   r   r   +test_successResultOfWithFailureHasTraceback  s    zCResultOfAssertionsTests.test_successResultOfWithFailureHasTracebackc                 C   s   |  | j| jt  dS )z
        L{SynchronousTestCase.failureResultOf} raises
        L{SynchronousTestCase.failureException} when called with a L{Deferred}
        with no current result.
        N)rp   r2   failureResultOfr   r   r   r   r   test_withoutFailureResult  s
      z1ResultOfAssertionsTests.test_withoutFailureResultc                 C   s   |  | j| jt| j dS )z
        L{SynchronousTestCase.failureResultOf} raises
        L{SynchronousTestCase.failureException} when called with a L{Deferred}
        with a success result.
        N)rp   r2   r  r   r6   r   r   r   r   test_failureResultOfWithSuccess  s
      z7ResultOfAssertionsTests.test_failureResultOfWithSuccessc                 C   s   |  | j| jt| jt dS )z
        L{SynchronousTestCase.failureResultOf} raises
        L{SynchronousTestCase.failureException} when called with a L{Deferred}
        with a failure type that was not expected.
        N)rp   r2   r  r   r   KeyErrorr   r   r   r   $test_failureResultOfWithWrongFailure  s      z<ResultOfAssertionsTests.test_failureResultOfWithWrongFailurec              
   C   sr   z|  t| jt W nV | jk
rl } z6| | j t| | dtj	tj
t| W 5 d}~X Y nX dS )aV  
        L{SynchronousTestCase.failureResultOf} raises
        L{SynchronousTestCase.failureException} when called with a L{Deferred}
        with a failure type that was not expected, and the
        L{SynchronousTestCase.failureException} message contains the original
        failure traceback as well as the expected failure type
        z%Failure of type ({0}.{1}) expected onN)r  r   r   r  r2   rN   r   r4   r   r   r   r   r   r   r   6test_failureResultOfWithWrongFailureOneExpectedFailure  s     zNResultOfAssertionsTests.test_failureResultOfWithWrongFailureOneExpectedFailurec                 C   s|   z|  t| jtt W n^ | jk
rv } z>| | j t| | d	tj
tjtj
tjt| W 5 d}~X Y nX dS )at  
        L{SynchronousTestCase.failureResultOf} raises
        L{SynchronousTestCase.failureException} when called with a L{Deferred}
        with a failure type that was not expected, and the
        L{SynchronousTestCase.failureException} message contains the original
        failure traceback as well as the expected failure types in the error
        message
        z0Failure of type ({0}.{1} or {2}.{3}) expected onN)r  r   r   r  IOErrorr2   rN   r   r4   r   r   r   r   r   r   r   8test_failureResultOfWithWrongFailureMultiExpectedFailure  s    	  zPResultOfAssertionsTests.test_failureResultOfWithWrongFailureMultiExpectedFailurec                 C   s   |  | j| t| j dS )a  
        When passed a L{Deferred} which currently has a result (ie,
        L{Deferred.addCallback} would cause the added callback to be called
        before C{addCallback} returns), L{SynchronousTestCase.successResultOf}
        returns that result.
        N)assertIdenticalr6   r   r   r   r   r   r   test_withSuccessResult'  s     z.ResultOfAssertionsTests.test_withSuccessResultc                 C   s&   |  | j| t| j| jjt dS )a7  
        When passed a L{Deferred} which currently has a L{Failure} result (ie,
        L{Deferred.addErrback} would cause the added errback to be called
        before C{addErrback} returns), L{SynchronousTestCase.failureResultOf}
        returns that L{Failure} if that L{Failure}'s type is expected.
        N)r
  r   r  r   r   r  r   r   r   r   test_withExpectedFailureResult2  s    z6ResultOfAssertionsTests.test_withExpectedFailureResultc                 C   s   |  | j| t| j dS )a  
        When passed a L{Deferred} which currently has a L{Failure} result
        (ie, L{Deferred.addErrback} would cause the added errback to be called
        before C{addErrback} returns), L{SynchronousTestCase.failureResultOf}
        returns that L{Failure}.
        N)r
  r   r  r   r   r   r   r   test_withFailureResult?  s     z.ResultOfAssertionsTests.test_withFailureResultc                 C   s   |  | j| jt| j dS )z
        When passed a L{Deferred} which currently has a success result (see
        L{test_withSuccessResult}), L{SynchronousTestCase.assertNoResult} raises
        L{SynchronousTestCase.failureException}.
        N)rp   r2   assertNoResultr   r6   r   r   r   r   test_assertNoResultSuccessJ  s
      z2ResultOfAssertionsTests.test_assertNoResultSuccessc                 C   s   |  | j| jt| j dS )z
        When passed a L{Deferred} which currently has a failure result (see
        L{test_withFailureResult}), L{SynchronousTestCase.assertNoResult} raises
        L{SynchronousTestCase.failureException}.
        N)rp   r2   r  r   r   r   r   r   r   test_assertNoResultFailureT  s
      z2ResultOfAssertionsTests.test_assertNoResultFailurec                 C   s   |  t  dS )zC
        When passed a L{Deferred} with no current result,
        N)r  r   r   r   r   r   test_assertNoResult^  s    z+ResultOfAssertionsTests.test_assertNoResultc                 C   s4   t  }| | || j | | j| | dS )z
        When passed a L{Deferred} with no current result, which is then
        fired with a success result, L{SynchronousTestCase.assertNoResult}
        doesn't modify the result of the L{Deferred}.
        N)r   r  callbackr6   r3   r   r   dr   r   r   $test_assertNoResultPropagatesSuccesse  s    
z<ResultOfAssertionsTests.test_assertNoResultPropagatesSuccessc                 C   s4   t  }| | || j | | j| | dS )z
        When passed a L{Deferred} with no current result, which is then
        fired with a L{Failure} result, L{SynchronousTestCase.assertNoResult}
        doesn't modify the result of the L{Deferred}.
        N)r   r  Zerrbackr   r3   r  r  r   r   r   )test_assertNoResultPropagatesLaterFailureq  s    
zAResultOfAssertionsTests.test_assertNoResultPropagatesLaterFailurec                 C   sF   t | j}z| | W n | jk
r.   Y nX | d| | dS )z
        When passed a L{Deferred} which currently has a L{Failure} result,
        L{SynchronousTestCase.assertNoResult} changes the result of the
        L{Deferred} to a success.
        N)r   r   r  r2   r3   r   r  r   r   r   +test_assertNoResultSwallowsImmediateFailure}  s    
zCResultOfAssertionsTests.test_assertNoResultSwallowsImmediateFailureN)r   r   r   r"   r   r6   r   	Exceptionr   r   r   r  r  r  r  r  r	  r  r  r  r  r  r  r  r  r  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 )AssertionNamesTestszK
    Tests for consistency of naming within TestCase assertion methods
    c                    s"   i  t |  d  fdd D S )Nassertc                    s&   g | ]}| d s|dkr | qS )ZNot_)r   ).0kZdctr   r   
<listcomp>  s     
  z3AssertionNamesTests._getAsserts.<locals>.<listcomp>)r   r   r   r  r   _getAsserts  s    zAssertionNamesTests._getAssertsc                 C   s   |j S r   )r   ry   r   r   r   _name  s    zAssertionNamesTests._namec                 C   s0   t |  }t t| d}| ||| dS )a  
        The C{failUnless*} test methods are a subset of the C{assert*} test
        methods.  This is intended to ensure that methods using the
        I{failUnless} naming scheme are not added without corresponding methods
        using the I{assert} naming scheme.  The I{assert} naming scheme is
        preferred, and new I{assert}-prefixed methods may be added without
        corresponding I{failUnless}-prefixed methods.
        r\   N)setr   r   r3   intersection)r   assertsZfailUnlessesr   r   r   test_failUnlessMatchesAssert  s    	 z0AssertionNamesTests.test_failUnlessMatchesAssertc                 C   s<   t tjd}t tjd}| t|| jdt|| jd d S )NZ	assertNotrR   )key)r   r   r>   r3   sortedr!  )r   r$  ZfailIfsr   r   r   test_failIf_matches_assertNot  s
    z1AssertionNamesTests.test_failIf_matches_assertNotc              	   C   s   t |  D ]\}}t|sq|dr\| t| |d d||f  | |t| |d  |dr| t| |d d d||d d f  | |t| |d d  qd S )NZEqualsz%s but no %ssZEqualsz%s but no %s)varsitemscallableendswithrZ   hasattrr3   r   )r   r   valuer   r   r   test_equalSpelling  s    


z&AssertionNamesTests.test_equalSpellingN)	r   r   r   r"   r   r!  r%  r(  r1  r   r   r   r   r    s   r  c                   @   sV   e Zd ZdZed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S )CallDeprecatedTestsz\
    Test use of the L{SynchronousTestCase.callDeprecated} method with version objects.
    ZTwisted   r   c                 C   s&   |  | jtd | |  g d dS )zj
        callDeprecated calls a deprecated callable, suppressing the
        deprecation warning.
        rn   zNo warnings should be shownN)callDeprecatedversion	oldMethodr3   r   r   r   r   r   $test_callDeprecatedSuppressesWarning  s      z8CallDeprecatedTests.test_callDeprecatedSuppressesWarningc                 C   s    |  | jtd}| d| dS )zn
        L{callDeprecated} actually calls the callable passed to it, and
        forwards the result.
        rn   N)r4  r5  r6  r3   r5   r   r   r    test_callDeprecatedCallsFunction  s    z4CallDeprecatedTests.test_callDeprecatedCallsFunctionc                 C   s6   dd }|  | j| j| j|}| d| t| dS )zd
        L{callDeprecated} raises a test failure if the callable is not
        deprecated.
        c                   S   s   d S r   r   r   r   r   r   notDeprecated  s    zGCallDeprecatedTests.test_failsWithoutDeprecation.<locals>.notDeprecatedz%r is not deprecated.N)rp   r2   r4  r5  r3   r4   )r   r9  rq   r   r   r   test_failsWithoutDeprecation  s       z0CallDeprecatedTests.test_failsWithoutDeprecationc                 C   sR   t dddd}| | j| j|td}| t| jt| | t|t| dS )z
        callDeprecated raises a test failure if the callable was deprecated
        at a different version to the one expected.
        Foor8   rG   rI   rn   N)	r
   rp   r2   r4  r6  rN   r   r5  r4   )r   differentVersionrq   r   r   r   "test_failsWithIncorrectDeprecation  s      z6CallDeprecatedTests.test_failsWithIncorrectDeprecationc                 C   sT   t dddd}dd }t||}| ||d |  }| t|dd| d	S )
a:  
        L{callDeprecated} ignores all deprecations apart from the first.

        Multiple warnings are generated when a deprecated function calls
        another deprecated function. The first warning is the one generated by
        the explicitly called function. That's the warning that we care about.
        r;  r8   rG   rI   c                  W   s   t |  S r   )r6  )argsr   r   r   nestedDeprecation  s    zECallDeprecatedTests.test_nestedDeprecation.<locals>.nestedDeprecation   zUnexpected warnings: {}N)r
   r	   r4  r   r3   r<   r   )r   r<  r?  ZwarningsShownr   r   r   test_nestedDeprecation  s    z*CallDeprecatedTests.test_nestedDeprecationc                 C   s   |  | jdftd dS )zj
        L{callDeprecated} can take a message argument used to check the warning
        emitted.
        	newMethodr8   N)r4  r5  oldMethodReplacedr   r   r   r   test_callDeprecationWithMessage
  s     z3CallDeprecatedTests.test_callDeprecationWithMessagec                 C   sF   |  | j| j| jdftd}| t| jt| | dt| dS )z|
        If the message passed to L{callDeprecated} doesn't match,
        L{callDeprecated} raises a test failure.
        zsomething.wrongr8   zplease use newMethod insteadN)rp   r2   r4  r5  rC  rN   r   r4   )r   rq   r   r   r   $test_callDeprecationWithWrongMessage  s     z8CallDeprecatedTests.test_callDeprecationWithWrongMessageN)r   r   r   r"   r
   r5  r7  r8  r:  r=  rA  rD  rE  r   r   r   r   r2    s   
		r2  c                 C   s   | S )z(
    Deprecated method for testing.
    r   rc   r   r   r   r6  #  s    r6  rB  )Zreplacementc                 C   s   d|  S )zh
    Another deprecated method, which has been deprecated in favor of the
    mythical 'newMethod'.
    rG   r   rF  r   r   r   rC  +  s    rC  ))r"   Z
__future__r   r   r   r   r+   Ztwisted.python.utilr   Ztwisted.python.reflectr   r   r   Ztwisted.python.deprecater	   Zincrementalr
   r   Ztwisted.python.failurer   Ztwisted.trialZtwisted.internet.deferr   r   r   r   r   r   re   r%   r>   r?   rW   r`   r   r   r  r2  r5  r6  rC  r   r   r   r   <module>   s:   9MM   s E S0g
