
    'iq                         d dl mZmZ d dlmZmZmZmZ d dlm	Z	 d dl
mZ  G d de      Z G d de      Z G d	 d
e      Z G d de      Z G d de      Z G d de      Z G d de      Zy)    )SkipTestTestCase)GrammarNodeVisitorVisitationErrorrule)Literal)Nodec                   8    e Zd ZdZ ed      Zd Zd Zd Zd Z	y)HtmlFormatterz3Visitor that turns a parse tree into HTML fragmentszbold_open  = '(('c                      yN<b> selfnodevisited_childrens      N/var/www/br/venv/lib/python3.12/site-packages/parsimonious/tests/test_nodes.pyvisit_bold_openzHtmlFormatter.visit_bold_open   s        c                      yNz</b>r   r   s      r   visit_bold_closezHtmlFormatter.visit_bold_close   s    r   c                     |j                   S zReturn the text verbatim.textr   s      r   
visit_textzHtmlFormatter.visit_text   s    yyr   c                 $    dj                  |      S N joinr   s      r   visit_bold_textzHtmlFormatter.visit_bold_text   s    ww'((r   N)
__name__
__module____qualname____doc__r   grammarr   r   r   r%   r   r   r   r   r      s$    =-.G)r   r   c                       e Zd ZdZd Zy)ExplosiveFormatterzVisitor which raises exceptionsc                     t         )N)
ValueErrorr   s      r   
visit_boomzExplosiveFormatter.visit_boom   s    r   N)r&   r'   r(   r)   r/   r   r   r   r,   r,      s
    )r   r,   c                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)SimpleTestsc                 :   t        d      }d}t        |d   |ddt        |d   |dd      t        |d   |dd	      t        |d
   |d	d      g      }| j                  |j                  |      |       t	               j                  |      }| j                  |d       y)z%Assert a tree gets visited correctly.z
            bold_text  = bold_open text bold_close
            text       = ~'[a-zA-Z 0-9]*'
            bold_open  = '(('
            bold_close = '))'
        z	((o hai))	bold_textr   	   	bold_open   r      
bold_closez<b>o hai</b>N)r   r
   assertEqualparser   visit)r   r*   r   treeresults        r   test_visitorzSimpleTests.test_visitor#   s       GK($1'+.a;'&/4A6',/q!<>? 	t,d3&&t,0r   c           
          | j                  t        t               j                  t	        t        d      ddd             y )Nr"   r   )assertRaisesr   r,   r;   r
   r	   r   s    r   test_visitation_exceptionz%SimpleTests.test_visitation_exception5   s0    /(*0072;Aq1	3r   c                 p    t        t        dd      ddd      }d}| j                  t        |      |       y)	z!Test str and unicode of ``Node``.	somethingr   )nameo hair      z%<Node called "text" matching "o hai">N)r
   r	   r9   str)r   ngoods      r   test_strzSimpleTests.test_str;   s2    62GQB6Q&r   c                    d}d}t        t        |      |ddt        t        d      |dd      t        t        d      |dd      g	      }| j                  t        |      t	        d
      j                  t        |      t        t        |            t        t        d            t        t        d                         y)zTest repr of ``Node``.u   hai öu   böogier             örG   )childrenzZs = {hai_o}
Node({boogie}, s, 0, 3, children=[Node({space}, s, 3, 4), Node({o}, s, 4, 5)]))hai_oboogiespaceoN)r
   r	   r9   reprrH   format)r   srS   rI   s       r   	test_reprzSimpleTests.test_reprB   s    !QWS\1a+T'$-Aq-I5K Laqryy1gGFO,73<(wt}%	 z 	
