a
    ze                    @   s   d dl Z d dlZ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	 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mZ G dd deZG dd	 d	eZd
d ZG dd dejZedkre  dS )    N)	ALWAYS_EQ)is_instancemock)callDEFAULTpatchsentinel	MagicMockMockNonCallableMockNonCallableMagicMock	AsyncMock_Call	_CallListcreate_autospecc                   @   s(   e Zd Zdd Zdd Zdd ZeZdS )Iterc                 C   s   t g d| _d S )NthisisZaniter)r   thingself r   5/usr/lib/python3.9/unittest/test/testmock/testmock.py__init__   s    zIter.__init__c                 C   s   | S Nr   r   r   r   r   __iter__   s    zIter.__iter__c                 C   s
   t | jS r   )nextr   r   r   r   r   r      s    z	Iter.nextN)__name__
__module____qualname__r   r   r   __next__r   r   r   r   r      s   r   c                   @   s2   e Zd ZdddZed	ddZed
ddZdS )	SomethingNc                 C   s   d S r   r   r   abcdr   r   r   meth        zSomething.methc                 C   s   d S r   r   )clsr&   r'   r(   r)   r   r   r   cmeth"   s    zSomething.cmethc                 C   s   d S r   r   r&   r'   r(   r)   r   r   r   smeth%   s    zSomething.smeth)N)N)N)r    r!   r"   r*   classmethodr-   staticmethodr/   r   r   r   r   r$      s
   
r$   c                 C   s   d S r   r   r&   r   r   r   	something)   r+   r3   c                   @   sX  e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zd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/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAdB Z#dCdD Z$dEdF Z%dGdH Z&dIdJ Z'dKdL Z(dMdN Z)dOdP Z*dQdR Z+dSdT Z,dUdV Z-dWdX Z.dYdZ Z/d[d\ Z0d]d^ Z1d_d` Z2dadb Z3dcdd Z4dedf Z5dgdh Z6didj Z7dkdl Z8dmdn Z9dodp Z:dqdr Z;dsdt Z<dudv Z=dwdx Z>dydz Z?d{d| Z@d}d~ ZAdd ZBdd ZCdd ZDdd ZEdd ZFdd ZGdd ZHdd ZIdd ZJdd ZKdd ZLdd ZMdd ZNdd ZOdd ZPdd ZQdd ZRdd ZSdd ZTdd ZUdd ZVdd ZWdd ZXdd ZYdd ZZdd 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܄ Zpddބ Zqdd Zrdd Zsdd Ztdd Zudd Zvdd Zwdd Zxdd Zydd Zzdd Z{dd Z|dd Z}dd Z~dd Zdd Zdd Zdd  Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS (  MockTestc                 C   s   t d d S )Nzfrom unittest.mock import *)execr   r   r   r   test_all.   s    zMockTest.test_allc                 C   s   t  }| |jd | |jdd | t|jt d | |jd d | |j	g d | |j
g d | d|jd	 | |jd
 | |jd | |ji d d S )Nz called not initialised correctlyr   z$call_count not initialised correctlyz&return_value not initialised correctlyz#call_args not initialised correctly(call_args_list not initialised correctlyz&method_calls not initialised correctlyZ_itemsz/default mock should not have '_items' attributez parent not initialised correctlyz!methods not initialised correctlyz$children not initialised incorrectly)r   assertFalsecalledassertEqual
call_count
assertTruer   return_value	call_argscall_args_listmethod_callsassertNotIn__dict__assertIsNone_mock_parent_mock_methods_mock_childrenr   r   r   r   r   test_constructor5   s:    





zMockTest.test_constructorc                 C   s   t d d}| |jd d S )Nr=   z(return value in constructor not honoured)r   rC   r=   rG   r   r   r    test_return_value_in_constructorQ   s    
z)MockTest.test_return_value_in_constructorc                 C   s*   dd }t |}d|j_| | d d S )Nc                   S   s   d S r   r   r   r   r   r   fX   r+   z9MockTest.test_change_return_value_via_delegate.<locals>.f   )r   r   r=   r:   r   rK   r   r   r   r   %test_change_return_value_via_delegateW   s    z.MockTest.test_change_return_value_via_delegatec                 C   sN   dd }t |}t |j_| t |  W d    n1 s@0    Y  d S )Nc                   S   s   d S r   r   r   r   r   r   rK   _   r+   z8MockTest.test_change_side_effect_via_delegate.<locals>.f)r   	TypeErrorr   side_effectassertRaisesrM   r   r   r   $test_change_side_effect_via_delegate^   s
    
z-MockTest.test_change_side_effect_via_delegatec                 C   s   t dd}| dt| | dt| t| t  dft dddfg}|D ]\}}| d| t|j | d| t|  | d| t| j | d	| t|  | d
| t|   | d| t|  jj j qNd S )Nfoonamez'%s'r   barz%s.barz%s.foo()z%s.foo().bingz%s()z%s()()z%s()().foo.bar.baz().bing)r   assertInrepridrV   rS   Zbingbaz)r   r   mocksrU   r   r   r   	test_reprf   s    

zMockTest.test_reprc                 C   s   G dd dt }t|d}| dt| t| d}| dt| t|d}| dt| t| d}| dt| t|dd}| dt| | d	t| tdd
}| dt| t }| dt| tdgd}| dt| d S )Nc                   @   s   e Zd ZdS )z'MockTest.test_repr_with_spec.<locals>.XNr    r!   r"   r   r   r   r   Xw   s   r^   specz
 spec='X' spec_setz spec_set='X' rS   )r`   rU   z name='foo' rT   r`   )objectr   rW   rX   rA   r   r^   r   r   r   r   test_repr_with_specv   s$    


zMockTest.test_repr_with_specc                    s   t  }dd }||_| jt|dddd |jdddd g d  fdd}||_| | | | gg d	d
 t tjd}| |jtjd dd }t |tjd}| | tj d S )Nc                  _   s   t dd S )NZkablooie)SystemError)argskwargsr   r   r   effect   s    z)MockTest.test_side_effect.<locals>.effectrL         fishrL   rj   rk   c                      s      S r   popr   resultsr   r   ri      s    rk   rj   rL   zside effect not used correctlyrP   z#side effect in constructor not usedc                   S   s   t S r   r   r   r   r   r   rP      s    z.MockTest.test_side_effect.<locals>.side_effect)rP   r=   )	r   rP   rQ   rf   assert_called_withr:   r	   
SideEffectRETURN)r   r   ri   rP   r   rq   r   test_side_effect   s$    zMockTest.test_side_effectc                    s   g d  fdd}dd }t |}g d|_| | | | gg dd g d t |}||_| | | | gg dd d S )	Nrn   c                      s      S r   ro   r   rq   r   r   ri      s    z2MockTest.test_autospec_side_effect.<locals>.effectc                   S   s   d S r   r   r   r   r   r   rK      r+   z-MockTest.test_autospec_side_effect.<locals>.fz1side effect not used correctly in create_autospecrs   z'callable side effect not used correctly)r   rP   r:   )r   ri   rK   r   r   rq   r   test_autospec_side_effect   s    
z"MockTest.test_autospec_side_effectc                 C   s,   dd }t |}td|_| td| d S )Nc                   S   s   d S r   r   r   r   r   r   rK      r+   z7MockTest.test_autospec_side_effect_exception.<locals>.fzBazinga!)r   
ValueErrorrP   assertRaisesRegexrM   r   r   r   #test_autospec_side_effect_exception   s    
z,MockTest.test_autospec_side_effect_exceptionc                 C   sR  t  }dg}t d||d}|tjtjd |j}|  tj|_|j}|  |  | 	|j
dd | 	|j|d | 	|j|d | |jd | 	|jd	d
 | 	|jd d | 	|jg d | 	|jg d|jg f  | 	|jg  | 	|jtjd | 	|j|d | |jd | 	|jd|id | 	|j|d | |jjd d S )Nr3   child)rU   parentr`   r3   zname incorrectly resetzparent incorrectly resetzmethods incorrectly resetzcalled not resetr   zcall_count not resetzcall_args not resetzcall_args_list not resetz0method_calls not initialised correctly: %r != %rzside_effect incorrectly resetzreturn_value incorrectly resetzreturn value mock not resetzchildren reset incorrectlyzchildren incorrectly clearedzchild not reset)r   r	   r$   ZSomethingElser3   rw   rP   r=   
reset_mockr:   
_mock_namerD   rE   r8   r9   r;   r>   r?   r@   
mock_callsrF   )r   r   r`   r   r3   r=   r   r   r   test_reset_mock   sV    





