B
    u9ag                 @   sD  d Z dZddlZddlZddlmZ ddlmZmZ G dd deZ	dd	 Z
G d
d deZe
dd ZG dd deZG dd deZG dd dejZG dd deZG dd dejeZG dd dejeZG dd de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d$kr@e  dS )%z
Mike Blandzmbland at acm dot org    N)deque)_GeneratorContextManagercontextmanagerc                   s,   e Zd Z fddZdd Zdd Z  ZS )MockContextManagerc                s"   t  j|  d| _d| _d | _d S )NF)super__init__enter_calledexit_called	exit_args)selfargs)	__class__ /usr/lib/python3.7/test_with.pyr      s    zMockContextManager.__init__c             C   s   d| _ t| S )NT)r   r   	__enter__)r   r   r   r   r      s    zMockContextManager.__enter__c             C   s"   d| _ |||f| _t| |||S )NT)r	   r
   r   __exit__)r   typevalue	tracebackr   r   r   r      s    zMockContextManager.__exit__)__name__
__module____qualname__r   r   r   __classcell__r   r   )r   r   r      s   r   c                s    fdd}|S )Nc                 s   t  | |S )N)r   )r   kwds)funcr   r   helper    s    z#mock_contextmanager.<locals>.helperr   )r   r   r   )r   r   mock_contextmanager   s    r   c               @   s   e Zd Zdd ZdS )MockResourcec             C   s   d| _ d| _d S )NF)yieldedstopped)r   r   r   r   r   &   s    zMockResource.__init__N)r   r   r   r   r   r   r   r   r   %   s   r   c              c   s$   t  } zd| _| V  W d d| _X d S )NT)r   r   r   )mockr   r   r   mock_contextmanager_generator+   s
    
r!   c               @   s$   e Zd Zdd Zdd Zdd ZdS )Nestedc             G   s   || _ d | _d S )N)managersentered)r   r#   r   r   r   r   7   s    zNested.__init__c             C   sn   | j d k	rtdt | _ g }y.x(| jD ]}||  | j | q(W W n   | jt	  sd Y nX |S )NzContext is not reentrant)
r$   RuntimeErrorr   r#   appendr   
appendleftr   sysexc_info)r   varsmgrr   r   r   r   ;   s    
zNested.__enter__c             G   sh   |}x6| j D ],}y|j| r d}W q   t }Y qX qW d | _ ||k	rd|d |d |d d S )N)NNNr         )r$   r   r(   r)   with_traceback)r   r)   Zexr+   r   r   r   r   I   s    
zNested.__exit__N)r   r   r   r   r   r   r   r   r   r   r"   5   s   r"   c               @   s$   e Zd Zdd Zdd Zdd ZdS )
MockNestedc             G   s&   t j| f|  d| _d| _d | _d S )NF)r"   r   r   r	   r
   )r   r#   r   r   r   r   Z   s    zMockNested.__init__c             C   s   d| _ t| S )NT)r   r"   r   )r   r   r   r   r   `   s    zMockNested.__enter__c             G   s   d| _ || _tj| f| S )NT)r	   r
   r"   r   )r   r)   r   r   r   r   d   s    zMockNested.__exit__N)r   r   r   r   r   r   r   r   r   r   r/   Y   s   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S )FailureTestCasec             C   s   dd }|  t| d S )Nc            	   S   s   t  W d Q R X d S )N)foor   r   r   r   fooNotDeclaredl   s     z5FailureTestCase.testNameError.<locals>.fooNotDeclared)assertRaises	NameError)r   r2   r   r   r   testNameErrork   s    zFailureTestCase.testNameErrorc                s.   G dd dt   fdd}| td| d S )Nc               @   s   e Zd Zdd ZdS )z<FailureTestCase.testEnterAttributeError1.<locals>.LacksEnterc             S   s   d S )Nr   )r   r   r   r   r   r   r   r   r   s    zEFailureTestCase.testEnterAttributeError1.<locals>.LacksEnter.__exit__N)r   r   r   r   r   r   r   r   
