o
    KXh                     @   s
  d dl Z d dlZd dlmZ d dlZd dlmZ d dlmZm	Z	 d dl
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mZ dZeje Zde Zdd
dZejaej Z!G dd de"Z#de Z$e#fddZ%G dd de"Z&G dd dej'Z(edkre)  dS dS )    N)OrderedDict)support)	SomeClassis_instance)uncache)NonCallableMockCallableMixinsentinel	MagicMockMockNonCallableMagicMockpatch_patchDEFAULTcall_get_targetbuiltinsz%s.PTModuleTc                    sB   G  fdddt }|s 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 J/opt/python-3.10.19/usr/lib/python3.10/unittest/test/testmock/testpatch.py__getattr__      
z%_get_proxy.<locals>.Proxy.__getattr__N)__name__
__module____qualname__r   r   r   r   r   Proxy       r!   c                    s   t  || d S r   )setattrr   r   valuer   r   r   __setattr__       z_get_proxy.<locals>.__setattr__c                    s   t  | d S r   )delattrr   r   r   r   __delattr__"      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edd	 Ze	d
d Z
G dd deZdS )Fooc                 C      d S r   r   r   ar   r   r   __init__/       zFoo.__init__c                 C   r/   r   r   r0   r   r   r   f0   r3   zFoo.fc                 C   r/   r   r   r   r   r   r   g1   r3   zFoo.gbarc                   C   r/   r   r   r   r   r   r   static_method4      zFoo.static_methodc                 C   r/   r   r   )clsr   r   r   class_method7   r9   zFoo.class_methodc                   @      e Zd Zdd ZdS )zFoo.Barc                 C   r/   r   r   r5   r   r   r   r1   ;   r3   z	Foo.Bar.aNr   r   r    r1   r   r   r   r   Bar:       r>   N)r   r   r    r2   r4   r6   foostaticmethodr8   classmethodr;   r+   r>   r   r   r   r   r.   .   s    

r.   z%s.Fooc                 C   r/   r   r   r1   br   r   r   function@   r3   rE   c                   @   s4   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 r   valuesr5   r   r   r   r2   D   r   zContainer.__init__c                 C   s
   | j | S r   rG   r   r   r   r   __getitem__G   r   zContainer.__getitem__c                 C   s   || j |< d S r   rG   r$   r   r   r   __setitem__J   r*   zContainer.__setitem__c                 C   s   | j |= d S r   rG   r   r   r   r   __delitem__M   s   zContainer.__delitem__c                 C   s
   t | jS r   )iterrH   r5   r   r   r   __iter__P   r   zContainer.__iter__N)r   r   r    r2   rI   rJ   rK   rM   r   r   r   r   rF   C   s    rF   c                   @   s  e Zd ZdddZdd Zdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Zd<d= Z d>d? Z!d@dA Z"dBdC Z#dDdE Z$dFdG Z%dHdI Z&dJdK Z'dLdM Z(dNdO Z)dPdQ Z*dRdS Z+dTdU Z,dVdW Z-dXdY Z.dZd[ Z/d\d] Z0d^d_ Z1d`da Z2dbdc Z3ddde Z4dfdg Z5dhdi Z6djdk Z7dldm Z8dndo Z9dpdq Z:drds Z;dtdu Z<dvdw Z=dxdy Z>dzd{ Z?d|d} Z@d~d ZAdd ZBdd ZCdd ZDdd ZEdd ZFdd ZGdd ZHdd ZIdd ZJdd ZKdd ZLdd ZMdd ZNdd ZOdd ZPdd ZQdd ZRdd ZSdd ZTdd ZUdd ZVdd ZWdd ZXeYdddd ZZeYdddd Z[dd Z\dd Z]dd Z^dd Z_dd Z`dd Zadd ZbddĄ ZcddƄ ZdddȄ Zeddʄ Zfdd̄ Zgdd΄ ZhddЄ Zidd҄ ZjddԄ Zkddք Zldd؄ Zmddڄ Zndd܄ Zoddބ ZpdS )	PatchTestTc                 C   s<   t }|st}| t| | t|| | t|t d S r   )r   r   assertRaises	TypeError
