a
    ¦ˆcò>  ã                   @   sÀ  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mZmZm	Z	m
Z
mZmZmZmZmZmZ d dlmZ d dlmZmZmZ G dd„ de	ƒZG dd	„ d	eƒZG d
d„ dejƒZG dd„ de	ƒZG dd„ de	ƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZ G dd„ deƒZ!G dd„ deƒZ"G dd„ deƒZ#G dd„ deƒZ$G d d!„ d!eƒZ%G d"d#„ d#eƒZ&G d$d%„ d%eƒZ'G d&d'„ d'eƒZ(G d(d)„ d)e	ƒZ)G d*d+„ d+ejƒZ*d,d-„ Z+dS ).é    N)Úmock)ÚAnyÚBoolÚcached_propertyÚ	HasTraitsÚInstanceÚIntÚListÚobserveÚon_trait_changeÚPropertyÚStr)Ú	Undefined)ÚtraitÚpop_exception_handlerÚpush_exception_handlerc                   @   s,   e Zd Zeƒ Zdd„ Zdd„ ZeeeƒZdS )ÚHasPropertyc                 C   s   | j  dd¡S )NÚ_valuer   )Ú__dict__Úget©Úself© r   úk/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traits/tests/test_property_notifications.pyZ__value_get*   s    zHasProperty.__value_getc                 C   s.   | j  dd¡}||kr*|| _|  d||¡ d S )Nr   r   Úvalue)r   r   r   Ztrait_property_changed)r   r   Ú	old_valuer   r   r   Z__value_set-   s    zHasProperty.__value_setN)	Ú__name__Ú
__module__Ú__qualname__r   Úoutput_bufferZ_HasProperty__value_getZ_HasProperty__value_setr   r   r   r   r   r   r   &   s   r   c                   @   s   e Zd Zdd„ ZdS )ÚHasPropertySubclassc                 C   s   | j  |¡ d S ©N)r   Úwrite)r   r   r   r   r   Ú_value_changed7   s    z"HasPropertySubclass._value_changedN)r   r   r   r#   r   r   r   r   r    6   s   r    c                   @   s   e Zd Zdd„ ZdS )ÚTestPropertyNotificationsc                 C   sB   t  ¡ }t|d�}d|_|  | ¡ d¡ d|_|  | ¡ d¡ d S )N)r   Zvalue_1Zvalue_2Zvalue_1value_2)ÚioÚStringIOr    r   ÚassertEqualÚgetvalue)r   r   Ztest_objr   r   r   Útest_property_notifications<   s    
z5TestPropertyNotifications.test_property_notificationsN)r   r   r   r)   r   r   r   r   r$   ;   s   r$   c                   @   s   e Zd Zeƒ Zeƒ ZdS )Ú
PersonInfoN)r   r   r   r   Úager   Úgenderr   r   r   r   r*   J   s   r*   c                   @   sP   e Zd ZeeƒZeeeƒdd�ZeeƒZe	ƒ Z
dd„ ZeeƒZe	ƒ Zdd„ ZdS )ÚClassWithInstanceDefaultInité   )Zcomparison_modec                 C   s   d| _ t| jjd�S )NT©r+   )Úsample_info_default_computedr*   Úinfo_without_defaultr+   r   r   r   r   Ú_sample_info_default[   s    z1ClassWithInstanceDefaultInit._sample_info_defaultc                 C   s   d| _ tdd�S )NTé   r/   )Úinfo_with_default_computedr*   r   r   r   r   Ú_info_with_default_defaultc   s    z7ClassWithInstanceDefaultInit._info_with_default_defaultN)r   r   r   r   r*   r1   r	   Úlist_of_infosÚsample_infor   r0   r2   Úinfo_with_defaultr4   r5   r   r   r   r   r-   Q   s   r-   c                   @   s(   e Zd ZdZedd�Zeƒ Zdd„ ZdS )Ú ClassWithPropertyObservesDefaultz» Dummy class for testing property with an observer on an extended
    attribute. 'info_with_default has a default initializer.
    To be compared with the next class using depends_on
    úinfo_with_default.age©r
   c                 C   s   |  j d7  _ | jjS ©Nr.   ©Úextended_age_n_calculationsr8   r+   r   r   r   r   Ú_get_extended_ager   s    z2ClassWithPropertyObservesDefault._get_extended_ageN©	r   r   r   Ú__doc__r   Úextended_ager   r>   r?   r   r   r   r   r9   h   s   
