3
6ßL]3  ã               @   sª   d Z ddlmZmZ ddlmZmZ ddlmZmZm	Z	 ddl
mZmZmZmZmZ ddl
mZ ddlmZ dd	„ ZG d
d„ deƒZG dd„ dejƒZedkr¦ejƒ  dS )z Tests for plugins. é    )ÚexistsÚjoin)ÚApplicationÚExtensionPoint)ÚIPluginActivatorÚPluginÚcontributes_to)Ú	HasTraitsÚInstanceÚIntÚ	InterfaceÚList)Úprovides)Úunittestc             C   s   | t _|t _|t _|t _dS )z, A useful trait change handler for testing! N)ÚlistenerÚobjÚ
trait_nameÚoldÚnew)r   r   r   r   © r   úE/tmp/pip-install-5sebvq2j/envisage/envisage/tests/plugin_test_case.pyr      s
    r   c               @   s   e Zd ZdZdZdS )ÚTestApplicationz, The type of application used in the tests. ÚtestN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úidr   r   r   r   r      s   r   c               @   sˆ   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d„ Zdd„ Zd S )!ÚPluginTestCasez Tests for plugins. c             C   s   dS )z> Prepares the test fixture before each test method is called. Nr   )Úselfr   r   r   ÚsetUp'   s    zPluginTestCase.setUpc             C   s   dS )z< Called immediately after each test method has been called. Nr   )r   r   r   r   ÚtearDown,   s    zPluginTestCase.tearDownc             C   sX   t ƒ }| jd|jƒ t dd�}| jd|jƒ t ddd�}| jd|jƒ | jd|jƒ dS )z id policy zenvisage.plugin.PluginÚwilma)r   Zfred)Únamer   N)r   ÚassertEqualr   r#   )r   Úpr   r   r   Útest_id_policy5   s    
zPluginTestCase.test_id_policyc             C   sT   t ƒ }| jd|jƒ t dd�}| jd|jƒ G dd„ dt ƒ}|ƒ }| jd|jƒ dS )z name policy r   r"   )r#   c               @   s   e Zd ZdS )z7PluginTestCase.test_name_policy.<locals>.ThisIsMyPluginN)r   r   r   r   r   r   r   ÚThisIsMyPluginS   s   r'   zThis Is My PluginN)r   r$   r#   )r   r%   r'   r   r   r   Útest_name_policyG   s    
zPluginTestCase.test_name_policyc             C   sŒ   t tƒG dd„ dtƒƒ}G dd„ dtƒ}G dd„ dtƒ}|ƒ }||d�}|ƒ }t||gd�}|jƒ  | j||jƒ |jƒ  | j||j	ƒ d	S )
z plugin activator. c               @   s    e Zd ZdZdd„ Zdd„ ZdS )zAPluginTestCase.test_plugin_activator.<locals>.NullPluginActivatorz' A plugin activator that does nothing! c             S   s
   || _ dS )z Start a plugin. N)Ústarted)r   Úpluginr   r   r   Ústart_pluginb   s    zNPluginTestCase.test_plugin_activator.<locals>.NullPluginActivator.start_pluginc             S   s
   || _ dS )z Stop a plugin. N)Ústopped)r   r*   r   r   r   Ústop_plugini   s    zMPluginTestCase.test_plugin_activator.<locals>.NullPluginActivator.stop_pluginN)r   r   r   r   r+   r-   r   r   r   r   ÚNullPluginActivator^   s   r.   c               @   s   e Zd ZdZdS )z5PluginTestCase.test_plugin_activator.<locals>.PluginAÚAN)r   r   r   r   r   r   r   r   ÚPluginAp   s   r0   c               @   s   e Zd ZdZdS )z5PluginTestCase.test_plugin_activator.<locals>.PluginBÚBN)r   r   r   r   r   r   r   r   ÚPluginBs   s   r2   )Z	activator)ÚpluginsN)