assertTruer   assertFalser   )r   r   magic	MockClassr   r   r   assertNotCallableW   s   zPatchTest.assertNotCallablec                    sJ   G dd dt  t  dtj fdd}|   jtjd d S )Nc                   @      e Zd ZejZdS )z4PatchTest.test_single_patchobject.<locals>.SomethingNr   r   r    r	   Original	attributer   r   r   r   	Somethingb       
rZ   rY   c                           jtjd d S NZ	unpatchedassertEqualrY   r	   Patchedr   rZ   r   r   r   teste      z/PatchTest.test_single_patchobject.<locals>.testpatch not restoredr+   r   r	   r`   r_   rY   rX   r   rb   r   ra   r   test_single_patchobjecta   s   z!PatchTest.test_single_patchobjectc                 C   sB   d}|  t| tdd W d    d S 1 sw   Y  d S )Nz>'Something' must be the actual object to be patched, not a strrZ   Zdo_something)ZassertRaisesRegexrP   r   r+   )r   msgr   r   r   &test_patchobject_with_string_as_targetm   s   "z0PatchTest.test_patchobject_with_string_as_targetc                    sH   G dd dt  t  dd  fdd}|   jtjd d S )Nc                   @   rV   )z7PatchTest.test_patchobject_with_none.<locals>.SomethingNrW   r   r   r   r   rZ   s   r[   rZ   rY   c                           jd d S r]   )assertIsNonerY   r   ra   r   r   rb   v      z2PatchTest.test_patchobject_with_none.<locals>.testrd   )r+   r   r_   rY   r	   rX   rf   r   ra   r   test_patchobject_with_noner   s   z$PatchTest.test_patchobject_with_nonec                    sl   G dd dt  t  dtjt  dtj fdd}|   jtjd  jtj	d d S )Nc                   @      e Zd ZejZejZdS )z6PatchTest.test_multiple_patchobject.<locals>.SomethingNr   r   r    r	   rX   rY   	Original2next_attributer   r   r   r   rZ          
rZ   rY   rq   c                      s(     jtjd   jtjd d S r]   )r_   rY   r	   r`   rq   Patched2r   ra   r   r   rb         z1PatchTest.test_multiple_patchobject.<locals>.testrd   )
r+   r   r	   r`   rs   r_   rY   rX   rq   rp   rf   r   ra   r   test_multiple_patchobject   s   z#PatchTest.test_multiple_patchobjectc                 C   sH   t }tdt tjdd }ztja |  | t tj W |a d S |a w )N%s.somethingc                   S   r/   r   r   r   r   r   r   rb         z8PatchTest.test_object_lookup_is_quite_lazy.<locals>.test)	somethingr   r   r	   
Something2Zreplacement_valuer_   r   originalrb   r   r   r    test_object_lookup_is_quite_lazy   s   
z*PatchTest.test_object_lookup_is_quite_lazyc                    s   t dt tj fdd}|   tjtjd t dt tjt dt tj fdd} tjtjd  tj	tjd |   tjtjd  tj	tjd t
 }tj|_t dt | fdd}|  |   t|d d S )	Nrv   c                           tjtjd d S r]   )r_   PTModulerx   r	   ry   r   r5   r   r   rb      s   z"PatchTest.test_patch.<locals>.testrd   z%s.something_elsec                      s(     tjtjd   tjtjd d S r]   )r_   r~   rx   r	   ry   something_elseSomethingElser   r5   r   r   rb      rt   %s.openc                      s     tddtjd d S )Nfilenamerzopen not patched)r_   openr	   Handler   r5   r   r   rb      s   )r   r   r	   ry   r_   r~   rx   rZ   r   r   r   r   return_valuebuiltin_stringassertNotEqualr   )r   rb   mockr   r5   r   
test_patch   s:   zPatchTest.test_patchc                    s6   t dt tj fdd}|   tjjd d S )Nz%s.SomeClass.class_attributec                      s     tjjtjd d S r]   )r_   r~   r   class_attributer	   ClassAttributer   r5   r   r   rb      s   
z2PatchTest.test_patch_class_attribute.<locals>.testrd   )r   r   r	   r   rk   r~   r   r   rf   r   r5   r   test_patch_class_attribute   s   
z$PatchTest.test_patch_class_attributec                    s   G dd dt  t  d fdd}|  t  dt  d fdd}d  |tjtj\|tjtj d S )Nc                   @   rn   )z:PatchTest.test_patchobject_with_default_mock.<locals>.TestN)r   r   r    r	   rX   rx   rp   
something2r   r   r   r   Test   rr   r   rx   c                    s"    |  jd | td d S )N"Mock not passed into test function.patch with two arguments did not create a mock)r_   rx   assertIsInstancer
   r   )r   r   r   r   rb      s   
z:PatchTest.test_patchobject_with_default_mock.<locals>.testr   c                    s    | tjd  |tjd  | jd  | jd |td |td |d |d ||fS )Nz0Patched function didn't receive initial argumentz/Patched function didn't receive second argumentr   z)Second Mock not passed into test functionr   zunexpected value for mock1)	r_   r	   this1this2r   rx   r   r
   r   )r   r   Zmock1Zmock2r   Z
outerMock1Z
outerMock2r   r   r   rb      s*   



)r+   r   r	   r   r   rf   r   r   r   "test_patchobject_with_default_mock   s   


z,PatchTest.test_patchobject_with_default_mockc                    s&   t dt td fdd}|  d S )N%s.SomeClassspecc                    2     t|   ttjt  tdd  d S )Nc                   S      t jS r   r   
not_wibbler   r   r   r   <lambda>      z>PatchTest.test_patch_with_spec.<locals>.test.<locals>.<lambda>r_   r   rQ   r   wibbler
   rO   AttributeErrorMockSomeClassr5   r   r   rb        z,PatchTest.test_patch_with_spec.<locals>.test)r   r   r   rf   r   r5   r   test_patch_with_spec  s   
zPatchTest.test_patch_with_specc                    s&   t jtdtd fdd}|  d S )Nr   r   c                    6     tj|   ttjjt  tdd  d S )Nc                   S      t jjS r   r   r   r   r   r   r   r   r         zDPatchTest.test_patchobject_with_spec.<locals>.test.<locals>.<lambda>	r_   r   r   rQ   r   r   r
   rO   r   ZMockAttributer5   r   r   rb        z2PatchTest.test_patchobject_with_spec.<locals>.testr   r+   r   rf   r   r5   r   test_patchobject_with_spec     
z$PatchTest.test_patchobject_with_specc                    s(   t dt dgd fdd}|  d S )Nr   r   r   c                    r   )Nc                   S   r   r   r   r   r   r   r   r     r   zFPatchTest.test_patch_with_spec_as_list.<locals>.test.<locals>.<lambda>r   r   r5   r   r   rb     r   z4PatchTest.test_patch_with_spec_as_list.<locals>.testr   r   rf   r   r5   r   test_patch_with_spec_as_list  s   
z&PatchTest.test_patch_with_spec_as_listc                    s(   t jtddgd fdd}|  d S )Nr   r   r   c                    r   )Nc                   S   r   r   r   r   r   r   r   r   *  r   zLPatchTest.test_patchobject_with_spec_as_list.<locals>.test.<locals>.<lambda>r   r   r5   r   r   rb   $  r   z:PatchTest.test_patchobject_with_spec_as_list.<locals>.testr   rf   r   r5   r   "test_patchobject_with_spec_as_list#  s   
z,PatchTest.test_patchobject_with_spec_as_listc                    s4   t dt t dt dgd fdd}|  d S )Nr   r   r   r   c                    r   )Nc                   S   r   r   r   r   r   r   r   r   6  r   zMPatchTest.test_nested_patch_with_spec_as_list.<locals>.test.<locals>.<lambda>r   )r   ZMockOpenr5   r   r   rb   1  s   z;PatchTest.test_nested_patch_with_spec_as_list.<locals>.test)r   r   r   rf   r   r5   r   #test_nested_patch_with_spec_as_list/  s   

z-PatchTest.test_nested_patch_with_spec_as_listc                    &   t dt dd fdd}|  d S )Nr   Tr   c                    *    t   j t fdd d S )Nc                          j S r   r   r   r   r   r   r   A  r   zIPatchTest.test_patch_with_spec_as_boolean.<locals>.test.<locals>.<lambda>r_   r   r   rO   r   r   r5   r   r   rb   ;     z7PatchTest.test_patch_with_spec_as_boolean.<locals>.testr   rf   r   r5   r   test_patch_with_spec_as_boolean:  r   z)PatchTest.test_patch_with_spec_as_booleanc                    s&   t jtddd fdd}|  d S )Nr   Tr   c                    r   )Nc                      r   r   r   r   r   r   r   r   M  r   zPPatchTest.test_patch_object_with_spec_as_boolean.<locals>.test.<locals>.<lambda>r   r   r5   r   r   rb   G  r   z>PatchTest.test_patch_object_with_spec_as_boolean.<locals>.test)r   r+   r~   rf   r   r5   r   &test_patch_object_with_spec_as_booleanF  r   z0PatchTest.test_patch_object_with_spec_as_booleanc                    r   )Nr   Tr   c                    s>    t| t |      j t fdd d S )Nc                      r   r   r   r   instancer   r   r   [  r   zVPatchTest.test_patch_class_acts_with_spec_is_inherited.<locals>.test.<locals>.<lambda>)rQ   r   r
   rU   r   rO   r   r   r5   r   r   rb   S  s
   
zDPatchTest.test_patch_class_acts_with_spec_is_inherited.<locals>.testr   rf   r   r5   r   ,test_patch_class_acts_with_spec_is_inheritedR  s   
	z6PatchTest.test_patch_class_acts_with_spec_is_inheritedc                    s:   t dt tjdd fdd}|   tdd  d S )N
%s.froobleTcreatec                      s     ttj d S r   )r_   froobler	   Froobler   r5   r   r   rb   a  rl   zLPatchTest.test_patch_with_create_mocks_non_existent_attributes.<locals>.testc                   S      t S r   r   r   r   r   r   r   f  r3   zPPatchTest.test_patch_with_create_mocks_non_existent_attributes.<locals>.<lambda>)r   r   r	   r   rO   	NameErrorrf   r   r5   r   4test_patch_with_create_mocks_non_existent_attributes`     z>PatchTest.test_patch_with_create_mocks_non_existent_attributesc                    s:   t jtdtjdd fdd}|   ttd d S )Nr   Tr   c                      s     tjtj d S r   )r_   r   r   r	   r   r   r5   r   r   rb   j     zRPatchTest.test_patchobject_with_create_mocks_non_existent_attributes.<locals>.test)r   r+   r   r	   r   rR   hasattrrf   r   r5   r   :test_patchobject_with_create_mocks_non_existent_attributesi  r   zDPatchTest.test_patchobject_with_create_mocks_non_existent_attributesc                 C   s\   |  t tdt tjdd }|  W d    n1 sw   Y  |  tdd  d S )Nr   c                   S   r/   r   r   r   r   r   r   rb   t  r9   z9PatchTest.test_patch_wont_create_by_default.<locals>.testc                   S   r   r   r   r   r   r   r   r   x  r3   z=PatchTest.test_patch_wont_create_by_default.<locals>.<lambda>)rO   r   r   r   r	   r   r   rf   r   r   r   !test_patch_wont_create_by_defaultr  s   
z+PatchTest.test_patch_wont_create_by_defaultc                 C   s\   |  t ttdtjdd }|  W d    n1 sw   Y  | ttd d S )Nordc                   S   r/   r   r   r   r   r   r   rb   }  r9   z?PatchTest.test_patchobject_wont_create_by_default.<locals>.test)	rO   r   r   r+   r   r	   r   rR   r   rf   r   r   r   'test_patchobject_wont_create_by_default{  s   
z1PatchTest.test_patchobject_wont_create_by_defaultc                 C   sH   t td dd }t td dd }| | d | | d d S )	Nz.ordc                 S   s   d| _ tdS )Ne   c)r   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}| }|  |S )Nabcdzdoesnotexists.txt)r   readr   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   r_   )r   r   r   r   r   r   "test_patch_builtins_without_create  s   



	z,PatchTest.test_patch_builtins_without_createc                    sP   G dd dt  t  dtdd  fdd}|    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   r   r   )r	   Staticr   r   r   r   woot  s   z:PatchTest.test_patch_with_static_methods.<locals>.Foo.wootN)r   r   r    rA   r   r   r   r   r   r.     s    r.   r   c                   S   r   r   )r	   r`   r   r   r   r   r     r   z:PatchTest.test_patch_with_static_methods.<locals>.<lambda>c                      s      tj d S r   )r_   r   r	   r`   r   r.   r   r   r   	anonymous  rc   z;PatchTest.test_patch_with_static_methods.<locals>.anonymous)r+   r   rA   r_   r   r	   r   )r   r   r   r   r   test_patch_with_static_methods  s
   z(PatchTest.test_patch_with_static_methodsc                    s8   t j}tt dd fdd}|   t j| d S )Nr.   c                      s     tjd d S Nr.   )r_   r	   r.   r   r5   r   r   r     rl   z-PatchTest.test_patch_local.<locals>.anonymous)r	   r.   r   r+   r_   )r   r@   r   r   r5   r   test_patch_local  s
   zPatchTest.test_patch_localc                    sT   G dd dt }|  tj _t  dd fdd}|   jtj d S )Nc                   @      e Zd ZdZdS )z'PatchTest.test_patch_slots.<locals>.Foo)r.   N)r   r   r    	__slots__r   r   r   r   r.         r.   c                      rj   r   )r_   r.   r   r@   r   r   r   r     rl   z-PatchTest.test_patch_slots.<locals>.anonymous)r+   r	   r.   r   r_   )r   r.   r   r   r   r   test_patch_slots  s   zPatchTest.test_patch_slotsc                    sf   G dd dt  G  fdddt }t  dtj|}| }|  |   jtjd d S )Nc                   @   rV   )z=PatchTest.test_patchobject_class_decorator.<locals>.SomethingNrW   r   r   r   r   rZ     r[   rZ   c                       (   e Zd Z fddZ fddZdS )z7PatchTest.test_patchobject_class_decorator.<locals>.Fooc                    r\   r]   r^   