r9   c                   @   s(   e Zd ZdZedd�Zeƒ Zdd„ ZdS )Ú!ClassWithPropertyDependsOnDefaultz„ Dummy class for testing property with an observer on an extended
    attribute. 'info_with_default' has a default initializer.
    r:   ©Z
depends_onc                 C   s   |  j d7  _ | jjS r<   r=   r   r   r   r   r?   €   s    z3ClassWithPropertyDependsOnDefault._get_extended_ageNr@   r   r   r   r   rC   w   s   
rC   c                   @   s(   e Zd ZdZedd�Zeƒ Zdd„ ZdS )ÚClassWithPropertyObservesInita   Dummy class for testing property with an observer on an extended
    attribute. sample_info has a default value depending on
    'info_without_default'. The value of 'info_without_default' is provided
    by __init__.
    To be compared with the next class using depends_on.
    úsample_info.ager;   c                 C   s   |  j d7  _ | jjS r<   )r>   r7   r+   r   r   r   r   r?   ‘   s    z/ClassWithPropertyObservesInit._get_extended_ageNr@   r   r   r   r   rE   …   s   
rE   c                   @   s   e Zd Zedd�Zdd„ ZdS )ÚClassWithPropertyDependsOnInitrF   rD   c                 C   s   | j jS r!   )r7   r+   r   r   r   r   r?   œ   s    z0ClassWithPropertyDependsOnInit._get_extended_ageN)r   r   r   r   rB   r?   r   r   r   r   rG   –   s   
rG   c                   @   s4   e Zd ZdZeedƒedƒgd�Zeƒ Zdd„ Z	dS )Ú!ClassWithPropertyMultipleObservesz4 Dummy class to test observing multiple values.
    r+   r,   r;   c                 C   s   |  j d7  _ t| jƒ| j S r<   ©Úcomputed_value_n_calculationsÚlenr,   r+   r   r   r   r   Ú_get_computed_value¨   s    z5ClassWithPropertyMultipleObserves._get_computed_valueN)
r   r   r   rA   r   r   Úcomputed_valuer   rJ   rL   r   r   r   r   rH       s   rH   c                   @   s,   e Zd ZdZeddgd�Zeƒ Zdd„ ZdS )Ú"ClassWithPropertyMultipleDependsOnzH Dummy class using 'depends_on', to be compared with the one above.
    r+   r,   rD   c                 C   s   |  j d7  _ t| jƒ| j S r<   rI   r   r   r   r   rL   µ   s    z6ClassWithPropertyMultipleDependsOn._get_computed_valueN)	r   r   r   rA   r   rM   r   rJ   rL   r   r   r   r   rN   ­   s   rN   c                   @   s:   e Zd ZdZeeƒ edƒ ¡  d¡d�Ze	ƒ Z
