a
    zeg                     @   sH  d Z dZddlZddlZddlmZ ddlmZ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 )%z
Mike Blandzmbland at acm dot org    N)deque)_GeneratorContextManagercontextmanagernullcontextc                       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.9/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 r   )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 r   )yieldedstoppedr   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| _nd| _0 d S r   )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 r    )managersenteredr   r-   r   r   r   r	   7   s    zNested.__init__c                 C   sj   | j d urtdt | _ g }z*| jD ]}||  | j | q&W n   | jt	  s` Y n0 |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   sd   |}| j D ],}z|j| rd}W q
   t }Y q
0 q
d | _ ||ur`|d |d |d d S )NNNNr         )r.   r   r3   r4   with_traceback)r   r4   exr6   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| g|R   d| _d| _d | _d S r   )r,   r	   r
   r   r   r/   r   r   r   r	   Z   s    zMockNested.__init__c                 C   s   d| _ t| S r   )r
   r,   r   r   r   r   r   r   `   s    zMockNested.__enter__c                 G   s   d| _ || _tj| g|R  S r   )r   r   r,   r   r   r4   r   r   r   r   d   s    zMockNested.__exit__Nr<   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    n1 s0    Y  d S r    foor   r   r   r   fooNotDeclaredl   s    z5FailureTestCase.testNameError.<locals>.fooNotDeclared)assertRaises	NameError)r   rB   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 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   rF   c                     s.     } |  W d    n1 s 0    Y  d S r    r   r@   rF   r   r   fooLacksEnteru   s    z?FailureTestCase.testEnterAttributeError1.<locals>.fooLacksEnterr   objectassertRaisesRegexAttributeError)r   rH   r   rG   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   rO   c                     s.     } |  W d    n1 s 0    Y  d S r    r   r@   rO   r   r   fooLacksEnterAndExit~   s    zFFailureTestCase.testEnterAttributeError2.<locals>.fooLacksEnterAndExitr   rI   )r   rQ   r   rP   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 r    r   r   r   r   r   r      s    zCFailureTestCase.testExitAttributeError.<locals>.LacksExit.__enter__N)r   r   r   r   r   r   r   r   	LacksExit   s   rS   c                     s.     } |  W d    n1 s 0    Y  d S r    r   r@   rS   r   r   fooLacksExit   s    z<FailureTestCase.testExitAttributeError.<locals>.fooLacksExitr   rI   )r   rU   r   rT   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)rC   SyntaxError)r   ZcodestrrZ   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r\   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,):
  passr]   r   r   r   r   ,testAssignmentToTupleOnlyContainingNoneError   s    
z<FailureTestCase.testAssignmentToTupleOnlyContainingNoneErrorc                 C   s   |  d d S )Nz%with mock as (foo, None, bar):
  passr]   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r0   r   r   r   r   r      s    z>FailureTestCase.testEnterThrows.<locals>.EnterThrows.__enter__c                 W   s   d S r    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   rc   c                     s6     } d _ | _ W d    n1 s(0    Y  d S r    r@   )ctrc   r   r   r   shouldThrow   s    z4FailureTestCase.testEnterThrows.<locals>.shouldThrow)rJ   rC   r0   assertEqualrA   r   rf   r   re   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 r    r   r   r   r   r   r      s    z<FailureTestCase.testExitThrows.<locals>.ExitThrows.__enter__c                 W   s   t dd S )N*   ra   r   r   r   r   r      s    z;FailureTestCase.testExitThrows.<locals>.ExitThrows.__exit__Nrb   r   r   r   r   
ExitThrows   s   rk   c                      s*      W d    n1 s0    Y  d S r    r   r   rk   r   r   rf      s    z3FailureTestCase.testExitThrows.<locals>.shouldThrow)rJ   rC   r0   rh   r   rl   r   testExitThrows   s    zFailureTestCase.testExitThrowsN)r   r   r   rE   rM   rR   rV   r\   r^   r_   r`   ri   rm   r   r   r   r   r?   j   s   
	
