
    Vhl$                         d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
  G d d      Z G d	 d
      Z G d dej                        Z G d dej                        Zy)z+
Test cases for twisted.words.xish.utility
    )OrderedDict)unittest)utility)Element)EventDispatcherc                       e Zd ZdZd Zd Zy)CallbackTrackerz
    Test helper for tracking callbacks.

    Increases a counter on each call to L{call} and stores the object
    passed in the call.
    c                      d| _         d | _        y )Nr   calledobjselfs    P/home/dcms/DCMS/lib/python3.12/site-packages/twisted/words/test/test_xishutil.py__init__zCallbackTracker.__init__   s        c                 :    | j                   dz   | _         || _        y )N   r   )r   r   s     r   callzCallbackTracker.call   s    kkAor   N)__name__
__module____qualname____doc__r   r    r   r   r	   r	      s    r   r	   c                   (    e Zd ZdZd Zd Zd Zd Zy)OrderedCallbackTrackerz=
    Test helper for tracking callbacks and their order.
    c                     g | _         y N)callListr   s    r   r   zOrderedCallbackTracker.__init__'   s	    r   c                 N    | j                   j                  | j                         y r   )r   appendcall1r   objects     r   r"   zOrderedCallbackTracker.call1*       TZZ(r   c                 N    | j                   j                  | j                         y r   )r   r!   call2r#   s     r   r'   zOrderedCallbackTracker.call2-   r%   r   c                 N    | j                   j                  | j                         y r   )r   r!   call3r#   s     r   r)   zOrderedCallbackTracker.call30   r%   r   N)r   r   r   r   r   r"   r'   r)   r   r   r   r   r   "   s    )))r   r   c                   X    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zy)EventDispatcherTestsz'
    Tests for L{EventDispatcher}.
    c                    t               }t               }t               }t               }|j                  d|j                         |j                  d|j                         |j                  d|j                         |j                  d|j                         t	        d      }|j                  d       t	        d      }|j                  d       |j                  |       | j                  |j                  d	       | j                  |j                  |       | j                  |j                  d
       |j                  |       | j                  |j                  d	       | j                  |j                  d       | j                  |j                  |       | j                  |j                  d
       |j                  |d       | j                  |j                  d       | j                  |j                  |       |j                  d|j                         |j                  |       | j                  |j                  d       y )N/message/body/message	/presence//event/testeventnsmessagebodyr2   presencer6      r   r   )r   r	   addObserverr   r   
addElementdispatchassertEqualr   r   removeObserver)r   dcb1cb2cb3msgpress          r   	testStuffzEventDispatcherTests.testStuff9   s   	osxx0	j#((+	k388,	)3884'(v)*
#	

3Q'#&Q'	

4Q'Q'$'Q'	

1)*Q'!$	chh/	

4Q'r   c                    t               }t               }t               }|j                  d|j                         |j                  d|j                         |j	                  |d       | j                  |j                  d       | j                  |j                  |       | j                  |j                  d       | j                  |j                  |       y)z
        Test adding two observers for the same query.

        When the event is dispatched both of the observers need to be called.
        r0   r   N)r   r	   r8   r   r:   r;   r   r   )r   r=   r>   r?   s       r   test_addObserverTwicez*EventDispatcherTests.test_addObserverTwice]   s     	)3884	)3884	

1)*Q'!$Q'!$r   c                    t               t        d      }t               fd}j                  d|       j	                  |       | j                  j                  d       j	                  |       | j                  j                  d       j	                  |       | j                  j                  d       y)zG
        Test for registration of an observer during dispatch.
        r1   c                 >    j                  dj                         y Nr.   )r8   r   )_cbr=   s    r   	onMessagezBEventDispatcherTests.test_addObserverInDispatch.<locals>.onMessagex   s    MM*bgg.r   r.   r   r   r7   Nr   r   r	   addOnetimeObserverr:   r;   r   r   rA   rK   rJ   r=   s      @@r   test_addObserverInDispatchz/EventDispatcherTests.test_addObserverInDispatchp   s     '(	/ 	
Z3	

3A&	

3A&	