r   r   r	   r   r   Ústartr$   r)   Ústopr,   )r   r.   r0   r2   Zplugin_activatorÚaÚbÚapplicationr   r   r   Útest_plugin_activator[   s    
z$PluginTestCase.test_plugin_activatorc                s  G dd„ dt ƒ‰G dd„ dt ƒ‰ G dd„ dt ƒ‰G ‡ ‡‡fdd„dtƒ}|ƒ }t|gd	�}|jƒ  | jd
|jˆƒƒ | j|j|jˆƒƒ | jd
|jˆ ƒƒ | j|j|jˆ ƒƒ | jd
|jˆƒƒ | j|j	|jˆƒƒ |j
ƒ  | jd
|jˆƒƒ | jd
|jˆ ƒƒ | jd
|jˆƒƒ d
S )z	 service c               @   s   e Zd ZdS )z(PluginTestCase.test_service.<locals>.FooN)r   r   r   r   r   r   r   ÚFooŒ   s   r:   c               @   s   e Zd ZdS )z(PluginTestCase.test_service.<locals>.BarN)r   r   r   r   r   r   r   ÚBar�   s   r;   c               @   s   e Zd ZdS )z(PluginTestCase.test_service.<locals>.BazN)r   r   r   r   r   r   r   ÚBaz’   s   r<   c                   s:   e Zd ZdZe”f dd�Ze” f dd�Ze”f dd�ZdS )z,PluginTestCase.test_service.<locals>.PluginAr/   T)ÚserviceN)r   r   r   r   r
   ÚfooÚbarÚbazr   )r;   r<   r:   r   r   r0   •   s   r0   )r3   N)r	   r   r   r4   ÚassertNotEqualÚget_servicer$   r>   r?   r@   r5   )r   r0   r6   r8   r   )r;   r<   r:   r   Útest_service‰   s$    zPluginTestCase.test_servicec                s®   G dd„ dt ƒ}G dd„ dt ƒ‰t|ˆƒG dd„ dtƒƒ‰ G ‡ ‡fdd„dtƒ}|ƒ }t|gd	�}|jƒ  | jd
|jˆƒƒ | j|j	|jˆƒƒ |j
ƒ  | jd
|jˆƒƒ d
S )z service protocol c               @   s   e Zd ZdS )z2PluginTestCase.test_service_protocol.<locals>.IFooN)r   r   r   r   r   r   r   ÚIFoo¶   s   rD   c               @   s   e Zd ZdS )z2PluginTestCase.test_service_protocol.<locals>.IBarN)r   r   r   r   r   r   r   ÚIBar¹   s   rE   c               @   s   e Zd ZdS )z1PluginTestCase.test_service_protocol.<locals>.FooN)r   r   r   r   r   r   r   r:   ¼   s   r:   c                   s    e Zd ZdZe” f d”d�ZdS )z5PluginTestCase.test_service_protocol.<locals>.PluginAr/   T)r=   Zservice_protocolN)r   r   r   r   r
   r>   r   )r:   rE   r   r   r0   À   s   r0   )r3   N)r   r   r	   r   r   r4   rA   rB   r$   r>   r5   )r   rD   r0   r6   r8   r   )r:   rE   r   Útest_service_protocol³   s    z$PluginTestCase.test_service_protocolc             C   sN   G dd„ dt ƒ}G dd„ dt ƒ}|ƒ }|ƒ }t||gd�}| jt|jdƒ dS )z multiple trait contributions c               @   s   e Zd ZdZeedd�ZdS )zAPluginTestCase.test_multiple_trait_contributions.<locals>.PluginAr/   Úx)r   N)r   r   r   r   r   r   rG   r   r   r   r   r0   ×   s   r0   c               @   s4   e Zd ZdZedddgdd�Zeddd	gdd�Zd