dd„ ZdS )ÚClassWithPropertyObservesItemsz7 Dummy class to test property observing container itemsr6   r+   r;   c                 C   s"   |  j d7  _ tdd„ | jD ƒƒS )Nr.   c                 s   s   | ]}|j d kV  qdS ©éF   Nr/   ©Ú.0Úinfor   r   r   Ú	<genexpr>Ä   ó    zAClassWithPropertyObservesItems._get_discounted.<locals>.<genexpr>©Údiscounted_n_calculationsÚanyr6   r   r   r   r   Ú_get_discountedÂ   s    z.ClassWithPropertyObservesItems._get_discountedN)r   r   r   rA   r   r   r   Z
list_itemsÚ
discountedr   rX   rZ   r   r   r   r   rO   º   s   ÿrO   c                   @   s,   e Zd ZdZeeƒ dd�Zeƒ Zdd„ Z	dS )ÚClassWithPropertyDependsOnItemszL Dummy class using depends_on to be compared with the one using
    observe.zlist_of_infos.agerD   c                 C   s"   |  j d7  _ tdd„ | jD ƒƒS )Nr.   c                 s   s   | ]}|j d kV  qdS rP   r/   rR   r   r   r   rU   Ñ   rV   zBClassWithPropertyDependsOnItems._get_discounted.<locals>.<genexpr>rW   r   r   r   r   rZ   Ï   s    z/ClassWithPropertyDependsOnItems._get_discountedN)
r   r   r   rA   r   r   r[   r   rX   rZ   r   r   r   r   r\   Ç   s   r\   c                   @   s,   e Zd Zeeƒ dd�Zeƒ Zedd„ ƒZ	dS )Ú"ClassWithPropertyObservesWithCacher+   r;   c                 C   s   |  j d7  _ | jdkS )Nr.   é
   ©rX   r+   r   r   r   r   rZ   Ú   s    z2ClassWithPropertyObservesWithCache._get_discountedN)
r   r   r   r   r   r[   r   rX   r   rZ   r   r   r   r   r]   Ô   s   r]   c                   @   sB   e Zd ZdZeeƒ dd�Zeƒ Zdd„ Z	e
ƒ Zedƒdd„ ƒZd	S )
Ú"ClassWithPropertyObservesDecoratedz@ Dummy class to test property with observers setup at init time.r+   r;   c                 C   s   |  j d7  _ | jdkS ©Nr.   é<   r_   r   r   r   r   rZ   ç   s    z2ClassWithPropertyObservesDecorated._get_discountedr[   c                 C   s   | j  |¡ d S r!   ©Údiscounted_eventsÚappend©r   Úeventr   r   r   Údiscounted_updatedí   s    z5ClassWithPropertyObservesDecorated.discounted_updatedN)r   r   r   rA   r   r   r[   r   rX   rZ   r	   rd   r
   rh   r   r   r   r   r`   à   s   r`   c                   @   sB   e Zd ZdZeeƒ dd�Zeƒ Zdd„ Z	e
ƒ Zedƒdd„ ƒZd	S )
Ú#ClassWithPropertyDependsOnDecoratedzY Dummy class to test property with depends_on and on_trait_change
    setup at init time.r+   rD   c                 C   s   |  j d7  _ | jdkS ra   r_   r   r   r   r   rZ   ú   s    z3ClassWithPropertyDependsOnDecorated._get_discountedr[   c                 C   s   | j  |¡ d S r!   rc   rf   r   r   r   rh      s    z6ClassWithPropertyDependsOnDecorated.discounted_updatedN)r   r   r   rA   r   r   r[   r   rX   rZ   r	   rd   r   rh   r   r   r   r   ri   ò   s   ri   c                   @   s   e Zd ZdZeeƒ dd�ZdS )ÚClassWithPropertyMissingGetterzD Dummy class to test property that does not have getter/setter.
    r+   r;   N)r   r   r   rA   r   r   r[   r   r   r   r   rj     s   rj   c                   @   s"   e Zd ZdZeeƒZedd�ZdS )ÚClassWithPropertyTraitNotFoundzI Dummy class to test error, prevent issues like enthought/traits#447
    zperson.last_namer;   N)	r   r   r   rA   r   r*   Úpersonr   Ú	last_namer   r   r   r   rk     s   rk   c                   @   sx   e Zd 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S )ÚTestHasTraitsPropertyObserveszc Tests Property notifications using 'observe', and compared the
    behavior with 'depends_on'
    c                 C   s   t dd� |  t¡ d S )NT)Zreraise_exceptions)r   Z
