+
    Qi'9                       ^ RI Ht ^ RIt^ RIt^ RIt^ RIHt ^ RIHt ^ RI	H
t
Ht ^ RIHtHt ^RIHt ^RIHt ^R	IHt R
 tR tR t ! R R]4      t ! R R4      tR R lt]! ]! 4       '       * R4      ]! ]! 4       '       * R4       ! R R]
4      4       4       t] ! R R]4      4       tR tR t]! ]! 4       '       * R4      ]! ]! 4       '       * R4       ! R R]
4      4       4       t ]! ]! 4       '       * R4      ]! ]! 4       '       * R4      ]! ]! 4       '       * R4       ! R R]
4      4       4       4       t!]! ]! 4       '       * R4      ]! ]! 4       '       * R4       ! R  R!]
4      4       4       t" ! R" R#]4      t# ! R$ R%]4      t$ ! R& R']4      t%]! ]! 4       '       * R4      ]! ]! 4       '       * R4      ]! ]! 4       '       * R4       ! R( R)]
4      4       4       4       t&R# )*    )annotationsN)	dataclass)Protocol)TestCaseskipIf)TypeMachineBuilderpep614)MethodicalMachine)TypeMachine)isTwistedInstalledc                 B     \        R4       R#   \         d     R# i ; i)z*
Is the graphviz Python module installed?
graphvizTF)
__import__ImportError     >/usr/lib/python3/dist-packages/automat/_test/test_visualize.pyisGraphvizModuleInstalledr      s(    :   s    c                     \         P                  ! 4       w  r\         P                  ! V4        \        P                  ! RV RR7      '       * \         P                  ! V 4       #   \         P                  ! T 4       i ; i)z#
Are the graphviz tools installed?
dotT)stdinshell)ospipeclose
subprocesscall)rws     r   isGraphvizInstalledr       sK     779DAHHQK??5>>
s   A# #A;c                 n   a \        4       o ! V3R lR\        4      p V ! 4       pVP                  4        S# )z?
Create a sample L{MethodicalMachine} with some sample states.
c                     < ] tR t^/t Z P	                  RR7      R 4       t Z P	                  4       R 4       t Z P                  4       R 4       t Z P                  4       R 4       t
]P                  ]]]
.4       RtR# )	#sampleMachine.<locals>.SampleObjectT)initialc                    R# )zinitial stateNr   selfs   &r   begin)sampleMachine.<locals>.SampleObject.begin0       r   c                    R# )z	end stateNr   r&   s   &r   end'sampleMachine.<locals>.SampleObject.end4   r*   r   c                    R# )zsample inputNr   r&   s   &r   go&sampleMachine.<locals>.SampleObject.go8   r*   r   c                    R# )zsample outputNr   r&   s   &r   out'sampleMachine.<locals>.SampleObject.out<   r*   r   r   N)__name__
__module____qualname____firstlineno__stater(   r,   inputr/   outputr2   upon__static_attributes__)mms   r   SampleObjectr#   /   s    	$		 
 	 
	 
	 
	 
	 
	 
	 	

2sSE""r   r>   )r
   objectr/   )r>   sor=   s     @r   sampleMachinerA   )   s0     
	B#v #& 
BEEGIr   c                  "    ] tR t^GtR R ltRtR# )Samplec                   V ^8  d   QhRR/#    returnNoner   )formats   "r   __annotate__Sample.__annotate__H   s    Dr   c                	    R # Nr   r&   s   &r   r/   	Sample.goH   s    #r   r   N)r4   r5   r6   r7   r/   r<   r   r   r   rC   rC   G   s    r   rC   c                      ] tR t^ItRtR# )Corer   N)r4   r5   r6   r7   r<   r   r   r   rP   rP   I   s    Cr   rP   c                   V ^8  d   QhRR/# )rF   rG   zTypeMachine[Sample, Core]r   )rI   s   "r   rJ   rJ   L   s      4 r   c                 2   \        \        \        4      p V P                  R4      pR R lpV P                  RV4      pV P                  R4      pVP	                  \        P
                  4      P                  V4      P                  R4       VP	                  \        P
                  4      P                  V4      P                  R4       \        VP	                  \        P
                  4      P                  V4      4      R R l4       pV P                  4       # )	z9