zMockTest.test_reset_mockc                 C   s   t  }||_|  d S r   )r   r=   r   rG   r   r   r   test_reset_mock_recursion   s    z"MockTest.test_reset_mock_recursionc                 C   s   t  }|  d S r   )r   	mock_openr   r   r&   r   r   r   (test_reset_mock_on_mock_open_issue_18622   s    z1MockTest.test_reset_mock_on_mock_open_issue_18622c                 C   s>  t  }| t|jt d | }| | |d |  |tj}| |jd | |j	dd | |j
tjfi fd | |j
jtjfd | |j
ji d | |jtjfi fgd tj|_|tjtjd}| |tjd	 | |j	d
d | |j
tjfdtjifd | |jtjfi ftjfdtjifgd d S )Nz%Default return_value should be a Mockz'different result from consecutive callszcalled not setrL   zcall_count incorrectzcall_args not setr7   )keyzincorrect return valuerj   r   zcall_args_list not set)r   r<   r   r=   r:   r   r	   Argr9   r;   r>   rg   rh   r?   ZReturnValueZKeyArg)r   r   resultZret_valr   r   r   	test_call   sP    



zMockTest.test_callc              	   C   s   t  }|  |tj |tjd |tjtjd | |jdtjffdtjiftjfdtjifg | |jtjfdtjif | |jjtjf | |jjdtji | 	|jdk d S )N)kwr   r   za long sequence)
r   r	   r   ZKwargr:   r?   r>   rg   rh   r8   rG   r   r   r   test_call_args_comparison$  s"    

z"MockTest.test_call_args_comparisonc                 C   s2  t  }| ||k | ||k | |t  k | |t  k | |t jk | |t jk | t j|k | t j|k | |tk | |tk t t  }t t j}| ||k | ||k | ||k | ||k | |tk | |tk | |dk | |dk d S )NrL   )r   r
   r<   r8   ANYr   r   )r   ZmmZcall1Zcall2r   r   r   test_calls_equal_with_any:  s*    z"MockTest.test_calls_equal_with_anyc                 C   sd   t  }|  |  | t|jd |  | t|j |dddddd |jdddddd d S )NrL   rj   rk   rm   Znothingr&   r'   )r   rv   rQ   AssertionErrorr   rG   r   r   r   test_assert_called_withV  s    z MockTest.test_assert_called_withc                 C   s    t  }|t   |tj d S r   )r
   rv   r   r   r   mr   r   r   test_assert_called_with_anye  s    
z$MockTest.test_assert_called_with_anyc                 C   s   ddd}t |d}|dddd |ddd |jdddd | jt|jdddd | t}|jd	d
 W d    n1 s0    Y  | |jjt d S )Nc                 S   s   d S r   r   r.   r   r   r   rK   l  r+   z9MockTest.test_assert_called_with_function_spec.<locals>.fr_   rL   rj   rk   r'   r(   r&   r'   r(      e)N)r   rv   rQ   r   assertIsInstance	exception	__cause__rO   r   rK   r   cmr   r   r   %test_assert_called_with_function_speck  s    


*z.MockTest.test_assert_called_with_function_specc                    sz    fdd}t t jd}|| t tjd}|| t t jd}|| t tjd}|| t t jd}|| d S )Nc                    sF   | dddd |  ddd | j dddd  jt| j dddd d S )NrL   rj   rk   r   r   )rv   rQ   r   r   r   r   r   _check|  s    
z<MockTest.test_assert_called_with_method_spec.<locals>._checkr_   )r   r$   r*   r-   r/   )r   r   r   r   r   r   #test_assert_called_with_method_spec{  s    z,MockTest.test_assert_called_with_method_specc                 C   s   d}|  t|d t   W d    n1 s60    Y  |  t|d tdd  W d    n1 sv0    Y  d S )Nz"Expected '{0}' to have been calledr   Z	test_namerT   )r|   r   formatr   assert_called)r   msgr   r   r   $test_assert_called_exception_message  s
    (z-MockTest.test_assert_called_exception_messagec                    s   t            | t j    | t j  dddd  jdddd     dddd | t fdd d S )NrS   rV   rj   rZ   c                      s    j ddddS )NbobrV   rj   r   assert_called_once_withr   r   r   r   <lambda>  r+   z7MockTest.test_assert_called_once_with.<locals>.<lambda>)r   r   rQ   r   r   r   r   r   r   test_assert_called_once_with  s    
z%MockTest.test_assert_called_once_withc                    s6   t    d  d | ttd fdd d S )NrL   rj   zCalls: [call(1), call(2)]c                      s
     dS )Nrj   r   r   r   r   r   r     r+   zAMockTest.test_assert_called_once_with_call_list.<locals>.<lambda>)r   r|   r   reescaper   r   r   r   &test_assert_called_once_with_call_list  s    
z/MockTest.test_assert_called_once_with_call_listc                 C   s   ddd}t |d}|dddd |ddd |jdddd | jt|jdddd | t}|jd	d
 W d    n1 s0    Y  | |jjt |ddd | t|jddd | t|jddd d S )Nc                 S   s   d S r   r   r.   r   r   r   rK     r+   z>MockTest.test_assert_called_once_with_function_spec.<locals>.fr_   rL   rj   rk   r   r   r   r            )N)r   r   rQ   r   r   r   r   rO   r   r   r   r   *test_assert_called_once_with_function_spec  s$    


*

z3MockTest.test_assert_called_once_with_function_specc                 C   sb   t  }|j}| t|t d | |j|d t  }d|j_| | dd | |jjd d S )Nzattribute isn't a mockz+different attributes returned for same namerk   zmethod returned wrong valuez!method didn't record being called)r   r3   r<   r   r:   r=   r9   r   r   r3   r   r   r   #test_attribute_access_returns_mocks  s    

z,MockTest.test_attribute_access_returns_mocksc                 C   s0   t  }|j}| |jdd | |j|d d S )Nr3   z attribute name not set correctlyz"attribute parent not set correctly)r   r3   r:   r   rD   r   r   r   r   (test_attributes_have_name_and_parent_set  s    

z1MockTest.test_attributes_have_name_and_parent_setc                 C   st   t  }|jdd d |jjdtjd | |jjdddtjifgd | |jdd	d
d ifdddtjifgd d S )Nrk   rl   r   )caker3   )r   r   z#method calls not recorded correctly)rk   rm   zsomething_else.something)r   r3   something_elser	   ZCaker:   r@   rG   r   r   r   test_method_calls_recorded  s    
z#MockTest.test_method_calls_recordedc                 C   s   t  }|  | |jdg | |jddi fg t  }|d | |jdg | |jddi fg t  }|jdd | |jdd	difg | |jddd	difg t  }|jdd
d | |jdddd
ifg d S )Nr   r3   r   	different)r3   r   r   rL   xr   Zmore)somer   )r   r3   r:   r@   rG   r   r   r    test_method_calls_compare_easily  s&    

z)MockTest.test_method_calls_compare_easilyc              	   C   sH   dgdfD ]8}dD ].}t f i ||i}|j | tdt|d qq
d S )Nr3   r   )r`   rb   z-Mock object has no attribute 'something_else'r   )r   r3   r|   AttributeErrorgetattr)r   r`   argr   r   r   r   test_only_allowed_methods_exist  s    z(MockTest.test_only_allowed_methods_existc                    s>   G dd dt } fdd}|t|d |t| d d S )Nc                   @   s   e Zd ZdZdZdd ZdS )z*MockTest.test_from_spec.<locals>.Somethingrk   Nc                 S   s   d S r   r   r   r   r   r   y   r+   z,MockTest.test_from_spec.<locals>.Something.y)r    r!   r"   r   __something__r   r   r   r   r   r$     s   r$   c                    s:   | j  | j | j  tdt| d  tdt| d d S )Nz Mock object has no attribute 'z'zz)Mock object has no attribute '__foobar__'Z
__foobar__)r   r   r   r|   r   r   r   r   r   r   test_attributes"  s    z0MockTest.test_from_spec.<locals>.test_attributesr_   )rc   r   )r   r$   r   r   r   r   test_from_spec  s    zMockTest.test_from_specc                 C   sJ   t  }t |d}| | |  |  |dddd |jdddd d S )NwrapsrL   rj   rk   rl   )r   r:   r   rv   r   realr   r   r   r   test_wraps_calls6  s    
zMockTest.test_wraps_callsc                    s8   G dd dt }| }t|d | t fdd d S )Nc                   @   s   e Zd ZdS )zFMockTest.test_wraps_prevents_automatic_creation_of_mocks.<locals>.RealNr]   r   r   r   r   RealC  s   r   r   c                      s      S r   )Znew_attrr   r   r   r   r   I  r+   zJMockTest.test_wraps_prevents_automatic_creation_of_mocks.<locals>.<lambda>)rc   r   rQ   r   )r   r   r   r   r   r   /test_wraps_prevents_automatic_creation_of_mocksB  s    
z8MockTest.test_wraps_prevents_automatic_creation_of_mocksc                 C   s4   t  }t |d}d|_| | d | |j d S )Nr   rk   )r   r=   r:   r8   r9   r   r   r   r   ,test_wraps_call_with_nondefault_return_valueL  s
    