addCleanupr   r   r   r   r   ÚsetUp  s    
z#TestHasTraitsPropertyObserves.setUpc           	   	   C   s²   t ƒ }t ¡ }| |d¡ tƒ }t ¡ }| t|ƒd¡ ||g}||g}t||ƒD ]Z\}}| j||d��4 d|j	_
|  |jd¡ |  |jd¡ W d   ƒ qR1 s¢0    Y  qRd S )NrB   )ÚinstanceÚhandlerrQ   r.   )r9   r   ÚMockr
   rC   r   Úget_otc_handlerÚzipÚsubTestr8   r+   r'   Ú
call_countr>   )	r   Úinstance_observeÚhandler_observeÚinstance_depends_onZhandler_otcZ	instancesÚhandlersrp   rq   r   r   r   Ú$test_property_observe_extended_trait  s    ÿzBTestHasTraitsPropertyObserves.test_property_observe_extended_traitc                 C   sP   t ƒ }t ¡ }| |d¡ tƒ }| tt ¡ ƒd¡ |  |j¡ |  	|j¡ d S )NrB   )
r9   r   rr   r
   rC   r   rs   ÚassertFalser4   Ú
assertTrue)r   rw   rx   ry   r   r   r   Ú+test_property_observe_does_not_fire_default4  s    ÿzITestHasTraitsPropertyObserves.test_property_observe_does_not_fire_defaultc              	   C   s¶   t ƒ }t ¡ }| |d¡ |  |jd¡ tƒ }| tt ¡ ƒd¡ |  |jd¡ ||fD ]X}| j	|d��8 d|_
|  |jd¡ d|_|  |jd¡ W d   ƒ qX1 s¦0    Y  qXd S )NrM   r   ©rp   r.   Úmaleé   )rH   r   rr   r
   r'   rJ   rN   r   rs   ru   r+   r,   ©r   rw   rx   ry   rp   r   r   r   Útest_property_multi_observeC  s    ÿz9TestHasTraitsPropertyObserves.test_property_multi_observec              	   C   sº   t ƒ }t ¡ }| |d¡ tƒ }| tt ¡ ƒd¡ ||fD ]x}| j|d��X |  |j	d¡ |j
 tdd�¡ |  |j	d¡ d|j
d _|  |j	d	¡ W d   ƒ q<1 sª0    Y  q<d S )
Nr[   r   r   é   r/   r.   éP   éÿÿÿÿr�   )rO   r   rr   r
   r\   r   rs   ru   r'   rX   r6   re   r*   r+   r‚   r   r   r   Útest_property_observe_containerY  s    ÿz=TestHasTraitsPropertyObserves.test_property_observe_containerc                 C   s¾   t ƒ }t ¡ }| |d¡ d|_|j\\\}}|  |j|¡ |  |j	d¡ |  |j
t¡ |  |jd¡ | ¡  d|_|j\\\}}|  |j|¡ |  |j	d¡ |  |j
t¡ |  |jd¡ d S )NrM   r.   r€   é   )rH   r   rr   r
   r+   Úcall_args_listÚassertIsÚobjectr'   ÚnameÚoldr   ÚnewÚ
reset_mockr,   ©r   rp   rq   rg   Ú_r   r   r   Ú test_property_old_value_uncachedl  s     z>TestHasTraitsPropertyObserves.test_property_old_value_uncachedc                 C   sÂ   t ƒ }t ¡ }| |d¡ d|_|jd \\}}|  |j|¡ |  |j	d¡ |  |j
t¡ |  |jd¡ | ¡  d|_|jd \\}}|  |j|¡ |  |j	d¡ |  |j
d¡ |  |jd¡ d S )Nr[   r.   r†   Fr…   T)r]   r   rr   r
   r+   r‰   rŠ   r‹   r'   rŒ   r�   r   rŽ   r�   r�   r   r   r   Útest_property_with_cache‚  s     z6TestHasTraitsPropertyObserves.test_property_with_cachec                 C   sÜ   |   t¡�  ttdd�d� W d   ƒ n1 s00    Y  ttdd�d�}t ¡ }| |d¡ |  |j	¡ |  
