o
    Ñó·i'9  ã                   @  s  d dl mZ d dlZd dlZd dlZd dlmZ d dl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 d
dlmZ dd„ Zdd„ Zdd„ ZG dd„ deƒZG dd„ dƒZd3dd„Zeeƒ  dƒeeƒ  dƒG dd„ de
ƒƒƒZeG dd„ deƒƒZd d!„ Zd"d#„ Zeeƒ  dƒeeƒ  dƒG d$d%„ d%e
ƒƒƒZ eeƒ  dƒeeƒ  d&ƒeeƒ  dƒG d'd(„ d(e
ƒƒƒƒZ!eeƒ  dƒeeƒ  dƒG d)d*„ d*e
ƒƒƒZ"G d+d,„ d,eƒZ#G d-d.„ d.eƒZ$G d/d0„ d0eƒZ%eeƒ  dƒeeƒ  d&ƒeeƒ  dƒG d1d2„ d2e
ƒƒƒƒZ&dS )4é    )ÚannotationsN)Ú	dataclass)ÚProtocol)ÚTestCaseÚskipIf)ÚTypeMachineBuilderÚpep614é   )ÚMethodicalMachine)ÚTypeMachineé   )ÚisTwistedInstalledc                   C  s$   zt dƒ W dS  ty   Y dS w )z2
    Is the graphviz Python module installed?
    ZgraphvizFTN)Ú
__import__ÚImportError© r   r   úbC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\automat/_test/test_visualize.pyÚisGraphvizModuleInstalled   s   
ýÿr   c               	   C  sB   t  ¡ \} }t  |¡ ztjd| dd� W t  | ¡ S t  | ¡ w )z+
    Are the graphviz tools installed?
    ÚdotT)ÚstdinÚshellN)ÚosÚpipeÚcloseÚ
subprocessÚcall)ÚrÚwr   r   r   ÚisGraphvizInstalled   s
   
