
nU                 @   sz  d  d l  Z  d  d l Z d  d l Z d  d l m Z d  d l m Z m Z m	 Z	 m
 Z
 d  d l m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z d  d l m Z m Z d Z e r d Z e Z e j e Z d e Z d	 d
 d  Z e j  a! e j" Z# Gd d   d e$  Z% d e Z& e% d d  Z' Gd d   d e$  Z( Gd d   d e j)  Z* e d k rve j+   d S)    N)support)inPy3k	SomeClassis_instancecallable)
NonCallableMockCallableMixinpatchsentinel	MagicMockMockNonCallableMagicMockr	   DEFAULTcall)_patch_get_targetZ__builtin__builtinsz%s.PTModuleTc                s_   G  f d d   d t   } | sX   f d d   }   f d d   } | | _ | | _ |   S)Nc                   s"   e  Z d  Z   f d d   Z d S)z_get_proxy.<locals>.Proxyc                s   t    |  S)N)getattr)selfname)obj 6/usr/lib/python3/dist-packages/mock/tests/testpatch.py__getattr__   s    z%_get_proxy.<locals>.Proxy.__getattr__N)__name__
__module____qualname__r   r   )r   r   r   Proxy   s   r   c                s   t    | |  d  S)N)setattr)r   r   value)r   r   r   __setattr__"   s    z_get_proxy.<locals>.__setattr__c                s   t    |  d  S)N)delattr)r   r   )r   r   r   __delattr__$   s    z_get_proxy.<locals>.__delattr__)objectr    r"   )r   get_onlyr   r    r"   r   )r   r   
_get_proxy   s    		r%   c               @   sP   e  Z d  Z d d   Z d d   Z d d   Z d Z Gd d	   d	 e  Z d
 S)Fooc             C   s   d  S)Nr   )r   ar   r   r   __init__1   s    zFoo.__init__c             C   s   d  S)Nr   )r   r'   r   r   r   f3   s    zFoo.fc             C   s   d  S)Nr   )r   r   r   r   g5   s    zFoo.gbarc               @   s   e  Z d  Z d d   Z d S)zFoo.Barc             C   s   d  S)Nr   )r   r   r   r   r'   :   s    z	Foo.Bar.aN)r   r   r   r'   r   r   r   r   Bar9   s   r,   N)	r   r   r   r(   r)   r*   foor#   r,   r   r   r   r   r&   0   s
   r&   z%s.Fooc             C   s   d  S)Nr   )r'   br   r   r   function@   s    r/   c               @   sL   e  Z d  Z d d   Z d d   Z d d   Z d d   Z d	 d
   Z d S)	Containerc             C   s   i  |  _  d  S)N)values)r   r   r   r   r(   E   s    zContainer.__init__c             C   s   |  j  | S)N)r1   )r   r   r   r   r   __getitem__H   s    zContainer.__getitem__c             C   s   | |  j  | <d  S)N)r1   )r   r   r   r   r   r   __setitem__K   s    zContainer.__setitem__c             C   s   |  j  | =d  S)N)r1   )r   r   r   r   r   __delitem__N   s    zContainer.__delitem__c             C   s   t  |  j  S)N)iterr1   )r   r   r   r   __iter__Q   s    zContainer.__iter__N)r   r   r   r(   r2   r3   r4   r6   r   r   r   r   r0   D   s
   r0   c               @   s  e  Z d  Z d d d  Z d d   Z d d   Z d d	   Z d
 d   Z d d   Z d d   Z	 d d   Z
 d d   Z d d   Z d d   Z d d   Z d d   Z d d   Z d d   Z d  d!   Z d" d#   Z d$ d%   Z d& d'   Z d( d)   Z d* d+   Z d, d-   Z d. d/   Z d0 d1   Z d2 d3   Z d4 d5   Z d6 d7   Z d8 d9   Z d: d;   Z d< d=   Z  d> d?   Z! d@ dA   Z" dB dC   Z# dD dE   Z$ e% j& dF dG    Z' dH dI   Z( dJ dK   Z) dL dM   Z* dN dO   Z+ dP dQ   Z, dR dS   Z- dT dU   Z. dV dW   Z/ dX dY   Z0 dZ d[   Z1 d\ d]   Z2 d^ d_   Z3 d` da   Z4 db dc   Z5 dd de   Z6 df dg   Z7 dh di   Z8 dj dk   Z9 dl dm   Z: dn do   Z; dp dq   Z< dr ds   Z= dt du   Z> dv dw   Z? dx dy   Z@ dz d{   ZA d| d}   ZB d~ d   ZC d d   ZD d d   ZE d d   ZF d d   ZG d d   ZH d d   ZI d d   ZJ d d   ZK d d   ZL d d   ZM d d   ZN d d   ZO d d   ZP d d   ZQ d d   ZR d d   ZS d d   ZT eU d d  d d    ZV eU d d  d d    ZW d d   ZX d d   ZY d d   ZZ d d   Z[ d d   Z\ d d   Z] d d   Z^ d d   Z_ d d   Z` d d   Za d d   Zb d d   Zc d d   Zd d d   Ze d d   Zf d d   Zg d d   Zh d d   Zi d d   Zj d S)	PatchTestTc             C   sR   t  } | s t } |  j t |  |  j t | |   |  j t | t   d  S)N)r   r   assertRaises	TypeError
assertTruer   assertFalser   )r   r   magic	MockClassr   r   r   assertNotCallableX   s    zPatchTest.assertNotCallablec                sg   Gd d   d t     t j    d t j     f d d    } |    j   j t j d  d  S)Nc               @   s   e  Z d  Z e j Z d S)z4PatchTest.test_single_patchobject.<locals>.SomethingN)r   r   r   r
   Original	attributer   r   r   r   	Somethingc   s   rA   r@   c                  s    j    j t j d  d  S)N	unpatched)assertEqualr@   r
   Patchedr   )rA   r   r   r   testf   s    z/PatchTest.test_single_patchobject.<locals>.testzpatch not restored)r#   r	   r
   rD   rC   r@   r?   )r   rE   r   )rA   r   r   test_single_patchobjectb   s
    -z!PatchTest.test_single_patchobjectc                sd   Gd d   d t     t j    d d      f d d    } |    j   j t j d  d  S)Nc               @   s   e  Z d  Z e j Z d S)z7PatchTest.test_patchobject_with_none.<locals>.SomethingN)r   r   r   r
   r?   r@   r   r   r   r   rA   p   s   rA   r@   c                  s    j    j d  d  S)NrB   )assertIsNoner@   r   )rA   r   r   r   rE   s   s    z2PatchTest.test_patchobject_with_none.<locals>.testzpatch not restored)r#   r	   rC   r@   r
   r?   )r   rE   r   )rA   r   r   test_patchobject_with_noneo   s
    *z$PatchTest.test_patchobject_with_nonec                s   Gd d   d t     t j    d t j  t j    d t j     f d d     } |    j   j t j d   j   j t j	 d  d  S)Nc               @   s"   e  Z d  Z e j Z e j Z d S)z6PatchTest.test_multiple_patchobject.<locals>.SomethingN)r   r   r   r
   r?   r@   	Original2next_attributer   r   r   r   rA   }   s   	rA   r@   rJ   c                  s6    j    j t j d   j    j t j d  d  S)NrB   )rC   r@   r
   rD   rJ   Patched2r   )rA   r   r   r   rE      s    z1PatchTest.test_multiple_patchobject.<locals>.testzpatch not restored)
r#   r	   r
   rD   rK   rC   r@   r?   rJ   rI   )r   rE   r   )rA   r   r   test_multiple_patchobject|   s    0z#PatchTest.test_multiple_patchobjectc          
   C   s]   t  } t d t t j  d d    } z' t j a  |   |  j t  t j  Wd  | a  Xd  S)Nz%s.somethingc               S   s   d  S)Nr   r   r   r   r   rE      s    z8PatchTest.test_object_lookup_is_quite_lazy.<locals>.test)	somethingr	   r   r
   
Something2Zreplacement_valuerC   )r   originalrE   r   r   r    test_object_lookup_is_quite_lazy   s    "	z*PatchTest.test_object_lookup_is_quite_lazyc                sP  t  d t t j    f d d    } |     j t j t j d  t  d t t j  t  d t t j    f d d     }   j t j t j d    j t j	 t j d  |     j t j t j d    j t j	 t j d  t
   } t j | _ t  d t |    f d d    } |   |     j t | d  d  S)	Nz%s.somethingc                  s     j  t j t j d  d  S)NrB   )rC   PTModulerM   r
   rN   r   )r   r   r   rE      s    z"PatchTest.test_patch.<locals>.testzpatch not restoredz%s.something_elsec                  s6     j  t j t j d    j  t j t j d  d  S)NrB   )rC   rQ   rM   r
   rN   something_elseSomethingElser   )r   r   r   rE      s    z%s.openc                  s#     j  t d d  t j d  d  S)Nfilenamerzopen not patched)rC   openr
   Handler   )r   r   r   rE      s    )r	   r   r
   rN   rC   rQ   rM   rA   rS   rR   r   rW   return_valuebuiltin_stringassertNotEqualrV   )r   rE   mockr   )r   r   
