
    ?dG$                         d Z ddlZddlZddlmZ ddlmZ  G d dej                        Z G d dej                        Z	e
d	k(  r ej                          yy)
z Tests for yapf.pytree_unwrapper.    N)pytree_utils)yapf_test_helperc                   r    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d Zd Zd Zd Zy)PytreeUnwrapperTestc                     g }|D ]^  }|j                   D cg c]*  }|j                  t        j                  vr|j                  , }}|j                  |j                  |f       ` | j                  ||       yc c}w )zCheck that the given LogicalLines match expectations.

    Args:
      llines: list of LogicalLine
      list_of_expected: list of (depth, values) pairs. Non-semantic tokens are
        filtered out from the expected values.
    N)tokensnamer   NONSEMANTIC_TOKENSvalueappenddepthassertEqual)selfllineslist_of_expectedactualllineftfiltered_valuess          A/usr/lib/python3/dist-packages/yapftests/pytree_unwrapper_test.py_CheckLogicalLinesz&PytreeUnwrapperTest._CheckLogicalLines   s     F 4 LLWWL;;; ((o 
 mmU[[/234 	%v.s   /A9c                     t        j                  d      }t        j                  |      }| j	                  |dg dfddgfdg dfg       y )Nz1
      x = 1
      # a comment
      y = 2
      r   x=1# a commentyr   2textwrapdedentr   ParseAndUnwrapr   r   coder   s      r   testSimpleFileScopez'PytreeUnwrapperTest.testSimpleFileScope-   sV    ?? 
 D
 ,,T2FF	
O	
]O	
O%     c                     t        j                  d      }t        j                  |      }| j	                  |dg dfg       y )Nz$
      y = (1 +
           x)
      r   )r   r   (r   +r   )r!   r%   s      r   testSimpleMultilineStatementz0PytreeUnwrapperTest.testSimpleMultilineStatement:   sE    ?? 
 D ,,T2FF	
/0% r(   c                     t        j                  d      }t        j                  |      }| j	                  |dg dfdg dfg       y )Nz.
      x = 1    # a comment
      y = 2
      r   )r   r   r   r   r   r!   r%   s      r   testFileScopeWithInlineCommentz2PytreeUnwrapperTest.testFileScopeWithInlineCommentD   sM    ?? 
 D ,,T2FF	
*+	
O% r(   c                     t        j                  d      }t        j                  |      }| j	                  |dg dfdg dfdg dfg       y )Nz5
      if foo:
          x = 1
          y = 2
      r   iffoo:   r   r   r!   r%   s      r   testSimpleIfz PytreeUnwrapperTest.testSimpleIfO   sU    ?? 
 D
 ,,T2FF	
	
O	
O% r(   c           	          t        j                  d      }t        j                  |      }| j	                  |ddgfdg dfdg dfdg dfg       y )NzE
      # c1
      if foo: # c2
          x = 1
          y = 2
      r   # c1)r2   r3   r4   # c2r5   r   r   r!   r%   s      r   testSimpleIfWithCommentsz,PytreeUnwrapperTest.testSimpleIfWithComments\   s^    ?? 
 D ,,T2FF	
VH	
&'	
O	
O	% r(   c           
          t        j                  d      }t        j                  |      }| j	                  |dg dfddgfdg dfddgfdg dfg       y )	NzX
      if foo:
          # c1
          x = 1 # c2
          # c3
          y = 2
      r   r1   r5   r8   )r   r   r   r9   # c3r   r!   r%   s      r   testIfWithCommentsInsidez,PytreeUnwrapperTest.testIfWithCommentsInsidek   sh    ?? 
 D ,,T2FF	
	
VH	
#$	
VH	
O% r(   c                     t        j                  d      }t        j                  |      }| j	                  |dg dfdg dfdg dfdg dfddd	gfdd
gfdg dfg       y )Nzu
       if x:
         x = 1 # c1
       elif y: # c2
         y = 1
       else:
         # c3
         z = 1
      r   r2   r   r4   r5   r   r   r   r8   )elifr   r4   r9   )r   r   r   elser4   r<   )zr   r   r!   r%   s      r   testIfElifElsez"PytreeUnwrapperTest.testIfElifElse|   s|    ?? 
 D ,,T2FF	
	
#$	
&'	
O	
VSM	
VH	
O% r(   c                     t        j                  d      }t        j                  |      }| j	                  |dg dfdg dfdg dfddgfdg d	fdg d
fg       y )Nzj
       if x:
         x = 1 # c1
         while t:
           # c2
           j = 1
         k = 1
      r   r?   r5   r@   )whiletr4      r9   )jr   r   )kr   r   r!   r%   s      r   testNestedCompoundTwoLevelz.PytreeUnwrapperTest.testNestedCompoundTwoLevel   sp    ?? 
 D ,,T2FF	
	
#$	
 	
VH	
O	
O% r(   c                     t        j                  d      }t        j                  |      }| j	                  |dg dfddgfdg dfg       y )Nz?
       while x > 1: # c1
          # c2
          x = 1
      r   )rF   r   >r   r4   r8   r5   r9   r   r!   r%   s      r   testSimpleWhilez#PytreeUnwrapperTest.testSimpleWhile   sV    ?? 
 D
 ,,T2FF	
12	
VH	
O% r(   c                     t        j                  d      }t        j                  |      }| j	                  |dddgfddgfdddgfddgfdddgfddgfdddgfddgfdd	dgfddgfg
       y )
Nz
      try:
        pass
      except:
        pass
      except:
        pass
      else:
        pass
      finally:
        pass
      r   tryr4   r5   passexceptrB   finallyr!   r%   s      r   testSimpleTryz!PytreeUnwrapperTest.testSimpleTry   s    ?? 
 D ,,T2FF	
UCL	
VH	
XsO	
VH	
XsO	
VH	
VSM	
VH	
Y	
VH% r(   c                     t        j                  d      }t        j                  |      }| j	                  |dg dfddgfdddgfg       y )Nz<
      def foo(x): # c1
        # c2
        return x
      r   defr3   r*   r   r,   r4   r8   r5   r9   returnr   r!   r%   s      r   testSimpleFuncdefz%PytreeUnwrapperTest.testSimpleFuncdef   sZ    ?? 
 D
 ,,T2FF	
67	
VH	
XsO% r(   c                     t        j                  d      }t        j                  |      }| j	                  |dg dfddgfdddgfdg dfdd	gfdddgfg       y )
Nzq
      def foo(x): # c1
        # c2
        return x

      def bar(): # c3
        # c4
        return x
      r   rV   r5   r9   rX   r   )rW   barr*   r,   r4   r<   z# c4r!   r%   s      r   testTwoFuncDefsz#PytreeUnwrapperTest.testTwoFuncDefs   sx    ?? 
 D ,,T2FF	
67	
VH	
XsO	
12	
VH	
XsO% r(   c                     t        j                  d      }t        j                  |      }| j	                  |dg dfddgfdg dfg       y )Nz:
      class Klass: # c1
        # c2
        p = 1
      r   )classKlassr4   r8   r5   r9   )pr   r   r!   r%   s      r   testSimpleClassDefz&PytreeUnwrapperTest.testSimpleClassDef   sV    ?? 
 D
 ,,T2FF	
+,	
VH	
O% r(   c                     t        j                  d      }t        j                  |      }| j	                  |dg dfdddgfg       y )Nz"
        def f(): return 37
      r   rW   fr*   r,   r4   r5   rX   37r!   r%   s      r   testSingleLineStmtInFuncz,PytreeUnwrapperTest.testSingleLineStmtInFunc   sR    ?? 
 D ,,T2FF	
'(	
Xt% r(   c                     t        j                  d      }t        j                  |      }| j	                  |ddgfddgfdg dfddgfg       y )NzR
        # Comment #1

        # Comment #2
        def f():
          pass
      r   z# Comment #1z# Comment #2rc   r5   rQ   r!   r%   s      r   testMultipleCommentsz(PytreeUnwrapperTest.testMultipleComments  sd    ?? 
 D ,,T2FF	
^	
^	
'(	
VH	% r(   c                     t        j                  d      }t        j                  |      }| j	                  |dg dfg       y )NzW
      a = [
          'a',
          'b',
          'c',  # hello world
      ]
      r   )ar   [z'a',z'b'rl   z'c'rl   z# hello world]r!   r%   s      r   testSplitListWithCommentz,PytreeUnwrapperTest.testSplitListWithComment  sH    ?? 
 D ,,T2FFa * & % 	r(   N)__name__
__module____qualname__r   r'   r-   r/   r6   r:   r=   rD   rK   rN   rT   rY   r\   ra   rf   rh   rn    r(   r   r   r      sW    /&	"*&6( 	r(   r   c                   $    e Zd Zd Zd Zd Zd Zy)MatchBracketsTestc                    g }|D ]W  }|j                   D cg c],  }|j                  t        j                  vr||j                  f. }}|sG|j                  |       Y t        |      D ]n  \  }}||   }|s|D ]  }	| j                  |	d           )|D ]A  \  }
}| j                  ||
   d   ||   d          | j                  ||   d   ||
   d          C p yc c}w )a*  Check that the tokens have the expected matching bracket.

    Arguments:
      llines: list of LogicalLine.
      list_of_expected: list of (index, index) pairs. The matching brackets at
        the indexes need to match. Non-semantic tokens are filtered out from the
        expected values.
    r5   r   N)	r   r	   r   r
   matching_bracketr   	enumerateassertIsNoner   )r   r   r   r   r   r   r   indexbracket_listr   open_bracketclose_brackets               r   _CheckMatchingBracketsz(MatchBracketsTest._CheckMatchingBrackets$  s    F '#(<<LGG<+J+JJ b112 Lo L 
o&'  ))9: L|Ume 	&E


E!H
%	& ,8 	L'L-


5.q153G3J
K


5/2E,4G4J
K	LLLs   1Cc                     t        j                  d      }t        j                  |      }| j	                  |g dg g       y )NzD        def foo(a, b=['w','d'], c=[42, 37]):
          pass
        ))rH      )      )      r"   r#   r   r$   r}   r%   s      r   testFunctionDefz!MatchBracketsTest.testFunctionDef?  sB    ??  D ,,T2F$
) r(   c                     t        j                  d      }t        j                  |      }| j	                  |dgdgg g       y )Nz@        @bar()
        def foo(a, b, c):
          pass
        )rH      rH      r   r%   s      r   testDecoratorzMatchBracketsTest.testDecoratorJ  sI    ??  D
 ,,T2F		
) r(   c                     t        j                  d      }t        j                  |      }| j	                  |dgg g       y )Nz1        class A(B, C, D):
          pass
        r   r   r%   s      r   testClassDefzMatchBracketsTest.testClassDefW  sD    ??  D ,,T2F	
) r(   N)ro   rp   rq   r}   r   r   r   rr   r(   r   rt   rt   "  s    L6		r(   rt   __main__)__doc__r"   unittestyapf.pytreer   	yapftestsr   YAPFTestr   rt   ro   mainrr   r(   r   <module>r      sZ    '   $ &G	*33 G	T>(11 >B z(--/ r(   