LacksEnterq   s   r6   c           	      s     } |  W d Q R X d S )Nr   )r1   )r6   r   r   fooLacksEnteru   s     z?FailureTestCase.testEnterAttributeError1.<locals>.fooLacksEnterr   )objectassertRaisesRegexAttributeError)r   r7   r   )r6   r   testEnterAttributeError1p   s    z(FailureTestCase.testEnterAttributeError1c                s.   G dd dt   fdd}| td| d S )Nc               @   s   e Zd ZdS )zCFailureTestCase.testEnterAttributeError2.<locals>.LacksEnterAndExitN)r   r   r   r   r   r   r   LacksEnterAndExit{   s   r<   c           	      s     } |  W d Q R X d S )Nr   )r1   )r<   r   r   fooLacksEnterAndExit~   s     zFFailureTestCase.testEnterAttributeError2.<locals>.fooLacksEnterAndExitr   )r8   r9   r:   )r   r=   r   )r<   r   testEnterAttributeError2z   s    z(FailureTestCase.testEnterAttributeError2c                s.   G dd dt   fdd}| td| d S )Nc               @   s   e Zd Zdd ZdS )z9FailureTestCase.testExitAttributeError.<locals>.LacksExitc             S   s   d S )Nr   )r   r   r   r   r      s    zCFailureTestCase.testExitAttributeError.<locals>.LacksExit.__enter__N)r   r   r   r   r   r   r   r   	LacksExit   s   r?   c           	      s     } |  W d Q R X d S )Nr   )r1   )r?   r   r   fooLacksExit   s     z<FailureTestCase.testExitAttributeError.<locals>.fooLacksExitr   )r8   r9   r:   )r   r@   r   )r?   r   testExitAttributeError   s    z&FailureTestCase.testExitAttributeErrorc             C   s   dd }|  t|| d S )Nc             S   s   t | dd d S )N Zsingle)compile)sr   r   r   shouldRaiseSyntaxError   s    zGFailureTestCase.assertRaisesSyntaxError.<locals>.shouldRaiseSyntaxError)r3   SyntaxError)r   ZcodestrrE   r   r   r   assertRaisesSyntaxError   s    z'FailureTestCase.assertRaisesSyntaxErrorc             C   s   |  d |  d d S )Nzwith mock as None:
  passzwith mock as (None):
  pass)rG   )r   r   r   r   testAssignmentToNoneError   s    
z)FailureTestCase.testAssignmentToNoneErrorc             C   s   |  d |  d d S )Nzwith mock as None,:
  passzwith mock as (None,):
  pass)rG   )r   r   r   r   ,testAssignmentToTupleOnlyContainingNoneError   s    
z<FailureTestCase.testAssignmentToTupleOnlyContainingNoneErrorc             C   s   |  d d S )Nz%with mock as (foo, None, bar):
  pass)rG   )r   r   r   r   (testAssignmentToTupleContainingNoneError   s    z8FailureTestCase.testAssignmentToTupleContainingNoneErrorc                s<   G dd dt   fdd}t| jd  d S )Nc               @   s   e Zd Zdd Zdd ZdS )z4FailureTestCase.testEnterThrows.<locals>.EnterThrowsc             S   s   t dd S )NzEnter threw)r%   )r   r   r   r   r      s    z>FailureTestCase.testEnterThrows.<locals>.EnterThrows.__enter__c             W   s   d S )Nr   )r   r   r   r   r   r      s    z=FailureTestCase.testEnterThrows.<locals>.EnterThrows.__exit__N)r   r   r   r   r   r   r   r   r   EnterThrows   s   rK   c           	      s"     } d _ | _ W d Q R X d S )N)r1   )Zct)rK   r   r   r   shouldThrow   s    z4FailureTestCase.testEnterThrows.<locals>.shouldThrow)r8   r3   r%   assertEqualr1   )r   rL   r   )rK   r   r   testEnterThrows   s    zFailureTestCase.testEnterThrowsc                s,   G dd dt   fdd}| t| d S )Nc               @   s   e Zd Zdd Zdd ZdS )z2FailureTestCase.testExitThrows.<locals>.ExitThrowsc             S   s   d S )Nr   )r   r   r   r   r      s    z<FailureTestCase.testExitThrows.<locals>.ExitThrows.__enter__c             W   s   t dd S )N*   )r%   )r   r   r   r   r   r      s    z;FailureTestCase.testExitThrows.<locals>.ExitThrows.__exit__N)r   r   r   r   r   r   r   r   r   
ExitThrows   s   rP   c            	      s      W d Q R X d S )Nr   r   )rP   r   r   rL      s    z3FailureTestCase.testExitThrows.<locals>.shouldThrow)r8   r3   r%   )r   rL   r   )rP   r   testExitThrows   s    zFailureTestCase.testExitThrowsN)r   r   r   r5   r;   r>   rA   rG   rH   rI   rJ   rN   rQ   r   r   r   r   r0   j   s   
	