r?   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)r0   TEST_EXCEPTIONr   r   r   r   setUp   s    z"ContextmanagerAssertionMixin.setUpc                 C   s*   |  |j | |j | |jd  d S r    )
assertTruer
   assertFalser   rg   r   r   mock_managerr   r   r   assertInWithManagerInvariants   s    z:ContextmanagerAssertionMixin.assertInWithManagerInvariantsc                 C   s*   |  |j |  |j | |j| d S r    )rq   r
   r   rg   r   )r   rt   r   r   r   r    assertAfterWithManagerInvariants   s    z=ContextmanagerAssertionMixin.assertAfterWithManagerInvariantsc                 C   s   |  |d d S )Nr7   )rv   rs   r   r   r   'assertAfterWithManagerInvariantsNoError   s    zDContextmanagerAssertionMixin.assertAfterWithManagerInvariantsNoErrorc                 C   s   |  |j | |j d S r    )rq   r'   rr   r(   r   Zmock_generatorr   r   r   assertInWithGeneratorInvariants   s    z<ContextmanagerAssertionMixin.assertInWithGeneratorInvariantsc                 C   s   |  |j |  |j d S r    rq   r'   r(   rx   r   r   r   )assertAfterWithGeneratorInvariantsNoError   s    zFContextmanagerAssertionMixin.assertAfterWithGeneratorInvariantsNoErrorc                 C   s
   | j d S r    )ro   r   r   r   r   raiseTestException   s    z/ContextmanagerAssertionMixin.raiseTestExceptionNc                 C   sx   |  |j |  |j |d u r>| |jd | j t| j}| |jd | | |jd | | |jd d  d S )Nr8   r   r9   )	rq   r
   r   rg   r   ro   r   ZassertIsInstanceZassertIsNot)r   rt   exc_typer   r   r   )assertAfterWithManagerInvariantsWithError   s    
zFContextmanagerAssertionMixin.assertAfterWithManagerInvariantsWithErrorc                 C   s   |  |j |  |j d S r    rz   rx   r   r   r   +assertAfterWithGeneratorInvariantsWithError   s    zHContextmanagerAssertionMixin.assertAfterWithGeneratorInvariantsWithError)N)r   r   r   rp   ru   rv   rw   ry   r{   r|   r~   r   r   r   r   r   rn      s    
rn   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    n1 s0    Y  d S r    r+   r   r   r   r   testInlineGeneratorSyntax   s    z0NonexceptionalTestCase.testInlineGeneratorSyntaxc                 C   s8   t  }| W d    n1 s 0    Y  | | d S r    )r+   rw   )r   r*   r   r   r   testUnboundGenerator   s    z+NonexceptionalTestCase.testUnboundGeneratorc                 C   s>   t  }| | W d    n1 s&0    Y  | | d S r    r+   ry   r{   r   rA   r   r   r   testInlineGeneratorBoundSyntax   s    (z5NonexceptionalTestCase.testInlineGeneratorBoundSyntaxc                 C   sB   d }t  }| | W d    n1 s*0    Y  | | d S r    r   r   r   r   r   *testInlineGeneratorBoundToExistingVariable   s    (zANonexceptionalTestCase.testInlineGeneratorBoundToExistingVariablec                 C   sD   t  | _| | j W d    n1 s*0    Y  | | j d S r    )r+   rA   ry   r{   r   r   r   r   (testInlineGeneratorBoundToDottedVariable  s    
*z?NonexceptionalTestCase.testInlineGeneratorBoundToDottedVariablec                 C   sV   t  }|$}| | | | W d    n1 s40    Y  | | | | d S r    )r+   ry   ru   r{   rw   )r   r*   rA   r   r   r   testBoundGenerator  s    
(
z)NonexceptionalTestCase.testBoundGeneratorc              	   C   s   t  }|}t  }|8}| | | | | | | | W d    n1 sT0    Y  | | | | | | | | W d    n1 s0    Y  | | | | d S r    )r+   ru   ry   rw   r{   )r   mock_arA   mock_bbarr   r   r   testNestedSingleStatements  s    