z5MockTest.test_wraps_call_with_nondefault_return_valuec                    s   G dd dt }| }t|d |   |  | t fdd |  j|j  jjdddd	}|jjjdddd	 | ||j  d S )
Nc                   @   s   e Zd Ze ZdS )z,MockTest.test_wraps_attributes.<locals>.RealN)r    r!   r"   r   	attributer   r   r   r   r   W  s   r   r   c                      s    j S r   rl   r   r   r   r   r   ^  r+   z0MockTest.test_wraps_attributes.<locals>.<lambda>rL   rj   rk   rl   )	rc   r   r:   r   rQ   r   assertNotEqualZfrogrv   )r   r   r   r   r   r   r   test_wraps_attributesV  s    
zMockTest.test_wraps_attributesc                 C   sd   G dd dt }| }t|d}tjtg|j_| | tj | | tj | 	t
|j d S )Nc                   @   s   e Zd Zdd ZdS )z[MockTest.test_customize_wrapped_object_with_side_effect_iterable_with_default.<locals>.Realc                 S   s   t jS r   )r	   ORIGINAL_VALUEr   r   r   r   methodh  s    zbMockTest.test_customize_wrapped_object_with_side_effect_iterable_with_default.<locals>.Real.methodNr    r!   r"   r   r   r   r   r   r   g  s   r   r   )rc   r   r	   VALUE1r   r   rP   r:   r   rQ   StopIterationr   r   r   r   r   r   r   Dtest_customize_wrapped_object_with_side_effect_iterable_with_defaultf  s    
zMMockTest.test_customize_wrapped_object_with_side_effect_iterable_with_defaultc                 C   sf   G dd dt }| }t|d}tjtjg|j_| | tj | | tj | t	|j d S )Nc                   @   s   e Zd Zdd ZdS )zNMockTest.test_customize_wrapped_object_with_side_effect_iterable.<locals>.Realc                 S   s   d S r   r   r   r   r   r   r   v  r+   zUMockTest.test_customize_wrapped_object_with_side_effect_iterable.<locals>.Real.methodNr   r   r   r   r   r   u  s   r   r   )
rc   r   r	   r   VALUE2r   rP   r:   rQ   r   r   r   r   r   7test_customize_wrapped_object_with_side_effect_iterablet  s    
z@MockTest.test_customize_wrapped_object_with_side_effect_iterablec                 C   s:   G dd dt }| }t|d}t|j_| t|j d S )Nc                   @   s   e Zd Zdd ZdS )zOMockTest.test_customize_wrapped_object_with_side_effect_exception.<locals>.Realc                 S   s   d S r   r   r   r   r   r   r     r+   zVMockTest.test_customize_wrapped_object_with_side_effect_exception.<locals>.Real.methodNr   r   r   r   r   r     s   r   r   )rc   r   RuntimeErrorr   rP   rQ   r   r   r   r   8test_customize_wrapped_object_with_side_effect_exception  s
    
zAMockTest.test_customize_wrapped_object_with_side_effect_exceptionc                 C   sF   G dd dt }dd }| }t|d}||j_| | tj d S )Nc                   @   s   e Zd Zdd ZdS )zNMockTest.test_customize_wrapped_object_with_side_effect_function.<locals>.Realc                 S   s   d S r   r   r   r   r   r   r     r+   zUMockTest.test_customize_wrapped_object_with_side_effect_function.<locals>.Real.methodNr   r   r   r   r   r     s   r   c                   S   s   t jS r   )r	   VALUEr   r   r   r   rP     s    zUMockTest.test_customize_wrapped_object_with_side_effect_function.<locals>.side_effectr   )rc   r   r   rP   r:   r	   r   )r   r   rP   r   r   r   r   r   7test_customize_wrapped_object_with_side_effect_function  s    
z@MockTest.test_customize_wrapped_object_with_side_effect_functionc                 C   s@   G dd dt }| }t|d}tj|j_| | tj d S )Nc                   @   s   e Zd Zdd ZdS )zFMockTest.test_customize_wrapped_object_with_return_value.<locals>.Realc                 S   s   d S r   r   r   r   r   r   r     r+   zMMockTest.test_customize_wrapped_object_with_return_value.<locals>.Real.methodNr   r   r   r   r   r     s   r   r   )rc   r   r	   r   r   r=   r:   r   r   r   r   /test_customize_wrapped_object_with_return_value  s
    

z8MockTest.test_customize_wrapped_object_with_return_valuec                 C   sp   G dd dt }| }t|d}tjtjg|j_tj|j_| 	| tj | 	| tj | 
t|j d S )Nc                   @   s   e Zd Zdd ZdS )zVMockTest.test_customize_wrapped_object_with_return_value_and_side_effect.<locals>.Realc                 S   s   d S r   r   r   r   r   r   r     r+   z]MockTest.test_customize_wrapped_object_with_return_value_and_side_effect.<locals>.Real.methodNr   r   r   r   r   r     s   r   r   )rc   r   r	   r   r   r   rP   ZWRONG_VALUEr=   r:   rQ   r   r   r   r   r   ?test_customize_wrapped_object_with_return_value_and_side_effect  s    

zHMockTest.test_customize_wrapped_object_with_return_value_and_side_effectc                 C   sL   G dd dt }| }t|d}dd |j_tj|j_| | tj d S )Nc                   @   s   e Zd Zdd ZdS )zWMockTest.test_customize_wrapped_object_with_return_value_and_side_effect2.<locals>.Realc                 S   s   d S r   r   r   r   r   r   r     r+   z^MockTest.test_customize_wrapped_object_with_return_value_and_side_effect2.<locals>.Real.methodNr   r   r   r   r   r     s   r   r   c                   S   s   t S r   ru   r   r   r   r   r     r+   z[MockTest.test_customize_wrapped_object_with_return_value_and_side_effect2.<locals>.<lambda>)rc   r   r   rP   r	   r   r=   r:   r   r   r   r   @test_customize_wrapped_object_with_return_value_and_side_effect2  s    

zIMockTest.test_customize_wrapped_object_with_return_value_and_side_effect2c                 C   sn   G dd dt }| }t|d}tjtg|j_tj|j_| 	| tj | 	| tj | 
t|j d S )Nc                   @   s   e Zd Zdd ZdS )z^MockTest.test_customize_wrapped_object_with_return_value_and_side_effect_default.<locals>.Realc                 S   s   d S r   r   r   r   r   r   r     r+   zeMockTest.test_customize_wrapped_object_with_return_value_and_side_effect_default.<locals>.Real.methodNr   r   r   r   r   r     s   r   r   )rc   r   r	   r   r   r   rP   rx   r=   r:   rQ   r   r   r   r   r   Gtest_customize_wrapped_object_with_return_value_and_side_effect_default  s    

zPMockTest.test_customize_wrapped_object_with_return_value_and_side_effect_defaultc                 C   s   ddi}t |d}| |dd | |d t  | d|v  d|j_| |dd tj|j_| |dd | |dd  | |d t  | d|v  d|d< | |dd | |d t  | d|v  |d= | |dd  d S )NrS   rV   r   r=   rZ   spam)r
   r:   getr   r8   r=   r	   r   )r   dataZwrapped_dictr   r   r   test_magic_method_wraps_dict  s$    

