
    Vh'9                    T   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 Zd Zd Z G d de      Z G d d      Zd+dZ e e        d       e e        d       G d de
                    Ze G d de             Zd Zd Z e e        d       e e        d       G d de
                    Z  e e        d       e e        d       e e        d       G d d e
                           Z! e e        d       e e        d       G d! d"e
                    Z" G d# d$e      Z# G d% d&e      Z$ G d' d(e      Z% e e        d       e e        d       e e        d       G d) d*e
                           Z&y),    )annotationsN)	dataclass)Protocol)TestCaseskipIf)TypeMachineBuilderpep614   )MethodicalMachine)TypeMachine   )isTwistedInstalledc                 :    	 t        d       y# t        $ r Y yw xY w)z2
    Is the graphviz Python module installed?
    graphvizTF)
__import__ImportError     L/home/dcms/DCMS/lib/python3.12/site-packages/automat/_test/test_visualize.pyisGraphvizModuleInstalledr      s(    :   s    	c                     t        j                         \  } }t        j                  |       	 t        j                  d| d       t        j                  |        S # t        j                  |        w xY w)z+
    Are the graphviz tools installed?
    dotT)stdinshell)ospipeclose
subprocesscall)rws     r   isGraphvizInstalledr"      sM     779DAqHHQK??5>>
s   A A3c                 l    t                G fddt              }  |        }|j                          S )zG
    Create a sample L{MethodicalMachine} with some sample states.
    c                      e Zd ZW  j                  d      d        ZW  j                         d        ZW  j                         d        ZW  j                         d        Z	ej                  eee	g       y)#sampleMachine.<locals>.SampleObjectT)initialc                     y)zinitial stateNr   selfs    r   beginz)sampleMachine.<locals>.SampleObject.begin0       r   c                     y)z	end stateNr   r(   s    r   endz'sampleMachine.<locals>.SampleObject.end4   r+   r   c                     y)zsample inputNr   r(   s    r   goz&sampleMachine.<locals>.SampleObject.go8   r+   r   c                     y)zsample outputNr   r(   s    r   outz'sampleMachine.<locals>.SampleObject.out<   r+   r   N)__name__
__module____qualname__stater*   r-   inputr/   outputr1   upon)mms   r   SampleObjectr%   /   s    	$		 
 	 
	 
	 
	 
	 
	 
	 	

2sSE"r   r:   )r   objectr/   )r:   sor9   s     @r   sampleMachiner=   )   s0     
	B#v #& 
BEEGIr   c                      e Zd ZddZy)Samplec                     y Nr   r(   s    r   r/   z	Sample.goH   r+   r   NreturnNone)r2   r3   r4   r/   r   r   r   r?   r?   G   s    r   r?   c                      e Zd Zy)CoreN)r2   r3   r4   r   r   r   rF   rF   I   s    r   rF   c                 *   t        t        t              } | j                  d      }dd}| j                  d|      }| j                  d      }|j	                  t        j
                        j                  |      j                  d       |j	                  t        j
                        j                  |      j                  d       t        |j	                  t        j
                        j                  |            dd       }| j                         S )	zA
    Create a sample L{TypeMachine} with some sample states.
    r*   c                     y)N   r   )protocores     r   builditz"sampleTypeMachine.<locals>.builditS   s    r   datar-   Nc                     y rA   r   )samplerK   s     r   r1   zsampleTypeMachine.<locals>.out[   s    14r   )rJ   r?   rK   rF   rC   int)rO   r?   rK   rF   rC   rD   )
r   r?   rF   r5   r8   r/   toreturnsr	   build)builderr*   rL   rM   r-   r1   s         r   sampleTypeMachinerU   L   s     !.GMM'"E ==)D
--
C	JJvyyT"**40IIfiiC ((.CHHVYY""5)*4 +4==?r   z!Graphviz module is not installed.zTwisted is not installed.c                  (    e Zd ZdZd Zd Zd Zd Zy)ElementMakerTestszL
    L{elementMaker} generates HTML representing the specified element.
    c                    ddl m} || _        y )Nr
   )elementMaker)
