B
    18™\ç  ã               @   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d„ ZdS )ÚSampleObjectc             C   s   d| _ d| _d S )Né   )Úattr_sample1Úattr_sample2)Úself© r   úg/oak/stanford/groups/akundaje/marinovg/programs/Python-3.7.3/Lib/unittest/test/testmock/testsealable.pyÚ__init__   s    zSampleObject.__init__c             C   s   d S )Nr   )r   r   r   r	   Úmethod_sample1
   s    zSampleObject.method_sample1c             C   s   d S )Nr   )r   r   r   r	   Úmethod_sample2   s    zSampleObject.method_sample2N)Ú__name__Ú
__module__Ú__qualname__r
   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%S )&ÚTestSealablec             C   sD   t  ¡ }|  |jt j¡ |  | ¡ t j¡ |  | ¡  ¡ t j¡ d S )N)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   sN   t  ¡ }t  |¡ |  t¡ |j W d Q R X |  t¡ |ƒ  W d Q R X d S )N)r   r   ÚsealÚassertRaisesÚAttributeErrorr   )r   r   r   r   r	   Ú.test_new_attributes_cannot_be_accessed_on_seal   s    
z;TestSealable.test_new_attributes_cannot_be_accessed_on_sealc          	   C   s2   t  ¡ }t  |¡ |  t¡ d|_W d Q R X d S )Nr   )r   r   r   r   r   r   )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   ÚassertEqual)r   r   r   r   r	   Ú+test_existing_attributes_can_be_set_on_seal*   s
    
z8TestSealable.test_existing_attributes_can_be_set_on_sealc          	   C   s<   t  ¡ }d|j_t  |¡ |  t¡ d|j_W d Q R X d S )Nr   )r   r   r   r   r   r   r   Útest3)r   r   r   r   r	   Ú2test_new_attributes_cannot_be_set_on_child_of_seal2   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   r	   Ú+test_existing_attributes_allowed_after_seal:   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   r	   Ú.test_initialized_attributes_allowed_after_sealB   s    
z;TestSealable.test_initialized_attributes_allowed_after_sealc          	   C   s2   t  ¡ }t  |¡ |  t¡ |ƒ  W d Q R X d S )N)r   r   r   r   r   )r   r   r   r   r	   Útest_call_on_sealed_mock_failsH   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   r	   Ú)test_call_on_defined_sealed_mock_succeedsO   s    
z6TestSealable.test_call_on_defined_sealed_mock_succeedsc          	   C   sv   t  ¡ }d|j ¡ _t  |¡ |  |j ¡ jd¡ |  t¡ |j ¡ j	 W d Q R X |  t¡ |jj W d Q R X d S )Né   )
r   r   Útest1r   r   r   r   r   r   Útest4)r   r   r   r   r	   Ú&test_seals_recurse_on_added_attributesU   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 Q R X |  t¡ |jjdd… j	 W d Q R X d S )Nr)   Úar   r'   )
r   Ú	MagicMockr*   r   r   r   r   r   r   r+   )r   r   r   r   r	   Ú#test_seals_recurse_on_magic_methodsa   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   r+   )r   r   r   r   r	   Ú,test_seals_dont_recurse_on_manual_attributeso   s    


z9TestSealable.test_seals_dont_recurse_on_manual_attributesc          	   C   s\   t  t¡}d|_d|_t  |¡ |  |jd¡ |  |jd¡ |  t¡ |j	 W dQ R X dS )z0You are not restricted when using mock with specr   r!   N)
r   r   r   r   Zattr_sample3r   r   r   r   r   )r   r   r   r   r	   Ú)test_integration_with_spec_att_definitionz   s    

z6TestSealable.test_integration_with_spec_att_definitionc          	   C   sN   t  t¡}d|j_t  |¡ |  | ¡ d¡ |  t¡ | 	¡  W dQ R X dS )z;You need to defin the methods, even if they are in the specr   N)
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Q R X dS )z)You cannot define methods out of the specr!   N)r   r   r   r   r   Zmethod_sample3r"   )r   r   r   r   r	   Ú:test_integration_with_spec_method_definition_respects_spec’   s    
zGTestSealable.test_integration_with_spec_method_definition_respects_specc          	   C   sD   t  ¡ }t  |¡ |  t¡}|j W d Q R X |  dt|jƒ¡ d S )NÚSECRETE_name)	r   r   r   r   r   r5   ÚassertInÚstrÚ	exception)r   r   Úcmr   r   r	   Ú(test_sealed_exception_has_attribute_name™   s
    
z5TestSealable.test_sealed_exception_has_attribute_namec          	   C   s\   t jdd}|jjjj t  |¡ |  t¡}|jjjjj	 W d Q R X |  
dt|jƒ¡ d S )NÚ	mock_name)r0   z&mock_name.test1.test2.test3.test4.boom)r   r   r*   r   r   r+   r   r   r   Zboomr6   r7   r8   )r   r   r9   r   r   r	   Ú"test_attribute_chain_is_maintained¡   s    
z/TestSealable.test_attribute_chain_is_maintainedc          	   C   s`   t  ¡ }| ¡ j ¡ j t  |¡ |  t¡}| ¡ j ¡  ¡  W d Q R X |  	dt
|jƒ¡ d S )Nz mock.test1().test2.test3().test4)r   r   r*   r   r   r+   r   r   r   r6   r7   r8   )r   r   r9   r   r   r	   Útest_call_chain_is_maintainedª   s    
z*TestSealable.test_call_chain_is_maintainedN)r   r   r   r   r   r   r   r    r#   r%   r&   r(   r,   r/   r1   r2   r3   r4   r:   r<   r=   r   r   r   r	   r      s$   		r   Ú__main__)Úunittestr   r   ÚTestCaser   r   Úmainr   r   r   r	   Ú<module>   s    $