z%MockTest.test_magic_method_wraps_dictc                 C   sT   G dd d}t |d}| }| |dd | |d d | | d d S )Nc                   @   s   e Zd Zdd Zdd ZdS )z3MockTest.test_magic_method_wraps_class.<locals>.Fooc                 S   s   |S r   r   )r   indexr   r   r   __getitem__  s    z?MockTest.test_magic_method_wraps_class.<locals>.Foo.__getitem__c                 S   s   dS NrS   r   r   r   r   r   __custom_method__  s    zEMockTest.test_magic_method_wraps_class.<locals>.Foo.__custom_method__N)r    r!   r"   r   r   r   r   r   r   Foo  s   r   r   rj   rS   )r
   r:   r   r   )r   r   klassobjr   r   r   test_magic_method_wraps_class  s    	
z&MockTest.test_magic_method_wraps_classc                 C   s4   t td}| t| t tdd}| t| d S Nrt   rS   )r   r   rQ   rG   r   r   r   test_exceptional_side_effect  s    
z%MockTest.test_exceptional_side_effectc                 C   s4   t td}| t| t tdd}| t| d S r   )r   KeyboardInterruptrQ   rG   r   r   r    test_baseexceptional_side_effect	  s    
z)MockTest.test_baseexceptional_side_effectc                 C   s   t  }| td|j d S )Nz
not called)r   r|   r   rv   rG   r   r   r   test_assert_called_with_message  s    z(MockTest.test_assert_called_with_messagec                 C   s   t dd}| td|j d S )NZgeoffreyrT   z'Expected 'geoffrey' to be called once\.)r   r|   r   r   rG   r   r   r   $test_assert_called_once_with_message  s
    
z-MockTest.test_assert_called_once_with_messagec                    s2   t   | t fdd d _|  jd d S )Nc                      s    j S r   )r    r   r   r   r   r      r+   z'MockTest.test__name__.<locals>.<lambda>rS   )r   rQ   r   r    r:   r   r   r   r   test__name__  s    zMockTest.test__name__c                 C   sJ   G dd dt }t|dgd}|d |jd | tt|d d S )Nc                   @   s   e Zd ZdS )z-MockTest.test_spec_list_subclass.<locals>.SubNr]   r   r   r   r   Sub'  s   r   rS   r_   rk   )listr   appendrv   rQ   r   r   )r   r   r   r   r   r   test_spec_list_subclass&  s
    
z MockTest.test_spec_list_subclassc                 C   s   G dd dt }t|d}| || t| d}| || | |j| | t jjd t|d}| || t| d}| || d S )Nc                   @   s   e Zd ZdS )z#MockTest.test_spec_class.<locals>.XNr]   r   r   r   r   r^   1  s   r^   r_   r   ra   )rc   r   r   assertIs	__class__r:   r    rd   r   r   r   test_spec_class0  s    

zMockTest.test_spec_classc                 C   s   G dd d}t |d}| || t | d}| || | |j| | t  jjd t |d}| || t | d}| || d S )Nc                   @   s   e Zd ZdS )z2MockTest.test_spec_class_no_object_base.<locals>.XNr]   r   r   r   r   r^   E  s   r^   r_   r   ra   )r   r   r   r   r:   r    rd   r   r   r   test_spec_class_no_object_baseD  s    

z'MockTest.test_spec_class_no_object_basec                    sL   G dd dt }t|d d _t|d  fdd}d _| t| d S )Nc                   @   s   e Zd ZdZdS )z8MockTest.test_setting_attribute_with_spec_set.<locals>.Xrk   N)r    r!   r"   r   r   r   r   r   r^   Y  s   r^   r_   rS   ra   c                      s
   d _ d S r   r   r   r   r   r   set_attr`  s    z?MockTest.test_setting_attribute_with_spec_set.<locals>.set_attr)rc   r   r   r   rQ   r   )r   r^   r  r   r   r   $test_setting_attribute_with_spec_setX  s    

z-MockTest.test_setting_attribute_with_spec_setc                 C   s4   t  }| t j| t td tt  d S )Ng    eA)sysgetrecursionlimit
addCleanupsetrecursionlimitintcopyr   )r   currentr   r   r   	test_copyg  s    zMockTest.test_copyc                    s\   G dd dt }||d |  jd  fdd}| t|  fdd}| t| d S )Nc                   @   s&   e Zd Zdd Zdd ZeeeZdS )z8MockTest.test_subclass_with_properties.<locals>.SubClassc                 S   s   dS Nrk   r   r   r   r   r   _gets  s    z=MockTest.test_subclass_with_properties.<locals>.SubClass._getc                 S   s   t dd S )Nzstrange error)	NameError)r   valuer   r   r   _setu  s    z=MockTest.test_subclass_with_properties.<locals>.SubClass._setN)r    r!   r"   r  r  propertysome_attributer   r   r   r   SubClassr  s   r  ra   rk   c                      s
   d _ d S r  )r  r   sr   r   test|  s    z4MockTest.test_subclass_with_properties.<locals>.testc                      s
   d _ d S )NrV   rS   r   r  r   r   r    s    )r   r:   r  rQ   r  r   )r   r  r  r   r  r   test_subclass_with_propertiesq  s    
z&MockTest.test_subclass_with_propertiesc                 C   s>   t  }dd }|t|_|d |d | t|dd d S )Nc                 S   s   |  | | |S r   )Z_increment_mock_callZ
_mock_callr   r   r   r   __call__  s    
z,MockTest.test_setting_call.<locals>.__call__onetwo)r   typer  rv   rQ   rO   )r   r   r  r   r   r   test_setting_call  s    

zMockTest.test_setting_callc                 C   s   t  }tt|}tdd tt D }| t ||  |j|jf | dt| | dt| d  |_|_| dt| | dt| dd |_	| d	t| d S )
Nc                 S   s   g | ]}| d s|qS )_)
startswith).0r   r   r   r   
<listcomp>  r+   z%MockTest.test_dir.<locals>.<listcomp>r&   r'   r(   r)   c                 S   s   t g S r   )r   r  r   r   r   r     r+   z#MockTest.test_dir.<locals>.<lambda>r   )
r   setdirr:   r&   r'   rW   r(   r)   r   )r   r   attrs
type_attrsr   r   r   test_dir  s    
zMockTest.test_dirc                 C   sX   t tjd}tttj}tt|}| t ||  d|_| t|dd d S )Nr_   rk   versionrL   )r   unittestTestCaser"  r#  r:   r'  count)r   r   Ztestcase_attrsr$  r   r   r   test_dir_from_spec  s    zMockTest.test_dir_from_specc                 C   s^   t tdd}|  z8ttt }ttt}| t ||  W |  n
|  0 d S )NZ
FILTER_DIRF)	r   rc   r   startr"  r#  r   r:   stop)r   Zpatcherr$  r%  r   r   r   test_filter_dir  s    zMockTest.test_filter_dirc                 C   s6   t  }d|j_| dt| |`| dt| d S )NrL   r~   )r   r~   r=   rW   r#  rA   rG   r   r   r   ,test_dir_does_not_include_deleted_attributes  s
    z5MockTest.test_dir_does_not_include_deleted_attributesc                 C   s   t dd}| |jd tdd}| |jd tdt d}t f i |}| t| | |j d | |jt t  }|jf i | | t| | |j d | |jt d S )NrV   r  !   )rP   zfoo.bar.return_valuerS   )	r   r:   rS   r
   KeyErrorrQ   rV   r   Zconfigure_mock)r   r   rh   r   r   r   test_configure_mock  s     