_visualizerY   )r)   rY   s     r   setUpzElementMakerTests.setUph   s    -(r   c           	     R    d}| j                  || j                  dddd             y)zK
        L{elementMaker} orders HTML attributes lexicographically.
        z<div a="1" b="2" c="3"></div>div213bacNassertEqualrY   r)   expecteds     r   test_sortsAttrsz!ElementMakerTests.test_sortsAttrsm   s-     44#4#4UcSC#4#PQr   c           	     R    d}| j                  || j                  dddd             y)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>r]   z	a " quoter   za stringra   Nre   rg   s     r   test_quotesAttrsz"ElementMakerTests.test_quotesAttrst   s1     Dd''Z'P	
r   c                J    d}| j                  || j                  d             y)zN
        L{elementMaker} should render an element with no attributes.
        z<div ></div>r]   Nre   rg   s     r   test_noAttrszElementMakerTests.test_noAttrs   s$     #4#4#4U#;<r   N)r2   r3   r4   __doc__r[   ri   rk   rm   r   r   r   rW   rW   a   s    )
R	
=r   rW   c                  0    e Zd ZU dZded<   ded<   ded<   y)	HTMLElementz2Holds an HTML element, as created by elementMaker.strnamezlist[HTMLElement]childrenzdict[str, str]
attributesN)r2   r3   r4   rn   __annotations__r   r   r   rp   rp      s    <
Ir   rp   c                     ||       r| gS t        |       rg S | j                  D cg c]  }t        ||      D ]  }|  c}}S c c}}w )zk
    Recursively collect all elements in an L{HTMLElement} tree that
    match the optional predicate.
    )isLeafrs   findElements)element	predicatechildresults       r   rx   rx      sd    
 y		 %%"5)4  	  s   Ac                $    t        | t               S )z2
    This HTML element is actually leaf node.
    )
isinstancerp   ry   s    r   rw   rw      s     ';///r   c                  .    e Zd ZdZd Zd Zd Zd Zd Zy)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                    t        |||      S )N)rr   rs   rt   )rp   )r)   rr   rs   rt   s       r   fakeElementMakerz TableMakerTests.fakeElementMaker   s    xJOOr   c                x    ddl m} d| _        d| _        t	        j
                  || j                        | _        y )Nr
   )
tableMakerinput labelzthe port)_E)rZ   r   
inputLabelport	functoolspartialr   )r)   r   s     r   r[   zTableMakerTests.setUp   s-    +'	#++J4;P;PQr   c                     fd}g dgfD ]  } j                   j                  | j                        } j                  t	        |j
                        d       |j
                  d   }t        ||      } j                  t	        |      d        j                  |d   j                  d        j                  t        |t               j                  g        y)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                p    t        |        xr( | j                  j                  d      j                  k(  S )Nr   )rw   rt   getr   )ry   r)   s    r   hasPortz3TableMakerTests.test_inputLabelRow.<locals>.hasPort   s/    g&V7+=+=+A+A&+ITYY+VVr   zan output labelr   r   r   tdN)