Create a sample L{TypeMachine} with some sample states.
r(   c               $    V ^8  d   QhRRRRRR/# )rF   protorC   corerP   rG   intr   )rI   s   "r   rJ   'sampleTypeMachine.<locals>.__annotate__S   s!      v T c r   c                    ^# )   r   )rT   rU   s   &&r   buildit"sampleTypeMachine.<locals>.builditS   s    r   datar,   Nc               $    V ^8  d   QhRRRRRR/# )rF   samplerC   rU   rP   rG   rH   r   )rI   s   "r   rJ   rW   \   s    44F4$444r   c                    R # rM   r   )r^   rU   s   &&r   r2   sampleTypeMachine.<locals>.out[   s    14r   )
r   rC   rP   r8   r;   r/   toreturnsr	   build)builderr(   rZ   r\   r,   r2   s         r   sampleTypeMachinere   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                  2    ] tR t^atRtR tR tR tR tRt	R# )ElementMakerTestszD
L{elementMaker} generates HTML representing the specified element.
c                	    ^RI Hp Wn        R# )rF   elementMakerN)
_visualizerj   )r'   rj   s   & r   setUpElementMakerTests.setUph   s    -(r   c           
     R    RpV P                  WP                  RRRRR7      4       R# )z;
L{elementMaker} orders HTML attributes lexicographically.
z<div a="1" b="2" c="3"></div>div213bacNassertEqualrj   r'   expecteds   & r   test_sortsAttrs!ElementMakerTests.test_sortsAttrsm   s+     4#4#4UcSC#4#PQr   c           
     R    RpV P                  WP                  RR^RR7      4       R# )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 " quoteza stringrs   Nrw   ry   s   & r   test_quotesAttrs"ElementMakerTests.test_quotesAttrst   s/     D''Z'P	
r   c                J    RpV P                  WP                  R4      4       R# )z>
L{elementMaker} should render an element with no attributes.
z<div ></div>ro   Nrw   ry   s   & r   test_noAttrsElementMakerTests.test_noAttrs   s"     ##4#4U#;<r   ri   N)
r4   r5   r6   r7   __doc__rl   r{   r~   r   r<   r   r   r   rg   rg   a   s    )
R	
=r   rg   c                  :    ] tR t^t$ RtR]R&   R]R&   R]R&   RtR	# )
HTMLElementz2Holds an HTML element, as created by elementMaker.strnamezlist[HTMLElement]childrenzdict[str, str]
attributesr   N)r4   r5   r6   r7   r   __annotations__r<   r   r   r   r   r      s    <
Ir   r   c                    V! V 4      '       d   V .# \        V 4      '       d   . # V P                   UUu. uF  p\        W!4       F  pVNK  	  K  	  upp# u uppi )z_
Recursively collect all elements in an L{HTMLElement} tree that
match the optional predicate.
)isLeafr   findElements)element	predicatechildresults   &&  r   r   r      sd    
 y		 %%%E"54F 	4 	%  s   Ac                ,    \        V \        4      '       * # )z*
This HTML element is actually leaf node.
)
isinstancer   r   s   &r   r   r      s     ';///r   c                  8    ] tR t^tRtR tR tR tR tR t	Rt
R# )	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                	    \        WVR 7      # ))r   r   r   )r   )r'   r   r   r   s   &&*,r   fakeElementMaker TableMakerTests.fakeElementMaker   s    JOOr   c                	z    ^RI Hp RV n        RV n        \        P
                  ! WP                  R7      V n        R# )rF   )
tableMakerinput labelzthe port)_EN)rk   r   
inputLabelport	functoolspartialr   )r'   r   s   & r   rl   TableMakerTests.setUp   s+    +'	#++J;P;PQr   c                  a  V 3R lp. R.3 F  pS P                  S P                  VS P                  R7      pS P                  \	        VP
                  4      ^ 4       VP
                  ^ ,          p\        W14      pS P                  \	        V4      ^4       S P                  V^ ,          P                  R4       S P                  \        V\        4      S P                  .4       K  	  R# )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                   < \        V 4      '       * ;'       d)    V P                  P                  R 4      SP                  8H  # r   )r   r   getr   )r   r'   s   &r   hasPort3TableMakerTests.test_inputLabelRow.<locals>.hasPort   s2    g&VV7+=+=+A+A&+ITYY+VVr   zan output labelr   tdN)