zMockTest.test_configure_mockc                 O   sR   |  |}||i | W d    n1 s.0    Y  t|j}| || d S r   )rQ   strr   r:   )r   r   messagefuncrg   rh   contextr   r   r   r   assertRaisesWithMsg  s    ,
zMockTest.assertRaisesWithMsgc              
   C   s  t  }d}d}d}| jt|||f |jddddd |jddddd	 |jj|jjg}|D ]0}d
}d}d}| jt|||f |ddddd qZ|D ]*}d
}d}d}| jt|||f |dd q|D ],}d
}d}d}| t|||f |ddd q|D ]&}d
}d}d}| t|||f | qd S )Nznot called.zmock(1, '2', 3, bar='foo')z0expected call not found.
Expected: %s
Actual: %srL   2rk   rS   )rV   r  zfoo(1, '2', 3, foo='foo')zfoo(1, '2', 3, bar='foo')zfoo(bar='foo')zfoo(1, 2, 3)rj   zfoo())r   r7  r   rv   rS   r   )r   r   actualexpectedr4  Z	assertersr*   r   r   r   'test_assert_called_with_failure_message  sT    
z0MockTest.test_assert_called_with_failure_messagec              	   C   s  t  }| |jg kd t  }|  ddi fg}| |j| |  |t  | |j| | |jjddi fg t  }| jdddddd	 ddi fd
dtddd	fg}| |j| | |jjjddtddd	fg | |jjddtddd	fg t  }| j	 
  ddi fddi fddi fg}| |j| | | jtj	 
   t tddfD ]8}t f i |}t|j ddi fg}| |j| t f i |}|   ddi fddi fg}| |j| | | jt g t f i |}|ddd | |jtddd  | | jtdd  | |  jtd  t f i |}|dddjjd | |jtdddjjd  | | jtddjjd  | |  jtdjjd  t f i |}t| j	 
  ddi f}| |jd | | | jtj	 
    | | j	 jt
    | | j	 j
jt    qpd S )NT r   rL   rj   rk   r   r   r   z().foorn   rS   z
().foo.barz().foo.bar().bazrV   rT   zfoo.__int__r&   za()z().foo.bar().baz().__int__)r
   r   r   r:   rS   r   r   dictr=   rV   rZ   	call_listr  r&   r'   r(   __int__)r   r   r:  rh   Z	last_callr   r   r   test_mock_calls!  s    






zMockTest.test_mock_callsc                 C   sF   t  }| }|  | |jt t  g | |jt g d S r   )r   Zwibbler:   r   r   )r   r   r   r   r   r   test_child_mock_call_equalo  s
    z#MockTest.test_child_mock_call_equalc                 C   sF   t  }|   | |jd t   | |jd t  d S NrL   r   )r   rS   r3   r   r   r   r   r:   r   r   r   r   test_mock_call_not_equal_leafy  s    z&MockTest.test_mock_call_not_equal_leafc                 C   sF   t  }|   | |jd t   | |jd t  d S rC  r   rS   rV   r   r   r   rZ   r   r   r   r   !test_mock_call_not_equal_non_leaf  s    z*MockTest.test_mock_call_not_equal_non_leafc                 C   s8   t  }|jdd  | |jd tjdd  d S )NrL   r   rj   )r   rS   rV   r:   r   r   r   r   r   r   2test_mock_call_not_equal_non_leaf_params_different  s    z;MockTest.test_mock_call_not_equal_non_leaf_params_differentc                 C   s,   t  }|j  | |jd tj  d S Nr   rE  r   r   r   r   &test_mock_call_not_equal_non_leaf_attr  s    
z/MockTest.test_mock_call_not_equal_non_leaf_attrc                 C   s.   t  }|j  | |jd t   d S rH  )r   rS   rV   r   r   r   r   r   r   r   2test_mock_call_not_equal_non_leaf_call_versus_attr  s    
z;MockTest.test_mock_call_not_equal_non_leaf_call_versus_attrc                 C   s^   t  }|  j  | t|jd d | t|jd d | t|jd d d S )Nr   z
call.foo()rL   zcall.foo().bar()rj   zcall.foo().bar().baz.bob())r   rS   rV   rZ   r   r:   rX   r   r   r   r   r   test_mock_call_repr  s
    zMockTest.test_mock_call_reprc                 C   s0   t  }||_t|  | t| d d S )Nz<Mock name='mock\(\)' id='\d+'>)r   rS   rX   ZassertRegexr   r   r   r   test_mock_call_repr_loop  s    z!MockTest.test_mock_call_repr_loopc                 C   s   t  }| t g|jv  d S r   )r   r8   r   r   r   r   r   r   test_mock_calls_contains  s    z!MockTest.test_mock_calls_containsc                 C   sh   G dd dt }| }| |j| | | | G dd dt }| }| |j| | | | d S )Nc                   @   s   e Zd ZdS )+MockTest.test_subclassing.<locals>.SubclassNr]   r   r   r   r   Subclass  s   rO  c                   @   s   e Zd Zdd ZdS )rN  c                 [   s   t f i |S r   )r   )r   rh   r   r   r   _get_child_mock  s    z;MockTest.test_subclassing.<locals>.Subclass._get_child_mockN)r    r!   r"   rP  r   r   r   r   rO    s   )r   r   rS   assertNotIsInstance)r   rO  r   r   r   r   test_subclassing  s    zMockTest.test_subclassingc                    s   t  t t t g} fdd}|D ]z}|| t|rb|  |dd |dd |  || |  |jjddd |d d |  || q$d S )Nc                    s@   d}|D ]2}t | |} |t  |t  |g  qd S )N)r?   r@   r   )r   r   r   r   r:   )r   namesrU   attrr   r   r   assert_attrs  s    
z-MockTest.test_arg_lists.<locals>.assert_attrsrL   rj   rk   r2   )	r   r
   r   r   callabler   rS   rV   rZ   )r   r[   rU  r   r   r   r   test_arg_lists  s&    

zMockTest.test_arg_listsc                 C   s   t  }|ddd |ddd | t|jd | |jjd | |jjtdd dtddfdtddfg}t||jD ]@\}}| t|d | |d	 |d	  | |d |d  q~d S )
NrL   rk   r2   rj   r   r'   )rj   rL   r   )	r   r:   lenr>   rg   rh   r>  zipr?   )r   r   Zexpected_listr:  r>   r   r   r   test_call_args_two_tuple  s    z!MockTest.test_call_args_two_tuplec                 C   s  t tg dd}| | | | gg d | t| tg dd}| | | | gg d | t| t dd}| | | | gg d | t| G dd dt}t|d}| | | t t d}| | | | | gg d | t| d S )	Nrn   rt   r   Zghi)ghic                   @   s   e Zd ZdS )z/MockTest.test_side_effect_iterator.<locals>.FooNr]   r   r   r   r   r     s   r   r   )	r   r   r:   rQ   r   r
   rc   r   r   )r   r   r   r   r   r   test_side_effect_iterator  s"    

z"MockTest.test_side_effect_iteratorc                 C   s\   t tfD ]N}tdtdf}||d}| t| | | d | t| | | d qd S )Nrk   r   rt   )r   r
   r{   r1  rQ   r:   )r   Klassiterabler   r   r   r   $test_side_effect_iterator_exceptions
  s    
z-MockTest.test_side_effect_iterator_exceptionsc                 C   s   t  }tg d|_| | | | gg d | t| |j}| |ttg  g d|_| | | | gg d | t| |j}| |ttg  t }||_| | | | | gg d | t| | 	|j| d S )Nrn   r   r   )
r   r   rP   r:   rQ   r   r   r  r   r   )r   r   rP   Z	this_iterr   r   r   !test_side_effect_setting_iterator  s$    
z*MockTest.test_side_effect_setting_iteratorc                 C   s4   t dd}tdtg|_| | | gddg d S )Nrj   rI   rL   )r   r   r   rP   r:   rG   r   r   r   !test_side_effect_iterator_default)  s    
z*MockTest.test_side_effect_iterator_defaultc                 C   s  t  }|dd |dd |dd |dd |dd tddddifd	d
ddifddddifddi fdd
ddifg}|D ]}|j|gdd q|tddtddt dd dfD ]}| jt|j|gdd qtddtddgtddtddgtddtddgg}|D ]}|j|dd q
tddtddtddgtddtddgtddtddtddgtddtddtddtddtddgg}|D ]}| jt|j|dd qd S )NrL   rj   rk   r2   r   r   rX  r&   )rk   r   r   )r<  rL   rj   r<  rg  TZ	any_orderr8  rS   r      )r   r   assert_has_callsrQ   r   )r   r   kallskallZ
kall_listsZ	kall_listr   r   r   test_assert_has_calls_any_order.  sD    




"*z(MockTest.test_assert_has_calls_any_orderc           	   
   C   s  t ddddifdt dddd	d