3A&r   c                    t               t        d      }t               fd}j                  d|       j	                  |       | j                  j                  d       j	                  |       | j                  j                  d       j	                  |       | j                  j                  d       y)zN
        Test for registration of a onetime observer during dispatch.
        r1   c                 >    j                  dj                         y rH   )rM   r   )rA   rJ   r=   s    r   rK   zIEventDispatcherTests.test_addOnetimeObserverInDispatch.<locals>.onMessage   s      RWW5r   r.   r   r   NrL   rN   s      @@r   !test_addOnetimeObserverInDispatchz6EventDispatcherTests.test_addOnetimeObserverInDispatch   s     '(	6 	
Z3	

3A&	

3A&	

3A&r   c                 .   t               }t        d      }t               }|j                  d|j                         |j                  |       | j                  |j                  d       |j                  |       | j                  |j                  d       y )Nr1   r.   r   )r   r   r	   rM   r   r:   r;   r   )r   r=   rA   rJ   s       r   testOnetimeDispatchz(EventDispatcherTests.testOnetimeDispatch   so    '(	Z1	

3A&	

3A&r   c                    t               }t        d      }t        d      }t               }|j                  d|j                         |j                  |      }| j                  d|       |j                  |      }| j                  d|       y )Nr1   r5   r/   FT)r   r   r	   r8   r   r:   r;   )r   r=   rA   rB   rJ   results         r   testDispatcherResultz)EventDispatcherTests.testDispatcherResult   su    '()*	k277+C'D!v&r   c                    t               }t               }|j                  d|j                         |j                  d|j                  d       |j                  d|j
                  d       t        d      }|j                  d       |j                  |       | j                  |j                  |j
                  |j                  |j                  gdt        |j                  D cg c]  }|j                   c}      z         y c c}w )Nr-   r.   r   r1   r4   zCalls out of order: %s)r   r   r8   r'   r)   r"   r   r9   r:   r;   r   reprr   )r   r=   rJ   rA   cs        r   testOrderedXPathDispatchz-EventDispatcherTests.testOrderedXPathDispatch   s    #%	orxx0	j"((B/	orxx3'(v	

3KKXXrxx*$t,MAQZZ,M'NN	
 -Ns   $Dc                 `   t               }t               }|j                  d|j                         |j	                  dd       | j                  d|j                         |j                  d|j                         | j                  dt        |j                  j                  d                   y)zO
        Test observer clean-up after removeObserver for named events.
        //event/testNr   r   )r   r	   r8   r   r:   r;   r   r<   len_eventObserverspopr   r=   rJ   s      r   test_cleanUpRemoveEventObserverz4EventDispatcherTests.test_cleanUpRemoveEventObserver   s    
 	nbgg.	

4(BII&	1C 1 1 5 5a 89:r   c                 t   t               }t               }t        d      }|j                  d|j                         |j                  |       | j                  d|j                         |j                  d|j                         | j                  dt        |j                  j                  d                   y)zO
        Test observer clean-up after removeObserver for XPath events.
        Nr3   r.   r   r   N)r   r	   r   r8   r   r:   r;   r   r<   r_   _xpathObserversra   r   r=   rJ   rA   s       r   test_cleanUpRemoveXPathObserverz4EventDispatcherTests.test_cleanUpRemoveXPathObserver   s    
 '(	j"''*	

3BII&	RWW-C 1 1 5 5a 89:r   c                 (   t               }t               }|j                  d|j                         |j	                  dd       | j                  d|j                         | j                  dt        |j                  j                  d                   y)zD
        Test observer clean-up after onetime named events.
        r^   Nr   r   )
r   r	   rM   r   r:   r;   r   r_   r`   ra   rb   s      r    test_cleanUpOnetimeEventObserverz5EventDispatcherTests.test_cleanUpOnetimeEventObserver   sp    
 	^RWW5	

4(BII&C 1 1 5 5a 89:r   c                 <   t               }t               }t        d      }|j                  d|j                         |j                  |       | j                  d|j                         | j                  dt        |j                  j                  d                   y)zD
        Test observer clean-up after onetime XPath events.
        re   r.   r   r   N)r   r	   r   rM   r   r:   r;   r   r_   rf   ra   rg   s       r    test_cleanUpOnetimeXPathObserverz5EventDispatcherTests.test_cleanUpOnetimeXPathObserver   sx    
 '(	Z1	