r0   c               @   sV   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dZ
dd ZdS )ContextmanagerAssertionMixinc             C   s   t d| _d S )Nztest exception)r%   TEST_EXCEPTION)r   r   r   r   setUp   s    z"ContextmanagerAssertionMixin.setUpc             C   s*   |  |j | |j | |jd  d S )N)
assertTruer   assertFalser	   rM   r
   )r   mock_managerr   r   r   assertInWithManagerInvariants   s    z:ContextmanagerAssertionMixin.assertInWithManagerInvariantsc             C   s*   |  |j |  |j | |j| d S )N)rU   r   r	   rM   r
   )r   rW   r
   r   r   r    assertAfterWithManagerInvariants   s    z=ContextmanagerAssertionMixin.assertAfterWithManagerInvariantsc             C   s   |  |d d S )N)NNN)rY   )r   rW   r   r   r   'assertAfterWithManagerInvariantsNoError   s    zDContextmanagerAssertionMixin.assertAfterWithManagerInvariantsNoErrorc             C   s   |  |j | |j d S )N)rU   r   rV   r   )r   mock_generatorr   r   r   assertInWithGeneratorInvariants   s    z<ContextmanagerAssertionMixin.assertInWithGeneratorInvariantsc             C   s   |  |j |  |j d S )N)rU   r   r   )r   r[   r   r   r   )assertAfterWithGeneratorInvariantsNoError   s    zFContextmanagerAssertionMixin.assertAfterWithGeneratorInvariantsNoErrorc             C   s
   | j d S )N)rS   )r   r   r   r   raiseTestException   s    z/ContextmanagerAssertionMixin.raiseTestExceptionNc             C   sx   |  |j |  |j |d kr>| |jd | j t| j}| |jd | | |jd | | |jd d  d S )Nr,   r   r-   )	rU   r   r	   rM   r
   rS   r   ZassertIsInstanceZassertIsNot)r   rW   exc_typer   r   r   )assertAfterWithManagerInvariantsWithError   s    
