B
    18\g                 @   sH  d Z 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rDe  dS )&z7Unit tests for the with statement specified in PEP 343.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__ R/oak/stanford/groups/akundaje/marinovg/programs/Python-3.7.3/Lib/test/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)   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   r0   Y   s   r0   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   r3   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   r7   c           	      s     } |  W d Q R X d S )Nr   )r2   )r7   r   r   fooLacksEnteru   s     z?FailureTestCase.testEnterAttributeError1.<locals>.fooLacksEnterr   )objectassertRaisesRegexAttributeError)r   r8   r   )r7   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   )r2   )r=   r   r   fooLacksEnterAndExit~   s     zFFailureTestCase.testEnterAttributeError2.<locals>.fooLacksEnterAndExitr   )r9   r:   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   )r2   )r@   r   r   fooLacksExit   s     z<FailureTestCase.testExitAttributeError.<locals>.fooLacksExitr   )r9   r:   r;   )r   rA   r   )r@   r   testExitAttributeError   s    z&FailureTestCase.testExitAttributeErrorc             C   s   dd }|  t|| d S )Nc             S   s   t | dd d S )N single)compile)sr   r   r   shouldRaiseSyntaxError   s    zGFailureTestCase.assertRaisesSyntaxError.<locals>.shouldRaiseSyntaxError)r4   SyntaxError)r   ZcodestrrG   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)rI   )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)rI   )r   r   r   r   ,testAssignmentToTupleOnlyContainingNoneError   s    
z<FailureTestCase.testAssignmentToTupleOnlyContainingNoneErrorc             C   s   |  d d S )Nz%with mock as (foo, None, bar):
  pass)rI   )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   rM   c           	      s"     } d _ | _ W d Q R X d S )N)r2   )ct)rM   r   r   r   shouldThrow   s    z4FailureTestCase.testEnterThrows.<locals>.shouldThrow)r9   r4   r%   assertEqualr2   )r   rO   r   )rM   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   rS   c            	      s      W d Q R X d S )Nr   r   )rS   r   r   rO      s    z3FailureTestCase.testExitThrows.<locals>.shouldThrow)r9   r4   r%   )r   rO   r   )rS   r   testExitThrows   s    zFailureTestCase.testExitThrowsN)r   r   r   r6   r<   r?   rB   rI   rJ   rK   rL   rQ   rT   r   r   r   r   r1   j   s   
	
r1   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	   rP   r
   )r   mock_managerr   r   r   assertInWithManagerInvariants   s    z:ContextmanagerAssertionMixin.assertInWithManagerInvariantsc             C   s*   |  |j |  |j | |j| d S )N)rX   r   r	   rP   r
   )r   rZ   r
   r   r   r    assertAfterWithManagerInvariants   s    z=ContextmanagerAssertionMixin.assertAfterWithManagerInvariantsc             C   s   |  |d d S )N)NNN)r\   )r   rZ   r   r   r   'assertAfterWithManagerInvariantsNoError   s    zDContextmanagerAssertionMixin.assertAfterWithManagerInvariantsNoErrorc             C   s   |  |j | |j d S )N)rX   r   rY   r   )r   mock_generatorr   r   r   assertInWithGeneratorInvariants   s    z<ContextmanagerAssertionMixin.assertInWithGeneratorInvariantsc             C   s   |  |j |  |j d S )N)rX   r   r   )r   r^   r   r   r   )assertAfterWithGeneratorInvariantsNoError   s    zFContextmanagerAssertionMixin.assertAfterWithGeneratorInvariantsNoErrorc             C   s
   | j d S )N)rV   )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-   )	rX   r   r	   rP   r
   rV   r   assertIsInstanceassertIsNot)r   rZ   exc_typer   r   r   )assertAfterWithManagerInvariantsWithError   s    