test_patch   s*    (+	%zPatchTest.test_patchc                sI   t  d t t j    f d d    } |     j t j j d  d  S)Nz%s.SomeClass.class_attributec                  s      j  t j j t j d  d  S)NrB   )rC   rQ   r   class_attributer
   ClassAttributer   )r   r   r   rE      s    z2PatchTest.test_patch_class_attribute.<locals>.testzpatch not restored)r	   r   r
   r^   rG   rQ   r   r]   )r   rE   r   )r   r   test_patch_class_attribute   s
    
z$PatchTest.test_patch_class_attributec                s   Gd d   d t     t j    d     f d d    } |   t j    d  t j    d       f d d     } d    | t j t j  \   | t j t j  d  S)Nc               @   s"   e  Z d  Z e j Z e j Z d S)z:PatchTest.test_patchobject_with_default_mock.<locals>.TestN)r   r   r   r
   r?   rM   rI   
something2r   r   r   r   Test   s   	ra   rM   c                s-    j  |    j d   j |  t d  d  S)Nz"Mock not passed into test functionz.patch with two arguments did not create a mock)rC   rM   assertIsInstancer   )r[   )ra   r   r   r   rE      s    z:PatchTest.test_patchobject_with_default_mock.<locals>.testr`   c                s    j  |  t j d   j  | t j d   j  |   j d   j  |   j d   j | t d   j | t d   j  | d   j  | d  | | f S)Nz0Patched function didn't receive initial argumentz/Patched function didn't receive second argumentz"Mock not passed into test functionz)Second Mock not passed into test functionz.patch with two arguments did not create a mockzunexpected value for mock1)	rC   r
   this1this2r`   rM   rb   r   rZ   )rc   rd   Zmock1Zmock2)ra   
outerMock1
outerMock2r   r   r   rE      s    )r#   r	   r
   rc   rd   )r   rE   r   )ra   re   rf   r   r   "test_patchobject_with_default_mock   s    '0
z,PatchTest.test_patchobject_with_default_mockc                s3   t  d t d t   f d d    } |   d  S)Nz%s.SomeClassspecc                sC     j  t |     j t t j t     j t d d    d  S)Nc               S   s   t  j S)N)r   
not_wibbler   r   r   r   <lambda>  s    z>PatchTest.test_patch_with_spec.<locals>.test.<locals>.<lambda>)rC   r   r:   r   wibbler   r8   AttributeError)MockSomeClass)r   r   r   rE     s    z,PatchTest.test_patch_with_spec.<locals>.test)r	   r   r   )r   rE   r   )r   r   test_patch_with_spec   s    (zPatchTest.test_patch_with_specc                s5   t  j t d d t   f d d    } |   d  S)Nr]   rh   c                sI     j  t j |     j t t j j t     j t d d    d  S)Nc               S   s
   t  j j S)N)r   r]   ri   r   r   r   r   rj     s    zDPatchTest.test_patchobject_with_spec.<locals>.test.<locals>.<lambda>)	rC   r   r]   r:   r   rk   r   r8   rl   )MockAttribute)r   r   r   rE     s
    
	z2PatchTest.test_patchobject_with_spec.<locals>.test)r	   r#   r   )r   rE   r   )r   r   test_patchobject_with_spec
  s    *z$PatchTest.test_patchobject_with_specc                s6   t  d t d d g   f d d    } |   d  S)Nz%s.SomeClassrh   rk   c                sC     j  t |     j t t j t     j t d d    d  S)Nc               S   s   t  j S)N)r   ri   r   r   r   r   rj     s    zFPatchTest.test_patch_with_spec_as_list.<locals>.test.<locals>.<lambda>)rC   r   r:   r   rk   r   r8   rl   )rm   )r   r   r   rE     s    z4PatchTest.test_patch_with_spec_as_list.<locals>.test)r	   r   )r   rE   r   )r   r   test_patch_with_spec_as_list  s    +z&PatchTest.test_patch_with_spec_as_listc                s8   t  j t d d d g   f d d    } |   d  S)Nr]   rh   rk   c                sI     j  t j |     j t t j j t     j t d d    d  S)Nc               S   s
   t  j j S)N)r   r]   ri   r   r   r   r   rj   '  s    zLPatchTest.test_patchobject_with_spec_as_list.<locals>.test.<locals>.<lambda>)	rC   r   r]   r:   r   rk   r   r8   rl   )ro   )r   r   r   rE   !  s
    
	z:PatchTest.test_patchobject_with_spec_as_list.<locals>.test)r	   r#   r   )r   rE   r   )r   r   "test_patchobject_with_spec_as_list   s    -z,PatchTest.test_patchobject_with_spec_as_listc                sF   t  d t  t  d t d d g   f d d     } |   d  S)Nz%s.openz%s.SomeClassrh   rk   c                sC     j  t |     j t t j t     j t d d    d  S)Nc               S   s   t  j S)N)r   ri   r   r   r   r   rj   3  s    zMPatchTest.test_nested_patch_with_spec_as_list.<locals>.test.<locals>.<lambda>)rC   r   r:   r   rk   r   r8   rl   )rm   ZMockOpen)r   r   r   rE   .  s    z;PatchTest.test_nested_patch_with_spec_as_list.<locals>.test)r	   rY   r   )r   rE   r   )r   r   #test_nested_patch_with_spec_as_list,  s    .z-PatchTest.test_nested_patch_with_spec_as_listc                s3   t  d t d d   f d d    } |   d  S)Nz%s.SomeClassrh   Tc                s7    j  t      j  j t   f d d    d  S)Nc                  s     j  S)N)ri   r   )rm   r   r   rj   >  s    zIPatchTest.test_patch_with_spec_as_boolean.<locals>.test.<locals>.<lambda>)rC   r   rk   r8   rl   )rm   )r   )rm   r   rE   8  s    z7PatchTest.test_patch_with_spec_as_boolean.<locals>.test)r	   r   )r   rE   r   )r   r   test_patch_with_spec_as_boolean7  s    (z)PatchTest.test_patch_with_spec_as_booleanc                s5   t  j t d d d   f d d    } |   d  S)Nr   rh   Tc                s7    j  t      j  j t   f d d    d  S)Nc                  s     j  S)N)ri   r   )rm   r   r   rj   J  s    zPPatchTest.test_patch_object_with_spec_as_boolean.<locals>.test.<locals>.<lambda>)rC   r   rk   r8   rl   )rm   )r   )rm   r   rE   D  s    z>PatchTest.test_patch_object_with_spec_as_boolean.<locals>.test)r	   r#   rQ   )r   rE   r   )r   r   &test_patch_object_with_spec_as_booleanC  s    *z0PatchTest.test_patch_object_with_spec_as_booleanc                s3   t  d t d d   f d d    } |   d  S)Nz%s.SomeClassrh   Tc                sS    j  t |  t   |       j      j  j t   f d d    d  S)Nc                  s     j  S)N)ri   r   )instancer   r   rj   X  s    zVPatchTest.test_patch_class_acts_with_spec_is_inherited.<locals>.test.<locals>.<lambda>)r:   r   r   r>   rk   r8   rl   )rm   )r   )rv   r   rE   P  s
    	zDPatchTest.test_patch_class_acts_with_spec_is_inherited.<locals>.test)r	   r   )r   rE   r   )r   r   ,test_patch_class_acts_with_spec_is_inheritedO  s    (
z6PatchTest.test_patch_class_acts_with_spec_is_inheritedc                sO   t  d t t j d d   f d d    } |     j t d d    d  S)Nz
%s.frooblecreateTc                  s     j  t t j  d  S)N)rC   froobler
   Froobler   )r   r   r   rE   ^  s    zLPatchTest.test_patch_with_create_mocks_non_existent_attributes.<locals>.testc               S   s   t  S)N)ry   r   r   r   r   rj   c  s    zPPatchTest.test_patch_with_create_mocks_non_existent_attributes.<locals>.<lambda>)r	   rY   r
   rz   r8   	NameError)r   rE   r   )r   r   4test_patch_with_create_mocks_non_existent_attributes]  s    .z>PatchTest.test_patch_with_create_mocks_non_existent_attributesc                sQ   t  j t d t j d d   f d d    } |     j t t d   d  S)Nry   rx   Tc                  s     j  t j t j  d  S)N)rC   r   ry   r
   rz   r   )r   r   r   rE   g  s    zRPatchTest.test_patchobject_with_create_mocks_non_existent_attributes.<locals>.test)r	   r#   r   r
   rz   r;   hasattr)r   rE   r   )r   r   :test_patchobject_with_create_mocks_non_existent_attributesf  s    0zDPatchTest.test_patchobject_with_create_mocks_non_existent_attributesc                so   y3 t  d t t j    f d d    } |   Wn t k
 rG Yn X  j d    j t d d    d  S)Nz
%s.frooblec                  s     j  t t j  d  S)N)rC   ry   r
   rz   r   )r   r   r   rE   q  s    z9PatchTest.test_patch_wont_create_by_default.<locals>.testz,Patching non existent attributes should failc               S   s   t  S)N)ry   r   r   r   r   rj   {  s    z=PatchTest.test_patch_wont_create_by_default.<locals>.<lambda>)r	   rY   r
   rz   rl   failr8   r{   )r   rE   r   )r   r   !test_patch_wont_create_by_defaulto  s    (z+PatchTest.test_patch_wont_create_by_defaultc                sq   y5 t  j t d t j    f d d    } |   Wn t k
 rI Yn X  j d    j t t d   d  S)Nordc                  s     j  d  d  S)Nz,Patching non existent attributes should fail)r   r   )r   r   r   rE     s    z?PatchTest.test_patchobject_wont_create_by_default.<locals>.testz,Patching non existent attributes should fail)	r	   r#   r   r
   rz   rl   r   r;   r}   )r   rE   r   )r   r   'test_patchobject_wont_create_by_default~  s    *z1PatchTest.test_patchobject_wont_create_by_defaultc             C   sb   t  t d  d d    } t  t d  d d    } |  j |   d  |  j |   d  d  S)	Nz.ordc             S   s   d |  _  t d  S)Ne   c)rX   r   )Zmock_ordr   r   r   test_ord  s    	z>PatchTest.test_patch_builtins_without_create.<locals>.test_ordz.openc             S   s;   |  j  } d | j _  t d  } | j   } | j   | S)Nabcdzdoesnotexists.txt)rX   readrV   close)Z	mock_openmZfobjdatar   r   r   	test_open  s    	
z?PatchTest.test_patch_builtins_without_create.<locals>.test_openr   r   )r	   r   rC   )r   r   r   r   r   r   "test_patch_builtins_without_create  s    
z,PatchTest.test_patch_builtins_without_createc                sp   Gd d   d t     t j    d t d d        f d d    } |    j   j   t j  d  S)Nc               @   s"   e  Z d  Z e d d    Z d S)z5PatchTest.test_patch_with_static_methods.<locals>.Fooc               S   s   t  j S)N)r
   Staticr   r   r   r   woot  s    z:PatchTest.test_patch_with_static_methods.<locals>.Foo.wootN)r   r   r   staticmethodr   r   r   r   r   r&     s   r&   r   c               S   s   t  j S)N)r
   rD   r   r   r   r   rj     s    z:PatchTest.test_patch_with_static_methods.<locals>.<lambda>c                  s    j    j   t j  d  S)N)rC   r   r
   rD   r   )r&   r   r   r   	anonymous  s    z;PatchTest.test_patch_with_static_methods.<locals>.anonymous)r#   r	   r   rC   r   r
   r   )r   r   r   )r&   r   r   test_patch_with_static_methods  s    6z(PatchTest.test_patch_with_static_methodsc                sN   t  j } t j t  d d    f d d    } |     j t  j |  d  S)Nr&   c                  s     j  t j d  d  S)Nr&   )rC   r
   r&   r   )r   r   r   r     s    z-PatchTest.test_patch_local.<locals>.anonymous)r
   r&   r	   r#   rC   )r   r-   r   r   )r   r   test_patch_local  s    	'zPatchTest.test_patch_localc                sv   Gd d   d t   } |     t j   _ t j    d d     f d d    } |    j   j t j  d  S)Nc               @   s   e  Z d  Z d Z d S)z'PatchTest.test_patch_slots.<locals>.Foor&   N)zFoo)r   r   r   	__slots__r   r   r   r   r&     s   r&   c                  s    j    j d  d  S)Nr&   )rC   r&   r   )r-   r   r   r   r     s    z-PatchTest.test_patch_slots.<locals>.anonymous)r#   r
   r&   r	   rC   )r   r&   r   r   )r-   r   r   test_patch_slots  s    	*zPatchTest.test_patch_slotsc                s   Gd d   d t     G   f d d   d t   } t j    d t j  |  } |   } | j   | j    j   j t j d  d  S)Nc               @   s   e  Z d  Z e j Z d S)z=PatchTest.test_patchobject_class_decorator.<locals>.SomethingN)r   r   r   r
   r?   r@   r   r   r   r   rA     s   rA   c                   s:   e  Z d  Z    f d d   Z    f d d   Z d S)z7PatchTest.test_patchobject_class_decorator.<locals>.Fooc                s    j    j t j d  d  S)NrB   )rC   r@   r
   rD   )
other_self)rA   r   r   r   test_method  s    zCPatchTest.test_patchobject_class_decorator.<locals>.Foo.test_methodc                s    j    j t j d  d  S)Nznon-test method patched)rC   r@   r
   r?   )r   )rA   r   r   r   not_test_method  s    zGPatchTest.test_patchobject_class_decorator.<locals>.Foo.not_test_methodN)r   r   r   r   r   r   )rA   r   r   r   r&     s   r&   r@   zpatch not restored)	r#   r	   r
   rD   r   r   rC   r@   r?   )r   r&   r)   r   )rA   r   r    test_patchobject_class_decorator  s    	

z*PatchTest.test_patchobject_class_decoratorc                s   Gd d   d t   } G  f d d   d t   } t d t  |  } |   } | j   | j     j | j t j d    j t	 j
 t j d  d  S)Nc               @   s   e  Z d  Z e j Z d S)z7PatchTest.test_patch_class_decorator.<locals>.SomethingN)r   r   r   r
   r?   r@   r   r   r   r   rA     s   rA   c                   s4   e  Z d  Z   f d d   Z   f d d   Z d S)z1PatchTest.test_patch_class_decorator.<locals>.Fooc                s     j  t j | d  d  S)NrB   )rC   rQ   rM   )r   Zmock_something)r   r   r   r     s    z=PatchTest.test_patch_class_decorator.<locals>.Foo.test_methodc                s     j  t j t j d  d  S)Nznon-test method patched)rC   rQ   rM   r
   rA   )r   )r   r   r   r     s    zAPatchTest.test_patch_class_decorator.<locals>.Foo.not_test_methodN)r   r   r   r   r   r   )r   r   r   r&     s   r&   z%s.somethingzpatch not restored)r#   r	   r   r   r   rC   r@   r
   r?   rQ   rM   rA   )r   rA   r&   r)   r   )r   r   test_patch_class_decorator  s    	

z$PatchTest.test_patch_class_decoratorc                s   Gd d   d t     t j    d t j  t j    d t j     f d d     } |    j   j t j d  d  S)Nc               @   s"   e  Z d  Z e j Z e j Z d S)z3PatchTest.test_patchobject_twice.<locals>.SomethingN)r   r   r   r
   r?   r@   rI   rJ   r   r   r   r   rA     s   	rA   r@   c                  s    j    j t j d  d  S)NrB   )rC   r@   r
   rD   r   )rA   r   r   r   rE     s    z.PatchTest.test_patchobject_twice.<locals>.testzpatch not restored)r#   r	   r
   rD   rC   r@   r?   )r   rE   r   )rA   r   r   test_patchobject_twice  s    0z PatchTest.test_patchobject_twicec                s   d t    d d i     j   } t j      f d d    } |    j   |  t j   d d i     f d d    } |    j   |  t j   d
 g     f d	 d    } |    j   |  d  S)NinitialotherrM   c                  s   d   d <  d =d   d <d  S)N   r'   r   zsomething elser   r   r   )r-   r   r   rE     s    
z'PatchTest.test_patch_dict.<locals>.testr'   r.   c                  s.    j  t    d   j    d d  d  S)Nr   r'   r.   )rC   lenr   )r-   r   r   r   rE     s    c                  s.    j  t    d   j    d d  d  S)Nr   r'   r.   )rC   r   r   )r-   r   r   r   rE     s    )r'   r.   )r#   copyr	   dictrC   )r   rO   rE   r   )r-   r   r   test_patch_dict  s    !-*zPatchTest.test_patch_dictc                s   t      t     d <d   d <  j j   } t j      f d d    } |    j   j |  t j   d d i     f d d    } |    j   j |  d  S)	Nr   rM   r   c                  s   d   d <  d =d   d <d  S)Nr   r'   r   zsomething elser   r   r   )r-   r   r   rE   ,  s    
z=PatchTest.test_patch_dict_with_container_object.<locals>.testr'   r.   c                  s1    j  t   j  d   j    d d  d  S)Nr   r'   r.   )rC   r   r1   r   )r-   r   r   r   rE   6  s    )r0   r#   r1   r   r	   r   rC   )r   rO   rE   r   )r-   r   r   %test_patch_dict_with_container_object%  s    	
!-z/PatchTest.test_patch_dict_with_container_objectc                s   d t    d d i     j   } t j   d d    f d d    } |    j   |  t j   d d	 i d d    f d
 d    } |    j   |  t j   d g d d    f d d    } |    j   |  d  S)Nr   r   rM   clearTc                  s(    j    i   d   d <d   d <d  S)Nr   r'   zsomething elser   )rC   r   )r-   r   r   r   rE   D  s    
z2PatchTest.test_patch_dict_with_clear.<locals>.testr'   r.   c                  s    j    d d i  d  S)Nr'   r.   )rC   r   )r-   r   r   r   rE   N  s    c                  s    j    d d i  d  S)Nr'   r.   )rC   r   )r-   r   r   r   rE   V  s    )r'   r.   )r#   r   r	   r   rC   )r   rO   rE   r   )r-   r   r   test_patch_dict_with_clear@  s    *30z$PatchTest.test_patch_dict_with_clearc                s   t      t     d <d   d <  j j   } t j   d d    f d d    } |    j   j |  t j   d d	 i d d    f d
 d    } |    j   j |  d  S)Nr   rM   r   r   Tc                  s+    j    j i   d   d <d   d <d  S)Nr   r'   zsomething elser   )rC   r1   r   )r-   r   r   r   rE   f  s    
zGPatchTest.test_patch_dict_with_container_object_and_clear.<locals>.testr'   r.   c                  s    j    j d d i  d  S)Nr'   r.   )rC   r1   r   )r-   r   r   r   rE   p  s    )r0   r#   r1   r   r	   r   rC   )r   rO   rE   r   )r-   r   r   /test_patch_dict_with_container_object_and_clear_  s    	
*3z9PatchTest.test_patch_dict_with_container_object_and_clearc             C   s   i  } t  d t t    t  d t t   d d t  j t t    t  j |  d d       } |  j | j d  d  S)Nz%s.SomeClassautospecTc               S   s   d  S)Nr   r   r   r   r   	some_name|  s    z0PatchTest.test_name_preserved.<locals>.some_namer   )r	   r   r#   r   r   rC   )r   r-   r   r   r   r   test_name_preservedy  s    $zPatchTest.test_name_preservedc             C   sk   i  } t  j | d d i  d d    } y |   Wn t k
 rI Yn X|  j d  |  j | i   d  S)Nr'   r.   c               S   s   t  d   d  S)NZKonrad)r{   r   r   r   r   rE     s    z1PatchTest.test_patch_with_exception.<locals>.testzNameError not raised by test)r	   r   r{   r   rC   )r   r-   rE   r   r   r   test_patch_with_exception  s    $z#PatchTest.test_patch_with_exceptionc                s5   t  j d d d i    f d d    } |   d  S)Nz
os.environkonrad_delongz
some valuec                  s     j  d t j  d  S)Nr   )assertInosenvironr   )r   r   r   rE     s    z3PatchTest.test_patch_dict_with_string.<locals>.test)r	   r   )r   rE   r   )r   r   test_patch_dict_with_string  s    *z%PatchTest.test_patch_dict_with_stringc                s   Gd d   d t     G    f d d   d t     j  j d  d   j    j  j d  d   j      }  j | j d  d  | j    j | j d  d  | j   d  S)Nc               @   s   e  Z d  Z d Z d S)z0PatchTest.test_patch_descriptor.<locals>.NothingN)r   r   r   r-   r   r   r   r   Nothing  s   r   c                   s   e  Z d  Z i  Z e j   d d  e   f d d     Z e j   d d  e d d     Z	 e j
 e  e   f d d     Z e j
 e  e d	 d
     Z d S)z2PatchTest.test_patch_descriptor.<locals>.Somethingr-      c                s    j  |     d  S)N)assertIs)cls)rA   r   r   r   klass  s    z8PatchTest.test_patch_descriptor.<locals>.Something.klassc             S   s   |  S)Nr   )argr   r   r   static  s    z9PatchTest.test_patch_descriptor.<locals>.Something.staticc                s    j  |     d  S)N)r   )r   )rA   r   r   r   
klass_dict  s    z=PatchTest.test_patch_descriptor.<locals>.Something.klass_dictc             S   s   |  S)Nr   )r   r   r   r   static_dict  s    z>PatchTest.test_patch_descriptor.<locals>.Something.static_dictN)r   r   r   r-   r	   r#   classmethodr   r   r   r   r   r   r   )r   rA   r   r   r   rA     s   rA   Zf00)r#   rC   r   r   r   r   )r   rM   r   )r   rA   r   r   test_patch_descriptor  s    "

	
zPatchTest.test_patch_descriptorc             C   s   t  d t d t d d    } |  j t |  t  j t d d t d d    } |  j t |  t  d t d d d d    } |  j t |  t  j t d d d d	 d    } |  j t |  d  S)
Nz%s.SomeClassspec_setc             S   s   d |  _  d  S)Nr-   )z)r=   r   r   r   rE     s    z+PatchTest.test_patch_spec_set.<locals>.testr   c             S   s   d |  _  d  S)Nr-   )r   )r=   r   r   r   rE     s    Tc             S   s   d |  _  d  S)Nr-   )r   )r=   r   r   r   rE     s    c             S   s   d |  _  d  S)Nr-   )r   )r=   r   r   r   rE     s    )r	   r   r   r8   rl   r#   r   )r   rE   r   r   r   test_patch_spec_set  s    "$"$zPatchTest.test_patch_spec_setc             C   s6   t  d t d d d d    } |  j t |  d  S)Nz%s.SomeClassr   Tc             S   s   |    } d | _  d  S)Nr-   )r   )r=   rv   r   r   r   rE     s    	z-PatchTest.test_spec_set_inherit.<locals>.test)r	   r   r8   rl   )r   rE   r   r   r   test_spec_set_inherit  s    "zPatchTest.test_spec_set_inheritc             C   sx   t  } t d t  } |  j t  |  | j   } z$ |  j | |  |  j t  |  Wd  | j   X|  j t  |  d  S)Nz%s.something)rM   r	   r   r   startassertIsNotstop)r   rO   patcherr[   r   r   r   test_patch_start_stop  s    zPatchTest.test_patch_start_stopc             C   s)   t  t d d  } |  j t | j  d  S)Nr+   r   )r	   foo_namer8   RuntimeErrorr   )r   r   r   r   r   test_stop_without_start  s    z!PatchTest.test_stop_without_startc             C   s}   t  } t j t d d  } |  j t  |  | j   } z$ |  j | d  |  j t  |  Wd  | j   X|  j t  |  d  S)NrM   r-   )rM   r	   r#   rQ   r   r   rC   r   )r   rO   r   Zreplacedr   r   r   test_patchobject_start_stop  s    z%PatchTest.test_patchobject_start_stopc             C   s   d d i } | j    } t j | d g d d } |  j | |  | j   z |  j | d d i  Wd  | j   X|  j | |  d  S)Nr-   r+   spameggsr   T)r   r   )r   r	   r   rC   r   r   )r   drO   r   r   r   r   test_patch_dict_start_stop  s    
z$PatchTest.test_patch_dict_start_stopc                s   |   d d i     j    } G   f d d   d t  } t j   d d i d d |  } |  j   |  |   } | j   |  j   |  | j   |  j   |  |   } | j   |  j   |  | j   |  j   |  d  S)	Nr   r   c                   s:   e  Z d  Z    f d d   Z    f d d   Z d S)z7PatchTest.test_patch_dict_class_decorator.<locals>.Testc                s    j    d d i  d  S)Nr-   r+   )rC   )r   )r   thisr   r   
test_first   s    zBPatchTest.test_patch_dict_class_decorator.<locals>.Test.test_firstc                s    j    d d i  d  S)Nr-   r+   )rC   )r   )r   r   r   r   test_second"  s    zCPatchTest.test_patch_dict_class_decorator.<locals>.Test.test_secondN)r   r   r   r   r   r   )r   r   r   r   ra     s   ra   r-   r+   r   T)r   r#   r	   r   rC   r   r   )r   rO   ra   rE   r   )r   r   r   test_patch_dict_class_decorator  s     $	

	

z)PatchTest.test_patch_dict_class_decoratorc                s   Gd d   d t   } Gd d   d  } x | | |   | f D]| } t |    t j    d d     f d d    } |    j   j d   j | j d   j d   j  q? Wd  S)	Nc               @   s   e  Z d  Z d Z d S)z0PatchTest.test_get_only_proxy.<locals>.Somethingr-   N)r   r   r   r-   r   r   r   r   rA   :  s   rA   c               @   s   e  Z d  Z d Z d S)z4PatchTest.test_get_only_proxy.<locals>.SomethingElser-   N)r   r   r   r-   r   r   r   r   rS   <  s   rS   r-   r+   c                  s    j    j d  d  S)Nr+   )rC   r-   r   )proxyr   r   r   rE   B  s    z+PatchTest.test_get_only_proxy.<locals>.test)r#   r%   r	   rC   r-   assertNotIn__dict__)r   rA   rS   thingrE   r   )r   r   r   test_get_only_proxy9  s    *zPatchTest.test_get_only_proxyc                s   Gd d   d t   } Gd d   d  } x | | |   | f D] } t | d d   t j    d d     f d	 d
    } |    j   j d   j | j d   j d   j  q? Wd  S)Nc               @   s   e  Z d  Z d Z d S)z6PatchTest.test_get_set_delete_proxy.<locals>.Somethingr-   N)r   r   r   r-   r   r   r   r   rA   L  s   rA   c               @   s   e  Z d  Z d Z d S)z:PatchTest.test_get_set_delete_proxy.<locals>.SomethingElser-   N)r   r   r   r-   r   r   r   r   rS   N  s   rS   r$   Fr-   r+   c                  s    j    j d  d  S)Nr+   )rC   r-   r   )r   r   r   r   rE   T  s    z1PatchTest.test_get_set_delete_proxy.<locals>.test)r#   r%   r	   rC   r-   r   r   )r   rA   rS   r   rE   r   )r   r   r   test_get_set_delete_proxyK  s    *z#PatchTest.test_get_set_delete_proxyc             C   s   d t  d d d t   i } t t |  } | j   } | j   |  j t  |  |  j | j j	   d  |  j
 | j t  d  S)Nside_effectzfoo.bar.return_value!   r-   )KeyErrorr   r	   r   r   r   r8   rC   r-   r+   rb   )r   kwargsr   r[   r   r   r   test_patch_keyword_args]  s    
z!PatchTest.test_patch_keyword_argsc             C   s   d t  d d d t   i } t j t d |  } | j   } | j   |  j t  |  |  j | j	 j
   d  |  j | j	 t  d  S)Nr   zfoo.bar.return_valuer   r-   r)   )r   r   r	   r#   r&   r   r   r8   rC   r-   r+   rb   )r   r   r   r[   r   r   r   test_patch_object_keyword_argsj  s    
z(PatchTest.test_patch_object_keyword_argsc             C   s   d d i } | j    } t j | d d d d d d } | j   z) |  j | t d d d d d d   Wd  | j   X|  j | |  d  S)Nr-   r+   r      Zbaz   )r   r	   r   r   rC   r   )r   rO   r   r   r   r   r   test_patch_dict_keyword_argsw  s    !
)z&PatchTest.test_patch_dict_keyword_argsc                s   Gd d   d t   }  f d d      f d d        f d d   } t t d	 d
 |  } |   }  j t |  |   t j t } t j  | d d	 d
 |  } |   }  j t |  |   d  S)Nc               @   sP   e  Z d  Z d d   Z d d   Z d d   Z d Z Gd d	   d	 e  Z d
 S)z$PatchTest.test_autospec.<locals>.Booc             S   s   d  S)Nr   )r   r'   r   r   r   r(     s    z-PatchTest.test_autospec.<locals>.Boo.__init__c             S   s   d  S)Nr   )r   r'   r   r   r   r)     s    z&PatchTest.test_autospec.<locals>.Boo.fc             S   s   d  S)Nr   )r   r   r   r   r*     s    z&PatchTest.test_autospec.<locals>.Boo.gr+   c               @   s   e  Z d  Z d d   Z d S)z(PatchTest.test_autospec.<locals>.Boo.Barc             S   s   d  S)Nr   )r   r   r   r   r'     s    z*PatchTest.test_autospec.<locals>.Boo.Bar.aN)r   r   r   r'   r   r   r   r   r,     s   r,   N)	r   r   r   r(   r)   r*   r-   r#   r,   r   r   r   r   Boo  s
   r   c                s+   |  d  |  j  d    j t |   d  S)N   )assert_called_withr8   r9   )r[   )r   r   r   _test  s    
z&PatchTest.test_autospec.<locals>._testc                sn  |  j  d  |  j  j d    j t |  j   |  j   |  j j     j t |  j d    j t t |  d  |  j j   |  j j j     j t t |  j d  |  j	   |  j	 j   |  j	 j
   |  j	 j
 j     j t |  j	 j
 d  |  j	   j
   |  j	   j
 j     j t |  j	   j
 d    j t t |  j	 d    j t t |  j	   d  d  S)Nr   hr+   r.   )r)   r   r8   r9   r*   rl   r   r-   lowerr,   r'   )r[   )r   r   r   _test2  s(    

z'PatchTest.test_autospec.<locals>._test2c                s8     |    |    |  d    j  |  t  |  S)Nr   )r   r&   )r[   )r   r   r   r   r   r/     s
    

z)PatchTest.test_autospec.<locals>.functionr   Tr&   )r#   r	   r   r   r&   sysmodulesr   )r   r   r/   rE   r[   moduler   )r   r   r   r   test_autospec  s    		zPatchTest.test_autospecc                s3   t  d t d d   f d d    } |   d  S)Nz%s.functionr   Tc                s^   t  d  t  j d  t  d d  t  j d d    j t t     j t t t  d  d  S)Nr   r   r   r-   )r/   r   r8   r9   rl   r   )r[   )r   r   r   rE     s    
z.PatchTest.test_autospec_function.<locals>.test)r	   r   )r   rE   r   )r   r   test_autospec_function  s    (
z PatchTest.test_autospec_functionc             C   sE   t  d t d d d d d d    } |   } |  j | d  d  S)Nz%s.functionr   TrX   r   c             S   s   t  d d  S)Nr   r   )r/   )Zmock_functionr   r   r   rE     s    z.PatchTest.test_autospec_keywords.<locals>.test)r	   r   rC   )r   rE   resultr   r   r   test_autospec_keywords  s    	z PatchTest.test_autospec_keywordsc             C   sq   t  d t d d d d } |  j t | j  t j t } t  j | d d d d d } |  j t | j  d  S)Nz%s.functionnewr   r   Tr/   )r	   r   r8   r9   r   r   r   r#   )r   r   r   r   r   r   test_autospec_with_new  s
    z PatchTest.test_autospec_with_newc          
   C   sm   Gd d   d t   } t t d | } | j   } z' |  j | |  |  j | j t  Wd  | j   Xd  S)Nc               @   s   e  Z d  Z g  Z d S)z0PatchTest.test_autospec_with_object.<locals>.BarN)r   r   r   extrar   r   r   r   r,     s   r,   r   )r&   r	   r   r   rb   r   listr   )r   r,   r   r[   r   r   r   test_autospec_with_object  s    z#PatchTest.test_autospec_with_objectc          
   C   s`   t  } t t d d } | j   } z* |  j | |  |  j | d  |  Wd  | j   Xd  S)Nr   Tr   )r&   r	   r   r   rb   r   )r   ZFooClassr   r[   r   r   r   test_autospec_inherits  s    z PatchTest.test_autospec_inheritsc             C   s   t  t d d } | j   } zn |  j d t |   |  j d t | j   |  j d t | d     |  j d t | d   j   Wd  | j   Xd  S)Nr   Tz name='Foo'z name='Foo.f'z name='Foo()'z name='Foo().f')r	   r   r   r   reprr)   r   )r   r   r[   r   r   r   test_autospec_name  s    #zPatchTest.test_autospec_namec          
   C   s   t  j t d t    d d    } y |   Wn t j   } Yn Xt j d  d  d  } | j | |   } |  j d |  d  S)Nr)   c               S   s
   t   d  S)N)AssertionErrorr   r   r   r   rE     s    z'PatchTest.test_tracebacks.<locals>.testr   zraise AssertionError)	r	   r#   r&   r   exc_infounittestZTextTestResultZ_exc_info_to_stringr   )r   rE   errr   	tracebackr   r   r   test_tracebacks  s    $zPatchTest.test_tracebacksc             C   sv   t  t d t } | j   } | j   | j   } | j   |  j | |  x! | | f D] } |  j |  q[ Wd  S)Nnew_callable)r	   r   r   r   r   r   r>   )r   r   m1m2r[   r   r   r   test_new_callable_patch"  s    

