a
    åzeÉ  ã                   @   sH   d dl Z d dl mZ G dd„ dƒZG dd„ de jƒZedkrDe  ¡  dS )é    N)Úmockc                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚSampleObjectc                 C   s   d S ©N© ©Úselfr   r   ú9/usr/lib/python3.9/unittest/test/testmock/testsealable.pyÚmethod_sample1   ó    zSampleObject.method_sample1c                 C   s   d S r   r   r   r   r   r   Úmethod_sample2	   r
   zSampleObject.method_sample2N)Ú__name__Ú
__module__Ú__qualname__r	   r   r   r   r   r   r      s   r   c                   @   s¤   e Zd Zdd„ Zdd„ Zdd„ Zd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'S )(ÚTestSealablec                 C   sD   t  ¡ }|  |jt j¡ |  | ¡ t j¡ |  | ¡  ¡ t j¡ d S r   )r   ÚMockÚassertIsInstanceÚtestÚtest2©r   Úmr   r   r   Ú,test_attributes_return_more_mocks_by_default   s    z9TestSealable.test_attributes_return_more_mocks_by_defaultc                 C   sv   t  ¡ }t  |¡ |  t¡ |j W d   ƒ n1 s80    Y  |  t¡ |ƒ  W d   ƒ n1 sh0    Y  d S r   ©r   r   ÚsealÚassertRaisesÚAttributeErrorr   r   r   r   r   Ú.test_new_attributes_cannot_be_accessed_on_seal   s    
$z;TestSealable.test_new_attributes_cannot_be_accessed_on_sealc                 C   sF   t  ¡ }t  |¡ |  t¡ d|_W d   ƒ n1 s80    Y  d S ©Né   r   r   r   r   r   Ú)test_new_attributes_cannot_be_set_on_seal   s    
z6TestSealable.test_new_attributes_cannot_be_set_on_sealc                 C   s6   t  ¡ }d|j_t  |¡ d|j_|  |jjd¡ d S )Nr   é   )r   r   r   r   r   ÚassertEqualr   r   r   r   Ú+test_existing_attributes_can_be_set_on_seal%   s
    
z8TestSealable.test_existing_attributes_can_be_set_on_sealc                 C   sP   t  ¡ }d|j_t  |¡ |  t¡ d|j_W d   ƒ n1 sB0    Y  d S r   )r   r   r   r   r   r   r   Útest3r   r   r   r   Ú2test_new_attributes_cannot_be_set_on_child_of_seal-   s
    
z?TestSealable.test_new_attributes_cannot_be_set_on_child_of_sealc                 C   s.   t  ¡ }d|j_t  |¡ |  | ¡ d¡ d S ©Né   )r   r   r   Úreturn_valuer   r    r   r   r   r   Ú+test_existing_attributes_allowed_after_seal5   s    
z8TestSealable.test_existing_attributes_allowed_after_sealc                 C   s(   t jdd}t  |¡ |  |jd¡ d S )Nr   )Ú
test_value)r   r   r   r    r(   r   r   r   r   Ú.test_initialized_attributes_allowed_after_seal=   s    
z;TestSealable.test_initialized_attributes_allowed_after_sealc                 C   sF   t  ¡ }t  |¡ |  t¡ |ƒ  W d   ƒ n1 s80    Y  d S r   )r   r   r   r   r   r   r   r   r   Útest_call_on_sealed_mock_failsC   s    
z+TestSealable.test_call_on_sealed_mock_failsc                 C   s(   t jdd}t  |¡ |  |ƒ d¡ d S )Né   )r&   )r   r   r   r    r   r   r   r   Ú)test_call_on_defined_sealed_mock_succeedsJ   s    
z6TestSealable.test_call_on_defined_sealed_mock_succeedsc                 C   sž   t  ¡ }d|j ¡ _t  |¡ |  |j ¡ jd¡ |  t¡ |j ¡ j	 W d   ƒ n1 s^0    Y  |  t¡ |jj W d   ƒ n1 s0    Y  d S ©Né   )
r   r   Útest1r   r"   r   r    r   r   Útest4r   r   r   r   Ú&test_seals_recurse_on_added_attributesP   s    
*z3TestSealable.test_seals_recurse_on_added_attributesc                 C   sÚ   t  ¡ }d|jjd _d|jjdd… _t  |¡ |  |jjd jd¡ |  |jjdd… jd¡ |  t¡ |jjd j	 W d   ƒ n1 s0    Y  |  t¡" |jjdd… j	 W d   ƒ n1 sÌ0    Y  d S )Nr.   Úar   r+   )
r   Ú	MagicMockr/   r   r"   r   r    r   r   r0   r   r   r   r   Ú#test_seals_recurse_on_magic_methods\   s    
,z0TestSealable.test_seals_recurse_on_magic_methodsc                 C   sZ   t jdd}t jdd|j_d|jj_t  |¡ |  |jjjd¡ |jjj d|jj_d S )NZ	root_mock©ÚnameZ
not_sealedr.   r   )r   r   r/   r   r"   r   r    r0   r   r   r   r   Ú,test_seals_dont_recurse_on_manual_attributesj   s    