zFContextmanagerAssertionMixin.assertAfterWithManagerInvariantsWithErrorc             C   s   |  |j |  |j d S )N)rX   r   r   )r   r^   r   r   r   +assertAfterWithGeneratorInvariantsWithError   s    zHContextmanagerAssertionMixin.assertAfterWithGeneratorInvariantsWithError)N)r   r   r   rW   r[   r\   r]   r_   r`   ra   re   rf   r   r   r   r   rU      s   
rU   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!   r]   )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   r2   r   r   r   testInlineGeneratorBoundSyntax   s    z5NonexceptionalTestCase.testInlineGeneratorBoundSyntaxc          	   C   s.   d }t  }| | W d Q R X | | d S )N)r!   r_   r`   )r   r2   r   r   r   *testInlineGeneratorBoundToExistingVariable   s    zANonexceptionalTestCase.testInlineGeneratorBoundToExistingVariablec          	   C   s0   t  | _| | j W d Q R X | | j d S )N)r!   r2   r_   r`   )r   r   r   r   (testInlineGeneratorBoundToDottedVariable  s    
z?NonexceptionalTestCase.testInlineGeneratorBoundToDottedVariablec          	   C   sB   t  }|}| | | | W d Q R X | | | | d S )N)r!   r_   r[   r`   r]   )r   r    r2   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!   r[   r_   r]   r`   )r   mock_ar2   mock_bbarr   r   r   testNestedSingleStatements  s    






z1NonexceptionalTestCase.testNestedSingleStatementsN)
r   r   r   rh   ri   rj   rk   rl   rm   rq   r   r   r   r   rg      s   rg   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"   r[   r]   )r   mr2   r   r   r   testSingleArgBoundToNonTuple'  s    
z9NestedNonexceptionalTestCase.testSingleArgBoundToNonTuplec          	   C   s2   t  }t|}| | W d Q R X | | d S )N)r!   r"   r[   r]   )r   rt   r2   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!   )r2   rp   r   r   r   shouldThrowValueError8  s    ziNestedNonexceptionalTestCase.testSingleArgBoundToMultipleElementTupleError.<locals>.shouldThrowValueError)r4   
ValueError)r   rw   r   r   r   -testSingleArgBoundToMultipleElementTupleError7  s    zJNestedNonexceptionalTestCase.testSingleArgBoundToMultipleElementTupleErrorc          	   C   sJ   t  }t|}| | | | | W d Q R X | | | | d S )N)r!   r0   r[   r]   )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!   r0   r[   r]   )r   rt   norz   r   r   r   testMultipleArgUnboundF  s    





z3NestedNonexceptionalTestCase.testMultipleArgUnboundc          	   C   s|   t t t t }|4\}}}| | | | | | | | W d Q R X | | | | | | | | d S )N)r0   r!   r_   r[   r`   r]   )r   rz   rt   r|   r}   r   r   r   testMultipleArgBoundU  s    





z1NestedNonexceptionalTestCase.testMultipleArgBoundN)
r   r   r   rs   ru   rv   ry   r{   r~   r   r   r   r   r   rr   !  s   	rr   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)resourcer[   r_   ra   r   )cmr   r   r   rO   f  s    
z;ExceptionalTestCase.testSingleResource.<locals>.shouldThrow)r!   r4   r%   re   rf   r   )r   rO   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   rO   q  s    z@ExceptionalTestCase.testExceptionNormalized.<locals>.shouldThrow)r!   r4   ZeroDivisionErrorre   )r   rO   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)r2   rp   r[   r_   ra   r   )rn   ro   r   r   r   rO   |  s    

zCExceptionalTestCase.testNestedSingleStatements.<locals>.shouldThrow)r!   r4   r%   re   rf   r2   rp   )r   rO   r   )rn   ro   r   r   rq   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_br[   r_   ra   r   )cm_acm_brz   r   r   r   rO     s    


zOExceptionalTestCase.testMultipleResourcesInSingleStatement.<locals>.shouldThrow)r!   r0   r4   r%   re   rf   r   r   )r   rO   r   )r   r   rz   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)r2   r[   r_   ra   rp   r   )rn   ro   r   r   r   rO     s    
zPExceptionalTestCase.testNestedExceptionBeforeInnerStatement.<locals>.shouldThrow)r!   rp   r4   r%   re   rf   r2   rP   rY   r   r	   r
   )r   rO   r   )rn   ro   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)r2   rp   r[   r_   ra   r   )rn   ro   r   r   r   rO     s    

zOExceptionalTestCase.testNestedExceptionAfterInnerStatement.<locals>.shouldThrow)	r!   r4   r%   re   r]   rf   r2   r`   rp   )r   rO   r   )rn   ro   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   rO     s    zAExceptionalTestCase.testRaisedStopIteration1.<locals>.shouldThrowz	from with)r   r:   r   )r   rO   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   rO     s    zAExceptionalTestCase.testRaisedStopIteration2.<locals>.shouldThrowz	from with)r9   r:   r   )r   rO   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   rO     s    zAExceptionalTestCase.testRaisedStopIteration3.<locals>.shouldThrow)r   r4   r   )r   rO   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   rO     s    zAExceptionalTestCase.testRaisedGeneratorExit1.<locals>.shouldThrow)r   r4   r   )r   rO   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   rO     s    zAExceptionalTestCase.testRaisedGeneratorExit2.<locals>.shouldThrow)r9   r4   r   )r   rO   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)r9   r4   AssertionErrorr   )r   r   r   r   r   )r   r   r   testErrorsInBool  s    z$ExceptionalTestCase.testErrorsInBoolN)r   r   r   r   r   rq   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!   rP   )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!   rP   )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   r2   ?  s    
z7NonLocalFlowControlTestCase.testWithReturn.<locals>.foor   )rP   )r   r2   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   )listrP   )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%   rP   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   r2   T)
r!   rP   r   keysr   r   valuessortr2   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   )rP   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   rX   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   rX   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%   rX   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   rX   r   r	   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   rP   )r   Za1Za2Zb1Zb2r   r   r   testEnterReturnsTuple  s    z NestedWith.testEnterReturnsTupleN)r   r   r   r9   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s   		r   __main__)__doc__
__author__Z	__email__r(   unittestcollectionsr   
contextlibr   r   r   r   r9   r   r!   r"   r0   TestCaser1   rU   rg   rr   r   r   r   r   r   r   mainr   r   r   r   <module>   s2   
$S/5A D9&L
