
    ?d*                         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                          yy)	zTests for yapf.comment_splicer.    N)pytree_utils)comment_splicer)	py3compatc                       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y)CommentSplicerTestc                 N    | j                  |t        j                  |             y N)assertEqualr   NodeName)selfexpected_typenodes      @/usr/lib/python3/dist-packages/yapftests/comment_splicer_test.py_AssertNodeTypez"CommentSplicerTest._AssertNodeType   s    ]L$9$9$$?@    Nc                    t        j                  |      dk(  r9| j                  d|j                  d          |j                  d   j                  }n| j                  d|       |j                  }|| j                  ||       y y )Nsimple_stmtCOMMENTr   )r   r   r   childrenvalueassertIn)r   r   text_in_comment
node_values       r   _AssertNodeIsCommentz'CommentSplicerTest._AssertNodeIsComment   st    T"m3
9dmmA&67==#))j
9d+::j"
mmOZ0 #r   c                     t        t        j                  fd|j                                     D ]  \  }}||dz
  k(  s|c S  t	        dj                  |            )Nc                 4    t        j                  |       k(  S r	   )r   r   )cnames    r   <lambda>z7CommentSplicerTest._FindNthChildNamed.<locals>.<lambda>*   s    L$9$9!$<$D r      zNo Nth child for n={0})	enumerater   ifilter	pre_orderRuntimeErrorformat)r   r   r   nichilds     `   r   _FindNthChildNamedz%CommentSplicerTest._FindNthChildNamed(   sa    D..*	,- 5 
a!e	
 /66q9
::r   c                 D   d}t        j                  |      }t        j                  |       |j                  d   j                  d   }| j                  d|       | j                  dt        |j                               |j                  d   }| j                  |d       y )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.testSimpleInline0   s    &D''-D""4(==$$Q'D 	d+QDMM*+==#Ll,=>r   c                    t        j                  d      }t        j                  |      }t	        j
                  |       | j                  dt        |j                               |j                  d   }| j                  |       y )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=   sl    ?? 
 D
 ''-D""4( 	QDMM*+==#Ll+r   c                    t        j                  d      }t        j                  |      }t	        j
                  |       | j                  dt        |j                               | j                  |j                  d          y )NzP
      foo = 1
      # first comment
      # second comment
      bar = 2
      r,   r    r7   )r   r1   r2   s      r   testTwoLineCommentz%CommentSplicerTest.testTwoLineCommentL   sg    ?? 
 D ''-D""4( 	QDMM*+dmmA./r   c                 4   t        j                  d      }t        j                  |      }t	        j
                  |       |j                  d   j                  d   }| j                  d|j                  d          | j                  |j                  d          y )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.testCommentIsFirstChildInCompoundZ   s    ?? 
 D
 ''-D""4(
 }}Q((+HH$5$5a$89h//23r   c                 4   t        j                  d      }t        j                  |      }t	        j
                  |       |j                  d   j                  d   }| j                  d|j                  d          | j                  |j                  d          y )Nz7
      if x:
        foo = 1
        # a comment
      r   r-   DEDENTr?   r@   s       r    testCommentIsLastChildInCompoundz3CommentSplicerTest.testCommentIsLastChildInCompoundj   s    ?? 
 D
 ''-D""4(
 }}Q((+H8#4#4R#89h//34r   c                 >   t        j                  d      }t        j                  |      }t	        j
                  |       |j                  d   }| j                  |d       |j                  d   j                  d   }|j                  d   }| j                  |d       y )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.testInlineAfterSeparateLinez   s    ?? 
 D
 ''-D""4( }}Q'.0@A==$$Q'D--+13EFr   c                 >   t        j                  d      }t        j                  |      }t	        j
                  |       |j                  d   }| j                  |d       |j                  d   j                  d   }|j                  d   }| j                  |d       y )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==$$Q'D--+13EFr   c                 4   t        j                  d      }t        j                  |      }t	        j
                  |       | j                  |j                  d          |j                  d   j                  d   }| j                  d|j                  d          y )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( 	dmmA./}}Q((+H8#4#4R#89r   c                 4   t        j                  d      }t        j                  |      }t	        j
                  |       |j                  d   j                  d   }| j                  |j                  d          | j                  d|j                  d          y )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8#4#4R#89r   c                 4   t        j                  d      }t        j                  |      }t	        j
                  |       |j                  d   j                  d   }| j                  |j                  d          | j                  d|j                  d          y )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8#4#4R#89r   c                    t        j                  d      }t        j                  |      }t	        j
                  |       | j                  |j                  d   d       | j                  |dd      }| j                  |j                  d   d       | j                  d|j                  d	          | j                  |dd
      }| j                  |j                  d   d       | j                  d|j                  d	          y )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( 	dmmA.> ((w!(<Jj11"5}E:#6#6r#:; ((w!(<Jj11"5}E:#6#6r#:;r   c                 \   t        j                  d      }t        j                  |      }t	        j
                  |       |j                  d   j                  d   }|j                  d   }| j                  |d       |j                  d   }|j                  d   }| j                  |d       y )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   }| j                  |d       |j                  d   }| j                  |d       y )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(mmAG&&q)Oo/IJ&&q)Oo/IJr   c                 B   t        j                  d      }t        j                  |      }t	        j
                  |       |j                  d   }|j                  d   }|j                  d   }| j                  |d       |j                  d   }| j                  |d       y )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!**1-Oo/CDr   c                     t        j                  d      }t        j                  |      }t	        j
                  |       | j                  |dd      }|j                  d   }| j                  |d       y )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.  sj    ??  	D ''-D""4(%%dIq9Gq!Gg'HIr   c                    t        j                  d      }t        j                  |      }t	        j
                  |       | j                  |dd      }| j                  |j                  d   d       | j                  |dd      }| j                  |j                  d   d       | j                  |dd      }| j                  |j                  d	   d
       y )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.testMultipleCommentsInOneExpr:  s    ??  	D ''-D""4(%%dIq9Gg..q19=%%dIq9Gg..q19=((|Q?Jj11"5y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      sh    A1;?,04 5 G$G$::":(<6?,K&E0
JBr   r   __main__)__doc__r8   unittestyapf.pytreer   r   yapf.yapflibr   TestCaser   rw   mainrz   r   r   <module>r      sJ    &   $ ' "rB** rBj	 z(--/ r   