r   r   r   assertGreaterlenr   r   rx   r   r   )r'   r   outputLabelstableinputLabelRowportCandidatess   f     r   test_inputLabelRow"TableMakerTests.test_inputLabelRow   s    	W  "3!45LOODOO\		OREs5>>2A6!NN1-M)%9NS0!4^A.33T:\-@4??BST 6r   c                    V P                  RRV P                  R7      pV P                  \        VP                  4      ^4       VP                  w  pV P                  RVP                  4       R# )zu
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   colspanNr   )r   r   rx   r   r   assertNotInr   )r'   r   r   s   &  r   test_noOutputLabels#TableMakerTests.test_noOutputLabels   sW    
 r		BU^^,a0 >>M$<$<=r   c                N   V P                  V P                  RV P                  R7      pV P                  \	        VP
                  4      ^4       VP
                  w  r#R pV P                  \	        \        W$4      4      ^4       V P                  \        V\        4      RR.4       R# )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   c                    \        V 4      '       * ;'       d6    V P                  R 8H  ;'       d    V P                  P                  R4      R8H  # )r   r   rp   )r   r   r   r   r   s   &r   hasCorrectColspan@TableMakerTests.test_withOutputLabels.<locals>.hasCorrectColspan   sJ    7O# = =LLD(= =&&**95<r   N)r   r   )r   r   r   rx   r   r   r   r   )r'   r   inputRow	outputRowr   s   &    r   test_withOutputLabels%TableMakerTests.test_withOutputLabels   s     OOA		   
 	U^^,a0#nn	 	\(FGKF+.>@P-Q	
r   )r   r   r   N)r4   r5   r6   r7   r   r   rl   r   r   r   r<   r   r   r   r   r      s#    PRU*>
r   r   z!Graphviz tools are not installed.c                  &    ] tR t^tRtR R ltRtR# )IntegrationTestszO
Tests which make sure Graphviz can understand the output produced by
Automat.
c                   V ^8  d   QhRR/# rE   r   )rI   s   "r   rJ   IntegrationTests.__annotate__   s     * *D *r   c                >   \        4       P                  4       pRP                  V4      P                  R4      p\        P
                  ! R\        P                  \        P                  R7      pVP                  V4      w  rEV P                  VP                  ^ 4       R# )z(
C{graphviz} emits valid graphviz data.
 zutf-8r   )r   stdoutN)
rA   	asDigraphjoinencoder   PopenPIPEcommunicaterx   
returncode)r'   digraphtextpr2   errs   &     r   test_validGraphviz#IntegrationTests.test_validGraphviz   sm      /++-www&&w/U*//*//R==&q)r   r   N)r4   r5   r6   r7   r   r   r<   r   r   r   r   r      s    
* *r   r   c                  &    ] tR tRtRtR tR tRtR# )
SpotChecksi
  z^
Tests to make sure that the output contains salient features of the machine
being generated.
c                    RP                  \        4       P                  4       4      pV P                  RV4       V P                  RV4       V P                  RV4       V P                  RV4       R# )zq
The output of L{graphviz.Digraph} should contain the names of the
states, inputs, outputs in the state machine.
r   r(   r,   r/   r2   N)r   rA   r   assertInr'   gvouts   & r   test_containsMachineFeatures'SpotChecks.test_containsMachineFeatures  sU    
 1134gu%eU#dE"eU#r   c                   RP                  \        4       P                  4       4      pV P                  RV4       V P                  RV4       V P                  RV4       V P                  RV4       V P                  RV4       R# )zq
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:builditr2   N)r   re   r   r   r   s   & r    test_containsTypeMachineFeatures+SpotChecks.test_containsTypeMachineFeatures  se    
 )+5578gu%eU#dE"ne,eU#r   r   N)r4   r5   r6   r7   r   r   r   r<   r   r   r   r   r   
  s    
	$
$r   r   c                  &    ] tR tRtRtR tR tRtR# )RecordsDigraphActionsi*  z'
Records calls made to L{FakeDigraph}.
c                	&    V P                  4        R # rM   )resetr&   s   &r   __init__RecordsDigraphActions.__init__/  s    