other_selfra   r   r   test_method     zCPatchTest.test_patchobject_class_decorator.<locals>.Foo.test_methodc                    r\   Nznon-test method patched)r_   rY   r	   rX   r   ra   r   r   not_test_method  r   zGPatchTest.test_patchobject_class_decorator.<locals>.Foo.not_test_methodN)r   r   r    r   r   r   ra   r   r   r.     s    r.   rY   rd   )	r+   r   r	   r`   r   r   r_   rY   rX   )r   r.   r4   r   ra   r    test_patchobject_class_decorator  s   z*PatchTest.test_patchobject_class_decoratorc                    sr   G dd dt }G  fdddt }tdt |}| }|  |   |jtjd  t	j
tjd d S )Nc                   @   rV   )z7PatchTest.test_patch_class_decorator.<locals>.SomethingNrW   r   r   r   r   rZ     r[   rZ   c                       s(   e Zd ZdZ fddZ fddZdS )z1PatchTest.test_patch_class_decorator.<locals>.FooZwhateverc                    s     tj|d d S r]   )r_   r~   rx   )r   Zmock_somethingr5   r   r   r     s   
z=PatchTest.test_patch_class_decorator.<locals>.Foo.test_methodc                    r}   r   )r_   r~   rx   r	   rZ   r   r5   r   r   r     r   zAPatchTest.test_patch_class_decorator.<locals>.Foo.not_test_methodN)r   r   r    Ztest_class_attrr   r   r   r5   r   r   r.     s    r.   rv   rd   )r+   r   r   r   r   r_   rY   r	   rX   r~   rx   rZ   )r   rZ   r.   r4   r   r5   r   test_patch_class_decorator  s   
z$PatchTest.test_patch_class_decoratorc                    sZ   G dd dt  t  dtjt  dtj fdd}|   jtjd d S )Nc                   @   rn   )z3PatchTest.test_patchobject_twice.<locals>.SomethingNro   r   r   r   r   rZ     rr   rZ   rY   c                      r\   r]   r^   r   ra   r   r   rb     s   z.PatchTest.test_patchobject_twice.<locals>.testrd   re   rf   r   ra   r   test_patchobject_twice  s   z PatchTest.test_patchobject_twicec                    s   t  dd   }t  fdd}|   | t ddi fdd}|   | t dg fd	d}|   | d S )
Nrx   initialotherc                         d d<  d= d d< d S N   r1   r   something elser   r   r   r@   r   r   rb        z'PatchTest.test_patch_dict.<locals>.testr1   rD   c                      $    t d   d d d S Nr   r1   rD   r_   lenr   r   r   r   rb        rC   c                      r   r   r   r   r   r   r   rb     r   r+   copyr   dictr_   rz   r   r   r   test_patch_dict  s   zPatchTest.test_patch_dictc                    s   t   t  d< d d<  j }t  fdd}|   j| t ddi fdd}|   j| d S )	Nr   rx   r   c                      r   r   r   r   r   r   r   rb   &  r   z=PatchTest.test_patch_dict_with_container_object.<locals>.testr1   rD   c                      s&    t jd   d d d S r   )r_   r   rH   r   r   r   r   rb   0  s   rF   r+   rH   r  r   r  r_   rz   r   r   r   %test_patch_dict_with_container_object  s   

z/PatchTest.test_patch_dict_with_container_objectc                    s   t  dd   }tj dd fdd}|   | tj ddidd fd	d}|   | tj d
gdd fdd}|   | d S )Nrx   r   Tclearc                      s      i  d d< d d< d S Nr   r1   r   r   r_   r   r   r   r   rb   >  s   z2PatchTest.test_patch_dict_with_clear.<locals>.testr1   rD   c                           ddi d S Nr1   rD   r	  r   r   r   r   rb   H  r   rC   c                      r
  r  r	  r   r   r   r   rb   P  r   r   rz   r   r   r   test_patch_dict_with_clear:  s   z$PatchTest.test_patch_dict_with_clearc                    s   t   t  d< d d<  j }tj dd fdd}|   j| tj dd	idd fd
d}|   j| d S )Nr   rx   r   Tr  c                      s"     ji  d d< d d< d S r  r_   rH   r   r   r   r   rb   `  s   zGPatchTest.test_patch_dict_with_container_object_and_clear.<locals>.testr1   rD   c                      s     jddi d S r  r  r   r   r   r   rb   j  rc   r  rz   r   r   r   /test_patch_dict_with_container_object_and_clearY  s   

z9PatchTest.test_patch_dict_with_container_object_and_clearc                 C   sZ   ddi}t j|dd}| |ddi W d    n1 sw   Y  | |ddi d S )Nr1   rD   r   )r1   )r   r  r_   )r   r@   patchedr   r   r   "test_patch_dict_as_context_managers  s
   z,PatchTest.test_patch_dict_as_context_managerc                 C   sZ   i }t dt t t dt t ddt tt t |dd }| |jd d S )Nr   Tautospecc                   S   r/   r   r   r   r   r   r   	some_name}  s   z0PatchTest.test_name_preserved.<locals>.some_namer  )r   r   r+   r   r  r_   )r   r@   r  r   r   r   test_name_preservedz  s   zPatchTest.test_name_preservedc                 C   s\   i }t |ddidd }| t |  W d    n1 s!w   Y  | |i  d S )Nr1   rD   c                   S      t d)NZKonradr   r   r   r   r   rb     s   z1PatchTest.test_patch_with_exception.<locals>.test)r   r  rO   r   r_   )r   r@   rb   r   r   r   test_patch_with_exception  s   