z!PatchTest.test_new_callable_patchc             C   s|   t  j t d d t } | j   } | j   | j   } | j   |  j | |  x! | | f D] } |  j |  qa Wd  S)Nr)   r   )r	   r#   r&   r   r   r   r   r>   )r   r   r   r   r[   r   r   r   test_new_callable_patch_object/  s    

z(PatchTest.test_new_callable_patch_objectc                s   G  f d d   d t     t t d   d d d d } | j   } z< |  j t |     |  j   j t d d d d   Wd  | j	   Xd  S)Nc                   s(   e  Z d  Z d Z   f d d   Z d S)z:PatchTest.test_new_callable_keyword_arguments.<locals>.BarNc                s   |   _  d  S)N)r   )r   r   )r,   r   r   r(   ?  s    zCPatchTest.test_new_callable_keyword_arguments.<locals>.Bar.__init__)r   r   r   r   r(   r   )r,   r   r   r,   =  s   r,   r   Zarg1r   Zarg2r   )
r#   r	   r   r   r   typerC   r   r   r   )r   r   r   r   )r,   r   #test_new_callable_keyword_arguments<  s    &z-PatchTest.test_new_callable_keyword_argumentsc                s   G  f d d   d t     t t d   d   } | j   z  |  j   j t d     Wd  | j   Xt t d   d   } | j   z  |  j   j t d     Wd  | j   Xd  S)Nc                   s(   e  Z d  Z d Z   f d d   Z d S)z-PatchTest.test_new_callable_spec.<locals>.BarNc                s   |   _  d  S)N)r   )r   r   )r,   r   r   r(   N  s    z6PatchTest.test_new_callable_spec.<locals>.Bar.__init__)r   r   r   r   r(   r   )r,   r   r   r,   L  s   r,   r   rh   r   )r#   r	   r   r   rC   r   r   r   )r   r   r   )r,   r   test_new_callable_specK  s    
 
 z PatchTest.test_new_callable_specc          
   C   s|   d t  } t | d t } |  j t | j  t | d t d d } | j   } z |  j | d d Wd  | j   Xd  S)Nz	%s.weeeeer   rx   Tr<   F)r   r	   r   r8   rl   r   r>   r   )r   Znon_existent_attrpr   r   r   r   test_new_callable_create`  s    
	z"PatchTest.test_new_callable_createc          	   C   sN   |  j  t t t d t   d t |  j  t t j t d d t   d t d  S)Nr   r   r)   )r8   
ValueErrorr	   r   r#   r   r&   )r   r   r   r   'test_new_callable_incompatible_with_newn  s
    z1PatchTest.test_new_callable_incompatible_with_newc          	   C   sH   |  j  t t t d t d d |  j  t t j t d d t d d d  S)Nr   r   Tr)   )r8   r  r	   r   r   r#   r&   )r   r   r   r   ,test_new_callable_incompatible_with_autospecx  s    z6PatchTest.test_new_callable_incompatible_with_autospecc       	      C   s   Gd d   d t   } t t t t  | f } x} | D]u } xl d D]d } | d i } t t d | | } | j   } z# | j } |  j t	 t
 | d  Wd  | j   Xq? Wq2 Wd  S)	Nc               @   s   e  Z d  Z d S)z>PatchTest.test_new_callable_inherit_for_mocks.<locals>.MockSubN)r   r   r   r   r   r   r   MockSub  s   r  rh   r   Tr   x)zspeczspec_set)r   r   r   r   r	   r   r   rX   r8   rl   r   r   )	r   r  ZMockClassesZKlassr   r   r  r   rv   r   r   r   #test_new_callable_inherit_for_mocks  s    	z-PatchTest.test_new_callable_inherit_for_mocksc             C   s   Gd d   d t   } t t d | d d } | j   } z0 |  j t | |   |  j t t | d  Wd  | j	   X|  j
 | j t  d  S)Nc               @   s   e  Z d  Z d d   Z d S)z>PatchTest.test_new_callable_inherit_non_mock.<locals>.NotAMockc             S   s   | |  _  d  S)N)rh   )r   rh   r   r   r   r(     s    zGPatchTest.test_new_callable_inherit_non_mock.<locals>.NotAMock.__init__N)r   r   r   r(   r   r   r   r   NotAMock  s   r
  r   rh   TrX   )r#   r	   r   r   r:   r   r8   rl   r   r   rC   rh   r&   )r   r
  r  r   r   r   r   "test_new_callable_inherit_non_mock  s    z,PatchTest.test_new_callable_inherit_non_mockc                sq   |   t    G   f d d   d t  } t t d t |  } |   j   |   j   |  j t     d  S)Nc                   s=   e  Z d  Z    f d d   Z d d   Z d d   Z d S)z>PatchTest.test_new_callable_class_decorating.<locals>.SomeTestc                s4    j  t     j t |   j t t  d  S)N)r   r&   r   rb   r   )r   mock_foo)rO   rE   r   r   r     s    zDPatchTest.test_new_callable_class_decorating.<locals>.SomeTest._testc             S   s   |  j  |  d  S)N)r   )r   r  r   r   r   test_two  s    zGPatchTest.test_new_callable_class_decorating.<locals>.SomeTest.test_twoc             S   s   |  j  |  d  S)N)r   )r   r  r   r   r   test_one  s    zGPatchTest.test_new_callable_class_decorating.<locals>.SomeTest.test_oneN)r   r   r   r   r  r  r   )rO   rE   r   r   SomeTest  s   r  r   )r&   r#   r	   r   r   r  r  r   )r   r  r   )rO   rE   r   "test_new_callable_class_decorating  s    z,PatchTest.test_new_callable_class_decoratingc                s(  t    t  j } t  j } t j t d d d d } t j t  d d d d } x | | f D] } | j   z:  j t      j t  j d   j t  j d  Wd  | j	   X j t      j t  j |   j t  j |  q[ Wt j t d d d d    f d d    } |   d  S)	Nr)   r   r*   r   r   r   c                  s:    j  t     j t j d   j t j d  d  S)Nr   r   )r   r&   rC   r)   r*   r   )original_foor   r   r   rE     s    z+PatchTest.test_patch_multiple.<locals>.test)
r&   r)   r*   r	   multipler   r   r   rC   r   )r   
original_f
original_gZpatcher1Zpatcher2r   rE   r   )r  r   r   test_patch_multiple  s"    		
0zPatchTest.test_patch_multiplec             C   s0   |  j  t t j t  |  j  t t j t  d  S)N)r8   r  r	   r  r   r&   )r   r   r   r   test_patch_multiple_no_kwargs  s    z'PatchTest.test_patch_multiple_no_kwargsc                s   t    t  j } t  j } t j t d t d d d t    f d d    } |    j t  j |   j t  j |  d  S)Nr)   r*   r   r-   c                sy    j  t     j  t j |    j t j d   j  t j |   j t |  t    j t | t   d  S)Nr   )	r   r&   r)   rC   r*   r-   r:   r   r   )r)   r-   )r  r   r   r   rE     s    z8PatchTest.test_patch_multiple_create_mocks.<locals>.test)r&   r)   r*   r	   r  r   r   rC   )r   r  r  rE   r   )r  r   r    test_patch_multiple_create_mocks  s    		6	z*PatchTest.test_patch_multiple_create_mocksc                s   t  j } t  j } t j t  d d  } d | _ t j t  d t  } d | _ | g | _ |   f d d    } |     j t  j |    j t  j |  d  S)Nr)   r   r*   c                s*     j  t j |     j t j d  d  S)Nr   )r   r&   r*   rC   r)   )r*   )r   r   r   rE     s    zHPatchTest.test_patch_multiple_create_mocks_different_order.<locals>.test)	r&   r)   r*   r	   r#   attribute_namer   additional_patchersrC   )r   r  r  r   r   rE   r   )r   r   0test_patch_multiple_create_mocks_different_order  s    				z:PatchTest.test_patch_multiple_create_mocks_different_orderc                sP  t   t  j } t  j } t j t d t t j t d t t t d    f d d      } t j t d t t t d  t j t d t   f d d      } t t d  t j t d t t j t d t   f d d	      }   f d
 d     |   |   |    j t  j |   j t  j |  d  S)Nr)   r-   z.gc                s     |  |  d  S)Nr   )r*   r   )r   r   r   test1  s    z?PatchTest.test_patch_multiple_stacked_decorators.<locals>.test1c                s     |  |  d  S)Nr   )r*   r   )r   r   r   test2  s    z?PatchTest.test_patch_multiple_stacked_decorators.<locals>.test2c                s     |  |  d  S)Nr   )r*   r   )r   r   r   test3  s    z?PatchTest.test_patch_multiple_stacked_decorators.<locals>.test3c                s   | j  d  } | j  d  }  j |   j t     j t j |   j t j |    j t j |   j t | t	    j t |  t	    j t | t	   d  S)Nr)   r-   )
popr;   r   r&   r)   r*   r-   r:   r   r   )r*   r   r)   r-   )r  r   r   r   r     s    z?PatchTest.test_patch_multiple_stacked_decorators.<locals>._test)r&   r)   r*   r	   r  r   r   rC   )r   r  r  r  r  r  r   )r   r  r   r   &test_patch_multiple_stacked_decorators  s$    		(--z0PatchTest.test_patch_multiple_stacked_decoratorsc             C   s  t  } t  j } t  j } t j t d t d d d t } | j   } z | d } | d } |  j t	 |  t	 d d g   |  j
 t  |  |  j
 t  j |  |  j
 t  j |  |  j t | t   |  j t | t   Wd  | j   X|  j t  j |  |  j t  j |  d  S)Nr)   r*   r   r-   )r&   r)   r*   r	   r  r   r   r   rC   setr   r-   r:   r   r   r   )r   r  r  r  r   r   r)   r-   r   r   r   (test_patch_multiple_create_mocks_patcher3  s"    		!

"z2PatchTest.test_patch_multiple_create_mocks_patcherc                s   |   t    t  j } t  j } G   f d d   d t  } t j t d t d d d t |  } |   } | j   | j	   |  j
 t  j |  |  j
 t  j |  d  S)Nc                   s=   e  Z d  Z    f d d   Z d d   Z d d   Z d S)z@PatchTest.test_patch_multiple_decorating_class.<locals>.SomeTestc                sy    j  t     j  t j |   j t j d   j  t j |   j t | t    j t | t   d  S)Nr   )	r   r&   r)   rC   r*   r-   r:   r   r   )r   r)   r-   )r  rE   r   r   r   T  s    zFPatchTest.test_patch_multiple_decorating_class.<locals>.SomeTest._testc             S   s   |  j  | |  d  S)N)r   )r   r)   r-   r   r   r   r  \  s    zIPatchTest.test_patch_multiple_decorating_class.<locals>.SomeTest.test_twoc             S   s   |  j  | |  d  S)N)r   )r   r)   r-   r   r   r   r  ^  s    zIPatchTest.test_patch_multiple_decorating_class.<locals>.SomeTest.test_oneN)r   r   r   r   r  r  r   )r  rE   r   r   r  R  s   r  r)   r*   r   r-   )r&   r)   r*   r#   r	   r  r   r   r  r  rC   )r   r  r  r  r   r   )r  rE   r   $test_patch_multiple_decorating_classL  s    				

z.PatchTest.test_patch_multiple_decorating_classc             C   s   t  j t d d } |  j t | j  t  j t d d d d } | j   z |  j t j d  Wd  | j   X|  j	 t
 t d   d  S)Nblamrx   T)r	   r  r&   r8   rl   r   rC   r#  r   r;   r}   )r   r   r   r   r   test_patch_multiple_createm  s    
z$PatchTest.test_patch_multiple_createc             C   s   t  j t d t d d d g } | j   } z} |  j t j | d  t j j d  t j j d  t j j j	 d  t j j j	 d  |  j
 t t t j d d   Wd  | j   Xd  S)Nr-   r   r'   r.   r   r   r   )r	   r  r&   r   r   rC   r-   r'   r.   r   r8   rl   r   r   )r   r   r   r   r   r   test_patch_multiple_spec_set{  s    ! z&PatchTest.test_patch_multiple_spec_setc          
   C   s   Gd d   d t   } t j t d t d t d | } | j   } zn |  j t j | d  |  j t j | d  |  j	 t j |  |  j	 t j |  |  j
 t j t j  Wd  | j   Xd  S)Nc               @   s   e  Z d  Z d S)z9PatchTest.test_patch_multiple_new_callable.<locals>.ThingN)r   r   r   r   r   r   r   Thing  s   r&  r)   r*   r   )r#   r	   r  r&   r   r   r   r)   r*   rb   r   r   )r   r&  r   r   r   r   r    test_patch_multiple_new_callable  s    z*PatchTest.test_patch_multiple_new_callablec             C   sG  t  j } t  j } t j t  d d  t j t  d d  t j t  d d  d d      } t j t  d d  t j t  d d  t j t  d d  d d      } t j t  d d  t j t  d d  t j t  d d  d	 d
      } xM | | | f D]< } |  j t |  |  j t  j |  |  j t  j |  qWd  S)Nr*   r   missingr)   c               S   s   d  S)Nr   r   r   r   r   thing1  s    z3PatchTest.test_nested_patch_failure.<locals>.thing1c               S   s   d  S)Nr   r   r   r   r   thing2  s    z3PatchTest.test_nested_patch_failure.<locals>.thing2c               S   s   d  S)Nr   r   r   r   r   thing3  s    z3PatchTest.test_nested_patch_failure.<locals>.thing3)r&   r)   r*   r	   r#   r8   rl   rC   )r   r  r  r)  r*  r+  funcr   r   r   test_nested_patch_failure  s    		'''z#PatchTest.test_nested_patch_failurec       	      C   sx  t  j } t  j } t  j } d d   } t j t  d d  t j t  d d | t j t  d d  d d	      } t j t  d d | t j t  d d  t j t  d d  d
 d      } t j t  d d  t j t  d d  t j t  d d | d d      } x` | | | f D]O } |  j t |  |  j t  j |  |  j t  j |  |  j t  j |  q!Wd  S)Nc               S   s   t  d   d  S)Ncrasher)r{   r   r   r   r   r.    s    z4PatchTest.test_new_callable_failure.<locals>.crasherr*   r   r-   r   r)   c               S   s   d  S)Nr   r   r   r   r   r)    s    z3PatchTest.test_new_callable_failure.<locals>.thing1c               S   s   d  S)Nr   r   r   r   r   r*    s    z3PatchTest.test_new_callable_failure.<locals>.thing2c               S   s   d  S)Nr   r   r   r   r   r+    s    z3PatchTest.test_new_callable_failure.<locals>.thing3)	r&   r)   r*   r-   r	   r#   r8   r{   rC   )	r   r  r  r  r.  r)  r*  r+  r,  r   r   r   test_new_callable_failure  s$    			''*z#PatchTest.test_new_callable_failurec             C   s   t  j } t  j } t j t  d d  } d | _ t j t  d d  } d | _ t j t  d d  } d | _ xq | | g | | g f D]W } | | _ | d d    } |  j t |  |  j	 t  j |  |  j	 t  j |  q Wd  S)Nr)   r   r*   r(  c               S   s   d  S)Nr   r   r   r   r   r,    s    z3PatchTest.test_patch_multiple_failure.<locals>.func)