difg}t  t dg}|t  jdd  |t jt i d	dg  g }t
 t fD ]F}|dd |dd |dd |dd |ddd |||f qt
 }|  |d | jdd |jt i d	dg |||f |D ]j\}}tt|D ]R}dD ]F}||||  }|| t|dkr>| t|jtt| q>q6q"d S )NrL   rj   r&   rk   rf  r   rX  r<  rY  r'   r2   r  r   rn   )r   rS   rV   extendr   rZ   r?  Zbamr"  rm   r   r
   r   rangerZ  rj  rQ   r   r   reversed)	r   Zkalls1Zkalls2r[   r   rk  r_  stepZtheser   r   r   test_assert_has_callsZ  s@    
"






zMockTest.test_assert_has_callsc              	   C   sF  G dd d}t |}|| fD ]H}|jddddd |tjdddddg |tddddg q |  || fD ]l}| t|jt g |ddd |tdtdddg |jtdt ddg q||  tdt	dt 	dt ddddg}|D ]}| t|j|g q(d S )Nc                   @   s,   e Zd Zdd ZdddZG dd dZdS )	z=MockTest.test_assert_has_calls_nested_spec.<locals>.Somethingc                 S   s   d S r   r   r   r   r   r   r     r+   zFMockTest.test_assert_has_calls_nested_spec.<locals>.Something.__init__Nc                 S   s   d S r   r   r%   r   r   r   r*     r+   zBMockTest.test_assert_has_calls_nested_spec.<locals>.Something.methc                   @   s   e Zd Zdd Zdd ZdS )zAMockTest.test_assert_has_calls_nested_spec.<locals>.Something.Fooc                 S   s   d S r   r   r   r   r   r   r     r+   zJMockTest.test_assert_has_calls_nested_spec.<locals>.Something.Foo.__init__c                 S   s   d S r   r   )r   r&   r'   r   r   r   meth1  r+   zGMockTest.test_assert_has_calls_nested_spec.<locals>.Something.Foo.meth1N)r    r!   r"   r   rs  r   r   r   r   r     s   r   )N)r    r!   r"   r   r*   r   r   r   r   r   r$     s   
r$   rL   rj   rk   r)   r   )
r   r*   rj  r   r   rQ   r   r   rs  Znon_existent)r   r$   Z
mock_classr   Zinvalid_callsrl  r   r   r   !test_assert_has_calls_nested_spec  s.    
" z*MockTest.test_assert_has_calls_nested_specc                 C   sJ   t  }|     |    t    }|	| d S r   )
r
   rS   rV   rZ   r  r  threer   r?  rj  )r   r   callsr   r   r   )test_assert_has_calls_nested_without_spec  s
    z2MockTest.test_assert_has_calls_nested_without_specc                 C   s  ddd}t |d}|dddd |dd	d
dd |dddd ddi fddddifdi fg}|| |j|dd ||dd   |j|dd  dd ||d d  |j|d d dd tt|}| t || W d    n1 s0    Y  |j|dd | t" ||dd   W d    n1 sH0    Y  |j|dd  dd | t" ||d d  W d    n1 s0    Y  |j|d d dd d S )Nc                 S   s   d S r   r   r.   r   r   r   rK     r+   z<MockTest.test_assert_has_calls_with_function_spec.<locals>.fr_   rL   rj   rk   r   r   r   r   ri  r(   r)   
         )r(   r<  rn   )r   r   r   r)   )rz  r{  r|  Trh  r=  )N)r   rj  r   rp  rQ   r   )r   rK   r   rw  r   r   r   (test_assert_has_calls_with_function_spec  s2    


(22z1MockTest.test_assert_has_calls_with_function_specc                 C   s   d	dd}t |d}|d | tdtd}|t g W d    n1 sX0    Y  | |j	j
 | tdtd&}|t tddg W d    n1 s0    Y  | |j	j
t d S )
Nc                 S   s   d S r   r   r   r   r   r   rK     r+   zAMockTest.test_assert_has_calls_not_matching_spec_error.<locals>.fr_   rL   z^{}$z5Calls not found.
Expected: [call()]
Actual: [call(1)]zError processing expected calls.
Errors: [None, TypeError('too many positional arguments')]
Expected: [call(), call(1, 2)]
Actual: [call(1)]rj   )N)r   r|   r   r   r   r   rj  r   rC   r   r   r   rO   r   r   r   r   -test_assert_has_calls_not_matching_spec_error  s.    

,4z6MockTest.test_assert_has_calls_not_matching_spec_errorc                 C   s   t  }|dd |dd |ddd |dd |jdd |jddd | t|j | t|jdd | jt|jdd d S )NrL   rj   rk   r2   r   rX  r   )r   assert_any_callrQ   r   rG   r   r   r   test_assert_any_call  s*    

zMockTest.test_assert_any_callc                 C   s   ddd}t |d}|dddd |dd	d
dd |ddd |jdddd |dd	d
d |jdd	d
dd | jt|jdddd | t}|jdd W d    n1 s0    Y  | |jjt d S )Nc                 S   s   d S r   r   r.   r   r   r   rK     r+   z;MockTest.test_assert_any_call_with_function_spec.<locals>.fr_   rL   rj   rk   r   r   r   r   ri  ry  r   r.   r   r   )N)r   r  rQ   r   r   r   r   rO   r   r   r   r   'test_assert_any_call_with_function_spec  s    


*z0MockTest.test_assert_any_call_with_function_specc                 C   sf   dd }t  }||_t|t|jg}|D ]6}|dd |dd | |jtddtddg q*d S )Nc                 S   s   d S r   r   r   r   r   r   rK     r+   z3MockTest.test_mock_calls_create_autospec.<locals>.frL   rj   rk   r   )r   rK   r   r:   r   r   )r   rK   r   Zfuncsr5  r   r   r   test_mock_calls_create_autospec  s    

z(MockTest.test_mock_calls_create_autospecc                 C   s$   t jt dd}| dt| d S )NZ
sweet_funcrT   )r   r   rc   rW   rX   r   r   r   r   test_create_autospec_with_name+  s    z'MockTest.test_create_autospec_with_namec              	   C   st   G dd d}dD ]\}| j |d< tt||}|  |  | t|d W d    q1 sd0    Y  qd S )Nc                   @   s$   e Zd Zedd Zedd ZdS )zMMockTest.test_create_autospec_classmethod_and_staticmethod.<locals>.TestClassc                 S   s   d S r   r   )r,   r   r   r   class_method2  s    zZMockTest.test_create_autospec_classmethod_and_staticmethod.<locals>.TestClass.class_methodc                   S   s   d S r   r   r   r   r   r   static_method5  s    z[MockTest.test_create_autospec_classmethod_and_staticmethod.<locals>.TestClass.static_methodN)r    r!   r"   r0   r  r1   r  r   r   r   r   	TestClass1  s   
r  )r  r  )r   Z	extra_arg)ZsubTestr   r   r   r   rQ   rO   )r   r  r   Zmock_methodr   r   r   1test_create_autospec_classmethod_and_staticmethod0  s    z:MockTest.test_create_autospec_classmethod_and_staticmethodc                 C   s   t  }d}| t| |  W d    n1 s40    Y  | t| |  W d    n1 sh0    Y  t dd}|  |  d S )Nz1Attributes cannot start with 'assert' or 'assret'T)Zunsafe)r   r|   r   Zassert_foo_callZassret_foo_call)r   r   r   r   r   r   test_mock_unsafe?  s    &&
zMockTest.test_mock_unsafec                 C   sP   t  }|j  |  | t |j  W d    n1 sB0    Y  d S r   )r   helloassert_not_calledrQ   r   r   r   r   r   test_assert_not_calledK  s
    