z#PatchTest.test_patch_with_exceptionc                    s&   t dddi fdd}|  d S )Nz
os.environkonrad_delongz
some valuec                      s     dtj d S )Nr  )assertInosenvironr   r5   r   r   rb     rl   z3PatchTest.test_patch_dict_with_string.<locals>.test)r   r  rf   r   r5   r   test_patch_dict_with_string  s   
z%PatchTest.test_patch_dict_with_stringc                    s^   t j }tdddi fdd}zddit _|   t jddi W |t _d S |t _w )Nz%unittest.test.testmock.support.targetr7   BARc                      s     tjddd d S )NBAZr  r@   r7   )r_   r   targetr   r5   r   r   rb     s   z<PatchTest.test_patch_dict_decorator_resolution.<locals>.testr@   r  )r   r   r  r   r  r_   rz   r   r5   r   $test_patch_dict_decorator_resolution  s   

z.PatchTest.test_patch_dict_decorator_resolutionc                 C   s   t dt tdddd }| t| t jtdtdddd }| t| t dt ddd	d }| t| t jtdddd
d }| t| d S )Nr   Tr   spec_setc                 S   
   d| _ d S Nr@   zrT   r   r   r   rb        
z+PatchTest.test_patch_spec_set.<locals>.testr   c                 S   r$  r%  r&  r(  r   r   r   rb     r)  r#  c                 S   r$  r%  r&  r(  r   r   r   rb     r)  c                 S   r$  r%  r&  r(  r   r   r   rb     r)  )r   r   r   rO   r   r+   r   rf   r   r   r   test_patch_spec_set  s   



zPatchTest.test_patch_spec_setc                 C   s(   t dt dddd }| t| d S )Nr   Tr*  c                 S   s   |  }d|_ d S r%  r&  )rT   r   r   r   r   rb     s   
z-PatchTest.test_spec_set_inherit.<locals>.test)r   r   rO   r   rf   r   r   r   test_spec_set_inherit  s   
zPatchTest.test_spec_set_inheritc                 C   sd   t }tdt }| t | | }z| || | t | W |  n|  w | t | d S )Nrv   )rx   r   r   assertIsstartassertIsNotstop)r   r{   patcherr   r   r   r   test_patch_start_stop  s   zPatchTest.test_patch_start_stopc                 C   s   t tdd}| |  d S Nr7   r   )r   foo_namerk   r0  r   r1  r   r   r   test_stop_without_start  s   z!PatchTest.test_stop_without_startc                 C   s.   t tdd}|  |  | |  d S r3  )r   r4  r.  r0  rk   r5  r   r   r   test_stop_idempotent  s   zPatchTest.test_stop_idempotentc                 C   sf   t }ttdd}| t | | }z| |d | t | W |  n|  w | t | d S )Nrx   r@   )rx   r   r+   r~   r-  r.  r_   r0  )r   r{   r1  Zreplacedr   r   r   test_patchobject_start_stop  s   z%PatchTest.test_patchobject_start_stopc                 C   sn   ddi}|  }tj|dgdd}| || |  z| |ddi W |  n|  w | || d S )Nr@   r7   spameggsTr  r:  r;  )r  r   r  r_   r.  r0  r   dr{   r1  r   r   r   test_patch_dict_start_stop  s   z$PatchTest.test_patch_dict_start_stopc                 C   s@   ddi}|  }tj|dgdd}| |  | || d S )Nr@   r7   r9  Tr  )r  r   r  rR   r0  r_   r<  r   r   r   "test_patch_dict_stop_without_start  s
   z,PatchTest.test_patch_dict_stop_without_startc                    s   | ddi    }G  fdddt}tj ddidd|}|  | | }|  |  | |  |  | | }|  |  | |  |  | d S )	Nr:  r;  c                       r   )z7PatchTest.test_patch_dict_class_decorator.<locals>.Testc                    r
  Nr@   r7   r	  r5   r=  thisr   r   
test_first     zBPatchTest.test_patch_dict_class_decorator.<locals>.Test.test_firstc                    r
  r@  r	  r5   rA  r   r   test_second  rD  zCPatchTest.test_patch_dict_class_decorator.<locals>.Test.test_secondN)r   r   r    rC  rE  r   rA  r   r   r     s    r   r@   r7   Tr  )r  r+   r   r  r_   rC  rE  )r   r{   r   rb   r   rA  r   test_patch_dict_class_decorator  s    z)PatchTest.test_patch_dict_class_decoratorc                    s   G dd dt }G dd d}||| |fD ],}t| t  dd fdd}|   jd |jd d j qd S )	Nc                   @   r   )z0PatchTest.test_get_only_proxy.<locals>.Somethingr@   Nr   r   r    r@   r   r   r   r   rZ   -  r   rZ   c                   @   r   )z4PatchTest.test_get_only_proxy.<locals>.SomethingElser@   NrG  r   r   r   r   r   /  r   r   r@   r7   c                      rj   Nr7   r_   r@   r   proxyr   r   r   rb   5  rl   z+PatchTest.test_get_only_proxy.<locals>.testr+   r-   r   r_   r@   ZassertNotIn__dict__r   rZ   r   thingrb   r   rJ  r   test_get_only_proxy,  s   zPatchTest.test_get_only_proxyc                    s   G dd dt }G dd d}||| |fD ].}t|dd t  dd fd	d
}|   jd |jd d j qd S )Nc                   @   r   )z6PatchTest.test_get_set_delete_proxy.<locals>.Somethingr@   NrG  r   r   r   r   rZ   ?  r   rZ   c                   @   r   )z:PatchTest.test_get_set_delete_proxy.<locals>.SomethingElser@   NrG  r   r   r   r   r   A  r   r   F)r,   r@   r7   c                      rj   rH  rI  r   rJ  r   r   rb   G  rl   z1PatchTest.test_get_set_delete_proxy.<locals>.testrL  rN  r   rJ  r   test_get_set_delete_proxy>  s   z#PatchTest.test_get_set_delete_proxyc                 C   s^   t dt d}ttfi |}| }|  | t | | |j	 d | 
|jt d S )N!   Zside_effectzfoo.bar.return_valuer@   )KeyErrorr
   r   r4  r.  r0  rO   r_   r@   r7   r   r   kwargsr1  r   r   r   r   test_patch_keyword_argsP  s   z!PatchTest.test_patch_keyword_argsc                 C   sb   t dt d}tjtdfi |}| }|  | t | | |j	
 d | |j	t d S )NrR  rS  r4   )rT  r
   r   r+   r.   r.  r0  rO   r_   r@   r7   r   rU  r   r   r   test_patch_object_keyword_args]  s   z(PatchTest.test_patch_object_keyword_argsc                 C   sh   ddi}|  }tj|dddd}|  z| |tdddd W |  n|  w | || d S )Nr@   r7   r         )r@   r7   Zbaz)r  r   r  r.  r_   r0  )r   r{   r  r1  r   r   r   test_patch_dict_keyword_argsj  s   z&PatchTest.test_patch_dict_keyword_argsc                    s   G dd dt }fdd fdd fdd}ttd	d
|}| }t| |  tjt }tj |dd	d
|}| }t| |  d S )Nc                   @   s8   e Zd Zdd Zdd Zdd ZdZG dd	 d	eZd
S )z$PatchTest.test_autospec.<locals>.Booc                 S   r/   r   r   r0   r   r   r   r2   {  r3   z-PatchTest.test_autospec.<locals>.Boo.__init__c                 S   r/   r   r   r0   r   r   r   r4   |  r3   z&PatchTest.test_autospec.<locals>.Boo.fc                 S   r/   r   r   r5   r   r   r   r6   }  r3   z&PatchTest.test_autospec.<locals>.Boo.gr7   c                   @   r<   )z(PatchTest.test_autospec.<locals>.Boo.Barc                 S   r/   r   r   r5   r   r   r   r1     r3   z*PatchTest.test_autospec.<locals>.Boo.Bar.aNr=   r   r   r   r   r>     r?   r>   N)	r   r   r    r2   r4   r6   r@   r+   r>   r   r   r   r   Booz  s    r\  c                    s"   | d |  d  t|  d S N   )assert_called_withrO   rP   r   r5   r   r   _test  s   
z&PatchTest.test_autospec.<locals>._testc                    s  |  d | j d  t| j  |   | j   t| jd  tt| d | j  | jj   tt| jd | 	  | j	  | j	
  | j	j
   t| j	j