r&   r)   r*   r	   r#   r  r  r8   rl   rC   )r   r  r  r   goodbadadditionalsr,  r   r   r   test_patch_multiple_failure  s    						z%PatchTest.test_patch_multiple_failurec       
      C   s  t  j } t  j } t  j } d d   } t j t  d d  } d | _ t j t  d d  } d | _ t j t  d d | } d | _ x | | g | | g f D]j } | | _ | d d	    }	 |  j t	 |	  |  j
 t  j |  |  j
 t  j |  |  j
 t  j |  q Wd  S)
Nc               S   s   t  d   d  S)Nr.  )r{   r   r   r   r   r.    s    zCPatchTest.test_patch_multiple_new_callable_failure.<locals>.crasherr)   r   r*   r-   r   c               S   s   d  S)Nr   r   r   r   r   r,    s    z@PatchTest.test_patch_multiple_new_callable_failure.<locals>.func)r&   r)   r*   r-   r	   r#   r  r  r8   r{   rC   )
r   r  r  r  r.  r   r0  r1  r2  r,  r   r   r   (test_patch_multiple_new_callable_failure  s"    							z2PatchTest.test_patch_multiple_new_callable_failurec                s   x| t  t f D]n } t d | f d d i  } |     t j   d d    f d d    } |    j   j d  q Wd  S)Nr&   fishZtastyznearly gonec                  s    j    j d  d  S)Nznearly gone)rC   r5  r   )r-   r   r   r   rE     s    z=PatchTest.test_patch_multiple_string_subclasses.<locals>.test)strunicoder   r	   r  rC   r5  )r   baser&   rE   r   )r-   r   r   %test_patch_multiple_string_subclasses  s    	*z/PatchTest.test_patch_multiple_string_subclasseszmock.patch.TEST_PREFIXr-   c             C   s   Gd d   d t   } t j  | d d  |  } |   } |  j | j   d  |  j | j   d  |  j | j   d  |  j | j   d  d  S)Nc               @   sF   e  Z d  Z d Z d d   Z d d   Z d d   Z d d	   Z d
 S)z-PatchTest.test_patch_test_prefix.<locals>.FoorO   c             S   s   |  j  S)N)r   )r   r   r   r   foo_one"  s    z5PatchTest.test_patch_test_prefix.<locals>.Foo.foo_onec             S   s   |  j  S)N)r   )r   r   r   r   foo_two$  s    z5PatchTest.test_patch_test_prefix.<locals>.Foo.foo_twoc             S   s   |  j  S)N)r   )r   r   r   r   r  &  s    z6PatchTest.test_patch_test_prefix.<locals>.Foo.test_onec             S   s   |  j  S)N)r   )r   r   r   r   r  (  s    z6PatchTest.test_patch_test_prefix.<locals>.Foo.test_twoN)r   r   r   r   r:  r;  r  r  r   r   r   r   r&     s
   r&   r   changedrO   )r#   r	   rC   r:  r;  r  r  )r   r&   r-   r   r   r   test_patch_test_prefix  s    	z PatchTest.test_patch_test_prefixr+   c                s   G  f d d   d t   } d d i   t j   d d |  } |   } |  j | j   d d i  |  j | j   d d i  |  j | j   d d i  |  j | j   d d i  d  S)Nc                   sX   e  Z d  Z   f d d   Z   f d d   Z   f d d   Z   f d d   Z d	 S)
