B
    .(b8                 @   s   d dl mZ d dlZd dlmZmZ d dlmZmZ d dlm	Z	m
Z
 d dlmZ d dlmZmZ d dlZdd	 Zd
d Zdd Zdd Zdd Zejdddddddddgdd Zdd ZdS )    )unicode_literalsN)Language	component)print_summaryvalidate_attrs)get_assigns_for_attrget_requires_for_attr)
is_python2)MockANYc              C   sF   t dddd } | jdks tts2| jdks2t| ddksBtd S )Ntest)namec             S   s   | S )	docstring )docr   r   Q/home/dcms/DCMS/lib/python3.7/site-packages/spacy/tests/pipeline/test_analysis.pytest_component   s    z9test_component_decorator_function.<locals>.test_componentr   foo)r   r   AssertionErrorr	   __doc__)r   r   r   r   !test_component_decorator_function   s
    r   c              C   s   t ddG dd dt} | jdks(t| jdks6tt| dsDt|  }|jdksXt|ddkshtt|dsvt|ddkstts| jdkst| j	jd	kst| jjd
kst|jdkst|j	jd	kst|jjd
kstd S )Nr   )r   c               @   s$   e Zd ZdZdZdd Zdd ZdS )z5test_component_decorator_class.<locals>.TestComponent
docstring1barc             S   s   |S )
docstring2r   )selfr   r   r   r   __call__    s    z>test_component_decorator_class.<locals>.TestComponent.__call__c             S   s   |S )
docstring3r   )r   xr   r   r   custom$   s    z<test_component_decorator_class.<locals>.TestComponent.customN)__name__
__module____qualname__r   r   r   r   r   r   r   r   TestComponent   s   r"   r   r   r   r   r   r   )
r   objectr   r   r   hasattrr   r	   r   r   )r"   r   r   r   r   test_component_decorator_class   s"    r%   c           	   C   s  dt j_tdddgddd } tddd	gd
dgddd }tdd
gdgddd }dtjksftdtjksttdtjkstt }||  t	t
 || W d Q R X || t|jd}dd |D ddgkst|d}|jdkst|jdks
t|j|dd |jddddgks0tdtjks@t|jd dksTt|jd dkshtt|jd}dd |D dddgkstt|jd	}dd |D dgkstt|ddst|dstd S )NTc1z	token.tagz
doc.tensor)assignsc             S   s   | S )Nr   )r   r   r   r   test_component1<   s    z9test_component_decorator_assigns.<locals>.test_component1c2z	token.posztoken.lemma)requiresr'   c             S   s   | S )Nr   )r   r   r   r   test_component2@   s    z9test_component_decorator_assigns.<locals>.test_component2c3ztoken._.custom_lemmac             S   s   | S )Nr   )r   r   r   r   test_component3F   s    z9test_component_decorator_assigns.<locals>.test_component3c             S   s   g | ]\}}|qS r   r   ).0r   _r   r   r   
<listcomp>T   s    z4test_component_decorator_assigns.<locals>.<listcomp>Zc4)r   c             S   s   g | ]\}}|qS r   r   )r.   r   r/   r   r   r   r0   ^   s    c             S   s   g | ]\}}|qS r   r   )r.   r   r/   r   r   r   r0   `   s    )Zno_printzhello world)spacylanguageENABLE_PIPELINE_ANALYSISr   r   	factoriesr   add_pipepytestwarnsUserWarningr   Zpipelinecreate_piper   factoryZ
pipe_namesZpipe_factoriesr   r   )r(   r+   r-   nlpZassigns_tensorZtest_component4Zrequires_posr   r   r    test_component_decorator_assigns9   s:    


r<   c              C   s   G dd dt } t }|  |_t|| _td| } dtjksBtt }|j	dddid}|
| |dspt|jt|dd d	S )
zTest that class components can implement a from_nlp classmethod that
    gives them access to the nlp object and config via the factory.c               @   s   e Zd Zdd ZdS )z9test_component_factories_from_nlp.<locals>.TestComponent5c             S   s   |S )Nr   )r   r   r   r   r   r   j   s    zBtest_component_factories_from_nlp.<locals>.TestComponent5.__call__N)r   r    r!   r   r   r   r   r   TestComponent5i   s   r=   Zc5r   r   )configzhello world)r   N)r#   r
   Zreturn_valueclassmethodZfrom_nlpr   r   r4   r   r9   r5   Zassert_called_once_withr   )r=   mockr;   piper   r   r   !test_component_factories_from_nlpe   s    

rB   c           	   C   s\   dddddg} t | stx| D ]}t |gs tq W tt t ddg W d Q R X d S )Nz	doc.sentszdoc.entsz	token.tagztoken._.xyzz
span._.xyzzdoc.xyz)r   r   r6   raises
ValueError)attrsattrr   r   r   "test_analysis_validate_attrs_valid{   s    
rG   rF   r   Zdoc_entszdoc.xyzz	token.xyzz
token.tag_ztoken.tag.xyzztoken._.xyz.abcz
span.labelc          	   C   s$   t t t| g W d Q R X d S )N)r6   rC   rD   r   )rF   r   r   r   $test_analysis_validate_attrs_invalid   s    rH   c           	   C   s   dt j_tddgddd } tddgd	d
d }t }||  tt || W dQ R X td}|	d W dQ R X |j
rtdS )z7Test that attributes are validated correctly on remove.Tr&   z	token.tag)r'   c             S   s   | S )Nr   )r   r   r   r   r&      s    z4test_analysis_validate_attrs_remove_pipe.<locals>.c1r)   z	token.pos)r*   c             S   s   | S )Nr   )r   r   r   r   r)      s    z4test_analysis_validate_attrs_remove_pipe.<locals>.c2N)r1   r2   r3   r   r   r5   r6   r7   r8   Zremove_pipelistr   )r&   r)   r;   recordr   r   r   (test_analysis_validate_attrs_remove_pipe   s    
rK   )
__future__r   Zspacy.languager1   r   r   Zspacy.analysisr   r   r   r   Zspacy.compatr	   r@   r
   r   r6   r   r%   r<   rB   rG   markZparametrizerH   rK   r   r   r   r   <module>   s.    ,	