(


(
z1NonexceptionalTestCase.testNestedSingleStatementsN)
r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s   r   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    n1 s 0    Y  d S r    r,   r+   r   r   r   r   "testSingleArgInlineGeneratorSyntax#  s    z?NestedNonexceptionalTestCase.testSingleArgInlineGeneratorSyntaxc                 C   sF   t  }t|}| | W d    n1 s.0    Y  | | d S r    r+   r,   ru   rw   r   mrA   r   r   r   testSingleArgBoundToNonTuple'  s    
(z9NestedNonexceptionalTestCase.testSingleArgBoundToNonTuplec                 C   sF   t  }t|}| | W d    n1 s.0    Y  | | d S r    r   r   r   r   r   2testSingleArgBoundToSingleElementParenthesizedList/  s    
(zONestedNonexceptionalTestCase.testSingleArgBoundToSingleElementParenthesizedListc                 C   s   dd }|  t| d S )Nc                  S   s2   t t \} }W d    n1 s$0    Y  d S r    r   )rA   r   r   r   r   shouldThrowValueError8  s    ziNestedNonexceptionalTestCase.testSingleArgBoundToMultipleElementTupleError.<locals>.shouldThrowValueError)rC   
ValueError)r   r   r   r   r   -testSingleArgBoundToMultipleElementTupleError7  s    zJNestedNonexceptionalTestCase.testSingleArgBoundToMultipleElementTupleErrorc                 C   s^   t  }t|}|$ | | | | W d    n1 s<0    Y  | | | | d S r    r+   r=   ru   rw   )r   r%   mock_nestedr   r   r   testSingleArgUnbound=  s    
(
z1NestedNonexceptionalTestCase.testSingleArgUnboundc                 C   s   t  }t  }t  }t|||}|8 | | | | | | | | W d    n1 s`0    Y  | | | | | | | | d S r    r   )r   r   nor   r   r   r   testMultipleArgUnboundF  s    


(


z3NestedNonexceptionalTestCase.testMultipleArgUnboundc                 C   s   t t t t }|>\}}}| | | | | | | | W d    n1 sZ0    Y  | | | | | | | | d S r    )r=   r+   ry   ru   r{   rw   )r   r   r   r   r   r   r   r   testMultipleArgBoundU  s    


(


z1NestedNonexceptionalTestCase.testMultipleArgBoundN)
r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   !  s   	r   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                      sH    0_   j    W d    n1 s:0    Y  d S r    )resourceru   ry   r|   r   cmr   r   r   rf   f  s    
z;ExceptionalTestCase.testSingleResource.<locals>.shouldThrow)r+   rC   r0   r~   r   r   rh   r   r   r   testSingleResourced  s
    
z&ExceptionalTestCase.testSingleResourcec                    s0   t    fdd}t|  t d S )Nc                      s2    _ dd  W d    n1 s$0    Y  d S Nr8   r   )r   r   r   r   r   rf   q  s    z@ExceptionalTestCase.testExceptionNormalized.<locals>.shouldThrow)r+   rC   ZeroDivisionErrorr~   rh   r   r   r   testExceptionNormalizedo  s    z+ExceptionalTestCase.testExceptionNormalizedc                    sX   t   t   fdd}t|    j j d S )Nc                	      s    l_ F_   j  j   W d    n1 sX0    Y  W d    n1 sv0    Y  d S r    rA   r   ru   ry   r|   r   r   r   r   r   r   rf   |  s    

zCExceptionalTestCase.testNestedSingleStatements.<locals>.shouldThrow)r+   rC   r0   r~   r   rA   r   rh   r   r   r   r   y  s    

z.ExceptionalTestCase.testNestedSingleStatementsc                    sn   t   t  t  fdd}t|     j j d S )Nc                      sn   V\_ _    j  j   W d    n1 s`0    Y  d S r    )
resource_a
resource_bru   ry   r|   r   Zcm_aZcm_br   r   r   r   rf     s    