zMockTest.test_assert_not_calledc                 C   s*   t  }|dd | ttd|j d S )NrL   rj   zCalls: [call(1, 2)])r   r|   r   r   r   r  r   r   r   r   test_assert_not_called_messageR  s    
z'MockTest.test_assert_not_called_messagec                 C   sb   t  }| t |j  W d    n1 s00    Y  |  |j  |  |j  d S r   )r   rQ   r   r  r   r   r   r   r   test_assert_calledY  s    (
zMockTest.test_assert_calledc                 C   s   t  }| t |j  W d    n1 s00    Y  |  |j  |  | t |j  W d    n1 s~0    Y  d S r   )r   rQ   r   r  assert_called_oncer   r   r   r   test_assert_called_oncec  s    (
z MockTest.test_assert_called_oncec                 C   s2   t  }|dd |d | ttd|j d S )NrL   rj   rk   zCalls: [call(1, 2), call(3)])r   r|   r   r   r   r  r   r   r   r   test_assert_called_once_messagen  s    
z(MockTest.test_assert_called_once_messagec                 C   sN   t  }| t}|  W d    n1 s.0    Y  | dt|j d S )NzCalls:)r   rQ   r   r  rA   r3  r   )r   r   r   r   r   r   *test_assert_called_once_message_not_calledv  s    &z3MockTest.test_assert_called_once_message_not_calledc                 C   s0   t  }|jddd d}| t|jj| d S )Nr  Zhero)rU   Zdaddyz call(name='hello', daddy='hero'))r   r  r:   rX   r>   )r   r   textr   r   r   test_ordered_call_signature}  s    z$MockTest.test_ordered_call_signaturec                 C   sV   t  }t dd}t }|  |dd | |jd | | |jd | d S )N   r  r   rL   )r   r*  r   r   r:   r@   )r   r(   r_  r   r   r   r   test_override_tuple_methods  s    z$MockTest.test_override_tuple_methodsc                 C   s>   t dddgd}|jddd | |jt  | |jd  d S )Nrz  rj   rk   r=   rP   T)r   r   r   r=   r:   rP   r   r   r   r   test_reset_return_sideeffect  s    z%MockTest.test_reset_return_sideeffectc                 C   s<   t dddgd}|jdd | |jt  | |jd  d S )Nrz  rj   rk   r  TrI   )r   r   r   r=   r   rP   r   r   r   r   test_reset_return  s    zMockTest.test_reset_returnc                 C   s<   t dddgd}|jdd | |jd | |jd  d S )Nrz  rj   rk   r  Trt   )r   r   r:   r=   rP   r   r   r   r   test_reset_sideeffect  s    zMockTest.test_reset_sideeffectc                 C   s@   t t ddd}| | d |jdd | | d d S )NrL   rI   rK   T)r
   r:   rK   r   r   r   r   r   r   test_reset_return_with_children  s    z(MockTest.test_reset_return_with_childrenc                 C   sD   t t ddgdd}| |jjd  |jdd | |jjd  d S )Nrj   rk   rt   r  T)r
   r   rK   rP   r   r:   r   r   r   r   +test_reset_return_with_children_side_effect  s    z4MockTest.test_reset_return_with_children_side_effectc           	         s  G dd dt }G dd dt }G dd dt  ttttg}t|D ]0}||f fdd	 ||f fd	d	 qD|D ]
}t td
dfD ]}| }|j|j	|j
f ||fdgdgffD ]\}}t td
dfD ]}|j|fi | |j | tt|d | tt|d d|v r.| tt|dd  |j|fi | | tt|d |j	 | tt|d d|v r| tt|dd  qqqqzd S )Nc                   @   s   e Zd ZdZdS )z)MockTest.test_mock_add_spec.<locals>._OnerL   N)r    r!   r"   r  r   r   r   r   _One  s   r  c                   @   s   e Zd ZdZdS )z)MockTest.test_mock_add_spec.<locals>._Tworj   N)r    r!   r"   r  r   r   r   r   _Two  s   r  c                   @   s   e Zd Zd Z ZZdS )z-MockTest.test_mock_add_spec.<locals>.AnythingZfourN)r    r!   r"   r  r  rv  r   r   r   r   Anything  s   r  c                    s
   |  dS )Nr_   r   Kr  r   r   r     r+   z-MockTest.test_mock_add_spec.<locals>.<lambda>c                    s
   |  dS )Nra   r   r  r  r   r   r     r+   Tra   r  r  rv  rb   )rc   r   r
   r   r   r   r   r>  r  r  rv  mock_add_specrQ   r   r   setattr)	r   r  r  Zklassesra  rh   r   ZOneZTwor   r  r   test_mock_add_spec  sL    



zMockTest.test_mock_add_specc                    s|   t tfD ]n}|  t   t | tt  |   d  d j_ t | 	t d | t fdd qd S )NrS   r   c                      s    d S r   r   r   r   r   r   r     r+   z;MockTest.test_mock_add_spec_magic_methods.<locals>.<lambda>)
r
   r   r  r  rc   rQ   rO   r@  r=   r:   )r   ra  r   r   r    test_mock_add_spec_magic_methods  s    

z)MockTest.test_mock_add_spec_magic_methodsc                 C   s   t ttttfD ]}| }t |_|  | |jt g | |j	t g | }tdd|_
|
  | |jg  | |j	g  | }t  |_|  | |jg  | |j	g  qd S )NrU   rT   )r   r   r
   r   r   rS   r:   r@   r   r   rV   rZ   r   ra  r   r   r   r   test_adding_child_mock  s$    
zMockTest.test_adding_child_mockc                 C   s@   t tfD ]2}| }t |_|   | |jt t  g qd S r   )r   r
   r=   r:   r   r   r  r   r   r   test_adding_return_value_mock
  s
    z&MockTest.test_adding_return_value_mockc                 C   s   G dd dt }t }t |d}t |d}| }| |j | }| |j ||d ||d |  |  | 	|j
t t g d S )Nc                   @   s   e Zd ZdZdZdS )z'MockTest.test_manager_mock.<locals>.Foor  r  N)r    r!   r"   r  r  r   r   r   r   r     s   r   r  r  )rc   r   r   r,  r  r-  attach_mockr  r  r:   r   r   )r   r   managerZp1Zp2Zmock_oneZmock_twor   r   r   test_manager_mock  s    zMockTest.test_manager_mockc                 C   sh   t tfD ]Z}| }t dd|_tdd|_t| t| | |jt t g | |j	g  qd S )Nrk   rI   g      @)
r   r
   r@  	__float__r  floatr:   r   r   r@   )r   ra  r   r   r   r   test_magic_methods_mock_calls)  s    z&MockTest.test_magic_methods_mock_callsc                 C   s<   t jdd}|d}| }|d}| }| || d S )Nr   Z	read_dataa-namezanother-name)r   r   readr:   )r   mocked_openf1Zf1_dataf2Zf2_datar   r   r    test_mock_open_reuse_issue_217504  s    z)MockTest.test_mock_open_reuse_issue_21750c                 C   sV   t jdd}|d}dd |D }| |d d | |d d	 | t|g  d S )
NzRemarkable
Norwegian Bluer  r  c                 S   s   g | ]}|qS r   r   r   liner   r   r   r!  A  r+   z=MockTest.test_mock_open_dunder_iter_issue.<locals>.<listcomp>r   zRemarkable
rL   zNorwegian Blue)r   r   r:   r   )r   r  r  linesr   r   r    test_mock_open_dunder_iter_issue<  s    z)MockTest.test_mock_open_dunder_iter_issuec                 C   s   t jdd}|d}t|}| }dd |D }| |d | |d | |d d	 | t|g  | t t| W d    n1 s0    Y  d S )
Nz1st line
2nd line
3rd liner  r  c                 S   s   g | ]}|qS r   r   r  r   r   r   r!  K  r+   z6MockTest.test_mock_open_using_next.<locals>.<listcomp>z	1st line
z	2nd line
r   z3rd line)r   r   r   r#   r:   r   rQ   r   )r   r  r  Zline1Zline2r  r   r   r   test_mock_open_using_nextF  s    z"MockTest.test_mock_open_using_nextc                 C   s,   t jdd}d|jj_| dt|  d S )Nfoo
barnr  abc)r   r   r=   readliner:   r   )r   mopenr   r   r   3test_mock_open_next_with_readline_with_return_valueS  s    
z<MockTest.test_mock_open_next_with_readline_with_return_valuec                 C   sl   t t jdd}t d|: |j}|j}td|_dd }| t| W d    n1 s^0    Y  d S )NZJLVrT   ztempfile.NamedTemporaryFilezTest 2 Errorc                   S   s   t  d d S )NZasd)tempfileNamedTemporaryFilewriter   r   r   r   attempt_  s    z.MockTest.test_mock_open_write.<locals>.attempt)	r   r   r
   r   r=   r  OSErrorrP   rQ   )r   Zmock_namedtempZmock_filehandleZ
mock_writer  r   r   r   test_mock_open_writeX  s    
zMockTest.test_mock_open_writec                 C   sJ   t jdd}dd |jj_|  }|  }| d| | d| d S )Nr  r  c                  W   s   dS )Nr  r   )rg   r   r   r   r   e  r+   z8MockTest.test_mock_open_alter_readline.<locals>.<lambda>r  )r   r   r=   r  rP   r:   )r   r  firstsecondr   r   r   test_mock_open_alter_readlinec  s    

