o
    KXîhÉ  ã                   @   sL   d dl Z d dl mZ G dd„ dƒZG dd„ de jƒZedkr$e  ¡  dS dS )é    N)Úmockc                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚSampleObjectc                 C   ó   d S ©N© ©Úselfr   r   úM/opt/python-3.10.19/usr/lib/python3.10/unittest/test/testmock/testsealable.pyÚmethod_sample1   ó    zSampleObject.method_sample1c                 C   r   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   sx   t  ¡ }t  |¡ |  t¡ |j W d   ƒ n1 sw   Y  |  t¡ |ƒ  W d   ƒ d S 1 s5w   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   sH   t  ¡ }t  |¡ |  t¡ d|_W d   ƒ d S 1 sw   Y  d S ©Né   r   r   r   r   r	   Ú)test_new_attributes_cannot_be_set_on_seal   ó
   
"ÿ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   sR   t  ¡ }d|j_t  |¡ |  t¡ d|j_W d   ƒ d S 1 s"w   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=   ó   
z;TestSealable.test_initialized_attributes_allowed_after_sealc                 C   sH   t  ¡ }t  |¡ |  t¡ |ƒ  W d   ƒ d S 1 sw   Y  d S r   )r   r   r   r   r   r   r   r   r	   Útest_call_on_sealed_mock_failsC   r    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   r+   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/w   Y  |  t¡ |jj W d   ƒ d S 1 sIw   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 sHw   Y  |  t¡ |jjdd… j	 W d   ƒ d S 1 sgw   Y  d S )Nr0   Úar!   r-   )
r   Ú	MagicMockr1   r   r$   r   r"   r   r   r2   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_sealedr0   r   )r   r   r1   r   r$   r   r"   r2   r   r   r   r	   Ú,test_seals_dont_recurse_on_manual_attributesj   s   


z9TestSealable.test_seals_dont_recurse_on_manual_attributesc                 C   sr   t  t¡}d|_d|_t  |¡ |  |jd¡ |  |jd¡ |  t¡ |j	 W d  ƒ dS 1 s2w   Y  dS )z0You are not restricted when using mock with specr   r&   N)
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   sd   t  t¡}d|j_t  |¡ |  | ¡ d¡ |  t¡ | 	¡  W d  ƒ dS 1 s+w   Y  dS )z<You need to define the methods, even if they are in the specr   N)
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   sB   t  t¡}|  t¡ d|j_W d  ƒ dS 1 sw   Y  dS )z)You cannot define methods out of the specr&   N)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 sw   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 s(w   Y  |  
dt|jƒ¡ d S )NZ	mock_namer7   z&mock_name.test1.test2.test3.test4.boom)r   r   r1   r   r$   r2   r   r   r   Zboomr>   r?   r@   rA   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 s*w   Y  |  	dt
|jƒ¡ d S )Nz mock.test1().test2.test3().test4)r   r   r1   r   r$   r2   r   r   r   r>   r?   r@   rA   r   r   r	   Útest_call_chain_is_maintained¥   s   
ÿz*TestSealable.test_call_chain_is_maintainedc              
   C   sH  G dd„ dƒ}dD ]—}| 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 sšw   Y  |  t¡ d	|_W d   ƒ n1 s²w   Y  |  t¡ | ¡  W d   ƒ n1 sËw   Y  d
|j_|  | ¡ d
¡ |  t¡ |j W d   ƒ n1 sïw   Y  |  t¡ d	|_W d   ƒ n	1 sw   Y  |  t¡ | ¡  W d   ƒ n	1 s"w   Y  |  t¡ |j ¡  W d   ƒ n	1 s=w   Y  |  t¡ |jj W d   ƒ n	1 sWw   Y  |  t¡ d	|j_W d   ƒ n	1 sqw   Y  |  t¡ |j ¡  W d   ƒ n	1 sŒw   Y  W d   ƒ n	1 sœw   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   ó   dS r   r   r   r   r   r	   Úbar1²   ó   z6TestSealable.test_seal_with_autospec.<locals>.Foo.bar1c                 S   rF   )Nr!   r   r   r   r   r	   Úbar2´   rH   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   rF   r/   r   r   r   r   r	   Úban¹   rH   z9TestSealable.test_seal_with_autospec.<locals>.Foo.Baz.banN)r   r   r   ÚbazrJ   r   r   r   r	   ÚBaz·   s    rL   N)r   r   r   ÚfoorG   rI   rL   r   r   r   r	   ÚFoo°   s
    rN   )TF)Úspec_setr4   ÚbZnew_aZnew_br   rI   )ZsubTestr   Zcreate_autospecrG   r'   rL   rJ   r   r   rM   ZNonCallableMagicMockr5   rI   rK   r"   r   Ú	TypeErrorr   ZbarZmissing_attrZmissing_method)r   rN   rO   rM   r   r   r	   Útest_seal_with_autospec®   sn   




ÿÿ
ÿÿÿ
ÿÿ
ÿ
ÿÿ€Õ€ÿz$TestSealable.test_seal_with_autospecN)r   r   r   r   r   r   r#   r%   r(   r*   r,   r.   r3   r6   r9   r:   r;   r<   rC   rD   rE   rR   r   r   r   r	   r      s(    			r   Ú__main__)Zunittestr   r   ZTestCaser   r   Úmainr   r   r   r	   Ú<module>   s     aÿ