r   r   r   assertGreaterlenrs   rx   rf   rr   rw   )r)   r   outputLabelstableinputLabelRowportCandidatess   `     r   test_inputLabelRowz"TableMakerTests.test_inputLabelRow   s    	W  "3!45 		ULOODOO\		OREs5>>2A6!NN1-M)%9NS0!4^A.33T:\-@4??BST		Ur   c                    | j                  dd| j                        }| j                  t        |j                        d       |j                  \  }| j                  d|j                         y)z
        L{tableMaker} does not add a colspan attribute to the input
        label's cell or a second row if there no output labels.
        r   r   r   r   colspanN)r   r   rf   r   rs   assertNotInrt   )r)   r   r   s      r   test_noOutputLabelsz#TableMakerTests.test_noOutputLabels   sW    
 r		BU^^,a0 >>M$<$<=r   c                P   | j                  | j                  d| j                        }| j                  t	        |j
                        d       |j
                  \  }}d }| j                  t	        t        ||            d       | j                  t        |t              ddg       y)	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                |    t        |        xr/ | j                  dk(  xr | j                  j                  d      dk(  S )Nr   r   r^   )rw   rr   rt   r   r   s    r   hasCorrectColspanz@TableMakerTests.test_withOutputLabels.<locals>.hasCorrectColspan   s@    7O# =LLD(=&&**95<r   r   r   r   N)r   r   r   rf   r   rs   rx   rw   )r)   r   inputRow	outputRowr   s        r   test_withOutputLabelsz%TableMakerTests.test_withOutputLabels   s     OOA		   
 	U^^,a0#nn)	 	\(4EFGKF+.>@P-Q	
r   N)	r2   r3   r4   rn   r   r[   r   r   r   r   r   r   r   r      s#    PRU*>
r   r   z!Graphviz tools are not installed.c                      e Zd ZdZddZy)IntegrationTestsz[
    Tests which make sure Graphviz can understand the output produced by
    Automat.
    c                <   t               j                         }dj                  |      j                  d      }t	        j
                  dt        j                  t        j                        }|j                  |      \  }}| j                  |j                  d       y)z8
        C{graphviz} emits valid graphviz data.
         zutf-8r   )r   stdoutr   N)
r=   	asDigraphjoinencoder   PopenPIPEcommunicaterf   
returncode)r)   digraphtextpr1   errs         r   test_validGraphvizz#IntegrationTests.test_validGraphviz   so      /++-www&&w/U*//*//R==&Sq)r   NrB   )r2   r3   r4   rn   r   r   r   r   r   r      s    
*r   r   c                      e Zd ZdZd Zd Zy)
SpotCheckszj
    Tests to make sure that the output contains salient features of the machine
    being generated.
    c                    dj                  t               j                               }| j                  d|       | j                  d|       | j                  d|       | j                  d|       y)z
        The output of L{graphviz.Digraph} should contain the names of the
        states, inputs, outputs in the state machine.
        r   r*   r-   r/   r1   N)r   r=   r   assertInr)   gvouts     r   test_containsMachineFeaturesz'SpotChecks.test_containsMachineFeatures  sU    
 1134gu%eU#dE"eU#r   c                   dj                  t               j                               }| j                  d|       | j                  d|       | j                  d|       | j                  d|       | j                  d|       y)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:builditr1   N)r   rU   r   r   r   s     r    test_containsTypeMachineFeaturesz+SpotChecks.test_containsTypeMachineFeatures  se    
 )+5578gu%eU#dE"ne,eU#r   N)r2   r3   r4   rn   r   r   r   r   r   r   r   
  s    
	$
$r   r   c                      e Zd ZdZd Zd Zy)RecordsDigraphActionsz/
    Records calls made to L{FakeDigraph}.
    c                $    | j                          y rA   )resetr(   s    r   __init__zRecordsDigraphActions.__init__/  s    

r   c                     g | _         g | _        y rA   )renderCalls	saveCallsr(   s    r   r   zRecordsDigraphActions.reset2  s    r   N)r2   r3   r4   rn   r   r   r   r   r   r   r   *  s    r   r   c                  "    e Zd ZdZd Zd Zd Zy)FakeDigraphzZ
    A fake L{graphviz.Digraph}.  Instantiate it with a
    L{RecordsDigraphActions}.
    c                    || _         y rA   )	_recorder)r)   recorders     r   r   zFakeDigraph.__init__=  s	    !r   c                N    | j                   j                  j                  |       y rA   )r   r   appendr)   kwargss     r   renderzFakeDigraph.render@  s    ""))&1r   c                N    | j                   j                  j                  |       y rA   )r   r   r   r   s     r   savezFakeDigraph.saveC  s      ''/r   N)r2   r3   r4   rn   r   r   r   r   r   r   r   r   7  s    
"20r   r   c                      e Zd ZdZd Zd Zy)FakeMethodicalMachinezL
    A fake L{MethodicalMachine}.  Instantiate it with a L{FakeDigraph}
    c                    || _         y rA   _digraph)r)   r   s     r   r   zFakeMethodicalMachine.__init__L  s	    r   c                    | j                   S rA   r   r(   s    r   r   zFakeMethodicalMachine.asDigraphO  s    }}r   N)r2   r3   r4   rn   r   r   r   r   r   r   r   G  s     r   r   c                  L    e Zd Zd Zd Zd Z	 ddZd Zd Zd Z	d	 Z
d
 Zd Zy)VisualizeToolTestsc                    t               | _        t        | j                        | _        d| _        dg| _        g | _        d| _        y )Nz	tool-testignoredz	fake.fqpn)r   digraphRecorderr   fakeDigraphfakePrognamefakeSysPathcollectedOutputfakeFQPNr(   s    r   r[   zVisualizeToolTests.setUpW  sA    46&t';';<'%;!#r   c                X    | j                   j                  dj                  |             y )N )r   r   r   )r)   argss     r   collectPrintsz VisualizeToolTests.collectPrints`  s    ##CHHTN3r   c              #  >   K   |t        | j                        f y wrA   )r   r   )r)   fqpns     r   fakeFindMachinesz#VisualizeToolTests.fakeFindMachinesc  s     )$*:*:;;;s   Nc                    ddl m}  ||xs | j                  |xs | j                  g|xs | j                  |xs | j
                  |xs | j                        S )Nr
   )tool)	_progname_argv_syspath_findMachines_print)rZ   r   r   r   r   r   r   )r)   prognameargvsyspathfindMachinesprintr   s          r   r   zVisualizeToolTests.toolf  sZ     	&3$"3"3)4==/0 0 0&?$*?*?.D..
 	
