
    tf6                        d dl mZ d dlZd dlZd dlZd dlmZmZ d dlZddl	m
Z
 ddlmZ d Zd	 Zd
 Z e e             d           e e             d           G d de                                  Zej         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dS )"    )print_functionN)TestCaseskipIf   )MethodicalMachine   )isTwistedInstalledc                  H    	 t          d           dS # t          $ r Y dS w xY w)z2
    Is the graphviz Python module installed?
    graphvizTF)
__import__ImportError     [/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/automat/_test/test_visualize.pyisGraphvizModuleInstalledr      s@    : t    uu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      s]     799DAqHQKKK?5>>>>
s   A A.c                      t                       G fddt                    }  |             }|                                 S )zG
    Create a sample L{MethodicalMachine} with some sample states.
    c                   ,   e Zd Z                     d          d             Z                                 d             Z                                 d             Z                                 d             Z	e
                    eee	g           dS )#sampleMachine.<locals>.SampleObjectT)initialc                     dS )zinitial stateNr   selfs    r   beginz)sampleMachine.<locals>.SampleObject.begin.         r   c                     dS )z	end stateNr   r#   s    r   endz'sampleMachine.<locals>.SampleObject.end1   r&   r   c                     dS )zsample inputNr   r#   s    r   goz&sampleMachine.<locals>.SampleObject.go4   r&   r   c                     dS )zsample outputNr   r#   s    r   outz'sampleMachine.<locals>.SampleObject.out7   r&   r   N)__name__
__module____qualname__stater%   r(   inputr*   outputr,   upon)mms   r   SampleObjectr    -   s        	$			 	 
 				 	 
			 	 
			 	 
	