d | 	 
  | 	 j
   t| 	 j
d  tt| j	d  tt| 	 d d S )Nr^  hr7   rD   )r4   r_  rO   rP   r6   r   r   r@   lowerr>   r1   r   r5   r   r   _test2  s(   




z'PatchTest.test_autospec.<locals>._test2c                    s,    |  |  | d  | t | S r]  )r-  r.   r   r`  rc  r   r   r   rE     s
   z)PatchTest.test_autospec.<locals>.functionTr  r.   )r+   r   r4  r/  r.   sysmodulesr   )r   r\  rE   rb   r   moduler   rd  r   test_autospecy  s   	

zPatchTest.test_autospecc                    r   )N%s.functionTr  c                    s   t    tt j  tt j t d  tt j t d t   t   t dd t dd  tt   tt	t d d S )Nr^     r   r@   )
rE   Zassert_not_calledrO   AssertionErrorZassert_calledZassert_called_oncer_  rP   r   r   r   r5   r   r   rb     s   

z.PatchTest.test_autospec_function.<locals>.testr   rf   r   r5   r   test_autospec_function  s   
z PatchTest.test_autospec_functionc                 C   s0   t dt ddddd }| }| |d d S )Nri  Tr   )r  r   c                 S   s
   t ddS )Nr^  rj  )rE   )Zmock_functionr   r   r   rb     s   
z.PatchTest.test_autospec_keywords.<locals>.testr   )r   rb   resultr   r   r   test_autospec_keywords  s   

z PatchTest.test_autospec_keywordsc                 C   F   t dt dd}t  |  W d    d S 1 sw   Y  d S )Nz%s.Foo.static_methodTr  )r   r   r.   r8   assert_called_once_withr   methodr   r   r   test_autospec_staticmethod     
"z$PatchTest.test_autospec_staticmethodc                 C   ro  )Nz%s.Foo.class_methodTr  )r   r   r.   r;   rp  rq  r   r   r   test_autospec_classmethod  rt  z#PatchTest.test_autospec_classmethodc                 C   sN   t dt ddd}| t|j tjt }t j|dddd}| t|j d S )Nri  r   T)newr  rE   )r   r   rO   rP   r.  re  rf  r+   )r   r1  rg  r   r   r   test_autospec_with_new  s
   
z PatchTest.test_autospec_with_newc                 C   sX   G dd dt }tt|d}| }z| || | |jt W |  d S |  w )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>     r   r>   r  )r.   r   r4  r.  r   rx  listr0  )r   r>   r1  r   r   r   r   test_autospec_with_object  s   z#PatchTest.test_autospec_with_objectc                 C   sN   t }ttdd}| }z| || | |d| W |  d S |  w )NTr  r   )r.   r   r4  r.  r   r0  )r   ZFooClassr1  r   r   r   r   test_autospec_inherits  s   z PatchTest.test_autospec_inheritsc                 C   sz   t tdd}| }z-| dt| | dt|j | dt|d  | dt|d j W |  d S |  w )NTr  z name='Foo'z name='Foo.f'z name='Foo()'z name='Foo().f')r   r4  r.  r  reprr4   r0  )r   r1  r   r   r   r   test_autospec_name  s   zPatchTest.test_autospec_namec                 C   s^   t tdt dd }z|  W n   t }Y td d d}||| }| d| d S )Nr4   c                   S      t r   )rk  r   r   r   r   rb     rw   z'PatchTest.test_tracebacks.<locals>.testr   zraise AssertionError)	r   r+   r.   re  exc_infounittestZTextTestResultZ_exc_info_to_stringr  )r   rb   errrm  	tracebackr   r   r   test_tracebacks  s   


zPatchTest.test_tracebacksc                 C   sT   t ttd}| }|  | }|  | || ||fD ]}| | q d S )Nnew_callable)r   r4  r   r.  r0  r/  rU   r   r1  Zm1Zm2r   r   r   r   test_new_callable_patch#  s   z!PatchTest.test_new_callable_patchc                 C   sX   t jtdtd}| }|  | }|  | || ||fD ]}| | q"d S )Nr4   r  )r   r+   r.   r   r.  r0  r/  rU   r  r   r   r   test_new_callable_patch_object0  s   z(PatchTest.test_new_callable_patch_objectc                    sl   G  fdddt  tt ddd}| }z| t|  |  jtddd W |	  d S |	  w )Nc                          e Zd ZdZ fddZdS )z:PatchTest.test_new_callable_keyword_arguments.<locals>.BarNc                    
   | _ d S r   rV  r   rV  r>   r   r   r2   @  r   zCPatchTest.test_new_callable_keyword_arguments.<locals>.Bar.__init__r   r   r    rV  r2   r   r  r   r   r>   >      r>   r^  rj  )r  arg1arg2)r  r  )
r+   r   r4  r.  r-  typer_   rV  r  r0  )r   r1  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}|  z|  jt d W |  n|  w tt  d}|  z|  jt d W |  d S |  w )Nc                       r  )z-PatchTest.test_new_callable_spec.<locals>.BarNc                    r  r   r  r  r  r   r   r2   O  r   z6PatchTest.test_new_callable_spec.<locals>.Bar.__init__r  r   r  r   r   r>   M  r  r>   r  r   r   )r  r#  r*  )r+   r   r4  r.  r_   rV  r  r0  r5  r   r  r   test_new_callable_specL  s   z PatchTest.test_new_callable_specc                 C   s`   dt  }t|td}| t|j t|tdd}| }z| j|dd W |  d S |  w )Nz	%s.weeeeer  T)r  r   F)rS   )r4  r   r   rO   r   r.  rU   r0  )r   Znon_existent_attrpr   r   r   r   test_new_callable_createa  s   z"PatchTest.test_new_callable_createc                 C   s4   | j tttt td | j ttjtdt td d S )N)rv  r  r4   )rO   
ValueErrorr   r4  r+   r
   r.   r5   r   r   r   'test_new_callable_incompatible_with_newo  s   
z1PatchTest.test_new_callable_incompatible_with_newc                 C   s0   | j ttttdd | j ttjtdtdd d S )NT)r  r  r4   )rO   r  r   r4  r
   r+   r.   r5   r   r   r   ,test_new_callable_incompatible_with_autospecy  s   
z6PatchTest.test_new_callable_incompatible_with_autospecc           	   
   C   s   G dd dt }tttt |f}|D ]/}dD ]*}|di}ttfd|i|}| }z|j}| t	t
|d W |  q|  w qd S )Nc                   @      e Zd ZdS )z>PatchTest.test_new_callable_inherit_for_mocks.<locals>.MockSubNr   r   r    r   r   r   r   MockSub      r  r"  Tr  x)r   r   r   r
   r   r4  r.  r   rO   r   r   r0  )	r   r  ZMockClassesZKlassargrV  r  r   r   r   r   r   #test_new_callable_inherit_for_mocks  s   