z2PatchTest.test_patch_dict_test_prefix.<locals>.Fooc                s
   t     S)N)r   )r   )the_dictr   r   bar_one7  s    z:PatchTest.test_patch_dict_test_prefix.<locals>.Foo.bar_onec                s
   t     S)N)r   )r   )r>  r   r   bar_two9  s    z:PatchTest.test_patch_dict_test_prefix.<locals>.Foo.bar_twoc                s
   t     S)N)r   )r   )r>  r   r   r  ;  s    z;PatchTest.test_patch_dict_test_prefix.<locals>.Foo.test_onec                s
   t     S)N)r   )r   )r>  r   r   r  =  s    z;PatchTest.test_patch_dict_test_prefix.<locals>.Foo.test_twoN)r   r   r   r?  r@  r  r  r   )r>  r   r   r&   6  s   r&   keyrO   r<  )r#   r	   r   rC   r?  r@  r  r  )r   r&   r-   r   )r>  r   test_patch_dict_test_prefix4  s    
	z%PatchTest.test_patch_dict_test_prefixc             C   s   x d
 D] } t  d t | d i  } | j   } zh |  j d t |   |  j d t | j   |  j d t |     |  j d	 t |   j   Wd  | j   Xq Wd  S)Nrh   r   r   z%s.SomeClassTz name='SomeClass'z! name='SomeClass.class_attribute'z name='SomeClass()'z# name='SomeClass().class_attribute')zspeczautospeczspec_set)r	   r   r   r   r   r]   r   )r   r   r  r   r   r   r   test_patch_with_spec_mock_reprJ  s    		z(PatchTest.test_patch_with_spec_mock_reprc          
   C   su   t  d d d } | j   } zE |  j d t | j j     |  j d t | j   j     Wd  | j   Xd  S)Nzmock.tests.supportr   Tz" name='support.SomeClass.wibble()'z$ name='support.SomeClass().wibble()')r	   r   r   r   r   rk   r   )r   r  r   r   r   r   test_patch_nested_autospec_reprY  s    		z)PatchTest.test_patch_nested_autospec_reprc             C   sZ  xSd D]K} t  d t | d i  } | j   } z| j   t j   g } |  j | j |  |  j | j |  |  j | j j t   g  |   } | j t    |  j | j |  | j   | j t   j    |  j | j |  |  j | j t j   g  |  j | j j t   g  |  j | j t j   g  Wd  | j	   Xq Wd  S)Nrh   r   r   z%s.SomeClassT)zspeczautospeczspec_set)