zFContextmanagerAssertionMixin.assertAfterWithManagerInvariantsWithErrorc             C   s   |  |j |  |j d S )N)rU   r   r   )r   r[   r   r   r   +assertAfterWithGeneratorInvariantsWithError   s    zHContextmanagerAssertionMixin.assertAfterWithGeneratorInvariantsWithError)N)r   r   r   rT   rX   rY   rZ   r\   r]   r^   r`   ra   r   r   r   r   rR      s   
rR   c               @   sD   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S )NonexceptionalTestCasec          	   C   s   t   W d Q R X d S )N)r!   )r   r   r   r   testInlineGeneratorSyntax   s    z0NonexceptionalTestCase.testInlineGeneratorSyntaxc          	   C   s$   t  }| W d Q R X | | d S )N)r!   rZ   )r   r    r   r   r   testUnboundGenerator   s    
z+NonexceptionalTestCase.testUnboundGeneratorc          	   C   s*   t  }| | W d Q R X | | d S )N)r!   r\   r]   )r   r1   r   r   r   testInlineGeneratorBoundSyntax   s    z5NonexceptionalTestCase.testInlineGeneratorBoundSyntaxc          	   C   s.   d }t  }| | W d Q R X | | d S )N)r!   r\   r]   )r   r1   r   r   r   *testInlineGeneratorBoundToExistingVariable   s    zANonexceptionalTestCase.testInlineGeneratorBoundToExistingVariablec          	   C   s0   t  | _| | j W d Q R X | | j d S )N)r!   r1   r\   r]   )r   r   r   r   (testInlineGeneratorBoundToDottedVariable  s    
z?NonexceptionalTestCase.testInlineGeneratorBoundToDottedVariablec          	   C   sB   t  }|}| | | | W d Q R X | | | | d S )N)r!   r\   rX   r]   rZ   )r   r    r1   r   r   r   testBoundGenerator  s    

z)NonexceptionalTestCase.testBoundGeneratorc          
   C   s   t  }|l}t  }|.}| | | | | | | | W d Q R X | | | | | | | | W d Q R X | | | | d S )N)r!   rX   r\   rZ   r]   )r   mock_ar1   mock_bbarr   r   r   testNestedSingleStatements  s    






z1NonexceptionalTestCase.testNestedSingleStatementsN)
r   r   r   rc   rd   re   rf   rg   rh   rl   r   r   r   r   rb      s   rb   c               @   sD   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S )NestedNonexceptionalTestCasec          	   C   s   t t  W d Q R X d S )N)r"   r!   )r   r   r   r   "testSingleArgInlineGeneratorSyntax#  s    z?NestedNonexceptionalTestCase.testSingleArgInlineGeneratorSyntaxc          	   C   s2   t  }t|}| | W d Q R X | | d S )N)r!   r"   rX   rZ   )r   mr1   r   r   r   testSingleArgBoundToNonTuple'  s    
z9NestedNonexceptionalTestCase.testSingleArgBoundToNonTuplec          	   C   s2   t  }t|}| | W d Q R X | | d S )N)r!   r"   rX   rZ   )r   ro   r1   r   r   r   2testSingleArgBoundToSingleElementParenthesizedList/  s    
zONestedNonexceptionalTestCase.testSingleArgBoundToSingleElementParenthesizedListc             C   s   dd }|  t| d S )Nc           	   S   s   t t 
\} }W d Q R X d S )N)r"   r!   )r1   rk   r   r   r   shouldThrowValueError8  s    ziNestedNonexceptionalTestCase.testSingleArgBoundToMultipleElementTupleError.<locals>.shouldThrowValueError)r3   
ValueError)r   rr   r   r   r   -testSingleArgBoundToMultipleElementTupleError7  s    zJNestedNonexceptionalTestCase.testSingleArgBoundToMultipleElementTupleErrorc          	   C   sJ   t  }t|}| | | | | W d Q R X | | | | d S )N)r!   r/   rX   rZ   )r   r   mock_nestedr   r   r   testSingleArgUnbound=  s    

z1NestedNonexceptionalTestCase.testSingleArgUnboundc          	   C   s   t  }t  }t  }t|||}|. | | | | | | | | W d Q R X | | | | | | | | d S )N)r!   r/   rX   rZ   )r   ro   noru   r   r   r   testMultipleArgUnboundF  s    





z3NestedNonexceptionalTestCase.testMultipleArgUnboundc          	   C   s|   t t t t }|4\}}}| | | | | | | | W d Q R X | | | | | | | | d S )N)r/   r!   r\   rX   r]   rZ   )r   ru   ro   rw   rx   r   r   r   testMultipleArgBoundU  s    





z1NestedNonexceptionalTestCase.testMultipleArgBoundN)
r   r   r   rn   rp   rq   rt   rv   ry   rz   r   r   r   r   rm   !  s   	rm   c               @   sl   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S )ExceptionalTestCasec                s:   t    fdd}t|   j d S )Nc            	      s4    &_   j    W d Q R X d S )N)resourcerX   r\   r^   r   )cmr   r   r   rL   f  s    
z;ExceptionalTestCase.testSingleResource.<locals>.shouldThrow)r!   r3   r%   r`   ra   r|   )r   rL   r   )r}   r   r   testSingleResourced  s
    
z&ExceptionalTestCase.testSingleResourcec                s0   t    fdd}t|  t d S )Nc            	      s    _ dd  W d Q R X d S )Nr,   r   )r|   r   )r}   r   r   r   rL   q  s    z@ExceptionalTestCase.testExceptionNormalized.<locals>.shouldThrow)r!   r3   ZeroDivisionErrorr`   )r   rL   r   )r}   r   r   testExceptionNormalizedo  s    z+ExceptionalTestCase.testExceptionNormalizedc                sX   t   t   fdd}t|    j j d S )Nc            
      s\    N_ <_   j  j   W d Q R X W d Q R X d S )N)r1   rk   rX   r\   r^   r   )ri   rj   r   r   r   rL   |  s    

zCExceptionalTestCase.testNestedSingleStatements.<locals>.shouldThrow)r!   r3   r%   r`   ra   r1   rk   )r   rL   r   )ri   rj   r   r   rl   y  s    

z.ExceptionalTestCase.testNestedSingleStatementsc                sn   t   t  t  fdd}t|     j j d S )Nc            	      sZ   L\_ _    j  j   W d Q R X d S )N)
resource_a
resource_brX   r\   r^   r   )cm_acm_bru   r   r   r   rL     s    


