3
dI                 @   s   d dl 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 dd ZG dd deZG dd deZG d	d
 d
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jZdS )    N)	BaseCallableCallableEither	HasTraitsIntStr
TraitErrorUnionValidateTraitc               C   s   d S )N r   r   r   </tmp/pip-build-7vycvbft/traits/traits/tests/test_callable.pyfunction   s    r   c               @   s   e Zd Zdd ZdS )Dummyc             C   s   d S )Nr   )selfr   r   r   instance_method!   s    zDummy.instance_methodN)__name__
__module____qualname__r   r   r   r   r   r      s   r   c                   s"   e Zd ZdZd fdd	Z  ZS )OldCallablez
    Old-style Callable, whose validation tuple doesn't include
    the allow_none field.

    We only care about this case because it's possible that old pickles
    could include Callable instances whose validation tuple has length 1.
    Nc                s   t jf| _t j|f| d S )N)r
   callableZfast_validatesuper__init__)r   valuemetadata)	__class__r   r   r   -   s    
zOldCallable.__init__)N)r   r   r   __doc__r   __classcell__r   r   )r   r   r   %   s   r   c               @   s   e Zd ZdZdd ZdS )UnboolzK
    Object that can't be interpreted as a bool, for testing purposes.
    c             C   s
   t  d S )N)ZeroDivisionError)r   r   r   r   __bool__6   s    zUnbool.__bool__N)r   r   r   r   r   r   r   r   r   r   2   s   r   c               @   sH   e Zd Ze ZeeeZeeeZ	eee
 deZeeddeZdS )
MyCallable)
allow_noneFN)r   r   r   r   r   r   r   callable_or_strr   old_callable_or_strr   bad_allow_nonenon_none_callable_or_strr   r   r   r   r    :   s
   

r    c               @   s   e Zd ZeZdS )MyBaseCallableN)r   r   r   r   r   r   r   r   r   r&   F   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S )TestCallablec             C   s   t  }| j|j d S )N)r    assertIsNoner   )r   ar   r   r   test_defaultL   s    zTestCallable.test_defaultc             C   s$   dd }t |d}| j|j| d S )Nc             S   s   | d S )N   r   )vr   r   r   <lambda>Q   s    z2TestCallable.test_accepts_lambda.<locals>.<lambda>)r   )r    ZassertIsr   )r   funcr)   r   r   r   test_accepts_lambdaP   s    
z TestCallable.test_accepts_lambdac             C   s   t td d S )N)r   )r    float)r   r   r   r   test_accepts_typeU   s    zTestCallable.test_accepts_typec             C   s   t  }t|jd d S )N)r   )r   r    r   )r   dummyr   r   r   test_accepts_methodX   s    z TestCallable.test_accepts_methodc             C   s   t td d S )N)r   )r    r   )r   r   r   r   test_accepts_function\   s    z"TestCallable.test_accepts_functionc             C   s8   t  }| jt}d|_W d Q R X | jdt|j d S )Nr+   zmust be a callable value)r    assertRaisesr   r   ZassertInstr	exception)r   r)   Zexception_contextr   r   r   test_rejects_int_   s
    zTestCallable.test_rejects_intc             C   s   t  }| j|j tdd tg}x |D ]}||_| j|j| q$W dddg}x<|D ]4}|j}| jt ||_W d Q R X | j|j| qPW d S )Npowg      ?y              @         )r:   r;   r<   )r    r(   r"   r9   intassertEqualr5   r   )r   r)   Zacceptable_valuesr   Zunacceptable_values	old_valuer   r   r   test_callable_in_complex_traith   s    


z+TestCallable.test_callable_in_complex_traitc             C   sH   dd }t  }d}|}xtdD ]}||_||_q W | j|j d d S )Nc               S   s   dS )NH   r   r   r   r   r   my_function|   s    zATestCallable.test_compound_callable_refcount.<locals>.my_functionzsome stringr:   rA   )r    ranger"   r>   )r   rB   r)   Zstring_valueZcallable_value_r   r   r   test_compound_callable_refcounty   s    
z,TestCallable.test_compound_callable_refcountc             C   sV   G dd dt }| }| j|j | jt d |_W d Q R X | jd|jdd d S )Nc               @   s   e Zd ZeeddZdS )z6TestCallable.test_disallow_none.<locals>.MyNewCallableF)default_valuer!   N)r   r   r   r   r9   r   r   r   r   r   MyNewCallable   s   rG            )r   assertIsNotNoner   r5   r   r>   )r   rG   objr   r   r   test_disallow_none   s    zTestCallable.test_disallow_nonec          	   C   s   G dd dt }| }| j|j | j|j d |_d |_| j|j | j|j d|_d|_| jt d |_W d Q R X d |_d S )Nc               @   s>   e Zd ZeeddZe ZeeddeZ	eeddeZ
dS )z@TestCallable.test_disallow_none_compound.<locals>.MyNewCallable2T)r!   FN)r   r   r   r   r9   r   empty_callabler	   r   a_non_none_uniona_allow_none_unionr   r   r   r   MyNewCallable2   s   rQ   r:   )	r   rK   r   r(   rN   rO   rP   r5   r   )r   rQ   rL   r   r   r   test_disallow_none_compound   s    z(TestCallable.test_disallow_none_compoundc             C   s"   t  }d|_d |_| j|j d S )NZbob)r    r#   r(   )r   rL   r   r   r   (test_implicitly_allowed_none_in_compound   s    z5TestCallable.test_implicitly_allowed_none_in_compoundc          	   C   s,   t  }d|_| jt d |_W d Q R X d S )Nza string)r    r$   r5   r   )r   rL   r   r   r   test_non_bool_allow_none   s    z%TestCallable.test_non_bool_allow_nonec          	   C   s&   t  }| jt d |_W d Q R X d S )N)r    r5   r   r%   )r   rL   r   r   r   !test_none_not_allowed_in_compound   s    z.TestCallable.test_none_not_allowed_in_compoundc             C   s,   G dd dt }| }d |_| j|j d S )Nc               @   s   e Zd Ze ZdS )z8TestCallable.test_old_style_callable.<locals>.MyCallableN)r   r   r   r   r   r   r   r   r   r       s   r    )r   r   r(   )r   r    rL   r   r   r   test_old_style_callable   s    z$TestCallable.test_old_style_callableN)r   r   r   r*   r/   r1   r3   r4   r8   r@   rE   rM   rR   rS   rT   rU   rV   r   r   r   r   r'   J   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S )TestBaseCallablec                sz   G dd dt  G  fdddt}|dd d | jt |dd d W d	Q R X | jt |d
d W d	Q R X d	S )zG Verify `BaseCallable` can be subclassed to create new traits.
        c               @   s   e Zd Zdd ZdS )zATestBaseCallable.test_override_validate.<locals>.ZeroArgsCallablec             S   s6   t |r$tj|}t|jdkr$|S | j||| d S )Nr   )r   inspect	signaturelen