r   c                    s,   t ƒ ‰ G ‡ fdd„dtƒ} | ƒ }| ¡  ˆ S )zG
    Create a sample L{MethodicalMachine} with some sample states.
    c                      s`   e Zd Z” jdd�dd„ ƒZ”  ¡ dd„ ƒZ”  ¡ dd„ ƒZ”  ¡ d	d
„ ƒZ	e 
eee	g¡ dS )z#sampleMachine.<locals>.SampleObjectT)Úinitialc                 S  ó   dS )zinitial stateNr   ©Úselfr   r   r   Úbegin0   ó    z)sampleMachine.<locals>.SampleObject.beginc                 S  r   )z	end stateNr   r    r   r   r   Úend4   r#   z'sampleMachine.<locals>.SampleObject.endc                 S  r   )zsample inputNr   r    r   r   r   Úgo8   r#   z&sampleMachine.<locals>.SampleObject.goc                 S  r   )zsample outputNr   r    r   r   r   Úout<   r#   z'sampleMachine.<locals>.SampleObject.outN)Ú__name__Ú
__module__Ú__qualname__Ústater"   r$   Úinputr%   Úoutputr&   Úuponr   ©Úmmr   r   ÚSampleObject/   s    




r0   N)r
   Úobjectr%   )r0   Úsor   r.   r   ÚsampleMachine)   s
   r3   c                   @  s   e Zd Zddd„ZdS )ÚSampleÚreturnÚNonec                 C  ó   d S ©Nr   r    r   r   r   r%   H   r#   z	Sample.goN©r5   r6   )r'   r(   r)   r%   r   r   r   r   r4   G   s    r4   c                   @  s   e Zd ZdS )ÚCoreN)r'   r(   r)   r   r   r   r   r:   I   s    r:   r5   úTypeMachine[Sample, Core]c                  C  sŒ   t ttƒ} |  d¡}ddd	„}|  d
|¡}|  d¡}| tj¡ |¡ d¡ | tj¡ |¡ d¡ t| tj¡ |¡ƒddd„ƒ}|  	¡ S )zA
    Create a sample L{TypeMachine} with some sample states.
    r"   Úprotor4   Úcorer:   r5   Úintc                 S  r   )Né   r   )r<   r=   r   r   r   ÚbuilditS   ó   z"sampleTypeMachine.<locals>.builditÚdatar$   NÚsampler6   c                 S  r7   r8   r   )rC   r=   r   r   r   r&   [   rA   zsampleTypeMachine.<locals>.out)r<   r4   r=   r:   r5   r>   )rC   r4   r=   r:   r5   r6   )
r   r4   r:   r*   r-   r%   ÚtoÚreturnsr   Úbuild)Zbuilderr"   r@   rB   r$   r&   r   r   r   ÚsampleTypeMachineL   s   



rG   z!Graphviz module is not installed.zTwisted is not installed.c                   @  s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚElementMakerTestszL
    L{elementMaker} generates HTML representing the specified element.
    c                 C  s   ddl m} || _d S )Nr	   )ÚelementMaker)Ú
_visualizerI   )r!   rI   r   r   r   ÚsetUph   s   
zElementMakerTests.setUpc              	   C  ó"   d}|   || jddddd�¡ dS )zK
        L{elementMaker} orders HTML attributes lexicographically.
        z<div a="1" b="2" c="3"></div>ÚdivÚ2Ú1Ú3©ÚbÚaÚcN©ÚassertEqualrI   ©r!   Úexpectedr   r   r   Útest_sortsAttrsm   s   z!ElementMakerTests.test_sortsAttrsc              	   C  rL   )z¡
        L{elementMaker} quotes HTML attributes according to DOT's quoting rule.

        See U{http://www.graphviz.org/doc/info/lang.html}, footnote 1.
        z-<div a="1" b="a \" quote" c="a string"></div>rM   z	a " quoter   za stringrQ   NrU   rW   r   r   r   Útest_quotesAttrst   s   ÿz"ElementMakerTests.test_quotesAttrsc                 C  s   d}|   ||  d¡¡ dS )zN
        L{elementMaker} should render an element with no attributes.
        z<div ></div>rM   NrU   rW   r   r   r   Útest_noAttrs   s   zElementMakerTests.test_noAttrsN)r'   r(   r)   Ú__doc__rK   rY   rZ   r[   r   r   r   r   rH   a   s    rH   c                   @  s*   e Zd ZU dZded< ded< ded< dS )	ÚHTMLElementz2Holds an HTML element, as created by elementMaker.ÚstrÚnamezlist[HTMLElement]Úchildrenzdict[str, str]Ú
attributesN)r'   r(   r)   r\   Ú__annotations__r   r   r   r   r]   ‡   s
   
 r]   c                   s.   ˆ | ƒr| gS t | ƒrg S ‡ fdd„| jD ƒS )zk
    Recursively collect all elements in an L{HTMLElement} tree that
    match the optional predicate.
    c                   s    g | ]}t |ˆ ƒD ]}|‘q	qS r   )ÚfindElements)Ú.0ÚchildÚresult©Ú	predicater   r   Ú
<listcomp>š   s    ýþÿz findElements.<locals>.<listcomp>N)ÚisLeafr`   )Úelementrh   r   rg   r   rc   �   s   
þrc   c                 C  s   t | tƒ S )z2
    This HTML element is actually leaf node.
    N)Ú
isinstancer]   ©rk   r   r   r   rj   ¡   s   rj   c                   @  s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚTableMakerTestszÚ
    Tests that ensure L{tableMaker} generates HTML tables usable as
    labels in DOT graphs.

    For more information, read the "HTML-Like Labels" section of
    U{http://www.graphviz.org/doc/info/shapes.html}.
    c                 O  s   t |||d�S )N)r_   r`   ra   )r]   )r!   r_   r`   ra   r   r   r   ÚfakeElementMaker³   s   z TableMakerTests.fakeElementMakerc                 C  s.   ddl m} d| _d| _tj|| jd�| _d S )Nr	   )Ú
tableMakerúinput labelzthe port)Z_E)rJ   rp   Ú
inputLabelÚportÚ	functoolsÚpartialro   )r!   rp   r   r   r   rK   ¶   s   zTableMakerTests.setUpc                   s’   ‡ fdd„}g dgfD ];}ˆ j ˆ j|ˆ jd�}ˆ  t|jƒd¡ |jd }t||ƒ}ˆ  t|ƒd¡ ˆ  |d jd¡ ˆ  t|t	ƒˆ jg¡ qdS )	zÍ
        The table returned by L{tableMaker} always contains the input
        symbol label in its first row, and that row contains one cell
        with a port attribute set to the provided port.
        c                   s   t | ƒ o| j d¡ˆ jkS )Nrs   )rj   ra   Úgetrs   rm   r    r   r   ÚhasPortÄ   s   z3TableMakerTests.test_inputLabelRow.<locals>.hasPortzan output label©rs   r   r   ÚtdN)