z-PatchTest.test_new_callable_inherit_for_mocksc                 C   sp   G dd dt }tt|dd}| }z| t|| | tt|d W |	  n|	  w | 
|jt d S )Nc                   @   r<   )z>PatchTest.test_new_callable_inherit_non_mock.<locals>.NotAMockc                 S   s
   || _ d S r   r   )r   r   r   r   r   r2     r   zGPatchTest.test_new_callable_inherit_non_mock.<locals>.NotAMock.__init__N)r   r   r    r2   r   r   r   r   NotAMock  r?   r  Tr  r   )r+   r   r4  r.  rQ   r   rO   r   r   r0  r_   r   r.   )r   r  r  r   r   r   r   "test_new_callable_inherit_non_mock  s   z,PatchTest.test_new_callable_inherit_non_mockc                    sR   | t  G  fdddt}tttd|}|   |   | t   d S )Nc                       *   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                    s(    t  t| tt d S r   )r/  r.   r-  r   r   r   Zmock_foor{   rb   r   r   r`    s   zDPatchTest.test_new_callable_class_decorating.<locals>.SomeTest._testc                 S      |  | d S r   r`  r  r   r   r   test_two  r*   zGPatchTest.test_new_callable_class_decorating.<locals>.SomeTest.test_twoc                 S   r  r   r  r  r   r   r   test_one  r*   zGPatchTest.test_new_callable_class_decorating.<locals>.SomeTest.test_oneNr   r   r    r`  r  r  r   r  r   r   SomeTest  s    r  r  )r.   r+   r   r4  r   r  r  r-  )r   r  r   r  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}tjt ddd}||fD ]:}|  zt   t jd t jd W |	  n|	  w t   t j| t j| qtjtddd fdd}|  d S )Nr^  rj  )r4   r6   r   rY  c                      s,    t  tjd tjd d S )Nr   rY  )r-  r.   r_   r4   r6   r   original_foor   r   r   rb     s   z+PatchTest.test_patch_multiple.<locals>.test)
r.   r4   r6   r   multipler4  r.  r-  r_   r0  )r   
original_f
original_gZpatcher1Zpatcher2r1  rb   r   r  r   test_patch_multiple  s$   
zPatchTest.test_patch_multiplec                 C   s$   |  ttjt |  ttjt d S r   )rO   r  r   r  r4  r.   r5   r   r   r   test_patch_multiple_no_kwargs  s   z'PatchTest.test_patch_multiple_no_kwargsc                    sV   t  t j}t j}tjttdtd fdd}|  t j| t j| d S )Nr   r4   r6   r@   c                    sZ    t   tj|  tjd  tj| t| t t|t d S Nr   	r-  r.   r4   r_   r6   r@   rQ   r   r
   )r4   r@   r  r   r   rb     s   z8PatchTest.test_patch_multiple_create_mocks.<locals>.testr.   r4   r6   r   r  r4  r   r_   )r   r  r  rb   r   r  r    test_patch_multiple_create_mocks  s   z*PatchTest.test_patch_multiple_create_mocksc                    sr   t j}t j}tt dd}d|_tt dt}d|_|g|_| fdd}|   t j|  t j| d S )Nr4   r   r6   c                    s      tj|   tjd d S r  )r-  r.   r6   r_   r4   )r6   r5   r   r   rb     s   zHPatchTest.test_patch_multiple_create_mocks_different_order.<locals>.test)	r.   r4   r6   r   r+   attribute_namer   additional_patchersr_   )r   r  r  r1  r   rb   r   r5   r   0test_patch_multiple_create_mocks_different_order  s   z:PatchTest.test_patch_multiple_create_mocks_different_orderc                    s   t t j}t j}tjttdtjttdttd  fdd}tjttdttd tjttd fdd}ttd tjttdtjttd fdd	}fd
d |  |  |  t j| t j| d S )N)r4   r   z.gc                        | fi | d S r   r   r6   rV  r  r   r   test1     z?PatchTest.test_patch_multiple_stacked_decorators.<locals>.test1c                    r  r   r   r  r  r   r   test2  r  z?PatchTest.test_patch_multiple_stacked_decorators.<locals>.test2c                    r  r   r   r  r  r   r   test3  r  z?PatchTest.test_patch_multiple_stacked_decorators.<locals>.test3c                    s   | d}| d}| t  tj| tj|  tj| t|t	 t| t	 t|t	 d S )Nr4   r@   )
poprR   r-  r.   r4   r6   r@   rQ   r   r
   )r6   rV  r4   r@   r  r   r   r`    s   


z?PatchTest.test_patch_multiple_stacked_decorators.<locals>._testr  )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tdtd}| }z>|d }|d }| t	|t	ddg | 
t | | 
t j| | 
t j| | t|t | t|t W |  n|  w | t j| | t j| d S )Nr   r  r4   r@   )r.   r4   r6   r   r  r4  r   r.  r_   setr-  r@   rQ   r   r
   r0  )r   r  r  r  r1  rm  r4   r@   r   r   r   (test_patch_multiple_create_mocks_patcher3  s"   z2PatchTest.test_patch_multiple_create_mocks_patcherc                    sv   | t  t j}t j}G  fdddt}tjttdtd|}| }|  |	  | 
t j| | 
t j| d S )Nc                       r  )z@PatchTest.test_patch_multiple_decorating_class.<locals>.SomeTestc                    sZ    t   tj| tjd  tj| t|t t|t d S r  r  r   r4   r@   r  rb   r   r   r`  T  s   zFPatchTest.test_patch_multiple_decorating_class.<locals>.SomeTest._testc                 S      |  || d S r   r  r  r   r   r   r  \  r'   zIPatchTest.test_patch_multiple_decorating_class.<locals>.SomeTest.test_twoc                 S   r  r   r  r  r   r   r   r  ^  r'   zIPatchTest.test_patch_multiple_decorating_class.<locals>.SomeTest.test_oneNr  r   r  r   r   r  R  s    r  r   r  )r.   r4   r6   r+   r   r  r4  r   r  r  r_   )r   r  r  r  rO  r   r  r   $test_patch_multiple_decorating_classL  s   z.PatchTest.test_patch_multiple_decorating_classc                 C   sn   t jtdd}| t|j t jtddd}|  z| tjd W |  n|  w | 	t
td d S )Nblam)r  T)r  r   )r   r  r.   rO   r   r.  r_   r  r0  rR   r   r5  r   r   r   test_patch_multiple_createm  s   z$PatchTest.test_patch_multiple_createc                 C   s   t jttddgd}| }z4| tj|d  tjd tjd tjj	d tjj	d | 
tttjdd  W |  d S |  w )Nr1   rD   )r@   r#  r@   r^  rj  r   )r   r  r.   r   r.  r_   r@   r1   rD   r_  rO   r   r#   r0  )r   r1  rm  r   r   r   test_patch_multiple_spec_set{  s   z&PatchTest.test_patch_multiple_spec_setc                 C   s   G dd dt }tjttt|d}| }z/| tj|d  | tj|d  | 	tj| | 	tj| | 
tjtj W |  d S |  w )Nc                   @   r  )z9PatchTest.test_patch_multiple_new_callable.<locals>.ThingNr  r   r   r   r   Thing  r  r  )r4   r6   r  r4   r6   )r+   r   r  r.   r   r.  r-  r4   r6   r   r/  r0  )r   r  r1  rm  r   r   r    test_patch_multiple_new_callable  s   z*PatchTest.test_patch_multiple_new_callablec                 C   s   t j}t j}tt ddtt ddtt dddd }tt ddtt ddtt dddd }tt ddtt ddtt ddd	d
 }|||fD ]}| t| | t j| | t j| qVd S )Nr6   r^  missingr4   c                   S   r/   r   r   r   r   r   r   thing1     z3PatchTest.test_nested_patch_failure.<locals>.thing1c                   S   r/   r   r   r   r   r   r   thing2  r  z3PatchTest.test_nested_patch_failure.<locals>.thing2c                   S   r/   r   r   r   r   r   r   thing3  r  z3PatchTest.test_nested_patch_failure.<locals>.thing3)r.   r4   r6   r   r+   rO   r   r_   )r   r  r  r  r  r  funcr   r   r   test_nested_patch_failure  s&   z#PatchTest.test_nested_patch_failurec           	      C   s   t j}t j}t j}dd }tt ddtjt d|dtt dddd	 }tjt d|dtt ddtt ddd