parameterserror)r   objectnamer   sigr   r   r   validate   s
    
zJTestBaseCallable.test_override_validate.<locals>.ZeroArgsCallable.validateN)r   r   r   r`   r   r   r   r   ZeroArgsCallable   s   ra   c                   s   e Zd Z ZdS )z4TestBaseCallable.test_override_validate.<locals>.FooN)r   r   r   r   r   )ra   r   r   Foo   s   rb   c               S   s   dS )Nr+   r   r   r   r   r   r-      s    z9TestBaseCallable.test_override_validate.<locals>.<lambda>)r   c             S   s   | S )Nr   )xr   r   r   r-      s    Nr+   )r   r   r5   r   )r   rb   r   )ra   r   test_override_validate   s    
z'TestBaseCallable.test_override_validatec             C   s   t dd d d S )Nc             S   s   | S )Nr   )rc   r   r   r   r-      s    z8TestBaseCallable.test_accepts_function.<locals>.<lambda>)r   )r&   )r   r   r   r   r4      s    z&TestBaseCallable.test_accepts_functionc             C   s   t tjd d S )N)r   )r&   r   r   )r   r   r   r   r3      s    z$TestBaseCallable.test_accepts_methodc             C   s   t td d S )N)r   )r&   r=   )r   r   r   r   r1      s    z"TestBaseCallable.test_accepts_typec             C   s   t d d d S )N)r   )r&   )r   r   r   r   test_accepts_none   s    z"TestBaseCallable.test_accepts_nonec             C   sF   | j t tt d W d Q R X | j t tdd W d Q R X d S )N)r   r+   )r5   r   r&   r   )r   r   r   r   test_rejects_non_callable   s    z*TestBaseCallable.test_rejects_non_callableN)	r   r   r   rd   r4   r3   r1   re   rf   r   r   r   r   rW      s   rW   )rX   ZunittestZ
traits.apir   r   r   r   r   r   r   r	   r
   r   r]   r   r   r   r    r&   ZTestCaser'   rW   r   r   r   r   <module>   s   , 