
    ~b6                     
   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y)"    )print_functionN)TestCaseskipIf   )MethodicalMachine   )isTwistedInstalledc                  :    	 t        d       y# t        $ r Y yw xY w)z2
    Is the graphviz Python module installed?
    graphvizTF)
__import__ImportError     >/usr/lib/python3/dist-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.begin.       r   c                      y)z	end stateNr   r#   s    r   endz'sampleMachine.<locals>.SampleObject.end1   r&   r   c                      y)zsample inputNr   r#   s    r   goz&sampleMachine.<locals>.SampleObject.go4   r&   r   c                      y)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    -   sz    	$		 
 			 
			 
			 
	

2sSE"r   r5   )r   objectr*   )r5   sor4   s     @r   sampleMachiner8   (   s0     
	B#v # 
BEEG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y)ElementMakerTestszL
    L{elementMaker} generates HTML representing the specified element.
    c                     ddl m} || _        y )Nr   )elementMaker)
_visualizer<   )r$   r<   s     r   setUpzElementMakerTests.setUpG   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assertEqualr<   r$   expecteds     r   test_sortsAttrsz!ElementMakerTests.test_sortsAttrsK   s7     4**5-0-0-0 + 2	3r   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 stringrD   NrH   rJ   s     r   test_quotesAttrsz"ElementMakerTests.test_quotesAttrsV   s8     D**5-8-.-7 + 9	:r   c                 J    d}| j                  || j                  d             y)zN
        L{elementMaker} should render an element with no attributes.
        z<div ></div>r@   NrH   rJ   s     r   test_noAttrszElementMakerTests.test_noAttrsc   s$     #4#4#4U#;<r   N)r-   r.   r/   __doc__r>   rL   rN   rP   r   r   r   r:   r:   @   s    )	3:=r   r:   c                   v    e Zd ZdZ ej
                         Z ej
                         Z ej
                         Zy)HTMLElementz2Holds an HTML element, as created by elementMaker.N)	r-   r.   r/   rQ   attribnamechildren
attributesr   r   r   rS   rS   k   s,    <4779DtwwyHJr   rS   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.
    )isLeafrW   findElements)element	predicatechildresults       r   r[   r[   s   sa    
 y		 !)):&ui8:  :F : : :s   Ac                 $    t        | t               S )z2
    This HTML element is actually leaf node.
    )