d }tt ddtt ddtjt d|ddd }|||fD ]}| t| | t j| | t j| | t j| q`d S )Nc                   S   r  Ncrasherr  r   r   r   r   r       z4PatchTest.test_new_callable_failure.<locals>.crasherr6   r^  r@   r  r4   c                   S   r/   r   r   r   r   r   r   r    r  z3PatchTest.test_new_callable_failure.<locals>.thing1c                   S   r/   r   r   r   r   r   r   r    r  z3PatchTest.test_new_callable_failure.<locals>.thing2c                   S   r/   r   r   r   r   r   r   r    r  z3PatchTest.test_new_callable_failure.<locals>.thing3)	r.   r4   r6   r@   r   r+   rO   r   r_   )	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t dd}d|_tt dd}d|_tt dd}d|_||g||gfD ]}||_|dd }| t| | 	t j| | 	t j| q,d S )Nr4   r^  r6   r  c                   S   r/   r   r   r   r   r   r   r    r9   z3PatchTest.test_patch_multiple_failure.<locals>.func)
r.   r4   r6   r   r+   r  r  rO   r   r_   )r   r  r  r1  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t dd}d|_tt dd}d|_tjt d|d}d|_||g||gfD ]&}||_|dd	 }	| t	|	 | 
t j| | 
t j| | 
t j| q4d S )
Nc                   S   r  r  r  r   r   r   r   r    r  zCPatchTest.test_patch_multiple_new_callable_failure.<locals>.crasherr4   r^  r6   r@   r  c                   S   r/   r   r   r   r   r   r   r     r9   z@PatchTest.test_patch_multiple_new_callable_failure.<locals>.func)r.   r4   r6   r@   r   r+   r  r  rO   r   r_   )
r   r  r  r  r  r1  r  r  r  r  r   r   r   (test_patch_multiple_new_callable_failure  s&   
z2PatchTest.test_patch_multiple_new_callable_failurec                    sL   t dtfddi}|  tj dd fdd}|   jd d S )Nr.   fishZtastynearly gone)r  c                      rj   )Nr  )r_   r  r   r   r   r   rb     rl   z=PatchTest.test_patch_multiple_string_subclasses.<locals>.test)r  strr   r  r_   r  )r   r.   rb   r   r   r   %test_patch_multiple_string_subclasses	  s   z/PatchTest.test_patch_multiple_string_subclasseszunittest.mock.patch.TEST_PREFIXr@   c                 C   sl   G dd dt }t |dd|}| }| | d | | d | | d | | d d S )Nc                   @   s0   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>.Foor{   c                 S      | j S r   rO  r5   r   r   r   foo_one     z5PatchTest.test_patch_test_prefix.<locals>.Foo.foo_onec                 S   r  r   r  r5   r   r   r   foo_two  r  z5PatchTest.test_patch_test_prefix.<locals>.Foo.foo_twoc                 S   r  r   r  r5   r   r   r   r    r  z6PatchTest.test_patch_test_prefix.<locals>.Foo.test_onec                 S   r  r   r  r5   r   r   r   r    r  z6PatchTest.test_patch_test_prefix.<locals>.Foo.test_twoN)r   r   r    rO  r  r  r  r  r   r   r   r   r.     s    r.   rO  changedr{   )r+   r   r_   r  r  r  r  r   r.   r@   r   r   r   test_patch_test_prefix  s   z PatchTest.test_patch_test_prefixr7   c                    s   G  fdddt }ddi tj dd|}| }| | ddi | | ddi | | ddi | | ddi d S )Nc                       s<   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                       t  S r   r  r5   Zthe_dictr   r   bar_one.  r  z:PatchTest.test_patch_dict_test_prefix.<locals>.Foo.bar_onec                    r  r   r  r5   r  r   r   bar_two0  r  z:PatchTest.test_patch_dict_test_prefix.<locals>.Foo.bar_twoc                    r  r   r  r5   r  r   r   r  2  r  z;PatchTest.test_patch_dict_test_prefix.<locals>.Foo.test_onec                    r  r   r  r5   r  r   r   r  4  r  z;PatchTest.test_patch_dict_test_prefix.<locals>.Foo.test_twoN)r   r   r    r  r  r  r  r   r  r   r   r.   -  s
    r.   keyr{   r  )r  )r+   r   r  r_   r  r  r  r  r  r   r  r   test_patch_dict_test_prefix+  s   
z%PatchTest.test_patch_dict_test_prefixc              	   C   s   dD ]A}t dt fi |di}| }z*| dt| | dt|j | dt|  | dt| j W |  q|  w d S )Nr   r  r#  r   Tz name='SomeClass'z! name='SomeClass.class_attribute'z name='SomeClass()'z# name='SomeClass().class_attribute')r   r   r.  r  r|  r   r0  )r   r  r  r   r   r   r   test_patch_with_spec_mock_reprA  s   
z(PatchTest.test_patch_with_spec_mock_reprc                 C   s`   t ddd }| dt|j  | dt|   W d    d S 1 s)w   Y  d S )Nunittest.test.testmock.supportTr  z" name='support.SomeClass.wibble()'z$ name='support.SomeClass().wibble()')r   r  r|  r   r   )r   r   r   r   r   test_patch_nested_autospec_reprP  s   "z)PatchTest.test_patch_nested_autospec_reprc              	   C   s  dD ]}t dt fi |di}| }zh|  t g}| |j| | |j| | |jjt g | }|t  | |j| |  |t   | |j| | |jt g | |jjt g | |jt g W |	  q|	  w d S )Nr  r   T)
r   r   r.  r   r   r_   Z
mock_callsZmethod_callsappendr0  )r   r  r  r   Zkallsrm  r   r   r   test_mock_calls_with_patchY  s(   
z$PatchTest.test_mock_calls_with_patchc                 C   s   t d}| t|j td" t }|tjd< d|_t d}d|_|  |	  W d    n1 s4w   Y  | 
|jd d S )Nsquizz.squozzsquizz   r   )r   rO   ImportErrorr.  r   r   re  rf  Zsquozzr0  r_   )r   Zp1r  r   r   r   test_patch_imports_lazilyt  s   


	z#PatchTest.test_patch_imports_lazilyc                    s   G dd dG fdddt   fdd}|ddd	 }td
 t }|tjd
< | t| W d    n1 s<w   Y  | jd t | 	jd d | 	jd d d S )Nc                   @   r   )z;PatchTest.test_patch_propagates_exc_on_exit.<locals>.holderNNNN)r   r   r    r  r   r   r   r   holder  r   r  c                       s   e Zd Zd fdd	ZeZdS )zAPatchTest.test_patch_propagates_exc_on_exit.<locals>.custom_patchNc                    s    t | ||| |||f _d S r   )r   __exit__r  )r   etypevaltbr  r   r   r     s   zJPatchTest.test_patch_propagates_exc_on_exit.<locals>.custom_patch.__exit__r  )r   r   r    r   r0  r   r  r   r   custom_patch  s    r  c              
      s$   t | \}} ||td dd d d i 	S )NF)r   r   )r   getterrY   )r  r   r   with_custom_patch  s
   zFPatchTest.test_patch_propagates_exc_on_exit.<locals>.with_custom_patchr  c                 S   r~  r   )RuntimeErrorr   r   r   r   rb     rw   z9PatchTest.test_patch_propagates_exc_on_exit.<locals>.testr  r   r^  zexception value not propagatedrj  z"exception traceback not propagated)
r   r   r   re  rf  rO   r  r-  r  ZassertIsNotNone)r   r  rb   r  r   )r  r  r   !test_patch_propagates_exc_on_exit  s"   