2sSE"""""r   r5   )r   objectr*   )r5   sor4   s     @r   sampleMachiner8   (   sc     
		B# # # # # # #v # # # 
BEEGGGIr   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dS )ElementMakerTestszL
    L{elementMaker} generates HTML representing the specified element.
    c                      ddl m} || _        d S )Nr   )elementMaker)
_visualizer<   )r$   r<   s     r   setUpzElementMakerTests.setUpG   s"    ------(r   c           	      d    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assertEqualr<   r$   expecteds     r   test_sortsAttrsz!ElementMakerTests.test_sortsAttrsK   sP     4**5-0-0-0 + 2 2	3 	3 	3 	3 	3r   c           	      d    d}|                      ||                     dddd                     dS )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 stringrD   NrH   rJ   s     r   test_quotesAttrsz"ElementMakerTests.test_quotesAttrsV   sQ     D**5-8-.-7 + 9 9	: 	: 	: 	: 	:r   c                 \    d}|                      ||                     d                     dS )zN
        L{elementMaker} should render an element with no attributes.
        z<div ></div>r@   NrH   rJ   s     r   test_noAttrszElementMakerTests.test_noAttrsc   s3     #4#4#4U#;#;<<<<<r   N)r-   r.   r/   __doc__r>   rL   rN   rP   r   r   r   r:   r:   @   sZ         ) ) )	3 	3 	3: : := = = = =r   r:   c                   l    e Zd ZdZ ej                    Z ej                    Z ej                    ZdS )HTMLElementz2Holds an HTML element, as created by elementMaker.N)	r-   r.   r/   rQ   attribnamechildren
attributesr   r   r   rS   rS   k   s;        <<4799DtwyyHJJJr   rS   c                 h     |           r| gS t          |           rg S fd| j        D             S )zk
    Recursively collect all elements in an L{HTMLElement} tree that
    match the optional predicate.
    c                 :    g | ]}t          |          D ]}|S r   )findElements).0childresult	predicates      r   
<listcomp>z findElements.<locals>.<listcomp>}   sH     : : :&ui88: :  : : : :r   )isLeafrW   )elementr_   s    `r   r[   r[   s   s`    
 y y	 	: : : : ): : : :r   c                 .    t          | t                     S )z2
    This HTML element is actually leaf node.
    )
isinstancerS   rb   s    r   ra   ra      s     ';////r   c                   0    e Zd ZdZd Zd Zd Zd Z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                 &    t          |||          S )N)rV   rW   rX   )rS   )r$   rV   rW   rX   s       r   fakeElementMakerz TableMakerTests.fakeElementMaker   s    xJOOOOr   c                 n    ddl m} d| _        d| _        t	          j        || j                  | _        d S )Nr   )
tableMakerinput labelzthe port)_E)r=   rk   
inputLabelport	functoolspartialri   )r$   rk   s     r   r>   zTableMakerTests.setUp   sJ    ++++++'	#+J/3/DF F Fr   c                      fd}g dgfD ]}                       j        | j                  }                     t	          |j                  d           |j        d         }t          ||          }                     t	          |          d                                |d         j        d                                t          |t                     j        g           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                 j    t          |            o"| j                            d          j        k    S )Nro   )ra   rX   getro   )rb   r$   s    r   hasPortz3TableMakerTests.test_inputLabelRow.<locals>.hasPort   s6    w' D*..v66$)CEr   zan output labelro   r   r   tdN)
rk   rn   ro   assertGreaterlenrW   r[   rI   rV   ra   )r$   ru   outputLabelstableinputLabelRowportCandidatess   `     r   test_inputLabelRowz"TableMakerTests.test_inputLabelRow   s   	E 	E 	E 	E 	E  "3!45 	0 	0LOODO\)- $ 4 4Es5>22A666!N1-M)%99NS00!444^A.3T:::\-@@"o.0 0 0 0	0 	0r   c                     |                      dd| j                  }|                     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.
        rl   r   rv   r   colspanN)rk   ro   rI   ry   rW   assertNotInrX   )r$   r{   r|   s      r   test_noOutputLabelsz#TableMakerTests.test_noOutputLabels   sf    
 r	BBU^,,a000 >M$<=====r   c                 l   |                      | j        d| j                  }|                     t	          |j                  d           |j        \  }}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 2rv   r   c                 t    t          |            o(| j        dk    o| j                            d          dk    S )Nrw   r   rA   )ra   rV   rX   rt   re   s    r   hasCorrectColspanz@TableMakerTests.test_withOutputLabels.<locals>.hasCorrectColspan   sC    w' A,A*..y99S@Br   r   r   r   N)rk   rn   ro   rI   ry   rW   r[   ra   )r$   r{   inputRow	outputRowr   s        r   test_withOutputLabelsz%TableMakerTests.test_withOutputLabels   s      2D%)Y   0 0 	U^,,a000#n)	B 	B 	B
 	\(4EFFGG	 	 	i88;K;K;M 	N 	N 	N 	N 	Nr   N)	r-   r.   r/   rQ   ri   r>   r~   r   r   r   r   r   rg   rg      st         P P PF F F0 0 00> > >N N N N Nr   rg   z!Graphviz tools are not installed.c                       e Zd ZdZd ZdS )IntegrationTestsz[
    Tests which make sure Graphviz can understand the output produced by
    Automat.
    c                 J   t          j        dt           j        t           j                  }|                    d                    t                                                                                    d                    \  }}|                     |j	        d           dS )z8
        L{graphviz} emits valid graphviz data.
        r   )r   stdout zutf-8r   N)
r   PopenPIPEcommunicatejoinr8   	asDigraphencoderI   
returncode)r$   pr,   errs       r   test_validGraphvizz#IntegrationTests.test_validGraphviz   s     U*/$.O5 5 5==)B)B)D)D!E!E"(&//3 3Sq)))))r   N)r-   r.   r/   rQ   r   r   r   r   r   r      s-         
* * * * *r   r   c                       e Zd ZdZd ZdS )
SpotCheckszj
    Tests to make sure that the output contains salient features of the machine
    being generated.
    c                    d                     t                                                                }|                     d|           |                     d|           |                     d|           |                     d|           dS )z
        The output of L{graphviz} should contain the names of the states,
        inputs, outputs in the state machine.
        r   r%   r(   r*   r,   N)r   r8   r   assertIn)r$   gvouts     r   test_containsMachineFeaturesz'SpotChecks.test_containsMachineFeatures   s{    
 113344gu%%%eU###dE"""eU#####r   N)r-   r.   r/   rQ   r   r   r   r   r   r      s-         
	$ 	$ 	$ 	$ 	$r   r   c                       e Zd ZdZd Zd ZdS )RecordsDigraphActionsz/
    Records calls made to L{FakeDigraph}.
    c                 .    |                                   d S N)resetr#   s    r   __init__zRecordsDigraphActions.__init__  s    

