
    `N`*                         d Z ddlZddlZddlmZ ddlmZ ddlmZ  G d dej                  Ze	dk    r ej
                     dS dS )	zTests for yapf.comment_splicer.    N)comment_splicer)	py3compat)pytree_utilsc                       e Zd Zd ZddZd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d Zd Zd ZdS )CommentSplicerTestc                 V    |                      |t          j        |                     d S N)assertEqualr   NodeName)selfexpected_typenodes      >lib/python3.11/site-packages/yapftests/comment_splicer_test.py_AssertNodeTypez"CommentSplicerTest._AssertNodeType   s)    ]L$9$$?$?@@@@@    Nc                    t          j        |          dk    r4|                     d|j        d                    |j        d         j        }n|                     d|           |j        }||                     ||           d S d S )Nsimple_stmtCOMMENTr   )r   r   r   childrenvalueassertIn)r   r   text_in_comment
node_values       r   _AssertNodeIsCommentz'CommentSplicerTest._AssertNodeIsComment   s    T""m33
9dmA&6777=#)jj
9d+++:j"
mmOZ00000 #"r      c                     t          t          j        fd|                                                    D ]\  }}||dz
  k    r|c S t	          d                    |                    )Nc                 4    t          j        |           k    S r	   )r   r   )cnames    r   <lambda>z7CommentSplicerTest._FindNthChildNamed.<locals>.<lambda>)   s    L$9!$<$<$D r   r   zNo Nth child for n={0})	enumerater   ifilter	pre_orderRuntimeErrorformat)r   r   r   nichilds     `   r   _FindNthChildNamedz%CommentSplicerTest._FindNthChildNamed'   s    DDDD..**	, 	,- -  5 
a!e 

/66q99
:
::r   c                 L   d}t          j        |          }t          j        |           |j        d         j        d         }|                     d|           |                     dt          |j                             |j        d         }|                     |d           d S )Nzfoo = 1 # and a comment
r   	expr_stmt      z# and a comment)	r   ParseCodeToTreer   SpliceCommentsr   r   r
   lenr   )r   codetreeexprcomment_nodes        r   testSimpleInlinez#CommentSplicerTest.testSimpleInline/   s    &D'--D"4(((=$Q'D 	d+++QDM**+++=#Ll,=>>>>>r   c                    t          j        d          }t          j        |          }t	          j        |           |                     dt          |j                             |j        d         }| 	                    |           d S )Nz9
      foo = 1
      # first comment
      bar = 2
      r,   r   
textwrapdedentr   r.   r   r/   r
   r0   r   r   )r   r1   r2   r4   s       r   testSimpleSeparateLinez)CommentSplicerTest.testSimpleSeparateLine<   s    ? 
  D
 '--D"4((( 	QDM**+++=#Ll+++++r   c                    t          j        d          }t          j        |          }t	          j        |           |                     dt          |j                             | 	                    |j        d                    d S )NzP
      foo = 1
      # first comment
      # second comment
      bar = 2
      r,   r   r7   )r   r1   r2   s      r   testTwoLineCommentz%CommentSplicerTest.testTwoLineCommentK   s~    ? 
  D '--D"4((( 	QDM**+++dmA./////r   c                 0   t          j        d          }t          j        |          }t	          j        |           |j        d         j        d         }|                     d|j        d                    |                     |j        d                    d S )Nz7
      if x:
        # a comment
        foo = 1
      r   r-   NEWLINEr   	r8   r9   r   r.   r   r/   r   r   r   r   r1   r2   if_suites       r   !testCommentIsFirstChildInCompoundz4CommentSplicerTest.testCommentIsFirstChildInCompoundY   s    ? 
  D
 '--D"4(((
 }Q(+HH$5a$8999h/233333r   c                 0   t          j        d          }t          j        |          }t	          j        |           |j        d         j        d         }|                     d|j        d                    |                     |j        d                    d S )Nz7
      if x:
        foo = 1
        # a comment
      r   r-   DEDENTr?   r@   s       r    testCommentIsLastChildInCompoundz3CommentSplicerTest.testCommentIsLastChildInCompoundi   s    ? 
  D
 '--D"4(((
 }Q(+H8#4R#8999h/344444r   c                 :   t          j        d          }t          j        |          }t	          j        |           |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   rE   # inline commentr8   r9   r   r.   r   r/   r   r   r   r1   r2   sep_comment_noder3   inline_comment_nodes         r   testInlineAfterSeparateLinez.CommentSplicerTest.testInlineAfterSeparateLiney   s    ? 
  D
 '--D"4((( }Q'.0@AAA=$Q'D-+13EFFFFFr   c                 :   t          j        d          }t          j        |          }t	          j        |           |j        d         }|                     |d           |j        d         j        d         }|j        d         }|                     |d           d S )NzI
      bar = 1
      foo = 1 # inline comment
      # line comment
      rF   rI   r   r   rE   rK   rL   rM   s         r   testSeparateLineAfterInlinez.CommentSplicerTest.testSeparateLineAfterInline   s    ? 
  D
 '--D"4((( }R(.0@AAA=$Q'D-+13EFFFFFr   c                 0   t          j        d          }t          j        |          }t	          j        |           |                     |j        d                    |j        d         j        d         }|                     d|j        d                    d S )NzA
      if bar:
        z = 1
      # a comment
      j = 2
      r   r   r-   rD   rE   )	r8   r9   r   r.   r   r/   r   r   r   r@   s       r   testCommentBeforeDedentz*CommentSplicerTest.testCommentBeforeDedent   s    ? 
  D '--D"4((( 	dmA.///}Q(+H8#4R#899999r   c                 0   t          j        d          }t          j        |          }t	          j        |           |j        d         j        d         }|                     |j        d                    |                     d|j        d                    d S )NzU
      if foo:
        if bar:
          z = 1
        # a comment
      y = 1
      r   r-   rF   rD   rE   	r8   r9   r   r.   r   r/   r   r   r   r@   s       r   testCommentBeforeDedentTwoLevelz2CommentSplicerTest.testCommentBeforeDedentTwoLevel   s    ? 
  D '--D"4(((}Q(+H 	h/34448#4R#899999r   c                 0   t          j        d          }t          j        |          }t	          j        |           |j        d         j        d         }|                     |j        d                    |                     d|j        d                    d S )NzV
      if foo:
        if bar:
          z = 1
         # comment 2
      y = 1
      r   r-   rF   rD   rE   rV   r@   s       r   1testCommentBeforeDedentTwoLevelImproperlyIndentedzDCommentSplicerTest.testCommentBeforeDedentTwoLevelImproperlyIndented   s    ? 
  D '--D"4(((
 }Q(+H 	h/34448#4R#899999r   c                 (   t          j        d          }t          j        |          }t	          j        |           |                     |j        d         d           |                     |dd          }|                     |j        d         d           | 	                    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 0suite)r&   rF   z# comment 1rD   rE   rJ   z# comment 2)
r8   r9   r   r.   r   r/   r   r   r)   r   )r   r1   r2   
if_suite_1
if_suite_2s        r   !testCommentBeforeDedentThreeLevelz4CommentSplicerTest.testCommentBeforeDedentThreeLevel   s   ? 
  D '--D"4((( 	dmA.>>> ((w!(<<Jj1"5}EEE:#6r#:;;; ((w!(<<Jj1"5}EEE:#6r#:;;;;;r   c                 T   t          j        d          }t          j        |          }t	          j        |           |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-   rF   z	# anotherz# top-levelrL   )r   r1   r2   class_suiteanother_commentfuncdeftoplevel_comments          r   testCommentsInClassz&CommentSplicerTest.testCommentsInClass   s    ? 
  D '--D"4(((-"+A.K!*2.Oo{;;; "1%G'*.>>>>>r   c                 $   t          j        d          }t          j        |          }t	          j        |           |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 2rL   )r   r1   r2   rb   block_comment_1block_comment_2s         r   testMultipleBlockCommentsz,CommentSplicerTest.testMultipleBlockComments  s    ?   D '--D"4(((mAG&q)Oo/IJJJ&q)Oo/IJJJJJr   c                 >   t          j        d          }t          j        |          }t	          j        |           |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.rL   )r   r1   r2   classdefr`   qux_commentinterim_comments          r   testCommentsOnDedentsz(CommentSplicerTest.testCommentsOnDedents  s    ? 
 
 
D '--D"4(((}QH#A&K&q)Kk+ABBB!*1-Oo/CDDDDDr   c                     t          j        d          }t          j        |          }t	          j        |           |                     |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.)	r8   r9   r   r.   r   r/   r)   r   r   )r   r1   r2   rp   comments        r   testExprCommentsz#CommentSplicerTest.testExprComments-  s|    ?  	 	D '--D"4(((%%dIq99Gq!Gg'HIIIIIr   c                    t          j        d          }t          j        |          }t	          j        |           |                     |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
        )
    rp   r   z# com 1arglistrJ   z# com 2
arith_exprrE   z# com 3)	r8   r9   r   r.   r   r/   r)   r   r   )r   r1   r2   rp   rt   ru   s         r   testMultipleCommentsInOneExprz0CommentSplicerTest.testMultipleCommentsInOneExpr9  s    ?  	 	D '--D"4(((%%dIq99Gg.q19===%%dIq99Gg.q19===((|Q??Jj1"5yAAAAAr   r	   )r   )__name__
__module____qualname__r   r   r)   r5   r:   r<   rB   rG   rP   rR   rT   rW   rY   r^   rd   rh   rn   rr   rv    r   r   r   r      sR       A A A1 1 1 1; ; ; ;? ? ?, , ,0 0 04 4 4 5 5 5 G G G$G G G$: : :: : :": : :(< < <6? ? ?,K K K&E E E0
J 
J 
JB B B B Br   r   __main__)__doc__r8   unittestyapf.yapflibr   r   r   TestCaser   rw   mainrz   r   r   <module>r      s    & %   ( ( ( ( ( ( " " " " " " % % % % % %rB rB rB rB rB* rB rB rBj	 z(-///// r   