|jjd¡ |  
|jd¡ |  
|jd¡ d|j_|  
|jd¡ ttdd�d�}|  |j	¡ |  
|jjd¡ d S )Nr„   r/   )r1   rB   r   é(   r.   )ÚassertRaisesÚAttributeErrorrG   r*   rE   r   rr   r
   r|   r0   r'   r7   r+   rB   rv   r-   )r   rp   rq   Zinstance_no_propertyr   r   r   Ú4test_property_default_initializer_with_value_in_init—  s(    ÿ$ÿÿzRTestHasTraitsPropertyObserves.test_property_default_initializer_with_value_in_initc              	   C   sd   t dd�}tdd�}||fD ]B}| j|d��" |  t|jƒd¡ W d   ƒ q1 sT0    Y  qd S )Nr„   r/   r   r.   )r`   ri   ru   r'   rK   rd   )r   rw   ry   rp   r   r   r   Ú test_property_decorated_observer·  s
    

z>TestHasTraitsPropertyObserves.test_property_decorated_observerc                 C   s"   t ƒ }t |¡}~|  |ƒ ¡ d S r!   )r9   ÚweakrefÚrefZassertIsNone)r   rp   Zinstance_refr   r   r   Útest_garbage_collectable¿  s    
z6TestHasTraitsPropertyObserves.test_garbage_collectablec                 C   s:   t ƒ }z| jd7  _W n ty4   |  d¡ Y n0 d S )Nr.   zgHaving property with undefined getter/setter should not prevent the observed traits from being changed.)rj   r+   Ú	ExceptionZfail)r   rp   r   r   r   Útest_property_with_no_getterÈ  s    ÿz:TestHasTraitsPropertyObserves.test_property_with_no_getterc                 C   sN   t ƒ }|  t¡�}tƒ |_W d   ƒ n1 s.0    Y  |  dt|jƒ¡ d S )Nz!Trait named 'last_name' not found)rk   r•   Ú
ValueErrorr*   rl   ZassertInÚstrÚ	exception)r   rp   Zexception_contextr   r   r   Ú$test_property_with_missing_dependentÒ  s    &þzBTestHasTraitsPropertyObserves.test_property_with_missing_dependentc                 C   s^   t ƒ }ttjd ƒD ]D}tj||d�}t |¡}t ¡ }| |d¡ d|_	|  
|jd¡ qd S )Nr.   )ÚprotocolrM   )rH   ÚrangeÚpickleÚHIGHEST_PROTOCOLÚdumpsÚloadsr   rr   r
   r+   r'   rv   )r   rp   r¢   Z
serializedZdeserializedrq   r   r   r   Ú,test_pickle_has_traits_with_property_observeÝ  s    
zJTestHasTraitsPropertyObserves.test_pickle_has_traits_with_property_observeN)r   r   r   rA   ro   r{   r~   rƒ   r‡   r’   r“   r—   r˜   r›   r�   r¡   r¨   r   r   r   r   rn     s    	
rn   c                    s   ‡ fdd„}|S )z¿ Return a callable to be used with on_trait_change, which will
    inspect call signature.

    Parameters
    ----------
    mock_obj : mock.Mock
        Mock object for tracking calls.
    c                      s
   ˆ ƒ  d S r!   r   r   ©Úmock_objr   r   rq   ó  s    z get_otc_handler.<locals>.handlerr   )rª   rq   r   r©   r   rs   é  s    
rs   ),r%   r¤   Zunittestr   r™   Z
traits.apir   r   r   r   r   r   r	   r
   r   r   r   Ztraits.trait_baser   Ztraits.observation.apir   r   r   r   r    ZTestCaser$   r*   r-   r9   rC   rE   rG   rH   rN   rO   r\   r]   r`   ri   rj   rk   rn   rs   r   r   r   r   Ú<module>   s8   4
	 U