r   c                 "    g | _         g | _        d S r   )renderCalls	saveCallsr#   s    r   r   zRecordsDigraphActions.reset	  s    r   N)r-   r.   r/   rQ   r   r   r   r   r   r   r     s<               r   r   c                   $    e Zd ZdZd Zd Zd ZdS )FakeDigraphzZ
    A fake L{graphviz.Digraph}.  Instantiate it with a
    L{RecordsDigraphActions}.
    c                     || _         d S r   )	_recorder)r$   recorders     r   r   zFakeDigraph.__init__  s    !r   c                 D    | j         j                            |           d S r   )r   r   appendr$   kwargss     r   renderzFakeDigraph.render  s!    "))&11111r   c                 D    | j         j                            |           d S r   )r   r   r   r   s     r   savezFakeDigraph.save  s!     ''/////r   N)r-   r.   r/   rQ   r   r   r   r   r   r   r   r     sK         
" " "2 2 20 0 0 0 0r   r   c                       e Zd ZdZd Zd ZdS )FakeMethodicalMachinezL
    A fake L{MethodicalMachine}.  Instantiate it with a L{FakeDigraph}
    c                     || _         d S r   _digraph)r$   digraphs     r   r   zFakeMethodicalMachine.__init__#  s    r   c                     | j         S r   r   r#   s    r   r   zFakeMethodicalMachine.asDigraph&  s
    }r   N)r-   r.   r/   rQ   r   r   r   r   r   r   r     s<                  r   r   c                   V    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dS )VisualizeToolTestsc                     t                      | _        t          | j                  | _        d| _        dg| _        g | _        d| _        d S )Nz	tool-testignoredz	fake.fqpn)r   digraphRecorderr   fakeDigraphfakePrognamefakeSysPathcollectedOutputfakeFQPNr#   s    r   r>   zVisualizeToolTests.setUp/  sH    466&t';<<'%;!#r   c                 `    | j                             d                    |                     d S )N )r   r   r   )r$   argss     r   collectPrintsz VisualizeToolTests.collectPrints8  s)    ##CHHTNN33333r   c              #   :   K   |t          | j                  fV  d S r   )r   r   )r$   fqpns     r   fakeFindMachinesz#VisualizeToolTests.fakeFindMachines;  s)      )$*:;;;;;;;;r   Nc                 v    ddl m}  ||p| j        |p| j        g|p| j        |p| j        |p| j                  S )Nr   )tool)	_progname_argv_syspath_findMachines_print)r=   r   r   r   r   r   r   )r$   prognameargvsyspathfindMachinesprintr   s          r   r   zVisualizeToolTests.tool>  sg     	&%%%%%t3$"3)4=/0 0&?$*?.D.0 0 0 	0r   c                     |                      | j        g           |                     | 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   rI   r   r#   s    r   test_checksCurrentDirectoryz.VisualizeToolTests.test_checksCurrentDirectoryL  s@     				''')!,b11111r   c                     |                      | j        dg           |                     | j                   |                      | j        dg           |                     | j                   dS )z6
        Passing -q/--quiet hides all output.
        z--quietr   z-qN)r   r   assertFalser   r#   s    r   test_quietHidesOutputz(VisualizeToolTests.test_quietHidesOutputU  sn     			y1	222-...		t,	----.....r   c                    dD ] }| j                                          g | _        |                     | j        |dg           |                     t          d | j        D                                  |                     t          | j         j	                  d           | j         j	        \  }|                     d
                    | j                  |d                    |                     | j         j                   dS )	ze
        Passing an empty string for --image-directory/-i disables
        rendering images.
        )--image-directoryz-ir   r   c              3      K   | ]}d |v V  	dS imageNr   r\   lines     r   	<genexpr>z6VisualizeToolTests.test_onlySaveDot.<locals>.<genexpr>h  sB       !C !C%) ")D !C !C !C !C !C !Cr   r   {}.dotfilenameN)r   r   r   r   r   r   anyrI   ry   r   formatr   r$   argr   s      r   test_onlySaveDotz#VisualizeToolTests.test_onlySaveDot^  s&   
 / 	? 	?C &&(((#%D IIDM33I444S !C !C-1-A!C !C !C C C D D D S!5!?@@!DDD*4GTX__T];;!*-/ / / T1=>>>>	? 	?r   c                 D   dD ]}| j                                          g | _        |                     | j        |dg           |                     t          d | j        D                                  |                     t          | j         j	                  d           | j         j	        \  }|                     d
                    | j                  |d                    |                     |d                    |                     | j         j                   d	S )
zc
        Passing an empty string for --dot-directory/-d disables saving dot
        files.
        )--dot-directoryz-dr   r   c              3      K   | ]}d |v V  	dS r   Nr   r   s     r   r   z8VisualizeToolTests.test_saveOnlyImage.<locals>.<genexpr>|  sB       !C !C%) "'$ !C !C !C !C !C !Cr   r   r   r   cleanupN)r   r   r   r   r   r   r   rI   ry   r   r   