z9TestSealable.test_seals_dont_recurse_on_manual_attributesc                 C   sp   t  t¡}d|_d|_t  |¡ |  |jd¡ |  |jd¡ |  t¡ |j	 W d   ƒ n1 sb0    Y  d S )Nr   r%   )
r   r   r   Zattr_sample1Zattr_sample3r   r    r   r   Zattr_sample2r   r   r   r   Ú)test_integration_with_spec_att_definitionu   s    

z6TestSealable.test_integration_with_spec_att_definitionc                 C   sb   t  t¡}d|j_t  |¡ |  | ¡ d¡ |  t¡ | 	¡  W d   ƒ n1 sT0    Y  d S r   )
r   r   r   r	   r&   r   r    r   r   r   r   r   r   r   Ú,test_integration_with_spec_method_definition‚   s    

z9TestSealable.test_integration_with_spec_method_definitionc                 C   s@   t  t¡}|  t¡ d|j_W d   ƒ n1 s20    Y  d S r$   )r   r   r   r   r   Zmethod_sample3r&   r   r   r   r   Ú:test_integration_with_spec_method_definition_respects_spec   s    
zGTestSealable.test_integration_with_spec_method_definition_respects_specc                 C   sX   t  ¡ }t  |¡ |  t¡}|j W d   ƒ n1 s80    Y  |  dt|jƒ¡ d S )NÚSECRETE_name)	r   r   r   r   r   r;   ÚassertInÚstrÚ	exception©r   r   Úcmr   r   r   Ú(test_sealed_exception_has_attribute_name”   s
    
$z5TestSealable.test_sealed_exception_has_attribute_namec                 C   sp   t jdd}|jjjj t  |¡ |  t¡}|jjjjj	 W d   ƒ n1 sP0    Y  |  
dt|jƒ¡ d S )NZ	mock_namer5   z&mock_name.test1.test2.test3.test4.boom)r   r   r/   r   r"   r0   r   r   r   Zboomr<   r=   r>   r?   r   r   r   Ú"test_attribute_chain_is_maintainedœ   s    
,z/TestSealable.test_attribute_chain_is_maintainedc                 C   st   t  ¡ }| ¡ j ¡ j t  |¡ |  t¡"}| ¡ j ¡  ¡  W d   ƒ n1 sT0    Y  |  	dt
|jƒ¡ d S )Nz mock.test1().test2.test3().test4)r   r   r/   r   r"   r0   r   r   r   r<   r=   r>   r?   r   r   r   Útest_call_chain_is_maintained¥   s    
0z*TestSealable.test_call_chain_is_maintainedc              
   C   sP  G dd„ dƒ}dD ]6}| j |d tj||d}d|j_d|jj_t |¡ |  |j	tj
¡ |  |jtj¡ |  |jtj¡ |  |jtj¡ |  |jjtj
¡ |  |jjtj¡ |  | ¡ d¡ d|j_|  | ¡ d¡ |  |j ¡ d¡ d|jj_|  |j ¡ d¡ |  t¡ | 	¡  W d   ƒ n1 s60    Y  |  t¡ d	|_W d   ƒ n1 sh0    Y  |  t¡ | ¡  W d   ƒ n1 sœ0    Y  d
|j_|  | ¡ d
¡ |  t¡ |j W d   ƒ n1 sæ0    Y  |  t¡ d	|_W d   ƒ n1 s0    Y  |  t¡ | ¡  W d   ƒ n1 sL0    Y  |  t¡ |j ¡  W d   ƒ n1 s‚0    Y  |  t¡ |jj W d   ƒ n1 s¶0    Y  |  t¡ d	|j_W d   ƒ n1 sê0    Y  |  t¡ |j ¡  W d   ƒ n1 s 0    Y  W d   ƒ q1 s@0    Y  qd S )Nc                   @   s.   e Zd ZdZdd„ Zdd„ ZG dd„ dƒZdS )	z1TestSealable.test_seal_with_autospec.<locals>.Foor   c                 S   s   dS r   r   r   r   r   r   Úbar1²   s    z6TestSealable.test_seal_with_autospec.<locals>.Foo.bar1c                 S   s   dS )Nr   r   r   r   r   r   Úbar2´   s    z6TestSealable.test_seal_with_autospec.<locals>.Foo.bar2c                   @   s   e Zd ZdZdd„ ZdS )z5TestSealable.test_seal_with_autospec.<locals>.Foo.Bazr%   c                 S   s   dS r-   r   r   r   r   r   Úban¹   s    z9TestSealable.test_seal_with_autospec.<locals>.Foo.Baz.banN)r   r   r   ÚbazrF   r   r   r   r   ÚBaz·   s   rH   N)r   r   r   ÚfoorD   rE   rH   r   r   r   r   ÚFoo°   s   rJ   )TF)Úspec_setr2   ÚbZnew_aZnew_br   rE   )ZsubTestr   Zcreate_autospecrD   r&   rH   rF   r   r   rI   ZNonCallableMagicMockr3   rE   rG   r    r   Ú	TypeErrorr   ZbarZmissing_attrZmissing_method)r   rJ   rK   rI   r   r   r   Útest_seal_with_autospec®   sR    



(&(&&(*((z$TestSealable.test_seal_with_autospecN)r   r   r   r   r   r   r!   r#   r'   r)   r*   r,   r1   r4   r7   r8   r9   r:   rA   rB   rC   rN   r   r   r   r   r      s&   			r   Ú__main__)Zunittestr   r   ZTestCaser   r   Úmainr   r   r   r   Ú<module>   s    a