r   c                |    | j                  | j                  g       | j                  | j                  d   d       y)z
        L{tool} adds '' to sys.path to ensure
        L{automat._discover.findMachines} searches the current
        directory.
        r   r   r   N)r   r   rf   r   r(   s    r   test_checksCurrentDirectoryz.VisualizeToolTests.test_checksCurrentDirectorys  s3     				'))!,b1r   c                    | j                  | j                  dg       | j                  | j                         | j                  | j                  dg       | j                  | j                         y)z6
        Passing -q/--quiet hides all output.
        z--quietr   z-qN)r   r   assertFalser   r(   s    r   test_quietHidesOutputz(VisualizeToolTests.test_quietHidesOutput|  sZ     			y1	2--.		t,	---.r   c                    dD ]  }| j                   j                          g | _        | j                  | j                  |dg       | j                  t        d | j                  D                     | j                  t        | j                   j                        d       | j                   j                  \  }| j                  dj                  | j                        |d          | j                  | j                   j                          y)	ze
        Passing an empty string for --image-directory/-i disables
        rendering images.
        )--image-directoryz-ir   r   c              3  $   K   | ]  }d |v  
 ywimageNr   .0lines     r   	<genexpr>z6VisualizeToolTests.test_onlySaveDot.<locals>.<genexpr>  s      RTD R   r   {}.dotfilenameN)r   r   r   r   r   r   anyrf   r   r   formatr   r)   argr   s      r   test_onlySaveDotz#VisualizeToolTests.test_onlySaveDot  s    
 / 	?C  &&(#%D IIDMM33I4S RT=Q=Q RRSS!5!5!?!?@!D**44GTX__T]];T*=MNT11==>	?r   c                H   dD ]  }| j                   j                          g | _        | j                  | j                  |dg       | j                  t        d | j                  D                     | j                  t        | j                   j                        d       | j                   j                  \  }| j                  dj                  | j                        |d          | j                  |d          | j                  | j                   j                          y	)
zc
        Passing an empty string for --dot-directory/-d disables saving dot
        files.
        )--dot-directoryz-dr   r   c              3  $   K   | ]  }d |v  
 ywr   Nr   r  s     r   r  z8VisualizeToolTests.test_saveOnlyImage.<locals>.<genexpr>  s      P4$ Pr  r   r  r  cleanupN)r   r   r   r   r   r   r  rf   r   r   r	  
