o
    ?n0j°*  ã                   @   sV   d Z ddlZddlZddlmZ ddlmZ G dd„ dejƒZedkr)e 	¡  dS dS )zTests for yapf.comment_splicer.é    N)Úcomment_splicer)Úpytree_utilsc                   @   s¨   e Zd Zdd„ Zd)dd„Zd*dd„Zd	d
„ Zdd„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ ZdS )+ÚCommentSplicerTestc                 C   s   |   |t |¡¡ d S ©N)ÚassertEqualr   ÚNodeName)ÚselfÚexpected_typeÚnode© r   úe/home/wildlama/miniconda3/envs/lam_a2e/lib/python3.10/site-packages/yapftests/comment_splicer_test.pyÚ_AssertNodeType   s   z"CommentSplicerTest._AssertNodeTypeNc                 C   s\   t  |¡dkr|  d|jd ¡ |jd j}n	|  d|¡ |j}|d ur,|  ||¡ d S d S )NZsimple_stmtÚCOMMENTr   )r   r   r   ÚchildrenÚvalueZassertIn)r   r
   Ztext_in_commentZ
node_valuer   r   r   Ú_AssertNodeIsComment   s   ÿz'CommentSplicerTest._AssertNodeIsCommenté   c                    sF   t ‡ fdd„| ¡ D ƒƒD ]\}}||d kr|  S qtd |¡ƒ‚)Nc                    s   g | ]}t  |¡ˆ kr|‘qS r   )r   r   )Ú.0Úc©Únamer   r   Ú
<listcomp>(   s    z9CommentSplicerTest._FindNthChildNamed.<locals>.<listcomp>r   zNo Nth child for n={0})Ú	enumerateZ	pre_orderÚRuntimeErrorÚformat)r   r
   r   ÚnÚiÚchildr   r   r   Ú_FindNthChildNamed&   s   ÿÿz%CommentSplicerTest._FindNthChildNamedc                 C   s`   d}t  |¡}t |¡ |jd jd }|  d|¡ |  dt|jƒ¡ |jd }|  |d¡ d S )Nzfoo = 1 # and a comment
r   Z	expr_stmté   é   z# and a comment)	r   ÚParseCodeToTreer   ÚSpliceCommentsr   r   r   Úlenr   )r   ÚcodeÚtreeÚexprÚcomment_noder   r   r   ÚtestSimpleInline-   s   


z#CommentSplicerTest.testSimpleInlinec                 C   sH   t  d¡}t |¡}t |¡ |  dt|jƒ¡ |jd }|  	|¡ d S )Nz9
      foo = 1
      # first comment
      bar = 2
      r   r   ©
ÚtextwrapÚdedentr   r!   r   r"   r   r#   r   r   )r   r$   r%   r'   r   r   r   ÚtestSimpleSeparateLine:   s   



z)CommentSplicerTest.testSimpleSeparateLinec                 C   sD   t  d¡}t |¡}t |¡ |  dt|jƒ¡ |  	|jd ¡ d S )NzP
      foo = 1
      # first comment
      # second comment
      bar = 2
      r   r   r)   )r   r$   r%   r   r   r   ÚtestTwoLineCommentI   s
   


z%CommentSplicerTest.testTwoLineCommentc                 C   sT   t  d¡}t |¡}t |¡ |jd jd }|  d|jd ¡ |  |jd ¡ d S )Nz7
      if x:
        # a comment
        foo = 1
      r   r    ÚNEWLINEr   ©	r*   r+   r   r!   r   r"   r   r   r   ©r   r$   r%   Zif_suiter   r   r   Ú!testCommentIsFirstChildInCompoundW   ó   


z4CommentSplicerTest.testCommentIsFirstChildInCompoundc                 C   sT   t  d¡}t |¡}t |¡ |jd jd }|  d|jd ¡ |  |jd ¡ d S )Nz7
      if x:
        foo = 1
        # a comment
      r   r    ÚDEDENTéÿÿÿÿéþÿÿÿr/   r0   r   r   r   Ú testCommentIsLastChildInCompoundg   r2   z3CommentSplicerTest.testCommentIsLastChildInCompoundc                 C   ó^   t  d¡}t |¡}t |¡ |jd }|  |d¡ |jd jd }|jd }|  |d¡ d S )NzI
      bar = 1
      # line comment
      foo = 1 # inline comment
      r   ú# line commenté   r   r4   ú# inline comment©r*   r+   r   r!   r   r"   r   r   ©r   r$   r%   Zsep_comment_noder&   Zinline_comment_noder   r   r   ÚtestInlineAfterSeparateLinew   ó   




z.CommentSplicerTest.testInlineAfterSeparateLinec                 C   r7   )NzI
      bar = 1
      foo = 1 # inline comment
      # line comment
      r5   r8   r   r   r4   r:   r;   r<   r   r   r   ÚtestSeparateLineAfterInline‰   r>   z.CommentSplicerTest.testSeparateLineAfterInlinec                 C   sT   t  d¡}t |¡}t |¡ |  |jd ¡ |jd jd }|  d|jd ¡ d S )NzA
      if bar:
        z = 1
      # a comment
      j = 2
      r   r   r    r3   r4   )	r*   r+   r   r!   r   r"   r   r   r   r0   r   r   r   ÚtestCommentBeforeDedent›   s   


z*CommentSplicerTest.testCommentBeforeDedentc                 C   óT   t  d¡}t |¡}t |¡ |jd jd }|  |jd ¡ |  d|jd ¡ d S )NzU
      if foo:
        if bar:
          z = 1
        # a comment
      y = 1
      r   r    r5   r3   r4   ©	r*   r+   r   r!   r   r"   r   r   r   r0   r   r   r   ÚtestCommentBeforeDedentTwoLevelª   s   


z2CommentSplicerTest.testCommentBeforeDedentTwoLevelc                 C   rA   )NzV
      if foo:
        if bar:
          z = 1
         # comment 2
      y = 1
      r   r    r5   r3   r4   rB   r0   r   r   r   Ú1testCommentBeforeDedentTwoLevelImproperlyIndented»   s   


zDCommentSplicerTest.testCommentBeforeDedentTwoLevelImproperlyIndentedc                 C   sœ   t  d¡}t |¡}t |¡ |  |jd d¡ | j|ddd}|  |jd d¡ |  	d|jd	 ¡ | j|dd
d}|  |jd d¡ |  	d|jd	 ¡ d S )Nz}
      if foo:
        if bar:
          z = 1
          # comment 2
        # comment 1
      # comment 0
      j = 2
      r   z# comment 0Zsuite)r   r5   z# comment 1r3   r4   r9   z# comment 2)
r*   r+   r   r!   r   r"   r   r   r   r   )r   r$   r%   Z
if_suite_1Z
if_suite_2r   r   r   Ú!testCommentBeforeDedentThreeLevelÏ   s   

	
z4CommentSplicerTest.testCommentBeforeDedentThreeLevelc                 C   sh   t  d¡}t |¡}t |¡ |jd jd }|jd }|  |d¡ |jd }|jd }|  |d¡ d S )Nz…
      class Foo:
        """docstring abc..."""
        # top-level comment
        def foo(): pass
        # another comment
      r   r    r5   z	# anotherz# top-levelr;   )r   r$   r%   Úclass_suiteZanother_commentÚfuncdefZtoplevel_commentr   r   r   ÚtestCommentsInClassê   s   





z&CommentSplicerTest.testCommentsInClassc                 C   sX   t  d¡}t |¡}t |¡ |jd }|jd }|  |d¡ |jd }|  |d¡ d S )Nzl
        # Block comment number 1

        # Block comment number 2
        def f():
          pass
        r   z# Block comment number 1r   z# Block comment number 2r;   )r   r$   r%   rG   Zblock_comment_1Zblock_comment_2r   r   r   ÚtestMultipleBlockComments   s   





z,CommentSplicerTest.testMultipleBlockCommentsc                 C   sb   t  d¡}t |¡}t |¡ |jd }|jd }|jd }|  |d¡ |jd }|  |d¡ d S )Nz¶
        class Foo(object):
          # A comment for qux.
          def qux(self):
            pass

          # Interim comment.

          def mux(self):
            pass
        r   é   r   z# A comment for qux.r   z# Interim comment.r;   )r   r$   r%   ZclassdefrF   Zqux_commentZinterim_commentr   r   r   ÚtestCommentsOnDedents  s   






z(CommentSplicerTest.testCommentsOnDedentsc                 C   sF   t  d¡}t |¡}t |¡ |  |dd¡}|jd }|  |d¡ d S )NzH
      foo( # Request fractions of an hour.
        948.0/3600, 20)
    Útrailerr   z# Request fractions of an hour.)	r*   r+   r   r!   r   r"   r   r   r   )r   r$   r%   rL   Úcommentr   r   r   ÚtestExprComments+  s   



z#CommentSplicerTest.testExprCommentsc                 C   s‚   t  d¡}t |¡}t |¡ |  |dd¡}|  |jd d¡ |  |dd¡}|  |jd d¡ |  |dd¡}|  |jd	 d
¡ d S )NzV
      foo( # com 1
        948.0/3600, # com 2
        20 + 12 # com 3
        )
    rL   r   z# com 1Úarglistr9   z# com 2Ú
arith_exprr4   z# com 3)	r*   r+   r   r!   r   r"   r   r   r   )r   r$   r%   rL   rO   rP   r   r   r   ÚtestMultipleCommentsInOneExpr7  s   


z0CommentSplicerTest.testMultipleCommentsInOneExprr   )r   )Ú__name__Ú
__module__Ú__qualname__r   r   r   r(   r,   r-   r1   r6   r=   r?   r@   rC   rD   rE   rH   rI   rK   rN   rQ   r   r   r   r   r      s(    


r   Ú__main__)
Ú__doc__r*   ZunittestZyapf.pytreer   r   ZTestCaser   rR   Úmainr   r   r   r   Ú<module>   s     6ÿ