3
Ndq                 @   s$  d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	j
Z
ddlmZmZmZmZ ddlmZmZ edk	rddlmZmZmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lm Z m!Z!m"Z" dZ#G dd deZ$G dd deZ%G dd deZ&eG dd dej'Z(dS )z! Tests for the trait documenter.     N)Bool	HasTraitsIntProperty)sphinxrequires_sphinx)ClassDocumenterINSTANCEATTROptions)DocumenterBridge)path)SphinxTestApp)LoggingReporter)_get_definition_tokenstrait_definitionTraitDocumenterzextensions = ['sphinx.ext.autodoc']

# The suffix of source filenames.
source_suffix = '.rst'

autodoc_mock_imports = [
    'dummy'
]
c               @   s   e Zd ZdZedddZdS )MyTestClassz 
    Class-level docstring.
    *   zd First line

        The answer to
        Life,
        the Universe,

        and Everything.
    )descN)__name__
__module____qualname____doc__r   bar r   r   [/var/www/html/virt/lib64/python3.6/site-packages/traits/util/tests/test_trait_documenter.pyr   6   s   r   c               @   s   e Zd ZeeddZdS )Fakeu   ミスあり)labelN)r   r   r   r   r   test_attributer   r   r   r   r   E   s   r   c               @   s*   e Zd ZdZeZe ZdZedd Z	dS )FindTheTraitszB
    Class for testing the can_document_member functionality.
    i  c             C   s   dS )z6
        I'm a regular property, not a trait.
        Nr   )selfr   r   r   not_a_traitY   s    zFindTheTraits.not_a_traitN)
r   r   r   r   r   an_intanother_intmagic_numberpropertyr!   r   r   r   r   r   K   s
   r   c               @   sd   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
ejdd Zejdd ZdS )TestTraitDocumenterz! Tests for the trait documenter. c             C   s(   d| _ tj| j }tj|j}|| _d S )Nzp
    depth_interval = Property(Tuple(Float, Float),
                              depends_on="_depth_interval")
)sourceioStringIOtokenizegenerate_tokensreadlinetokens)r    	string_ior-   r   r   r   setUpd   s    zTestTraitDocumenter.setUpc             C   sF   t jd}tj|}tj|j}t|}tj|}| j	|j
 | d S )Nz        depth_interval = Property(Tuple(Float, Float),
                                  depends_on="_depth_interval")
        )textwrapdedentr(   r)   r*   r+   r,   r   
untokenizeassertEqualrstrip)r    srcr.   r-   Zdefinition_tokensstringr   r   r   test_get_definition_tokensm   s    

z.TestTraitDocumenter.test_get_definition_tokensc          
   C   s\   t j }t|dd}d|_t|_t jd |jd W d Q R X | jt	|j
jjjd d S )Ntestz   r   zFtraits.util.trait_documenter.ClassLevelDocumenter.add_directive_header    )mockZ	MagicMockr   Zobject_namer   parentpatchZadd_directive_headerr3   len	directiveresultappendZ
mock_calls)r    Zmocked_directive
documenterr   r   r   test_add_line~   s    z!TestTraitDocumenter.test_add_linec             C   sx   | j  &}t|td }|jdd |j}W d Q R X x&|D ]}|j jdr:P q:W | jd | jd| | j	d| d S )Nz.MyTestClass.barT)Zall_membersz:annotation:z+Didn't find the expected trait :annotation:z
First line
)
create_directiver   r   generater@   lstrip
startswithZfailZassertInZassertNotIn)r    r?   rB   r@   itemr   r   r   test_abbreviated_annotations   s    


z0TestTraitDocumenter.test_abbreviated_annotationsc             C   s   t tdd}| j|d d S )Nr   )cls
trait_nameu$   Property(Bool, label="ミスあり"))r   r   r3   )r    Z
definitionr   r   r    test_successful_trait_definition   s    z4TestTraitDocumenter.test_successful_trait_definitionc             C   s&   | j t ttdd W d Q R X d S )Nr!   )rK   rL   )assertRaises
ValueErrorr   r   )r    r   r   r   test_failed_trait_definition   s    z0TestTraitDocumenter.test_failed_trait_definitionc             C   s   | j  |}t|td }|j  |j  | jtjtdd| | jtjtdd| | j	tjtdd| | j	tjtdd| W d Q R X d S )Nz.FindTheTraitsr"   Tr#   r$   r!   )
rE   r   r   
parse_nameZimport_object
assertTruer   Zcan_document_memberr	   ZassertFalse)r    r?   Zclass_documenterr   r   r   test_can_document_member   s"    
z,TestTraitDocumenter.test_can_document_memberc             c   s   | j  }tjj|d}t|ddd}|jt W dQ R X tt|d}||jj	_
d|jj	jd< i }tj }d	|jj_||d
< t|j	tdt df|V  W dQ R X dS )aW  
        Helper function to create a a "directive" suitable
        for instantiating the TraitDocumenter with, along with resources
        to support that directive, and clean up the resources afterwards.

        Returns
        -------
        contextmanager
            A context manager that returns a DocumenterBridge instance.
        zconf.pywzutf-8)encodingN)srcdirdummyZdocname   stater9   r:   )tmpdirosr   joinopenwriteCONF_PYr   ZbuilderenvappZ	temp_datar;   ZMockdocumentsettingsZ	tab_widthr   r   r
   )r    rZ   Z	conf_filefra   kwdsrY   r   r   r   rE      s    


z$TestTraitDocumenter.create_directivec             c   s$   t j }z
|V  W dtj| X dS )z
        Helper function to create a temporary directory.

        Returns
        -------
        contextmanager
            Context manager that returns the path to a temporary directory.
        N)tempfilemkdtempshutilrmtree)r    rZ   r   r   r   rZ      s    

zTestTraitDocumenter.tmpdirN)r   r   r   r   r/   r7   rC   rJ   rM   rP   rS   
contextlibcontextmanagerrE   rZ   r   r   r   r   r&   `   s   	"r&   ))r   rj   r(   r[   rh   rf   r0   r*   ZunittestZunittest.mockr;   Z
traits.apir   r   r   r   Z$traits.testing.optional_dependenciesr   r   Zsphinx.ext.autodocr   r	   r
   Zsphinx.ext.autodoc.directiver   Zsphinx.testing.pathr   Zsphinx.testing.utilr   Zsphinx.util.docutilsr   Ztraits.util.trait_documenterr   r   r   r_   r   r   r   ZTestCaser&   r   r   r   r   <module>   s0   