r   c                	"    . V n         . V n        R # rM   renderCalls	saveCallsr&   s   &r   r   RecordsDigraphActions.reset2  s    r   r   N)r4   r5   r6   r7   r   r   r   r<   r   r   r   r   r   *  s    r   r   c                  ,    ] tR tRtRtR tR tR tRtR# )FakeDigraphi7  zN
A fake L{graphviz.Digraph}.  Instantiate it with a
L{RecordsDigraphActions}.
c                	    Wn         R # rM   	_recorder)r'   recorders   &&r   r   FakeDigraph.__init__=  s    !r   c                	P    V P                   P                  P                  V4       R # rM   )r   r   appendr'   kwargss   &,r   renderFakeDigraph.render@  s    ""))&1r   c                	P    V P                   P                  P                  V4       R # rM   )r   r   r   r   s   &,r   saveFakeDigraph.saveC  s      ''/r   r   N)	r4   r5   r6   r7   r   r   r   r   r<   r   r   r   r   r   7  s    
"20r   r   c                  &    ] tR tRtRtR tR tRtR# )FakeMethodicalMachineiG  zD
A fake L{MethodicalMachine}.  Instantiate it with a L{FakeDigraph}
c                	    Wn         R # rM   _digraph)r'   r   s   &&r   r   FakeMethodicalMachine.__init__L  s    r   c                	    V P                   # rM   r   r&   s   &r   r   FakeMethodicalMachine.asDigraphO  s    }}r   r   N)r4   r5   r6   r7   r   r   r   r<   r   r   r   r   r   G  s     r   r   c                  V    ] tR tRtR tR tR tRR ltR tR t	R	 t
R
 tR tR tRtR# )VisualizeToolTestsiS  c                	    \        4       V n        \        V P                  4      V n        R V n        R.V n        . V n        RV n        R# )z	tool-testignoredz	fake.fqpnN)r   digraphRecorderr   fakeDigraphfakePrognamefakeSysPathcollectedOutputfakeFQPNr&   s   &r   rl   VisualizeToolTests.setUpW  sA    46&t';';<'%;!#r   c                	Z    V P                   P                  R P                  V4      4       R# ) N)r  r   r   )r'   argss   &*r   collectPrints VisualizeToolTests.collectPrints`  s    ##CHHTN3r   c              #  	@   "   V\        V P                  4      3x  R # 5irM   )r   r  )r'   fqpns   &&r   fakeFindMachines#VisualizeToolTests.fakeFindMachinesc  s     )$*:*:;;;s   Nc                	    ^RI Hp T! T;'       g    V P                  T;'       g    V P                  .T;'       g    V P                  T;'       g    V P
                  T;'       g    V P                  R7      # )rF   )tool)	_progname_argv_syspath_findMachines_print)rk   r  r	  r  r
  r  r  )r'   prognameargvsyspathfindMachinesprintr  s   &&&&&& r   r  VisualizeToolTests.toolf  si     	&33$"3"3))4==/00 0 0&??$*?*?..D..
 	
r   c                    V P                  V P                  .R7       V P                  V P                  ^ ,          R4       R# )zi
L{tool} adds '' to sys.path to ensure
L{automat._discover.findMachines} searches the current
directory.
r  r   N)r  r  rx   r
  r&   s   &r   test_checksCurrentDirectory.VisualizeToolTests.test_checksCurrentDirectorys  s3     				'))!,b1r   c                    V P                  V P                  R.R7       V P                  V P                  4       V P                  V P                  R.R7       V P                  V P                  4       R# )z&