zOExceptionalTestCase.testMultipleResourcesInSingleStatement.<locals>.shouldThrow)r+   r=   rC   r0   r~   r   r   r   rh   r   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                	      sn    V_   j    _W d    n1 sB0    Y  W d    n1 s`0    Y  d S r    )rA   ru   ry   r|   r   r   r   r   r   rf     s    
zPExceptionalTestCase.testNestedExceptionBeforeInnerStatement.<locals>.shouldThrow)r+   r   rC   r0   r~   r   rA   rg   rr   r
   r   r   rh   r   r   r   'testNestedExceptionBeforeInnerStatement  s    
z;ExceptionalTestCase.testNestedExceptionBeforeInnerStatementc                    sX   t   t   fdd}t|    j j d S )Nc                	      s    l_ >_   j  j W d    n1 sP0    Y    W d    n1 sv0    Y  d S r    r   r   r   r   r   rf     s    

*zOExceptionalTestCase.testNestedExceptionAfterInnerStatement.<locals>.shouldThrow)	r+   rC   r0   r~   rw   r   rA   r{   r   rh   r   r   r   &testNestedExceptionAfterInnerStatement  s    

z:ExceptionalTestCase.testNestedExceptionAfterInnerStatementc                    sN   t dd   fdd}| td |  W d    n1 s@0    Y  d S )Nc                   s   s
   d V  d S r    r   r   r   r   r   r     s    z8ExceptionalTestCase.testRaisedStopIteration1.<locals>.cmc                      s2      t dW d    n1 s$0    Y  d S N	from withStopIterationr   r   r   r   rf     s    zAExceptionalTestCase.testRaisedStopIteration1.<locals>.shouldThrowr   )r   rK   r   rh   r   r   r   testRaisedStopIteration1  s
    
z,ExceptionalTestCase.testRaisedStopIteration1c                    sR   G dd dt   fdd}| td |  W d    n1 sD0    Y  d S )Nc                   @   s   e Zd Zdd Zdd ZdS )z8ExceptionalTestCase.testRaisedStopIteration2.<locals>.cmc                 S   s   d S r    r   r   r   r   r   r     s    zBExceptionalTestCase.testRaisedStopIteration2.<locals>.cm.__enter__c                 S   s   d S r    r   r   r   r   r   r     s    zAExceptionalTestCase.testRaisedStopIteration2.<locals>.cm.__exit__Nrb   r   r   r   r   r     s   r   c                      s2      t dW d    n1 s$0    Y  d S r   r   r   r   r   r   rf     s    zAExceptionalTestCase.testRaisedStopIteration2.<locals>.shouldThrowr   )rJ   rK   r   rh   r   r   r   testRaisedStopIteration2  s    z,ExceptionalTestCase.testRaisedStopIteration2c                    sL   t dd   fdd}| t |  W d    n1 s>0    Y  d S )Nc                   s   s
   d V  d S r    r   r   r   r   r   r     s    z8ExceptionalTestCase.testRaisedStopIteration3.<locals>.cmc                      s6      t tg W d    n1 s(0    Y  d S r    )nextiterr   r   r   r   rf     s    zAExceptionalTestCase.testRaisedStopIteration3.<locals>.shouldThrow)r   rC   r   rh   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 r    r   r   r   r   r   r     s    z8ExceptionalTestCase.testRaisedGeneratorExit1.<locals>.cmc                      s2      t dW d    n1 s$0    Y  d S r   GeneratorExitr   r   r   r   rf     s    zAExceptionalTestCase.testRaisedGeneratorExit1.<locals>.shouldThrow)r   rC   r   rh   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 r    r   r   r   r   r   r     s    zBExceptionalTestCase.testRaisedGeneratorExit2.<locals>.cm.__enter__c                 S   s   d S r    r   r   r   r   r   r     s    zAExceptionalTestCase.testRaisedGeneratorExit2.<locals>.cm.__exit__Nrb   r   r   r   r   r     s   r   c                      s2      t dW d    n1 s$0    Y  d S r   r   r   r   r   r   rf     s    zAExceptionalTestCase.testRaisedGeneratorExit2.<locals>.shouldThrow)rJ   rC   r   rh   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 r    r   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   Z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 r    r   r   abcr   r   r   r     s    z9ExceptionalTestCase.testErrorsInBool.<locals>.cm.__exit__Nr<   r   r   r   r   r     s   r   c                      s:    dd   d W d    n1 s,0    Y  d S )Nc                   S   s   dS r   r   r   r   r   r   <lambda>      zJExceptionalTestCase.testErrorsInBool.<locals>.trueAsBool.<locals>.<lambda>Should NOT see thisfailr   r   r   r   
trueAsBool  s    z8ExceptionalTestCase.testErrorsInBool.<locals>.trueAsBoolc                      s:    dd   d W d    n1 s,0    Y  d S )Nc                   S   s   dS r   r   r   r   r   r   r     r   zKExceptionalTestCase.testErrorsInBool.<locals>.falseAsBool.<locals>.<lambda>zShould raiser   r   r   r   r   falseAsBool  s    z9ExceptionalTestCase.testErrorsInBool.<locals>.falseAsBoolc                      s:    dd   d W d    n1 s,0    Y  d S )Nc                   S   s   dd S r   r   r   r   r   r   r   !  r   zJExceptionalTestCase.testErrorsInBool.<locals>.failAsBool.<locals>.<lambda>r   r   r   r   r   r   
failAsBool   s    z8ExceptionalTestCase.testErrorsInBool.<locals>.failAsBool)rJ   rC   AssertionErrorr   )r   r   r   r   r   r   r   testErrorsInBool  s    z$ExceptionalTestCase.testErrorsInBoolN)r   r   r   r   r   r   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   sb   d}|d7 }t  & |d7 }W d    qRW d    n1 s>0    Y  |d7 }q| |d d S )Nr   r8   
   d      r+   rg   r   counterr   r   r   testWithBreak(  s    ,
z)NonLocalFlowControlTestCase.testWithBreakc                 C   sl   d}|d7 }|dkrq\t  & |d7 }W d    qW d    n1 sH0    Y  |d7 }q| |d d S )Nr   r8   r9   r   r      r   r   r   r   r   testWithContinue2  s    ,
z,NonLocalFlowControlTestCase.testWithContinuec                 C   s   dd }|  | d d S )Nc                  S   sL   d} | d7 } t   | d7 } | W  d    S 1 s40    Y  | d7 } qd S )Nr   r8   r   r   r   )r   r   r   r   rA   ?  s    "z7NonLocalFlowControlTestCase.testWithReturn.<locals>.foor   )rg   r   r   r   r   testWithReturn>  s    z*NonLocalFlowControlTestCase.testWithReturnc                 C   s&   dd }t | }| |ddg d S )Nc                   s   s6   t   dV  dV  W d    n1 s(0    Y  d S )Nr      r   r   r   r   r   genJ  s    z6NonLocalFlowControlTestCase.testWithYield.<locals>.genr   r   )listrg   )r   r   xr   r   r   testWithYieldI  s    
z)NonLocalFlowControlTestCase.testWithYieldc                 C   sx   d}zF|d7 }t   |d7 }tW d    n1 s60    Y  |d7 }W n tyh   | |d Y n0 | d d S )Nr   r8   r   r   r   zDidn't raise RuntimeError)r+   r0   rg   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d ZdS )AssignmentTargetTestCasec                 C   sl  dg di}t  D|d d< | t| dg | |d d jt W d    n1 s\0    Y  t  Lt| d d< | t| dg | |d d jt W d    n1 s0    Y  t  8|d< t| }|  | |ddg W d    n1 s0    Y  G dd d}| }t  $|_| t	|dd W d    n1 s^0    Y  d S )	Nr8   r   r8   r9   r   r9   c                   @   s   e Zd ZdS )z;AssignmentTargetTestCase.testSingleComplexTarget.<locals>.CNrN   r   r   r   r   Cm  r   r   rA   T)
r+   rg   r   keysr   r&   valuessortrA   hasattr)r   targetsr   r   blahr   r   r   testSingleComplexTargeta  s    440
z0AssignmentTargetTestCase.testSingleComplexTargetc                 C   s  G dd d}dg di}| B\|d d< |d d< |d d< |  |dg di W d    n1 sh0    Y  | Z\t| d d< t| d d< t| d d< |  |dg di W d    n1 s0    Y  | 6\|d< |d< |d	< |  |ddd	d W d    n1 s&0    Y  G d
d d}| }| F\|_|_|_|  |jd |  |jd |  |jd	 W d    n1 s0    Y  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r8   r9   r   r   r   r   r   r   r   t  r   zHAssignmentTargetTestCase.testMultipleComplexTargets.<locals>.C.__enter__c                 S   s   d S r    r   r   tvtbr   r   r   r   u  r   zGAssignmentTargetTestCase.testMultipleComplexTargets.<locals>.C.__exit__Nrb   r   r   r   r   r   s  s   r   r8   r   r   r9   r   )r   r9   r8   r   c                   @   s   e Zd ZdS )z>AssignmentTargetTestCase.testMultipleComplexTargets.<locals>.BNrN   r   r   r   r   B}  r   r   )rg   r   r   ZoneZtwoZthree)r   r   r   r   r   r   r   r   testMultipleComplexTargetsr  s    &2>24z3AssignmentTargetTestCase.testMultipleComplexTargetsc                 C   sb   t tdd@^}}}| |d | |ddg | |d W d    n1 sT0    Y  d S )Nr8      r9   r      )r   rangerg   r   r   r   r   testWithExtendedTargets  s    z0AssignmentTargetTestCase.testWithExtendedTargetsN)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 )ExitSwallowsExceptionTestCasec                 C   sb   G dd d}z2|  dd  W d    n1 s40    Y  W n t y\   | d Y n0 d S )Nc                   @   s   e Zd Zdd Zdd ZdS )zSExitSwallowsExceptionTestCase.testExitTrueSwallowsException.<locals>.AfricanSwallowc                 S   s   d S r    r   r   r   r   r   r     r   z]ExitSwallowsExceptionTestCase.testExitTrueSwallowsException.<locals>.AfricanSwallow.__enter__c                 S   s   dS r   r   r   r   r   r   r     r   z\ExitSwallowsExceptionTestCase.testExitTrueSwallowsException.<locals>.AfricanSwallow.__exit__Nrb   r   r   r   r   AfricanSwallow  s   r   r8   r   z,ZeroDivisionError should have been swallowedr   r   )r   r   r   r   r   testExitTrueSwallowsException  s    *z;ExitSwallowsExceptionTestCase.testExitTrueSwallowsExceptionc                 C   sb   G dd d}z2|  dd  W d    n1 s40    Y  W n t yR   Y n0 | 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 r    r   r   r   r   r   r     r   zdExitSwallowsExceptionTestCase.testExitFalseDoesntSwallowException.<locals>.EuropeanSwallow.__enter__c                 S   s   dS r   r   r   r   r   r   r     r   zcExitSwallowsExceptionTestCase.testExitFalseDoesntSwallowException.<locals>.EuropeanSwallow.__exit__Nrb   r   r   r   r   EuropeanSwallow  s   r   r8   r   z)ZeroDivisionError should have been raisedr   )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 u r| }|| _ || _d| _d| _d S r   )r   gobbler
   r   )r   r   r   r   r   r   r	     s    zNestedWith.Dummy.__init__c                 C   s   d| _ | jS r   )r
   r   r   r   r   r   r     s    zNestedWith.Dummy.__enter__c                 G   s   d| _ || _| jrdS d S r   )r   r4   r   r>   r   r   r   r     s    zNestedWith.Dummy.__exit__)NFr<   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 r    ra   r   r   r   r   r	     r   zNestedWith.InitRaises.__init__Nr)   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 r    ra   r   r   r   r   r     r   z NestedWith.EnterRaises.__enter__c                 G   s   d S r    r   r>   r   r   r   r     r   zNestedWith.EnterRaises.__exit__Nrb   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 r    r   r   r   r   r   r     r   zNestedWith.ExitRaises.__enter__c                 G   s
   t  d S r    ra   r>   r   r   r   r     r   zNestedWith.ExitRaises.__exit__Nrb   r   r   r   r   
ExitRaises  s   r  c              	   C   s   |   P}|   (}| |j | |j W d    n1 s@0    Y  W d    n1 s^0    Y  | |j | |j d S r    )r   rq   r
   r   )r   r   r   r   r   r   testNoExceptions  s
    HzNestedWith.testNoExceptionsc              	   C   s~   zT|   8}|   W d    n1 s*0    Y  W d    n1 sH0    Y  W n   Y n0 | |j | |j d S r    )r   r  rq   r
   r   r   r   r   r   r   testExceptionInExprList  s    @z"NestedWith.testExceptionInExprListc              	   C   s   z^|   B}|   | d W d    n1 s40    Y  W d    n1 sR0    Y  W n typ   Y n0 | d | |j | |j d S )Nzbody of bad with executedzRuntimeError not reraised)r   r  r   r0   rq   r
   r   r  r   r   r   testExceptionInEnter  s    J
zNestedWith.testExceptionInEnterc              	   C   s   d}| j dd<}|   d}W d    n1 s40    Y  W d    n1 sR0    Y  | |j | |j | | | |jd d  d S )NFT)r   r   )r   r  rq   r
   r   ZassertNotEqualr4   )r   Zbody_executedr   r   r   r   testExceptionInExit  s    @
zNestedWith.testExceptionInExitc              	   C   s   | j ddt\}}| j ddD\}}| d| | d| | d| | d| W d    n1 sh0    Y  W d    n1 s0    Y  d S )N)r8   r9   )r   )r      r8   r9   r   r	  )r   rg   )r   Za1Za2b1b2r   r   r   testEnterReturnsTuple  s    
z NestedWith.testEnterReturnsTupleN)r   r   r   rJ   r   r  r  r  r  r  r  r  r  r   r   r   r   r     s   		r   __main__)
__author__Z	__email__r3   Zunittestcollectionsr   
contextlibr   r   r   r   r%   rJ   r&   r+   r,   r=   ZTestCaser?   rn   r   r   r   r   r   r   r   r   mainr   r   r   r   <module>   s4   
	$S/5B D9,L