3BII&C 1 1 5 5a 89:r   c                 <    G d dt         j                        } G d dt              fd}t               }t	               }t         j                  }	 |t         _        |j                  d|       |j                  d|j                         	 |j                  dd       | j                  d	t        | j                                     | j                  d	|j                         |t         _        y# $ r | j                  d       Y jw xY w# |t         _        w xY w)
z
        Test that exceptions in observers do not bubble up to dispatch.

        The exceptions raised in observers should be logged and other
        observers should be called as if nothing happened.
        c                       e Zd Zd Zy)OEventDispatcherTests.test_observerRaisingException.<locals>.OrderedCallbackListc                 "    t               | _        y r   )r   	callbacksr   s    r   r   zXEventDispatcherTests.test_observerRaisingException.<locals>.OrderedCallbackList.__init__  s    !,r   N)r   r   r   r   r   r   r   OrderedCallbackListro   
  s    /r   rr   c                       e Zd Zy)EEventDispatcherTests.test_observerRaisingException.<locals>.TestErrorN)r   r   r   r   r   r   	TestErrorrt     s    r   ru   c                             r   r   )rI   ru   s    r   
raiseErrorzFEventDispatcherTests.test_observerRaisingException.<locals>.raiseError  s    +r   r^   Nz2TestError raised. Should have been logged instead.r   )r   CallbackList	Exceptionr   r	   r8   r   r:   failr;   r_   flushLoggedErrorsr   )r   rr   rw   r=   rJ   originalCallbackListru   s         @r   test_observerRaisingExceptionz2EventDispatcherTests.test_observerRaisingException  s    	/'"6"6 	/		 		 &33	8#6G MM.*5MM."''2P

40 QD$:$:9$E FGQ		*#7G   P		NOP $8G s1   9D C2  AD 2DD 
DD DN)r   r   r   r   rC   rE   rO   rR   rT   rW   r\   rc   rh   rj   rl   r}   r   r   r   r+   r+   4   sD    "(H%&',',	''
,;;;;$8r   r+   c                   "    e Zd ZdZd Zd Zd Zy)XmlPipeTestsz:
    Tests for L{twisted.words.xish.utility.XmlPipe}.
    c                 6    t        j                         | _        y r   )r   XmlPipepiper   s    r   setUpzXmlPipeTests.setUp.  s    OO%	r   c                     fd}g | j                   j                  j                  d|       t        d      }| j                   j                  j                  |       | j                  |g       y)zO
        Send an element from the source and observe it from the sink.
        c                 (    j                  |        y r   r!   r   r   s    r   rJ   z,XmlPipeTests.test_sendFromSource.<locals>.cb6      MM#r   /test[@xmlns="testns"]testnstestN)r   sinkr8   r   sourcesendr;   r   rJ   elementr   s      @r   test_sendFromSourcez XmlPipeTests.test_sendFromSource1  s]    
	 		""#;R@,-		g&'F+r   c                     fd}g | j                   j                  j                  d|       t        d      }| j                   j                  j                  |       | j                  |g       y)zO
        Send an element from the sink and observe it from the source.
        c                 (    j                  |        y r   r   r   s    r   rJ   z*XmlPipeTests.test_sendFromSink.<locals>.cbD  r   r   r   r   N)r   r   r8   r   r   r   r;   r   s      @r   test_sendFromSinkzXmlPipeTests.test_sendFromSink?  s]    
	 		$$%=rB,-		G$'F+r   N)r   r   r   r   r   r   r   r   r   r   r   r   )  s    &,,r   r   N)r   collectionsr   twisted.trialr   twisted.words.xishr   twisted.words.xish.domishr   twisted.words.xish.utilityr   r	   r   TestCaser+   r   r   r   r   <module>r      sV   
 $ " & - 6 ") )$r88,, r8j",8$$ ",r   