assertTruer   r   s      r   test_saveOnlyImagez%VisualizeToolTests.test_saveOnlyImager  s:   
 - 	= 	=C &&(((#%D IIDM33I444S !C !C-1-A!C !C !C C C D D D S!5!ABBAFFF*6GTX__T];;!*-/ / /OODO,,,T1;<<<<	= 	=r   c                    d}d}|                      | j        d|d|g           |                     t          d | j        D                                  |                     t          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              3      K   | ]}d |v V  	dS r   r   r   s     r   r   zQVisualizeToolTests.test_saveDotAndImagesInDifferentDirectories.<locals>.<genexpr>  s:       > > $ $tO > > > > > >r   c              3      K   | ]}d |v V  	dS r   r   r   s     r   r   zQVisualizeToolTests.test_saveDotAndImagesInDifferentDirectories.<locals>.<genexpr>  s:       > > $ "TM > > > > > >r   r   	directoryr   N)
r   r   r   r   r   rI   ry   r   r   r   )r$   imageDirectorydotDirectory
renderCallsaveCalls        r   +test_saveDotAndImagesInDifferentDirectoriesz>VisualizeToolTests.test_saveDotAndImagesInDifferentDirectories  st   
 !		+^)<9	 	: 	: 	: 	 > >(,(<> > > > > 	? 	? 	? > >(,(<> > > > > 	? 	? 	? 	T1=>>BBB,8K0.AAA
9-...T1;<<a@@@*4+.=====r   c                    d}|                      | j        d|d|g           |                     t          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              3      K   | ]}d |v V  	dS )zimage and dotNr   r   s     r   r   zJVisualizeToolTests.test_saveDotAndImagesInSameDirectory.<locals>.<genexpr>  s;       > > $ ,t3 > > > > > >r   r   r   r   N)r   r   r   r   r   rI   ry   r   r   r   r   )r$   r   r   s      r   $test_saveDotAndImagesInSameDirectoryz7VisualizeToolTests.test_saveDotAndImagesInSameDirectory  s   
 #			+Y)96	 	7 	7 	7 	 > >(,(<> > > > > 	? 	? 	? 	T1=>>BBB,8K0)<<<I.///T1;<<=====r   )NNNNN)r-   r.   r/   r>   r   r   r   r   r   r   r   r   r  r   r   r   r   r   *  s        
$ $ $4 4 4< < < 0 0 0 02 2 2/ / /? ? ?(= = =*> > >2> > > > >r   r   )
__future__r   rp   r   r   unittestr   r   rT   _methodicalr   test_discoverr	   r   r   r8   r:   sr6   rS   r[   ra   rg   r   r   r   r   r   r   r   r   r   <module>r     sC   % % % % % %     				     % % % % % % % %  + + + + + + - - - - - -	 	 		 	 	  0 %%''')LMM   "=>>&= &= &= &= &= &= &= ?> NM&=R     &   : : :0 0 0 %%''')LMM   "=>>KN KN KN KN KNh KN KN ?> NMKN\ %%''')LMM!!!#FGG   "=>>* * * * *x * * ?> HG NM*" %%''')LMM   "=>>$ $ $ $ $ $ $ ?> NM$$
 
 
 
 
F 
 
 
0 0 0 0 0& 0 0 0 	 	 	 	 	F 	 	 	 %%''')LMM!!!#FGG   "=>>E> E> E> E> E> E> E> ?> HG NME> E> E>r   