r	   r   r   rk   r   rC   Z
mock_callsZmethod_callsappendr   )r   r   r  r   Zkallsr   r   r   r   test_mock_calls_with_patche  s&    
	
 z$PatchTest.test_mock_calls_with_patchc             C   s   t  j j d d   t d  } |  j t | j  t   } d | _ | t  j d <t d  } d | _ | j   | j	   |  j
 | j d  d  S)Nsquizzzsquizz.squozz   r   )r   r   r  r	   r8   ImportErrorr   r   Zsquozzr   rC   )r   Zp1rG  r   r   r   test_patch_imports_lazily  s    			

z#PatchTest.test_patch_imports_lazilyc                s   Gd d   d   G f d d   d t       f d d   } | d  d d	    } |  j t |  |  j  j d
 t  |  j  j d d  |  j  j d d  d  S)Nc               @   s   e  Z d  Z d Z d S)z<PatchTest.test_patch_propogrates_exc_on_exit.<locals>.holderN)NNN)r   r   r   r   r   r   r   r   holder  s   rK  c                   s1   e  Z d  Z d d d   f d d  Z e Z d S)zBPatchTest.test_patch_propogrates_exc_on_exit.<locals>.custom_patchNc                s,   t  j |  | | |  | | | f   _ d  S)N)r   __exit__r   )r   etypevaltb)rK  r   r   rL    s    zKPatchTest.test_patch_propogrates_exc_on_exit.<locals>.custom_patch.__exit__)r   r   r   rL  r   r   )rK  r   r   custom_patch  s   rP  c          
      s4   t  |   \ } }   | | t d  d d  d  d  i  	 S)NF)r   r   )targetgetterr@   )rP  r   r   with_custom_patch  s    zGPatchTest.test_patch_propogrates_exc_on_exit.<locals>.with_custom_patchzsquizz.squozzc             S   s
   t   d  S)N)r   )r[   r   r   r   rE     s    z:PatchTest.test_patch_propogrates_exc_on_exit.<locals>.testr   r   zexception value not propgatedr   z!exception traceback not propgated)r   r8   r   r   r   ZassertIsNotNone)r   rS  rE   r   )rP  rK  r   "test_patch_propogrates_exc_on_exit  s    z,PatchTest.test_patch_propogrates_exc_on_exitc             C   s   x d	 D]} } t  d t d d | d i } |  j t | j  |  j t d d    t  t d d | d i } | j   | j   q Wd  S)