zOExceptionalTestCase.testMultipleResourcesInSingleStatement.<locals>.shouldThrow)r!   r/   r3   r%   r`   ra   r   r   )r   rL   r   )r   r   ru   r   r   &testMultipleResourcesInSingleStatement  s    



z:ExceptionalTestCase.testMultipleResourcesInSingleStatementc                s|   t   t  d _ fdd}t|   j jd  j	 j
 jd  d S )Nc            
      sF    8_   j    _W d Q R X W d Q R X d S )N)r1   rX   r\   r^   rk   r   )ri   rj   r   r   r   rL     s    
zPExceptionalTestCase.testNestedExceptionBeforeInnerStatement.<locals>.shouldThrow)r!   rk   r3   r%   r`   ra   r1   rM   rV   r   r	   r
   )r   rL   r   )ri   rj   r   r   'testNestedExceptionBeforeInnerStatement  s    
z;ExceptionalTestCase.testNestedExceptionBeforeInnerStatementc                sX   t   t   fdd}t|    j j d S )Nc            
      s\    N_ 4_   j  j W d Q R X   W d Q R X d S )N)r1   rk   rX   r\   r^   r   )ri   rj   r   r   r   rL     s    

zOExceptionalTestCase.testNestedExceptionAfterInnerStatement.<locals>.shouldThrow)	r!   r3   r%   r`   rZ   ra   r1   r]   rk   )r   rL   r   )ri   rj   r   r   &testNestedExceptionAfterInnerStatement  s    

z:ExceptionalTestCase.testNestedExceptionAfterInnerStatementc          	      s:   t dd   fdd}| td |  W d Q R X d S )Nc               s   s
   d V  d S )Nr   r   r   r   r   r}     s    z8ExceptionalTestCase.testRaisedStopIteration1.<locals>.cmc            	      s      t dW d Q R X d S )Nz	from with)StopIterationr   )r}   r   r   rL     s    zAExceptionalTestCase.testRaisedStopIteration1.<locals>.shouldThrowz	from with)r   r9   r   )r   rL   r   )r}   r   testRaisedStopIteration1  s    z,ExceptionalTestCase.testRaisedStopIteration1c          	      s>   G dd dt   fdd}| td |  W d Q R X d S )Nc               @   s   e Zd Zdd Zdd ZdS )z8ExceptionalTestCase.testRaisedStopIteration2.<locals>.cmc             S   s   d S )Nr   )r   r   r   r   r     s    zBExceptionalTestCase.testRaisedStopIteration2.<locals>.cm.__enter__c             S   s   d S )Nr   )r   r   r   r   r   r   r   r     s    zAExceptionalTestCase.testRaisedStopIteration2.<locals>.cm.__exit__N)r   r   r   r   r   r   r   r   r   r}     s   r}   c            	      s      t dW d Q R X d S )Nz	from with)r   r   )r}   r   r   rL     s    zAExceptionalTestCase.testRaisedStopIteration2.<locals>.shouldThrowz	from with)r8   r9   r   )r   rL   r   )r}   r   testRaisedStopIteration2  s    z,ExceptionalTestCase.testRaisedStopIteration2c          	      s8   t dd   fdd}| t |  W d Q R X d S )Nc               s   s
   d V  d S )Nr   r   r   r   r   r}     s    z8ExceptionalTestCase.testRaisedStopIteration3.<locals>.cmc            	      s"      t tg W d Q R X d S )N)nextiterr   )r}   r   r   rL     s    zAExceptionalTestCase.testRaisedStopIteration3.<locals>.shouldThrow)r   r3   r   )r   rL   r   )r}   r   testRaisedStopIteration3  s    z,ExceptionalTestCase.testRaisedStopIteration3c                s(   t dd   fdd}| t| d S )Nc               s   s
   d V  d S )Nr   r   r   r   r   r}     s    z8ExceptionalTestCase.testRaisedGeneratorExit1.<locals>.cmc            	      s      t dW d Q R X d S )Nz	from with)GeneratorExitr   )r}   r   r   rL     s    zAExceptionalTestCase.testRaisedGeneratorExit1.<locals>.shouldThrow)r   r3   r   )r   rL   r   )r}   r   testRaisedGeneratorExit1  s    z,ExceptionalTestCase.testRaisedGeneratorExit1c                s,   G dd dt   fdd}| t| d S )Nc               @   s   e Zd Zdd Zdd ZdS )z8ExceptionalTestCase.testRaisedGeneratorExit2.<locals>.cmc             S   s   d S )Nr   )r   r   r   r   r     s    zBExceptionalTestCase.testRaisedGeneratorExit2.<locals>.cm.__enter__c             S   s   d S )Nr   )r   r   r   r   r   r   r   r     s    zAExceptionalTestCase.testRaisedGeneratorExit2.<locals>.cm.__exit__N)r   r   r   r   r   r   r   r   r   r}     s   r}   c            	      s      t dW d Q R X d S )Nz	from with)r   r   )r}   r   r   rL     s    zAExceptionalTestCase.testRaisedGeneratorExit2.<locals>.shouldThrow)r8   r3   r   )r   rL   r   )r}   r   testRaisedGeneratorExit2  s    z,ExceptionalTestCase.testRaisedGeneratorExit2c                s\   G dd dt   fdd}|   fdd}t|  fdd}t| d S )	Nc               @   s$   e Zd Zdd Zdd Zdd ZdS )z0ExceptionalTestCase.testErrorsInBool.<locals>.cmc                s   G  fddd}| | _ d S )Nc                   s   e Zd Z fddZdS )zGExceptionalTestCase.testErrorsInBool.<locals>.cm.__init__.<locals>.Boolc                s     S )Nr   )r   )bool_conversionr   r   __bool__  s    zPExceptionalTestCase.testErrorsInBool.<locals>.cm.__init__.<locals>.Bool.__bool__N)r   r   r   r   r   )r   r   r   Bool  s   r   )exit_result)r   r   r   r   )r   r   r     s    z9ExceptionalTestCase.testErrorsInBool.<locals>.cm.__init__c             S   s   dS )N   r   )r   r   r   r   r     s    z:ExceptionalTestCase.testErrorsInBool.<locals>.cm.__enter__c             S   s   | j S )N)r   )r   abcr   r   r   r     s    z9ExceptionalTestCase.testErrorsInBool.<locals>.cm.__exit__N)r   r   r   r   r   r   r   r   r   r   r}     s   r}   c            	      s&    dd   d W d Q R X d S )Nc               S   s   dS )NTr   r   r   r   r   <lambda>      zJExceptionalTestCase.testErrorsInBool.<locals>.trueAsBool.<locals>.<lambda>zShould NOT see this)failr   )r}   r   r   r   
trueAsBool  s    z8ExceptionalTestCase.testErrorsInBool.<locals>.trueAsBoolc            	      s&    dd   d W d Q R X d S )Nc               S   s   dS )NFr   r   r   r   r   r     r   zKExceptionalTestCase.testErrorsInBool.<locals>.falseAsBool.<locals>.<lambda>zShould raise)r   r   )r}   r   r   r   falseAsBool  s    z9ExceptionalTestCase.testErrorsInBool.<locals>.falseAsBoolc            	      s&    dd   d W d Q R X d S )Nc               S   s   dd S )Nr,   r   r   r   r   r   r   r   !  r   zJExceptionalTestCase.testErrorsInBool.<locals>.failAsBool.<locals>.<lambda>zShould NOT see this)r   r   )r}   r   r   r   
failAsBool   s    z8ExceptionalTestCase.testErrorsInBool.<locals>.failAsBool)r8   r3   AssertionErrorr   )r   r   r   r   r   )r}   r   r   testErrorsInBool  s    z$ExceptionalTestCase.testErrorsInBoolN)r   r   r   r~   r   rl   r   r   r   r   r   r   r   r   r   r   r   r   r   r{   c  s   
r{   c               @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )NonLocalFlowControlTestCasec          	   C   sF   d}x0|d7 }t   |d7 }P W d Q R X |d7 }qW | |d d S )Nr   r,   
   d      )r!   rM   )r   counterr   r   r   testWithBreak(  s    z)NonLocalFlowControlTestCase.testWithBreakc          	   C   sP   d}x:|d7 }|dkrP t   |d7 }wW d Q R X |d7 }qW | |d d S )Nr   r,   r-   r   r      )r!   rM   )r   r   r   r   r   testWithContinue2  s    z,NonLocalFlowControlTestCase.testWithContinuec             C   s   dd }|  | d d S )Nc           	   S   s8   d} x.| d7 } t   | d7 } | S Q R X | d7 } qW d S )Nr   r,   r   r   )r!   )r   r   r   r   r1   ?  s    
z7NonLocalFlowControlTestCase.testWithReturn.<locals>.foor   )rM   )r   r1   r   r   r   testWithReturn>  s    z*NonLocalFlowControlTestCase.testWithReturnc             C   s&   dd }t | }| |ddg d S )Nc            	   s   s"   t   dV  dV  W d Q R X d S )Nr      )r!   r   r   r   r   genJ  s    z6NonLocalFlowControlTestCase.testWithYield.<locals>.genr   r   )listrM   )r   r   xr   r   r   testWithYieldI  s    
z)NonLocalFlowControlTestCase.testWithYieldc          	   C   sf   d}y2|d7 }t   |d7 }tW d Q R X |d7 }W n  tk
rV   | |d Y nX | d d S )Nr   r,   r   r   r   zDidn't raise RuntimeError)r!   r%   rM   r   )r   r   r   r   r   testWithRaiseQ  s    z)NonLocalFlowControlTestCase.testWithRaiseN)r   r   r   r   r   r   r   r   r   r   r   r   r   &  s
   
r   c               @   s   e Zd Zdd Zdd ZdS )AssignmentTargetTestCasec          	   C   s  ddddgi}t  :|d d< | t| dg | |d d jt W d Q R X t  Bt| d d< | t| dg | |d d jt W d Q R X t  .|d< t| }|  | |ddg W d Q R X G dd d}| }t  |_| t	|dd W d Q R X d S )Nr,   r   r-   c               @   s   e Zd ZdS )z;AssignmentTargetTestCase.testSingleComplexTarget.<locals>.CN)r   r   r   r   r   r   r   Cm  s    r   r1   T)
r!   rM   r   keysr   r   valuessortr1   hasattr)r   targetsr   r   blahr   r   r   testSingleComplexTargeta  s      
z0AssignmentTargetTestCase.testSingleComplexTargetc          	   C   sX  G dd d}ddddgi}| :\|d d< |d d< |d d< |  |ddddgi W d Q R X | R\t| d d< t| d d< t| d d< |  |ddddgi W d Q R X | ,\|d< |d< |d< |  |dddd W d Q R X G dd	 d	}| }| <\|_|_|_|  |jd |  |jd |  |jd W d Q R X d S )
Nc               @   s   e Zd Zdd Zdd ZdS )z>AssignmentTargetTestCase.testMultipleComplexTargets.<locals>.Cc             S   s   dS )N)r,   r-   r   r   )r   r   r   r   r   t  r   zHAssignmentTargetTestCase.testMultipleComplexTargets.<locals>.C.__enter__c             S   s   d S )Nr   )r   tvtbr   r   r   r   u  r   zGAssignmentTargetTestCase.testMultipleComplexTargets.<locals>.C.__exit__N)r   r   r   r   r   r   r   r   r   r   s  s   r   r,   r   r-   r   )r,   r-   r   c               @   s   e Zd ZdS )z>AssignmentTargetTestCase.testMultipleComplexTargets.<locals>.BN)r   r   r   r   r   r   r   B}  s    r   )rM   r   r   ZoneZtwoZthree)r   r   r   r   r   r   r   r   testMultipleComplexTargetsr  s    & > z3AssignmentTargetTestCase.testMultipleComplexTargetsN)r   r   r   r   r   r   r   r   r   r   _  s   r   c               @   s   e Zd Zdd Zdd ZdS )ExitSwallowsExceptionTestCasec          	   C   sP   G dd d}y|  dd  W d Q R X W n t k
rJ   | d Y nX d S )Nc               @   s   e Zd Zdd Zdd ZdS )zSExitSwallowsExceptionTestCase.testExitTrueSwallowsException.<locals>.AfricanSwallowc             S   s   d S )Nr   )r   r   r   r   r     r   z]ExitSwallowsExceptionTestCase.testExitTrueSwallowsException.<locals>.AfricanSwallow.__enter__c             S   s   dS )NTr   )r   r   r   r   r   r   r   r     r   z\ExitSwallowsExceptionTestCase.testExitTrueSwallowsException.<locals>.AfricanSwallow.__exit__N)r   r   r   r   r   r   r   r   r   AfricanSwallow  s   r   r,   r   z,ZeroDivisionError should have been swallowed)r   r   )r   r   r   r   r   testExitTrueSwallowsException  s    z;ExitSwallowsExceptionTestCase.testExitTrueSwallowsExceptionc          	   C   sP   G dd d}y|  dd  W d Q R X W n t k
r@   Y nX | d d S )Nc               @   s   e Zd Zdd Zdd ZdS )zZExitSwallowsExceptionTestCase.testExitFalseDoesntSwallowException.<locals>.EuropeanSwallowc             S   s   d S )Nr   )r   r   r   r   r     r   zdExitSwallowsExceptionTestCase.testExitFalseDoesntSwallowException.<locals>.EuropeanSwallow.__enter__c             S   s   dS )NFr   )r   r   r   r   r   r   r   r     r   zcExitSwallowsExceptionTestCase.testExitFalseDoesntSwallowException.<locals>.EuropeanSwallow.__exit__N)r   r   r   r   r   r   r   r   r   EuropeanSwallow  s   r   r,   r   z)ZeroDivisionError should have been raised)r   r   )r   r   r   r   r   #testExitFalseDoesntSwallowException  s    zAExitSwallowsExceptionTestCase.testExitFalseDoesntSwallowExceptionN)r   r   r   r   r   r   r   r   r   r     s   
r   c               @   st   e Zd ZG dd deZG dd deZG dd deZG dd deZd	d
 Zdd Z	dd Z
dd Zdd ZdS )
NestedWithc               @   s&   e Zd Zd	ddZdd Zdd ZdS )
zNestedWith.DummyNFc             C   s(   |d kr| }|| _ || _d| _d| _d S )NF)r   gobbler   r	   )r   r   r   r   r   r   r     s    zNestedWith.Dummy.__init__c             C   s   d| _ | jS )NT)r   r   )r   r   r   r   r     s    zNestedWith.Dummy.__enter__c             G   s   d| _ || _| jrdS d S )NT)r	   r)   r   )r   r)   r   r   r   r     s    zNestedWith.Dummy.__exit__)NF)r   r   r   r   r   r   r   r   r   r   Dummy  s   
r   c               @   s   e Zd Zdd ZdS )zNestedWith.InitRaisesc             C   s
   t  d S )N)r%   )r   r   r   r   r     r   zNestedWith.InitRaises.__init__N)r   r   r   r   r   r   r   r   
InitRaises  s   r   c               @   s   e Zd Zdd Zdd ZdS )zNestedWith.EnterRaisesc             C   s
   t  d S )N)r%   )r   r   r   r   r     r   z NestedWith.EnterRaises.__enter__c             G   s   d S )Nr   )r   r)   r   r   r   r     r   zNestedWith.EnterRaises.__exit__N)r   r   r   r   r   r   r   r   r   EnterRaises  s   r   c               @   s   e Zd Zdd Zdd ZdS )zNestedWith.ExitRaisesc             C   s   d S )Nr   )r   r   r   r   r     r   zNestedWith.ExitRaises.__enter__c             G   s
   t  d S )N)r%   )r   r)   r   r   r   r     r   zNestedWith.ExitRaises.__exit__N)r   r   r   r   r   r   r   r   r   
ExitRaises  s   r   c          
   C   s\   |   2}|   }| |j | |j W d Q R X W d Q R X | |j | |j d S )N)r   rU   r   r	   )r   r   r   r   r   r   testNoExceptions  s
     zNestedWith.testNoExceptionsc          
   C   sV   y,|   }|   W d Q R X W d Q R X W n   Y nX | |j | |j d S )N)r   r   rU   r   r	   )r   r   r   r   r   testExceptionInExprList  s    z"NestedWith.testExceptionInExprListc          
   C   sr   y6|   $}|   | d W d Q R X W d Q R X W n tk
rJ   Y nX | d | |j | |j d S )Nzbody of bad with executedzRuntimeError not reraised)r   r   r   r%   rU   r   r	   )r   r   r   r   r   testExceptionInEnter  s    "
zNestedWith.testExceptionInEnterc          
   C   sl   d}| j dd}|  
 d}W d Q R X W d Q R X | |j | |j | | | |jd d  d S )NFT)r   r   )r   r   rU   r   r	   ZassertNotEqualr)   )r   Zbody_executedr   r   r   r   testExceptionInExit  s    
zNestedWith.testExceptionInExitc          
   C   sl   | j ddV\}}| j dd:\}}| d| | d| | d| | d| W d Q R X W d Q R X d S )N)r,   r-   )r   )r      r,   r-   r   r   )r   rM   )r   Za1Za2Zb1Zb2r   r   r   testEnterReturnsTuple  s    z NestedWith.testEnterReturnsTupleN)r   r   r   r8   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s   		r   __main__)
__author__Z	__email__r(   Zunittestcollectionsr   
contextlibr   r   r   r   r8   r   r!   r"   r/   ZTestCaser0   rR   rb   rm   r{   r   r   r   r   r   mainr   r   r   r   <module>   s0   
$S/5A D9&L