z&MockTest.test_mock_open_alter_readlinec                 C   s   t jdd}|d}|  | d|  | d|  | d|  | d|  | g |  | g |  d S )NrS   r  rV   r<  )r   r   r  r:   r  	readlines)r   _openr^  r   r   r   test_mock_open_after_eofk  s    z!MockTest.test_mock_open_after_eofc                 C   s
  t tfD ]}| }t|}||_| | | | t|| |  | | | | t|| | }|j|_| 	dt|j | 	dt|j |  | 	dt|j | 	dt|j | }t|}| |_||j_| t|| | t| | qd S )Nzname='mock.a')
r   r
   rX   r=   r   r:   r   r&   r'   rW   )r   ra  r   Zoriginal_reprr   r   r   test_mock_parentsw  s,    zMockTest.test_mock_parentsc                 C   s   t tttf}|D ]}|D ]z}| }|dd}||d | |j| | dt| |j	d | 
|jtj	dg | 
|jtj	dg qqd S )NrS   rT   rV   zname='mock.bar'rL   )r   r
   r   r   r  r   rV   rW   rX   rZ   r:   r   r   r@   r   classesra  ZKlass2r   Zm2r   r   r   test_attach_mock  s    
zMockTest.test_attach_mockc                 C   s   t tttf}t tfD ]h}|D ]^}| }|dd}||d | | | | dt| |  | 	|j
t    qqd S )NrS   rT   r=   zname='mock()')r   r
   r   r   r  r   rW   rX   rS   r:   r   r   r?  r  r   r   r   test_attach_mock_return_value  s    
z&MockTest.test_attach_mock_return_valuec                 C   s  t  }tjt ddd}| |j d ||d |d td |d t	dt	dt	dg}t	dt	dt	dg}| |j
| | |jj
| | tj
| | |j
| | d	t|jj | |j d	 W d    n1 s0    Y  d S )
N
.somethingTZautospecr3   r~   rL   rj   rk   
mock.child)r   r   r   r    r:   Z_extract_mock_namer  r~   r3   r   r   rW   rX   )r   r   Z	mock_funcZparent_callsZchild_callsr   r   r   test_attach_mock_patch_autospec  s    
z(MockTest.test_attach_mock_patch_autospecc              
   C   s  t jt ddd}t }||d t }|jddddd	 |tj	t j
ddddd	g |jtt j
ddddd	g |tt j
ddddd	g W d    n1 s0    Y  t jt d
dd\}t }||d td |tdg ttdg |tdg W d    n1 s.0    Y  t jt ddd}t }||d t }|jddddd	 |t t jddddd	g |jtddddd	g |t t jddddd	g W d    n1 s0    Y  d S )Nz.Something.methTr  attach_methrL   rj   rk   r   rt  r  attach_funcz
.Something
attach_obj)r   r   r    r   r  r$   r*   rj  r   r  r   r3   r  r  )r   Zmockedr  r   r   r   r   )test_attach_mock_patch_autospec_signature  s2    :0
z2MockTest.test_attach_mock_patch_autospec_signaturec                 C   sf   t  t t t fD ]L}| t|d |`| t|d |`| t|d | 	t
t|d qd S )Nr   rK   )r   r
   r   r   r<   hasattrr   r8   rK   rQ   r   r   rG   r   r   r   test_attribute_deletion  s    z MockTest.test_attribute_deletionc                 C   s   t  t t t fD ]t}d|_| t|d | |jd |`| t|d d|_| t|d | |jd |`| t|d qd S )Nrk   rS   r   )	r   r
   r   r   rS   r<   r  r:   r8   rG   r   r   r   7test_mock_does_not_raise_on_repeated_attribute_deletion  s    z@MockTest.test_mock_does_not_raise_on_repeated_attribute_deletionc              	   C   sP   t  t t t fD ]6}|`| t |`W d    q1 s@0    Y  qd S r   )r   r
   r   r   rS   rQ   r   rG   r   r   r   4test_mock_raises_when_deleting_nonexistent_attribute  s    z=MockTest.test_mock_raises_when_deleting_nonexistent_attributec                 C   s,   t  }d|_|`|  | t|d d S )NTr~   )r   r~   r   r8   r  rG   r   r   r   /test_reset_mock_does_not_raise_on_attr_deletion  s
    z8MockTest.test_reset_mock_does_not_raise_on_attr_deletionc                 C   s:   t  t fD ](}| |t t|_| |t |j qd S r   )r   r
   rQ  r  r   r   rS   rG   r   r   r   test_class_assignable  s
    zMockTest.test_class_assignablec                 C   s8   |  tj | ttjt | tt jt d S r   )assertIsNotNoner   rU   r:   r  r   r   r   r   r   test_name_attribute_of_call   s    z$MockTest.test_name_attribute_of_callc                 C   s8   |  tj | ttjt | tt jt d S r   )r  r   r   r:   r  r   r   r   r   r   test_parent_attribute_of_call'  s    z&MockTest.test_parent_attribute_of_callc                 C   sd   dd }t  }t||_|dd | t|jd | |jtddg | dt	|jj
 d S )Nc                 S   s   d S r   r   r   r   r   r   rS   1  r+   zBMockTest.test_parent_propagation_with_create_autospec.<locals>.foorL   rj   r  )r   r   r~   rQ   rO   r:   r   r   rW   rX   r   )r   rS   r   r   r   r   ,test_parent_propagation_with_create_autospec/  s    
z5MockTest.test_parent_propagation_with_create_autospecc                 C   sp   dd }t  }|t|ddd |dd | t|jd | |jjtddg | 	dt
|jj d S )	Nc                 S   s   d S r   r   r   r   r   r   rS   =  r+   zGMockTest.test_parent_propagation_with_autospec_attach_mock.<locals>.foorV   rT   r~   rL   rj   r  )r   r  r   r~   rQ   rO   r:   r   r   rW   rX   r   )r   rS   r   r   r   r   1test_parent_propagation_with_autospec_attach_mock;  s    z:MockTest.test_parent_propagation_with_autospec_attach_mockc           	         s   t jj}| dd | td tjd=  fdd | tjt  t  ddl	m
}m}m}m} ||||tg}|D ]}|td	}| |t q~W d    n1 s0    Y  d S )
Nc                 S   s   t tjd| S )Nr   )r  r(  r   )r   r   r   r   r   V  r+   z9MockTest.test_isinstance_under_settrace.<locals>.<lambda>zsys.moduleszunittest.mockc                    s    S r   r   )frameeventr   tracer   r   r  ]  s    z6MockTest.test_isinstance_under_settrace.<locals>.tracer   )r   r
   r   r   r_   )r(  r   r   r  r>  r  modulessettracegettraceunittest.mockr   r
   r   r   r   r$   r   )	r   Z	old_patchr   r
   r   r   r[   r   r   r   r  r   test_isinstance_under_settraceH  s    	



z'MockTest.test_isinstance_under_settracec                 C   s^   G dd d}| }t jjj|ddd W d    n1 s>0    Y  | |jjjd d S )Nc                   @   s   e Zd Zdd ZdS )zFMockTest.test_bool_not_called_when_passing_spec_arg.<locals>.Somethingc                 S   s   t j | _d S r   )r(  r   r
   obj_with_bool_funcr   r   r   r   r   q  s    zOMockTest.test_bool_not_called_when_passing_spec_arg.<locals>.Something.__init__N)r    r!   r"   r   r   r   r   r   r$   p  s   r$   r  Tr  r   )r(  r   r   rc   r:   r  __bool__r;   )r   r$   r   r   r   r   *test_bool_not_called_when_passing_spec_argo  s    4z3MockTest.test_bool_not_called_when_passing_spec_argN)r    r!   r"   r6   rH   rJ   rN   rR   r\   re   ry   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  r&  r+  r.  r/  r2  r7  r;  rA  rB  rD  rF  rG  rI  rJ  rK  rL  rM  rR  rW  r\  r`  rc  rd  re  rm  rr  ru  rx  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  r  r  r  r   r   r   r   r4   ,   s  	)&	


!

	8N
#
,((
	5	


'r4   __main__)r	  r   r  r  Ztest.supportr   r(  Zunittest.test.testmock.supportr   r   r  r   r   r   r	   r
   r   r   r   r   r   r   r   rc   r   r$   r3   r)  r4   r    mainr   r   r   r   <module>   s<   8
                ^