Nrh   r   r   z%s.doesnotexistrx   Tc               S   s   t  S)N)doesnotexistr   r   r   r   rj     s    z1PatchTest.test_create_and_specs.<locals>.<lambda>)zspeczspec_setzautospec)r	   r   r8   r9   r   r{   MODNAMEr   )r   kwargr  r   r   r   test_create_and_specs  s    
zPatchTest.test_create_and_specsc             C   s   t  } xL d D]D } t t d d | d i } |  j t | j  |  j t  |  q WxL d D]D } t t d d | d i } |  j t | j  |  j t  |  q\ WxL d D]D } t t d d | d i } |  j t | j  |  j t  |  q Wd  S)Nrh   r   r   r   )specspec_set)rY  r   )rZ  r   )rQ   r	   rV  r8   r9   r   r   )r   rO   rW  r  r   r   r   test_multiple_specs  s    zPatchTest.test_multiple_specsc          
   C   sP   t  t d d d d d d } | j   } z | j d | _ Wd  | j   Xd  S)Nrh   Fr   r   r   )r	   rV  r   Zdoes_not_existr   )r   r  r[   r   r   r    test_specs_false_instead_of_none  s    z*PatchTest.test_specs_false_instead_of_nonec             C   s^   xW d D]O } t  t | d i  } | j   } z |  j t t | d  Wd  | j   Xq Wd  S)Nrh   r   r   r   Zdoesnotexit)zspeczautospeczspec_set)r	   rV  r   r8   rl   r   r   )r   rW  r  r   r   r   r   test_falsey_spec  s    zPatchTest.test_falsey_specc             C   s}   xv d D]n } t  t d d | d i } | j   } z3 |  j t t | d d  |  j t t | d  Wd  | j   Xq Wd  S)Nrh   r   r   TrU  rM   )zspeczautospec)r	   rV  r   r8   rl   r   r   r   )r   rW  r  r   r   r   r   test_spec_set_true  s    