S )zAPluginTestCase.test_multiple_trait_contributions.<locals>.PluginBr1   é   é   é   rG   )r   é   é   é   N)r   r   r   r   r   rG   Úyr   r   r   r   r2   Û   s   r2   )r3   rG   N)r   r   ÚfailUnlessRaisesÚ
ValueErrorÚget_extensions)r   r0   r2   r6   r7   r8   r   r   r   Ú!test_multiple_trait_contributionsÔ   s    z0PluginTestCase.test_multiple_trait_contributionsc             C   sN   G dd„ dt ƒ}G dd„ dt ƒ}|ƒ }|ƒ }t||gd�}| jt|jdƒ dS )z! exception in trait contribution c               @   s   e Zd ZdZeedd�ZdS )zDPluginTestCase.test_exception_in_trait_contribution.<locals>.PluginAr/   rG   )r   N)r   r   r   r   r   r   rG   r   r   r   r   r0   ï   s   r0   c               @   s"   e Zd ZdZedd�Zdd„ ZdS )zDPluginTestCase.test_exception_in_trait_contribution.<locals>.PluginBr1   rG   )r   c             S   s   dd ‚dS )z Trait initializer. rH   r   Nr   )r   r   r   r   Ú
_x_defaultø   s    zOPluginTestCase.test_exception_in_trait_contribution.<locals>.PluginB._x_defaultN)r   r   r   r   r   rG   rS   r   r   r   r   r2   ó   s   
r2   )r3   rG   N)r   r   rO   ÚZeroDivisionErrorrQ   )r   r0   r2   r6   r7   r8   r   r   r   Ú$test_exception_in_trait_contributionì   s    
z3PluginTestCase.test_exception_in_trait_contributionc             C   sV   G dd„ dt ƒ}G dd„ dt ƒ}|ƒ }|ƒ }t||gd�}| jdddg|jd	ƒƒ d
S )z contributes to c               @   s   e Zd ZdZeedd�ZdS )z3PluginTestCase.test_contributes_to.<locals>.PluginAr/   rG   )r   N)r   r   r   r   r   r   rG   r   r   r   r   r0     s   r0   c               @   s"   e Zd ZdZedddgdd�ZdS )z3PluginTestCase.test_contributes_to.<locals>.PluginBr1   rH   rI   rJ   rG   )r   N)r   r   r   r   r   rG   r   r   r   r   r2     s   r2   )r3   rH   rI   rJ   rG   N)r   r   r$   rQ   )r   r0   r2   r6   r7   r8   r   r   r   Útest_contributes_to
  s    z"PluginTestCase.test_contributes_toc             C   sV   G dd„ dt ƒ}G dd„ dt ƒ}|ƒ }|ƒ }t||gd�}| jdddg|jd	ƒƒ d
S )z contributes to decorator c               @   s   e Zd ZdZeedd�ZdS )z=PluginTestCase.test_contributes_to_decorator.<locals>.PluginAr/   rG   )r   N)r   r   r   r   r   r   rG   r   r   r   r   r0   #  s   r0   c               @   s    e Zd ZdZedƒdd„ ƒZdS )z=PluginTestCase.test_contributes_to_decorator.<locals>.PluginBr1   rG   c             S   s
   dddgS )NrH   rI   rJ   r   )r   r   r   r   Ú_x_contributions*  s    zNPluginTestCase.test_contributes_to_decorator.<locals>.PluginB._x_contributionsN)r   r   r   r   r   rW   r   r   r   r   r2   '  s   r2   )r3   rH   rI   rJ   rG   N)r   r   r$   rQ   )r   r0   r2   r6   r7   r8   r   r   r   Útest_contributes_to_decorator   s    z,PluginTestCase.test_contributes_to_decoratorc             C   sV   G dd„ dt ƒ}G dd„ dt ƒ}|ƒ }|ƒ }t||gd�}| jdddg|jd	ƒƒ d
S )z3 contributes to decorator ignored if trait present c               @   s   e Zd ZdZeedd�ZdS )zVPluginTestCase.test_contributes_to_decorator_ignored_if_trait_present.<locals>.PluginAr/   rG   )r   N)r   r   r   r   r   r   rG   r   r   r   r   r0   9  s   r0   c               @   s2   e Zd ZdZedddgdd�Zedƒdd„ ƒZd	S )
zVPluginTestCase.test_contributes_to_decorator_ignored_if_trait_present.<locals>.PluginBr1   rH   rI   rJ   rG   )r   c             S   s
   dddgS )NrK   rL   rM   r   )r   r   r   r   rW   A  s    zgPluginTestCase.test_contributes_to_decorator_ignored_if_trait_present.<locals>.PluginB._x_contributionsN)r   r   r   r   r   rG   r   rW   r   r   r   r   r2   =  s   r2   )r3   rH   rI   rJ   rG   N)r   r   r$   rQ   )r   r0   r2   r6   r7   r8   r   r   r   Ú6test_contributes_to_decorator_ignored_if_trait_present6  s    zEPluginTestCase.test_contributes_to_decorator_ignored_if_trait_presentc             C   s>  G dd„ dt ƒ}G dd„ dt ƒ}G dd„ dt ƒ}|ƒ }|ƒ }|ƒ }tƒ }|jƒ  |j|ƒ |j|ƒ | jg |jƒ |j|ƒ | jddd	g|jƒ | jddd	g|jƒ |j|ƒ | jddd	d