Passing -q/--quiet hides all output.
z--quietr%  z-qN)r  r  assertFalser  r&   s   &r   test_quietHidesOutput(VisualizeToolTests.test_quietHidesOutput|  sZ     			y1	2--.		t,	---.r   c                   R	 EF9  pV P                   P                  4        . V n        V P                  V P                  VR.R7       T P                  \        ;QJ d&    R V P                   4       F  '       g   K   RM	  RM! R V P                   4       4      4       V P                  \        V P                   P                  4      ^4       V P                   P                  w  pV P                  RP                  V P                  4      VR,          4       V P                  V P                   P                  4       EK<  	  R# )
zM
Passing an empty string for --image-directory/-i disables
rendering images.
r   r%  c              3  ,   "   T F
  pR V9   x  K  	  R# 5iimageNr   .0lines   & r   	<genexpr>6VisualizeToolTests.test_onlySaveDot.<locals>.<genexpr>  s      R=QTD=Q   TF{}.dotfilenameN)--image-directoryz-i)r  r   r  r  r  r)  anyrx   r   r   rI   r   r'   argr   s   &  r   test_onlySaveDot#VisualizeToolTests.test_onlySaveDot  s    
 /C  &&(#%D IIDMM33I4SS RT=Q=Q RSSS RT=Q=Q RRSS!5!5!?!?@!D**44GTX__T]];T*=MNT11==> /r   c                   R
 EFQ  pV P                   P                  4        . V n        V P                  V P                  VR.R7       T P                  \        ;QJ d&    R V P                   4       F  '       g   K   RM	  RM! R V P                   4       4      4       V P                  \        V P                   P                  4      ^4       V P                   P                  w  pV P                  RP                  V P                  4      VR,          4       V P                  VR,          4       V P                  V P                   P                  4       EKT  	  R	# )zK
Passing an empty string for --dot-directory/-d disables saving dot
files.
r   r%  c              3  ,   "   T F
  pR V9   x  K  	  R# 5ir   Nr   r0  s   & r   r3  8VisualizeToolTests.test_saveOnlyImage.<locals>.<genexpr>  s      P;O4$;Or5  TFr6  r7  cleanupN)--dot-directoryz-d)r  r   r  r  r  r)  r9  rx   r   r   rI   
assertTruer   r:  s   &  r   test_saveOnlyImage%VisualizeToolTests.test_saveOnlyImage  s    
 -C  &&(#%D IIDMM33I4SS P4;O;O PSSS 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                \   RpRpV P                  V P                  RVRV.R7       T P                  \        ;QJ d&    R V P                   4       F  '       g   K   RM	  RM! R V P                   4       4      4       T P                  \        ;QJ d&    R	 V P                   4       F  '       g   K   RM	  RM! R	 V P                   4       4      4       V P                  \        V P                  P                  4      ^4       V P                  P                  w  pV P                  VR
,          V4       V P                  VR,          4       V P                  \        V P                  P                  4      ^4       V P                  P                  w  pV P                  VR
,          V4       R# )zz
Passing different directories to --image-directory and --dot-directory
writes images and dot files to those directories.
r/  r   r8  rC  r%  c              3  ,   "   T F
  pR V9   x  K  	  R# 5ir.  r   r0  s   & r   r3  QVisualizeToolTests.test_saveDotAndImagesInDifferentDirectories.<locals>.<genexpr>  s     M8LGtO8Lr5  TFc              3  ,   "   T F
  pR V9   x  K  	  R# 5ir@  r   r0  s   & r   r3  rI    s     K6JdETM6Jr5  	directoryrB  N)
r  r  rD  r9  r  rx   r   r  r   r   )r'   imageDirectorydotDirectory
renderCallsaveCalls   &    r   +test_saveDotAndImagesInDifferentDirectories>VisualizeToolTests.test_saveDotAndImagesInDifferentDirectories  sB   
 !		#! 	 	
 	M8L8LMM8L8LMMNKd6J6JKKd6J6JKKLT11==>B,,88K0.A
9-.T11;;<a@**44+.=r   c                F   RpV P                  V P                  RVRV.R7       T P                  \        ;QJ d&    R V P                   4       F  '       g   K   RM	  RM! R V P                   4       4      4       V P                  \        V P                  P                  4      ^4       V P                  P                  w  pV P                  VR,          V4       V P                  VR	,          4       V P                  \        V P                  P                  4      4       R
# )zx
Passing the same directory to --image-directory and --dot-directory
writes images and dot files to that one directory.
imagesAndDotr8  rC  r%  c              3  ,   "   T F
  pR V9   x  K  	  R# 5i)zimage and dotNr   r0  s   & r   r3  JVisualizeToolTests.test_saveDotAndImagesInSameDirectory.<locals>.<genexpr>  s     U@TOt3@Tr5  TFrK  rB  N)r  r  rD  r9  r  rx   r   r  r   r)  r   )r'   rK  rN  s   &  r   $test_saveDotAndImagesInSameDirectory7VisualizeToolTests.test_saveDotAndImagesInSameDirectory  s    
 #			#! 	 	
 	U@T@TUU@T@TUUVT11==>B,,88K0)<I./T11;;<=r   )r  r  r  r  r	  r
  )NNNNN)r4   r5   r6   r7   rl   r  r  r  r&  r*  r<  rE  rP  rV  r<   r   r   r   r  r  S  s4    $4<
2/?$=&>:>r   r  )'
__future__r   r   r   r   dataclassesr   typingr   unittestr   r   automatr   r	   _methodicalr
   _typedr   test_discoverr   r   r    rA   rC   rP   re   rg   r?   r   r   r   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   