isinstancerS   r\   s    r   rZ   rZ      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)rV   rW   rX   )rS   )r$   rV   rW   rX   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)r=   rh   
inputLabelport	functoolspartialrf   )r$   rh   s     r   r>   zTableMakerTests.setUp   s1    +'	#++J/3/D/DF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 )Nrl   )rZ   rX   getrl   )r\   r$   s    r   hasPortz3TableMakerTests.test_inputLabelRow.<locals>.hasPort   s6    w' D**..v6$))CEr   zan output labelrl   r   r   tdN)
rh   rk   rl   assertGreaterlenrW   r[   rI   rV   rZ   )r$   rr   outputLabelstableinputLabelRowportCandidatess   `     r   test_inputLabelRowz"TableMakerTests.test_inputLabelRow   s    	E  "3!45 	0LOODOO\)- $ 4Es5>>2A6!NN1-M)%9NS0!4^A.33T:\-@"oo.0	0r   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.
        ri   r   rs   r   colspanN)rh   rl   rI   rv   rW   assertNotInrX   )r$   rx   ry   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 2rs   r   c                 |    t        |        xr/ | j                  dk(  xr | j                  j                  d      dk(  S )Nrt   r}   rA   )rZ   rV   rX   rq   rb   s    r   hasCorrectColspanz@TableMakerTests.test_withOutputLabels.<locals>.hasCorrectColspan   sC    w' A,A**..y9S@Br   r   r   r   N)rh   rk   rl   rI   rv   rW   r[   rZ   )r$   rx   inputRow	outputRowr   s        r   test_withOutputLabelsz%TableMakerTests.test_withOutputLabels   s      2D%)YY   0 	U^^,a0#nn)	B
 	\(4EFG	i8;K;K;M 	Nr   N)	r-   r.   r/   rQ   rf   r>   r{   r   r   r   r   r   rd   rd      s#    PF00>Nr   rd   z!Graphviz tools are not installed.c                       e Zd ZdZd Zy)IntegrationTestsz[
    Tests which make sure Graphviz can understand the output produced by
    Automat.
    c                 4   t        j                  dt         j                  t         j                        }|j                  dj	                  t               j                               j                  d            \  }}| j                  |j                  d       y)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   sh     U*//$.OO5==)B)B)D!E"(&/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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} 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   sU    
 113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y)RecordsDigraphActionsz/
    Records calls made to L{FakeDigraph}.
    c                 $    | j                          y N)resetr#   s    r   __init__zRecordsDigraphActions.__init__  s    

r   c                      g | _         g | _        y 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y)FakeDigraphzZ
    A fake L{graphviz.Digraph}.  Instantiate it with a
    L{RecordsDigraphActions}.
    c                     || _         y r   )	_recorder)r$   recorders     r   r   zFakeDigraph.__init__  s	    !r   c                 N    | j                   j                  j                  |       y r   )r   r   appendr$   kwargss     r   renderzFakeDigraph.render  s    ""))&1r   c                 N    | j                   j                  j                  |       y 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     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 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                   T    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.setUp/  sA    46&t';';<'%;!#r   c                 X    | j                   j                  dj                  |             y )N )r   r   r   )r$   argss     r   collectPrintsz VisualizeToolTests.collectPrints8  s    ##CHHTN3r   c              #   >   K   |t        | j                        f y wr   )r   r   )r$   fqpns     r   fakeFindMachinesz#VisualizeToolTests.fakeFindMachines;  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)r=   r   r   r   r   r   r   )r$   prognameargvsyspathfindMachinesprintr   s          r   r   zVisualizeToolTests.tool>  sZ     	&3$"3"3)4==/0 0 0&?$*?*?.D..0 	0r   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   rI   r   r#   s    r   test_checksCurrentDirectoryz.VisualizeToolTests.test_checksCurrentDirectoryL  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_quietHidesOutputU  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>h  s      !C%) ")D !C   r   {}.dotfilenameN)r   r   r   r   r   r   anyrI   rv   r   formatr   r$   argr   s      r   test_onlySaveDotz#VisualizeToolTests.test_onlySaveDot^  s    
 / 	?C  &&(#%D IIDMM33I4S !C-1-A-A!C C D S!5!5!?!?@!D**44GTX__T]];!*-/ 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      !C%) "'$ !Cr   r   r   r   cleanupN)r   r   r   r   r   r   r   rI   rv   r   r   
assertTruer   r   s      r   test_saveOnlyImagez%VisualizeToolTests.test_saveOnlyImager  s    
 - 	=C  &&(#%D IIDMM33I4S !C-1-A-A!C C D S!5!5!A!ABAF**66GTX__T]];!*-/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      > $ $tO >r   c              3   $   K   | ]  }d |v  
 ywr   r   r   s     r   r   zQVisualizeToolTests.test_saveDotAndImagesInDifferentDirectories.<locals>.<genexpr>  s      > $ "TM >r   r   	directoryr   N)
r   r   r   r   r   rI   rv   r   r   r   )r$   imageDirectorydotDirectory
renderCallsaveCalls        r   +test_saveDotAndImagesInDifferentDirectoriesz>VisualizeToolTests.test_saveDotAndImagesInDifferentDirectories  s$   
 !		+^)<9	 	: 	 >(,(<(<> > 	? >(,(<(<> > 	? 	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      > $ ,t3 >r   r   r   r   N)r   r   r   r   r   rI   rv   r   r   r   r   )r$   r   r   s      r   $test_saveDotAndImagesInSameDirectoryz7VisualizeToolTests.test_saveDotAndImagesInSameDirectory  s    
 #			+Y)96	 	7 	 >(,(<(<> > 	? 	T11==>B,,88K0)<I./T11;;<=r   )NNNNN)r-   r.   r/   r>   r   r   r   r   r   r   r   r   r   r   r   r   r   r   *  sE    
$4< 02/?(=*>2>r   r   )
__future__r   rm   r   r   unittestr   r   rT   _methodicalr   test_discoverr	   r   r   r8   r:   sr6   rS   r[   rZ   rd   r   r   r   r   r   r   r   r   r   <module>r     s   %  	  %  + -		0 %'')LM  "=>&= &= ? N&=R &  :0 %'')LM  "=>KNh KN ? NKN\ %'')LM!!#FG  "=>*x * ? H N*" %'')LM  "=>$ $ ? N$$
F 
0& 0 	F 	 %'')LM!!#FG  "=>E> E> ? H NE>r   