rp   rr   rs   ZassertGreaterÚlenr`   rc   rV   r_   rj   )r!   rw   ZoutputLabelsÚtableÚinputLabelRowZportCandidatesr   r    r   Útest_inputLabelRow½   s   

÷z"TableMakerTests.test_inputLabelRowc                 C  s>   | j dd| jd�}|  t|jƒd¡ |j\}|  d|j¡ dS )z�
        L{tableMaker} does not add a colspan attribute to the input
        label's cell or a second row if there no output labels.
        rq   r   rx   r   ÚcolspanN)rp   rs   rV   rz   r`   ZassertNotInra   )r!   r{   r|   r   r   r   Útest_noOutputLabelsÒ   s   z#TableMakerTests.test_noOutputLabelsc                 C  sh   | j | jd| jd�}|  t|jƒd¡ |j\}}dd„ }|  tt||ƒƒd¡ |  t|tƒddg¡ d	S )
z¹
        L{tableMaker} adds a colspan attribute to the input label's cell
        equal to the number of output labels and a second row that
        contains the output labels.
        )úoutput label 1úoutput label 2rx   r	   c                 S  s$   t | ƒ o| jdko| j d¡dkS )Nry   r~   rN   )rj   r_   ra   rv   rm   r   r   r   ÚhasCorrectColspané   s
   
ÿýz@TableMakerTests.test_withOutputLabels.<locals>.hasCorrectColspanr   r€   r�   N)rp   rr   rs   rV   rz   r`   rc   rj   )r!   r{   ZinputRowZ	outputRowr‚   r   r   r   Útest_withOutputLabelsÜ   s   
ÿ
ÿz%TableMakerTests.test_withOutputLabelsN)	r'   r(   r)   r\   ro   rK   r}   r   rƒ   r   r   r   r   rn   ¨   s    
rn   z!Graphviz tools are not installed.c                   @  s   e Zd ZdZddd„ZdS )ÚIntegrationTestsz[
    Tests which make sure Graphviz can understand the output produced by
    Automat.
    r5   r6   c                 C  sN   t ƒ  ¡ }d |¡ d¡}tjdtjtjd�}| |¡\}}|  |j	d¡ dS )z8
        C{graphviz} emits valid graphviz data.
        Ú zutf-8r   )r   Ústdoutr   N)
r3   Ú	asDigraphÚjoinÚencoder   ÚPopenÚPIPEÚcommunicaterV   Ú
returncode)r!   ÚdigraphÚtextÚpr&   Úerrr   r   r   Útest_validGraphvizÿ   s
   
z#IntegrationTests.test_validGraphvizNr9   )r'   r(   r)   r\   r’   r   r   r   r   r„   ö   s    r„   c                   @  ó    e Zd ZdZdd„ Zdd„ ZdS )Ú
SpotCheckszj
    Tests to make sure that the output contains salient features of the machine
    being generated.
    c                 C  sD   d  tƒ  ¡ ¡}|  d|¡ |  d|¡ |  d|¡ |  d|¡ dS )z‰
        The output of L{graphviz.Digraph} should contain the names of the
        states, inputs, outputs in the state machine.
        r…   r"   r$   r%   r&   N)rˆ   r3   r‡   ÚassertIn©r!   Zgvoutr   r   r   Útest_containsMachineFeatures  s
   z'SpotChecks.test_containsMachineFeaturesc                 C  sP   d  tƒ  ¡ ¡}|  d|¡ |  d|¡ |  d|¡ |  d|¡ |  d|¡ dS )z‰
        The output of L{graphviz.Digraph} should contain the names of the states,
        inputs, outputs in the state machine.
        r…   r"   r$   r%   zdata:builditr&   N)rˆ   rG   r‡   r•   r–   r   r   r   Ú test_containsTypeMachineFeatures  s   z+SpotChecks.test_containsTypeMachineFeaturesN)r'   r(   r)   r\   r—   r˜   r   r   r   r   r”   
  s    r”   c                   @  r“   )ÚRecordsDigraphActionsz/
    Records calls made to L{FakeDigraph}.
    c                 C  s   |   ¡  d S r8   )Úresetr    r   r   r   Ú__init__/  s   zRecordsDigraphActions.__init__c                 C  s   g | _ g | _d S r8   )ÚrenderCallsÚ	saveCallsr    r   r   r   rš   2  s   
zRecordsDigraphActions.resetN)r'   r(   r)   r\   r›   rš   r   r   r   r   r™   *  ó    r™   c                   @  s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚFakeDigraphzZ
    A fake L{graphviz.Digraph}.  Instantiate it with a
    L{RecordsDigraphActions}.
    c                 C  ó
   || _ d S r8   )Ú	_recorder)r!   Zrecorderr   r   r   r›   =  ó   
zFakeDigraph.__init__c                 K  ó   | j j |¡ d S r8   )r¡   rœ   Úappend©r!   Úkwargsr   r   r   Úrender@  ó   zFakeDigraph.renderc                 K  r£   r8   )r¡   r�   r¤   r¥   r   r   r   ÚsaveC  r¨   zFakeDigraph.saveN)r'   r(   r)   r\   r›   r§   r©   r   r   r   r   rŸ   7  s
    rŸ   c                   @  r“   )ÚFakeMethodicalMachinezL
    A fake L{MethodicalMachine}.  Instantiate it with a L{FakeDigraph}
    c                 C  r    r8   ©Z_digraph)r!   rŽ   r   r   r   r›   L  r¢   zFakeMethodicalMachine.__init__c                 C  s   | j S r8   r«   r    r   r   r   r‡   O  s   zFakeMethodicalMachine.asDigraphN)r'   r(   r)   r\   r›   r‡   r   r   r   r   rª   G  rž   rª   c                   @  s`   e Zd Zdd„ Zdd„ Zdd„ Z	ddd	„Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )ÚVisualizeToolTestsc                 C  s2   t ƒ | _t| jƒ| _d| _dg| _g | _d| _d S )Nz	tool-testZignoredz	fake.fqpn)r™   ÚdigraphRecorderrŸ   ÚfakeDigraphÚfakePrognameÚfakeSysPathÚcollectedOutputÚfakeFQPNr    r   r   r   rK   W  s   