assertTruer   r
  s      r   test_saveOnlyImagez%VisualizeToolTests.test_saveOnlyImage  s    
 - 	=C  &&(#%D IIDMM33I4S P4;O;O PPQS!5!5!A!ABAF**66GTX__T]];T*=MNOODO,T11;;<	=r   c                   d}d}| j                  | j                  d|d|g       | j                  t        d | j                  D                     | j                  t        d | j                  D                     | j                  t        | j                  j                        d       | j                  j                  \  }| j                  |d	   |       | j                  |d
          | j                  t        | j                  j                        d       | j                  j                  \  }| j                  |d	   |       y)z
        Passing different directories to --image-directory and --dot-directory
        writes images and dot files to those directories.
        r   r   r   r  r   c              3  $   K   | ]  }d |v  
 ywr   r   r  s     r   r  zQVisualizeToolTests.test_saveDotAndImagesInDifferentDirectories.<locals>.<genexpr>  s     MGtOMr  c              3  $   K   | ]  }d |v  
 ywr  r   r  s     r   r  zQVisualizeToolTests.test_saveDotAndImagesInDifferentDirectories.<locals>.<genexpr>  s     KdETMKr  r   	directoryr  N)
r   r   r  r  r   rf   r   r   r   r   )r)   imageDirectorydotDirectory
renderCallsaveCalls        r   +test_saveDotAndImagesInDifferentDirectoriesz>VisualizeToolTests.test_saveDotAndImagesInDifferentDirectories  s   
 !		#! 	 	
 	M8L8LMMNKd6J6JKKLT11==>B,,88K0.A
9-.T11;;<a@**44+.=r   c                   d}| j                  | j                  d|d|g       | j                  t        d | j                  D                     | j                  t        | j                  j                        d       | j                  j                  \  }| j                  |d   |       | j                  |d          | j                  t        | j                  j                               y	)
z
        Passing the same directory to --image-directory and --dot-directory
        writes images and dot files to that one directory.
        imagesAndDotr   r  r   c              3  $   K   | ]  }d |v  
 yw)zimage and dotNr   r  s     r   r  zJVisualizeToolTests.test_saveDotAndImagesInSameDirectory.<locals>.<genexpr>  s     UOt3Ur  r   r  r  N)r   r   r  r  r   rf   r   r   r   r   r   )r)   r  r  s      r   $test_saveDotAndImagesInSameDirectoryz7VisualizeToolTests.test_saveDotAndImagesInSameDirectory  s    
 #			#! 	 	
 	U@T@TUUVT11==>B,,88K0)<I./T11;;<=r   )NNNNN)r2   r3   r4   r[   r   r   r   r   r   r  r  r  r   r   r   r   r   r   S  s;    $4< PT
2/?$=&>:>r   r   )rC   zTypeMachine[Sample, Core])'
__future__r   r   r   r   dataclassesr   typingr   unittestr   r   automatr   r	   _methodicalr   _typedr   test_discoverr   r   r"   r=   r?   rF   rU   rW   r;   rp   rx   rw   r   r   r   r   r   r   r   r   r   r   <module>r)     s   "  	  !  % . +   -		<X  * %'')LM  "=>!= != ? N!=H &  "0 %'')LM  "=>I
h I
 ? NI
X %'')LM!!#FG  "=>*x * ? H N*" %'')LM  "=>$ $ ? N$<
F 
0& 0 	F 	 %'')LM!!#FG  "=>H> H> ? H NH>r   