zPatchTest.test_spec_set_truec          
   C   sM   d } t  t d | } | j   } z |  j t |   Wd  | j   Xd  S)N__call__rh   )z__call__)r	   rV  r   r:   r   r   )r   rh   r  r   r   r   r   test_callable_spec_as_list  s    z$PatchTest.test_callable_spec_as_listc          
   C   sM   d } t  t d | } | j   } z |  j t |   Wd  | j   Xd  S)Nr-   r+   rh   )zfoozbar)r	   rV  r   r;   r   r   )r   rh   r  r   r   r   r   test_not_callable_spec_as_list  s    z(PatchTest.test_not_callable_spec_as_listc                s   t  j  t  j   t  j } t d t  j   t d t  j   t d      f d d    } |    j t  j |  d  S)Nz	os.unlinkzos.chdirzos.pathc                sG   t  j    j t j |    j t j    j t j    d  S)N)r	   stopallr   r   pathunlinkchdir)Z	mock_path)re  r   rd  r   r   patched  s    
z-PatchTest.test_patch_stopall.<locals>.patched)	r   rd  re  rc  r	   rM   r   rR   r   )r   rc  rf  r   )re  r   rd  r   test_patch_stopall  s    			$zPatchTest.test_patch_stopallc             C   s)   t  d  t  } |  j | j t  d  S)Nzsys.modules)r	   r/   r   __wrapped__)r   	decoratedr   r   r   test_wrapped_patch  s    zPatchTest.test_wrapped_patchc             C   s;   t  d  t  } t  d  |  } |  j | j t  d  S)Nzsys.modules)r	   r/   r   rh  )r   ri  r   r   r    test_wrapped_several_times_patch  s    z*PatchTest.test_wrapped_several_times_patchc             C   s/   t  j t d  t  } |  j | j t  d  S)Nr   )r	   r#   r   r/   r   rh  )r   ri  r   r   r   test_wrapped_patch_object  s    z#PatchTest.test_wrapped_patch_objectc             C   s,   t  j d  t  } |  j | j t  d  S)Nzsys.modules)r	   r   r/   r   rh  )r   ri  r   r   r   test_wrapped_patch_dict#  s    z!PatchTest.test_wrapped_patch_dictc             C   s2   t  j d d i  t  } |  j | j t  d  S)Nr   r   )r	   r  r/   r   rh  )r   ri  r   r   r   test_wrapped_patch_multiple(  s    z%PatchTest.test_wrapped_patch_multiplec                so   g   Gd d   d t      f d d       f d d   d
 D t j   |  j  d	 d d g  d  S)Nc               @   s   e  Z d  Z d Z Z Z d S)z*PatchTest.test_stopall_lifo.<locals>.thingN)r   r   r   onetwothreer   r   r   r   r   .  s   r   c          
      sP   G    f d d   d t      f d d     d  d  d d  d  d  i  	 S)Nc                   s+   e  Z d  Z      f d d   Z   S)z?PatchTest.test_stopall_lifo.<locals>.get_patch.<locals>.mypatchc                s     j    t  |   j   S)N)rE  superr   )r   )	__class__r@   mypatchstoppedr   r   r   3  s    zDPatchTest.test_stopall_lifo.<locals>.get_patch.<locals>.mypatch.stop)r   r   r   r   r   )r@   rt  ru  )rs  r   rt  2  s   rt  c                  s     S)Nr   r   )r   r   r   rj   6  s    z@PatchTest.test_stopall_lifo.<locals>.get_patch.<locals>.<lambda>F)r   )r@   )ru  r   )r@   rt  r   	get_patch1  s    "z.PatchTest.test_stopall_lifo.<locals>.get_patchc                s"   g  |  ] }   |  j     q Sr   )r   ).0rN  )rv  r   r   
<listcomp>8  s   	 z/PatchTest.test_stopall_lifo.<locals>.<listcomp>ro  rp  rq  )zoneztwozthree)r#   r	   rb  rC   )r   r   )rv  ru  r   r   test_stopall_lifo,  s    
zPatchTest.test_stopall_lifoN)kr   r   r   r>   rF   rH   rL   rP   r\   r_   rg   rn   rp   rq   rr   rs   rt   ru   rw   r|   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ZexpectedFailurer   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r	  r  r  r  r  r  r  r  r!  r"  r$  r%  r'  r-  r/  r3  r4  r9  r	   r=  rB  rC  rD  rF  rJ  rT  rX  r[  r\  r]  r^  r`  ra  rg  rj  rk  rl  rm  rn  ry  r   r   r   r   r7   V   s   
+*

		
!+	G	
+!!


r7   __main__),r   r   Z	unittest2r   Z
mock.testsr   Zmock.tests.supportr   r   r   r   r[   r   r   r	   r
   r   r   r   r   r   Z	mock.mockr   r   rY   r6  r7  r   r   rQ   rV  r%   rA   rM   rS   rR   r#   r&   r   r/   r0   ZTestCaser7   mainr   r   r   r   <module>   s8   "F
		
      