zVisualizeToolTests.setUpc                 G  s   | j  d |¡¡ d S )Nú )r±   r¤   rˆ   )r!   Úargsr   r   r   ÚcollectPrints`  s   z VisualizeToolTests.collectPrintsc                 c  s   � |t | jƒfV  d S r8   )rª   r®   )r!   Zfqpnr   r   r   ÚfakeFindMachinesc  s   €z#VisualizeToolTests.fakeFindMachinesNc                 C  s>   ddl m} ||p| j|p| jg|p| j|p| j|p| jd�S )Nr	   )Útool)Z	_prognameZ_argvZ_syspathZ_findMachinesZ_print)rJ   r·   r¯   r²   r°   r¶   rµ   )r!   ZprognameÚargvZsyspathZfindMachinesÚprintr·   r   r   r   r·   f  s   
ûzVisualizeToolTests.toolc                 C  s&   | j | jgd� |  | jd d¡ dS )z‰
        L{tool} adds '' to sys.path to ensure
        L{automat._discover.findMachines} searches the current
        directory.
        ©r¸   r   r…   N)r·   r²   rV   r°   r    r   r   r   Útest_checksCurrentDirectorys  s   z.VisualizeToolTests.test_checksCurrentDirectoryc                 C  s@   | j | jdgd� |  | j¡ | j | jdgd� |  | j¡ dS )z6
        Passing -q/--quiet hides all output.
        z--quietrº   z-qN)r·   r²   ÚassertFalser±   r    r   r   r   Útest_quietHidesOutput|  s   z(VisualizeToolTests.test_quietHidesOutputc                 C  s�   dD ]C}| j  ¡  g | _| j| j|dgd� |  tdd„ | jD ƒƒ¡ |  t| j j	ƒd¡ | j j	\}|  d 
| j¡|d ¡ |  | j j¡ qd	S )
ze
        Passing an empty string for --image-directory/-i disables
        rendering images.
        )ú--image-directoryz-ir…   rº   c                 s  ó   � | ]}d |v V  qdS ©ÚimageNr   ©rd   Úliner   r   r   Ú	<genexpr>�  ó   € z6VisualizeToolTests.test_onlySaveDot.<locals>.<genexpr>r   ú{}.dotÚfilenameN)r­   rš   r±   r·   r²   r¼   ÚanyrV   rz   r�   Úformatrœ   ©r!   Úargr   r   r   r   Útest_onlySaveDot…  s   

õz#VisualizeToolTests.test_onlySaveDotc                 C  sž   dD ]J}| j  ¡  g | _| j| j|dgd� |  tdd„ | jD ƒƒ¡ |  t| j j	ƒd¡ | j j	\}|  d 
| j¡|d ¡ |  |d	 ¡ |  | j j¡ qd
S )zc
        Passing an empty string for --dot-directory/-d disables saving dot
        files.
        )ú--dot-directoryz-dr…   rº   c                 s  r¿   ©r   Nr   rÂ   r   r   r   rÄ   ¡  rÅ   z8VisualizeToolTests.test_saveOnlyImage.<locals>.<genexpr>r   rÆ   rÇ   ÚcleanupN)r­   rš   r±   r·   r²   r¼   rÈ   rV   rz   rœ   rÉ   Ú
assertTruer�   rÊ   r   r   r   Útest_saveOnlyImage—  s   

ôz%VisualizeToolTests.test_saveOnlyImagec                 C  sÂ   d}d}| j | jd|d|gd� |  tdd„ | jD ƒƒ¡ |  tdd„ | jD ƒƒ¡ |  t| jjƒd	¡ | jj\}|  |d
 |¡ |  |d ¡ |  t| jj	ƒd	¡ | jj	\}|  |d
 |¡ dS )z’
        Passing different directories to --image-directory and --dot-directory
        writes images and dot files to those directories.
        rÁ   r   r¾   rÍ   rº   c                 s  r¿   rÀ   r   rÂ   r   r   r   rÄ   »  rÅ   zQVisualizeToolTests.test_saveDotAndImagesInDifferentDirectories.<locals>.<genexpr>c                 s  r¿   rÎ   r   rÂ   r   r   r   rÄ   ¼  rÅ   r   Ú	directoryrÏ   N)
r·   r²   rÐ   rÈ   r±   rV   rz   r­   rœ   r�   )r!   ZimageDirectoryZdotDirectoryÚ
renderCallZsaveCallr   r   r   Ú+test_saveDotAndImagesInDifferentDirectoriesª  s&   ûÿ


z>VisualizeToolTests.test_saveDotAndImagesInDifferentDirectoriesc                 C  sˆ   d}| j | jd|d|gd� |  tdd„ | jD ƒƒ¡ |  t| jjƒd¡ | jj\}|  |d |¡ |  	|d	 ¡ |  	t| jj
ƒ¡ d
S )z�
        Passing the same directory to --image-directory and --dot-directory
        writes images and dot files to that one directory.
        ZimagesAndDotr¾   rÍ   rº   c                 s  r¿   )zimage and dotNr   rÂ   r   r   r   rÄ   ×  rÅ   zJVisualizeToolTests.test_saveDotAndImagesInSameDirectory.<locals>.<genexpr>r   rÒ   rÏ   N)r·   r²   rÐ   rÈ   r±   rV   rz   r­   rœ   r¼   r�   )r!   rÒ   rÓ   r   r   r   Ú$test_saveDotAndImagesInSameDirectoryÇ  s   ûÿ

z7VisualizeToolTests.test_saveDotAndImagesInSameDirectory)NNNNN)r'   r(   r)   rK   rµ   r¶   r·   r»   r½   rÌ   rÑ   rÔ   rÕ   r   r   r   r   r¬   S  s    	
ÿ		r¬   )r5   r;   )'Ú
__future__r   rt   r   r   Údataclassesr   Útypingr   Zunittestr   r   Zautomatr   r   Z_methodicalr
   Z_typedr   Ztest_discoverr   r   r   r3   r4   r:   rG   rH   r1   r]   rc   rj   rn   r„   r”   r™   rŸ   rª   r¬   r   r   r   r   Ú<module>   sR    
$L