z+PatchTest.test_patch_propagates_exc_on_exitc                 C   sp   dD ]3}t dt fddi|di}| t|j | tdd  t tfddi|di}|  |  qd S )Nr   r#  r  z%s.doesnotexistr   Tc                   S   r   r   )doesnotexistr   r   r   r   r     r3   z1PatchTest.test_create_and_specs.<locals>.<lambda>)r   r   rO   rP   r.  r   MODNAMEr0  )r   kwargr  r   r   r   test_create_and_specs  s   
zPatchTest.test_create_and_specsc                 C   s   t }dD ]}ttfddi|di}| t|j | t | qdD ]}ttfddi|di}| t|j | t | q"dD ]}ttfddi|di}| t|j | t | q@d S )Nr"  r  r   r   r  r#  )r#  r  r   )r~   r   r  rO   rP   r.  r-  )r   r{   r  r  r   r   r   test_multiple_specs  s   zPatchTest.test_multiple_specsc                 C   s>   t tdddd}| }z|j d|_W |  d S |  w )NFr
  r   )r   r  r.  Zdoes_not_existr0  )r   r  r   r   r   r    test_specs_false_instead_of_none  s   z*PatchTest.test_specs_false_instead_of_nonec              	   C   sP   dD ]#}t tfi |di}| }z| tt|d W |  q|  w d S )Nr  r   Zdoesnotexit)r   r  r.  rO   r   r   r0  r   r  r  r   r   r   r   test_falsey_spec  s   zPatchTest.test_falsey_specc              	   C   sf   dD ].}t tfddi|di}| }z| tt|dd | tt|d W |  q|  w d S )Nr  r#  Tr  rx   )r   r  r.  rO   r   r#   r   r0  r  r   r   r   test_spec_set_true  s   
zPatchTest.test_spec_set_truec                 C   @   d}t t|d}| }z| t| W |  d S |  w )N)__call__r   )r   r  r.  rQ   callabler0  r   r   r  r   r   r   r   test_callable_spec_as_list     z$PatchTest.test_callable_spec_as_listc                 C   r  )Nr  r   )r   r  r.  rR   r  r0  r  r   r   r   test_not_callable_spec_as_list  r  z(PatchTest.test_not_callable_spec_as_listc                    s^   t jt j t j}tdt  tdt  td fdd}|  t j| d S )N	os.unlinkos.chdirzos.pathc                    s6   t   tj|  tj tj  d S r   )r   stopallr-  r  pathunlinkchdir)Z	mock_pathr!  r   r   r   r   r     s   z-PatchTest.test_patch_stopall.<locals>.patched)	r  r   r!  r  r   rx   r.  r   r-  )r   r  r  r   r"  r   test_patch_stopall  s   zPatchTest.test_patch_stopallc                    sP   g G dd dt fdd  fdddD  t  | g d 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   rO    r"   rO  c              
      s8   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  ZS )z?PatchTest.test_stopall_lifo.<locals>.get_patch.<locals>.mypatchc                    s     t|  S r   )r  superr0  r5   )	__class__rY   mypatchstoppedr   r   r0    s   
zDPatchTest.test_stopall_lifo.<locals>.get_patch.<locals>.mypatch.stop)r   r   r    r0  __classcell__r   )rY   r)  r*  )r(  r   r)    s    r)  c                      s    S r   r   r   r  r   r   r     r3   z@PatchTest.test_stopall_lifo.<locals>.get_patch.<locals>.<lambda>F)r   )rY   )r*  rO  )rY   r)  r   	get_patch  s   
z.PatchTest.test_stopall_lifo.<locals>.get_patchc                    s   g | ]} |  qS r   )r.  ).0r  )r,  r   r   
<listcomp>  s    z/PatchTest.test_stopall_lifo.<locals>.<listcomp>)r$  r%  r&  )r&  r%  r$  )r+   r   r  r_   r5   r   )r,  r*  rO  r   test_stopall_lifo
  s   zPatchTest.test_stopall_lifoc                    s   i }ddi}ddd |  }|  }   }t|ddd  t|dd	i  t  fd
d}|  | || | || |  | t  | || | || |  | d S )Nr^  r1   ABr^  rj  IIIrj  rD   c                      s
    d= d S r]  r   r   Zdic3r   r   r  %  r)  z2PatchTest.test_patch_dict_stopall.<locals>.patched)r  r   r  r.  r   r_   r  )r   dic1dic2origdic1origdic2Zorigdic3r  r   r5  r   test_patch_dict_stopall  s$   
z!PatchTest.test_patch_dict_stopallc                 C   s   t j}t j}i }ddd}| }| }tdt  tdt  t|ddd  t|  |d= | 	t j| | 	t j| | 
|| | 
|| t  | t j| | t j| | || | || d S )	Nr0  r1  r2  r  r  r3  r4  r^  )r  r   r!  r  r   rx   r.  r   r  r/  r   r  r-  r_   )r   Zoriginal_unlinkZoriginal_chdirr6  r7  r8  r9  r   r   r   !test_patch_and_patch_dict_stopall5  s(   
z+PatchTest.test_patch_and_patch_dict_stopallc                 C   s  ddd}t |dd | | d W d    n1 sw   Y  | | d |j}t |dd | |jd W d    n1 sFw   Y  | |j| t |d	d
 | |jd
 W d    n1 skw   Y  | |jd t |dtdg | |jtdg W d    n1 sw   Y  | |jt  dddd}t |dtdg | | d W d    n1 sw   Y  | | d d S )Nr   c                 S      | S )ZTESTr   r  r   r   r   r@   O  rw   z)PatchTest.test_special_attrs.<locals>.foo__defaults__)r^  r^  __doc__ZFUNr   Z
testpatch2z unittest.test.testmock.testpatch__annotations__)sr^  r=  c                 W   r<  r   r   )r  r1   r   r   r   r@   c  r9   __kwdefaults__)r  r^  )r   )r   r+   r_   r?  r   r  r@  )r   r@   Zorig_docr   r   r   test_special_attrsN  s.   
zPatchTest.test_special_attrsc                 C   s   t  }t |d< d|d< | }ttdtd}t| | }t|t | | 	t| | W d    n1 s>w   Y  | 	|| t|| | 	t| | W d    n1 sdw   Y  | 	|| d S )Nr1   ZpythonrD   Zcdefghijklmnopqrstuvwxyz   )
r   r+   r  ry  ziprangeitemsr   r  r_   )r   r@   r{   Zupdate_valuesZpatched_valuesr   r   r   test_patch_orderdicti  s   
zPatchTest.test_patch_orderdictc                 C   sn   dd l }td$ tjd= tjd= tjd= tjd= tddd	 }|  W d    d S 1 s0w   Y  d S )
Nr   zsys.modulesr  zunittest.test.testmockzunittest.testr   unittest.test.testmock.support.Xc                 S   r/   r   r   r   r   r   r   rb     rw   z9PatchTest.test_dotted_but_module_not_loaded.<locals>.test)unittest.test.testmock.supportr   r  re  rf  )r   r  rb   r   r   r   !test_dotted_but_module_not_loaded|  s   
"z+PatchTest.test_dotted_but_module_not_loadedc              
   C   s   G dd d}dd| fD ]1}| j |d! | t t| W d    n1 s*w   Y  W d    n1 s9w   Y  qd S )Nc                   @   r  )z*PatchTest.test_invalid_target.<locals>.FooNr  r   r   r   r   r.     r  r.       )r   )ZsubTestrO   rP   r   )r   r.   r   r   r   r   test_invalid_target  s   
zPatchTest.test_invalid_targetc                 C   sN   t dt dddd }| t |  W d    d S 1 s w   Y  d S )NrI  r^  )rv  r  c                   S   r/   r   r   r   r   r   r   rb     r9   z@PatchTest.test_cant_set_kwargs_when_passing_a_mock.<locals>.test)r   r+   rO   rP   rf   r   r   r   (test_cant_set_kwargs_when_passing_a_mock  s
   
"z2PatchTest.test_cant_set_kwargs_when_passing_a_mockNT)qr   r   r    rU   rg   ri   rm   ru   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,  r2  r6  r7  r8  r>  r?  rF  rP  rQ  rW  rX  r[  rh  rl  rn  rs  ru  rw  rz  r{  r}  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r  r	  r  r  r  r  r  r  r  r#  r/  r:  r;  rC  rH  rK  rN  rO  r   r   r   r   rN   U   s    

+*

			
!		C	
+!

	"



rN   __main__rP  )*r  re  collectionsr   r  Zunittest.test.testmockr   rJ  r   r   Ztest.test_importlib.utilr   Zunittest.mockr   r   r	   r
   r   r   r   r   r   r   r   r   rf  r   r~   r  r-   rZ   rx   r   r   r+   r.   r4  rE   rF   ZTestCaserN   mainr   r   r   r   <module>   sH   4

              Y