o
    >h'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?
    graphvizFT)
__import__ImportError r   r   |/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/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shell)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    




r1   )r
   objectr&   )r1   sor   r/   r   sampleMachine)   s
   r4   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r6   r7   )r(   r)   r*   r&   r   r   r   r   r5   G   s    r5   c                   @  s   e Zd ZdS )CoreN)r(   r)   r*   r   r   r   r   r;   I   s    r;   r6   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#   protor5   corer;   r6   intc                 S  r    )N   r   )r=   r>   r   r   r   builditS      z"sampleTypeMachine.<locals>.builditdatar%   Nsampler7   c                 S  r8   r9   r   )rD   r>   r   r   r   r'   [   rB   zsampleTypeMachine.<locals>.out)r=   r5   r>   r;   r6   r?   )rD   r5   r>   r;   r6   r7   )
r   r5   r;   r+   r.   r&   toreturnsr   build)builderr#   rA   rC   r%   r'   r   r   r   sampleTypeMachineL   s   



rI   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)
_visualizerK   )r"   rK   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assertEqualrK   r"   expectedr   r   r   test_sortsAttrsm   s   z!ElementMakerTests.test_sortsAttrsc              	   C  rN   )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>rO   z	a " quoter   za stringrS   NrW   rY   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>rO   NrW   rY   r   r   r   test_noAttrs   s   zElementMakerTests.test_noAttrsN)r(   r)   r*   __doc__rM   r[   r\   r]   r   r   r   r   rJ   a   s    rJ   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>)isLeafrb   )elementrj   r   ri   r   re      s   
re   c                 C  s   t | t S )z2
    This HTML element is actually leaf node.
    )
isinstancer_   rm   r   r   r   rl      s   rl   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)ra   rb   rc   )r_   )r"   ra   rb   rc   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)_E)rL   rr   
inputLabelport	functoolspartialrq   )r"   rr   r   r   r   rM      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 )Nrv   )rl   rc   getrv   ro   r!   r   r   hasPort   s   z3TableMakerTests.test_inputLabelRow.<locals>.hasPortzan output labelrv   r   r   tdN)
rr   ru   rv   assertGreaterlenrb   re   rX   ra   rl   )r"   rz   outputLabelstableinputLabelRow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.
        rs   r   r{   r   colspanN)rr   rv   rX   r~   rb   assertNotInrc   )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 2r{   r	   c                 S  s$   t |  o| jdko| jddkS )Nr|   r   rP   )rl   ra   rc   ry   ro   r   r   r   hasCorrectColspan   s
   
z@TableMakerTests.test_withOutputLabels.<locals>.hasCorrectColspanr   r   r   N)rr   ru   rv   rX   r~   rb   re   rl   )r"   r   inputRow	outputRowr   r   r   r   test_withOutputLabels   s   

z%TableMakerTests.test_withOutputLabelsN)	r(   r)   r*   r^   rq   rM   r   r   r   r   r   r   r   rp      s    
rp   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.
    r6   r7   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)
r4   	asDigraphjoinencoder   PopenPIPEcommunicaterX   
returncode)r"   digraphtextpr'   errr   r   r   test_validGraphviz   s
   
z#IntegrationTests.test_validGraphvizNr:   )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   r4   r   assertInr"   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   rI   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 r9   )resetr!   r   r   r   __init__/  s   zRecordsDigraphActions.__init__c                 C  s   g | _ g | _d S r9   )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 r9   )	_recorder)r"   recorderr   r   r   r   =     
zFakeDigraph.__init__c                 K     | j j| d S r9   )r   r   appendr"   kwargsr   r   r   render@     zFakeDigraph.renderc                 K  r   r9   )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   r9   _digraph)r"   r   r   r   r   r   L  r   zFakeMethodicalMachine.__init__c                 C  s   | j S r9   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-testignoredz	fake.fqpn)r   digraphRecorderr   fakeDigraphfakePrognamefakeSysPathcollectedOutputfakeFQPNr!   r   r   r   rM   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 r9   )r   r   )r"   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)	_progname_argv_syspath_findMachines_print)rL   r   r   r   r   r   r   )r"   prognameargvsyspath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   rX   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   rf   liner   r   r   	<genexpr>      z6VisualizeToolTests.test_onlySaveDot.<locals>.<genexpr>r   {}.dotfilenameN)r   r   r   r   r   r   anyrX   r~   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   rX   r~   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   rX   r~   r   r   r   )r"   imageDirectorydotDirectory
renderCall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.
        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   rX   r~   r   r   r   r   )r"   r   r   r   r   r   $test_saveDotAndImagesInSameDirectory  s   

z7VisualizeToolTests.test_saveDotAndImagesInSameDirectory)NNNNN)r(   r)   r*   rM   r   r   r   r   r   r   r   r   r   r   r   r   r   r   S  s    	
		r   )r6   r<   )'
__future__r   rw   r   r   dataclassesr   typingr   unittestr   r   automatr   r   _methodicalr
   _typedr   test_discoverr   r   r   r4   r5   r;   rI   rJ   r2   r_   re   rl   rp   r   r   r   r   r   r   r   r   r   r   <module>   sR    
$L