ddg|jƒ | jd
ddg|jƒ |j|ƒ | jd
ddg|jƒ | jddd	g|j	ƒ |j|ƒ | jg |jƒ | jd
ddg|j	ƒ dS )z" add plugins to empty application c               @   s(   e Zd ZdZeeeƒdd�Zdd„ ZdS )zEPluginTestCase.test_add_plugins_to_empty_application.<locals>.PluginAr/   rG   )r   c             S   s   |j | _ |j| _d S )N)ÚaddedÚremoved)r   Úeventr   r   r   Ú_x_items_changedT  s    zVPluginTestCase.test_add_plugins_to_empty_application.<locals>.PluginA._x_items_changedN)	r   r   r   r   r   r   r   rG   r]   r   r   r   r   r0   P  s   r0   c               @   s$   e Zd ZdZeedddgdd�ZdS )zEPluginTestCase.test_add_plugins_to_empty_application.<locals>.PluginBr1   rH   rI   rJ   rG   )r   N)r   r   r   r   r   r   rG   r   r   r   r   r2   Z  s   r2   c               @   s$   e Zd ZdZeedddgdd�ZdS )zEPluginTestCase.test_add_plugins_to_empty_application.<locals>.PluginCÚCrK   rL   rM   rG   )r   N)r   r   r   r   r   r   rG   r   r   r   r   ÚPluginC^  s   r_   rH   rI   rJ   rK   rL   rM   N)
r   r   r4   Z
add_pluginr+   r$   rG   rZ   Zremove_pluginr[   )r   r0   r2   r_   r6   r7   Úcr8   r   r   r   Ú%test_add_plugins_to_empty_applicationM  s0    






z4PluginTestCase.test_add_plugins_to_empty_applicationc             C   s   G dd„ dt ƒ}G dd„ dt ƒ}|ƒ }|ƒ }t||gd�}| jt|jd|jƒ|jƒ | jt|jd|jƒ|jƒ | jt|jƒƒ | jt|jƒƒ |ƒ }|ƒ }t||gd�}| jt|jd|jƒ|jƒ | jt|jd|jƒ|jƒ | jt|jƒƒ | jt|jƒƒ dS )z home c               @   s   e Zd ZdZdS )z)PluginTestCase.test_home.<locals>.PluginAr/   N)r   r   r   r   r   r   r   r   r0      s   r0   c               @   s   e Zd ZdZdS )z)PluginTestCase.test_home.<locals>.PluginBr1   N)r   r   r   r   r   r   r   r   r2   £  s   r2   )r3   r3   N)r   r   r$   r   Úhomer   Zassert_r   )r   r0   r2   r6   r7   r8   r   r   r   Ú	test_home�  s"    zPluginTestCase.test_homec             C   s,   G dd„ dt ƒ}t|ƒ gd�}|jdƒ dS )z Regression test for #119. c               @   s   e Zd ZdZeedd�ZdS )z1PluginTestCase.test_no_recursion.<locals>.PluginAr/   Úbob)r   N)r   r   r   r   r   r   rG   r   r   r   r   r0   Ç  s   r0   )r3   rd   N)r   r   rQ   )r   r0   r8   r   r   r   Útest_no_recursionÄ  s    z PluginTestCase.test_no_recursionN)r   r   r   r   r    r!   r&   r(   r9   rC   rF   rR   rU   rV   rX   rY   ra   rc   re   r   r   r   r   r       s    	.*!P'r   Ú__main__N)r   Úos.pathr   r   Zenvisage.apir   r   r   r   r   Z
traits.apir	   r
   r   r   r   r   Ztraits.testing.unittest_toolsr   r   r   ZTestCaser   r   Úmainr   r   r   r   Ú<module>   s      3