r   c                 V    | j                  t               j                  d      d       y)zDExercise the simple case in which the visitor takes care of parsing.z((r   N)r9   r   r:   rA   s    r   test_parse_shortcutzSimpleTests.test_parse_shortcutQ   s    ..t4e<r   c                 V    | j                  t               j                  d      d       y)zEExercise the simple case in which the visitor takes care of matching.z((other thingsr   N)r9   r   matchrA   s    r   test_match_shortcutzSimpleTests.test_match_shortcutV   s     ../?@%Hr   N)	r&   r'   r(   r>   rB   rK   rY   r[   r^   r   r   r   r1   r1   "   s!    1$3'
=
Ir   r1   c                   |    e Zd Z ed      d        Z ed      d        Z ed      d        Z ed      d        Zy	)
CoupledFormatterzbold_open text bold_closec                 $    dj                  |      S r!   r#   r   s      r   r%   z CoupledFormatter.visit_bold_text\   s    ww'((r   z"(("c                      yr   r   r   s      r   r   z CoupledFormatter.visit_bold_open`   s    r   z"))"c                      yr   r   r   s      r   r   z!CoupledFormatter.visit_bold_closed   s    r   z~"[a-zA-Z 0-9]*"c                     |j                   S r   r   r   s      r   r   zCoupledFormatter.visit_texth   s     yyr   N)r&   r'   r(   r   r%   r   r   r   r   r   r   r`   r`   [   sd    	
%&) ') 
&\  
&\  

 r   r`   c                       e Zd Zd Zd Zy)DecoratorTestsc                 V    | j                  t               j                  d      d       y)z$Make sure the @rule decorator works.z((hi))z	<b>hi</b>N)r9   r`   r:   rA   s    r   test_rule_decoratorz"DecoratorTests.test_rule_decoratorn   s!    )+11(;[Ir   c                 6     G d dt               }t        d      )zoMake sure we can subclass and override visitor methods without blowing
        away the rules attached to them.c                   .    e Zd Zd Z ed      d        Zy)KDecoratorTests.test_rule_decorator_subclassing.<locals>.OverridingFormatterc                 6    |j                   j                         S )zReturn the text capitalized.)r   upperr   s      r   r   zVDecoratorTests.test_rule_decorator_subclassing.<locals>.OverridingFormatter.visit_textw   s    yy((r   z
"not used"c                      y)ziGet in the way. Tempt the metaclass to pave over the
                superclass's grammar with a new one.Nr   r   s      r   visit_uselesszYDecoratorTests.test_rule_decorator_subclassing.<locals>.OverridingFormatter.visit_useless{   s    r   N)r&   r'   r(   r   r   ro   r   r   r   OverridingFormatterrk   v   s     ) ,8  8r   rp   z-I haven't got around to making this work yet.)r`   r   r9   r:   )r   rp   s     r   test_rule_decorator_subclassingz.DecoratorTests.test_rule_decorator_subclassings   s    	8"2 	8 FGGr   N)r&   r'   r(   rh   rq   r   r   r   rf   rf   m   s    J
Mr   rf   c                       e Zd Zy)PrimalScreamN)r&   r'   r(   r   r   r   rs   rs      s    r   rs   c                   $    e Zd Zd Zd Zd Zd Zy)SpecialCasesTestsc                 n     G d dt               }| j                  t         |       j                  d       y )Nc                   (    e Zd Z ed      ZefZd Zy)=SpecialCasesTests.test_unwrapped_exceptions.<locals>.Screamerzgreeting = 'howdy'c                     t        d      )NzThis should percolate up!)rs   )r   thingr   s      r   visit_greetingzLSpecialCasesTests.test_unwrapped_exceptions.<locals>.Screamer.visit_greeting   s    "#>??r   N)r&   r'   r(   r   r*   rs   unwrapped_exceptionsr{   r   r   r   Screamerrx      s    67G$0? @r   r}   howdy)r   r@   rs   r:   )r   r}   s     r   test_unwrapped_exceptionsz+SpecialCasesTests.test_unwrapped_exceptions   s,    	@{ 	@ 	,
(8(8'Br   c           	      (   t        t        d      ddd      }| j                  |dk7         | j                  |d k7         | j                  |t        t        d      ddd      k7         | j                  |t        t        d      ddd      k7          y )N12345rF   r   rG   23456)r
   r	   
assertTrue)r   r   s     r   test_node_inequalityz&SpecialCasesTests.test_node_inequality   s|    GG$gq!4	"%WW%5w1 EEFTT''*:GQ%JJKLr   c                      G d dt               }| j                  t              5 } |       j                  d       ddd       | j	                  dt        j                               y# 1 sw Y   /xY w)z
        Test that generic_visit provides informative error messages
        when visitors are not defined.

        Regression test for https://github.com/erikrose/parsimonious/issues/110
        c                   "    e Zd Z ed      ZefZy)XSpecialCasesTests.test_generic_visit_NotImplementedError_unnamed_node.<locals>.MyVisitorz/
                bar = "b" "a" "r"
            Nr&   r'   r(   r   r*   NotImplementedErrorr|   r   r   r   	MyVisitorr      s      G %8#: r   r   barNz6No visitor method was defined for this expression: 'b'r   r@   r   r:   assertInrH   	exceptionr   r   es      r   3test_generic_visit_NotImplementedError_unnamed_nodezESpecialCasesTests.test_generic_visit_NotImplementedError_unnamed_node   sb    	; 	; 23 	%qKe$	%NPSTUT_T_P`a	% 	%   A**A3c                      G d dt               }| j                  t              5 } |       j                  d       ddd       | j	                  dt        j                               y# 1 sw Y   /xY w)zt
        Test that generic_visit provides informative error messages
        when visitors are not defined.
        c                   "    e Zd Z ed      ZefZy)VSpecialCasesTests.test_generic_visit_NotImplementedError_named_node.<locals>.MyVisitorzZ
                bar = myrule myrule myrule
                myrule = ~"[bar]"
            Nr   r   r   r   r   r      s      G %8#: r   r   r   NzDNo visitor method was defined for this expression: myrule = ~'[bar]'r   r   s      r   1test_generic_visit_NotImplementedError_named_nodezCSpecialCasesTests.test_generic_visit_NotImplementedError_named_node   sb    
	; 	; 23 	%qKe$	%\^abcbmbm^no	% 	%r   N)r&   r'   r(   r   r   r   r   r   r   r   ru   ru      s    CMb$pr   ru   N)unittestr   r   parsimoniousr   r   r   r   parsimonious.expressionsr	   parsimonious.nodesr
   r   r,   r1   r`   rf   	Exceptionrs   ru   r   r   r   <module>r      so    ' D D , #)K )& 6I( 6Ir{ $MX M.	9 	4p 4pr   