
    `N`$                         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                     dS dS )
z Tests for yapf.pytree_unwrapper.    N)pytree_utils)yapf_test_helperc                   t    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dS )PytreeUnwrapperTestc                     g }|D ]/}d |j         D             }|                    |j        |f           0|                     ||           dS )zCheck that the given UnwrappedLines match expectations.

    Args:
      uwlines: list of UnwrappedLine
      list_of_expected: list of (depth, values) pairs. Non-semantic tokens are
        filtered out from the expected values.
    c                 B    g | ]}|j         t          j        v|j        S  )namer   NONSEMANTIC_TOKENSvalue.0fts     ?lib/python3.11/site-packages/yapftests/pytree_unwrapper_test.py
<listcomp>z<PytreeUnwrapperTest._CheckUnwrappedLines.<locals>.<listcomp>$   s4       WL;;; (;;;    N)tokensappenddepthassertEqual)selfuwlineslist_of_expectedactualuwlfiltered_valuess         r   _CheckUnwrappedLinesz(PytreeUnwrapperTest._CheckUnwrappedLines   ss     F 2 2 J  o
 mmSY01111%v.....r   c                     t          j        d          }t          j        |          }|                     |dg dfddgfdg dfg           d S )Nz1
      x = 1
      # a comment
      y = 2
      r   x=1# a commentyr!   2textwrapdedentr   ParseAndUnwrapr   r   coder   s      r   testSimpleFileScopez'PytreeUnwrapperTest.testSimpleFileScope-   su    ? 
  D
 -d33Gg	
OOO	
]O	
OOO(     r   c                     t          j        d          }t          j        |          }|                     |dg dfg           d S )Nz$
      y = (1 +
           x)
      r   )r%   r!   (r"   +r    )r'   r+   s      r   testSimpleMultilineStatementz0PytreeUnwrapperTest.testSimpleMultilineStatement:   sb    ? 
  D -d33Gg	
///0(     r   c                     t          j        d          }t          j        |          }|                     |dg dfdg dfg           d S )Nz.
      x = 1    # a comment
      y = 2
      r   )r    r!   r"   r#   r$   r'   r+   s      r   testFileScopeWithInlineCommentz2PytreeUnwrapperTest.testFileScopeWithInlineCommentD   sn    ? 
  D -d33Gg	
***+	
OOO(     r   c                     t          j        d          }t          j        |          }|                     |dg dfdg dfdg dfg           d S )Nz5
      if foo:
          x = 1
          y = 2
      r   iffoo:   r   r$   r'   r+   s      r   testSimpleIfz PytreeUnwrapperTest.testSimpleIfO   sz    ? 
  D
 -d33Gg	
	
OOO	
OOO(     r   c           	          t          j        d          }t          j        |          }|                     |ddgfdg dfdg dfdg dfg           d S )NzE
      # c1
      if foo: # c2
          x = 1
          y = 2
      r   # c1)r7   r8   r9   # c2r:   r   r$   r'   r+   s      r   testSimpleIfWithCommentsz,PytreeUnwrapperTest.testSimpleIfWithComments\   s    ? 
  D -d33Gg	
VH	
&&&'	
OOO	
OOO	(     r   c           
          t          j        d          }t          j        |          }|                     |dg dfddgfdg dfddgfdg dfg           d S )	NzX
      if foo:
          # c1
          x = 1 # c2
          # c3
          y = 2
      r   r6   r:   r=   )r    r!   r"   r>   # c3r$   r'   r+   s      r   testIfWithCommentsInsidez,PytreeUnwrapperTest.testIfWithCommentsInsidek   s    ? 
  D -d33Gg	
	
VH	
###$	
VH	
OOO(     r   c                     t          j        d          }t          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           d S )Nzu
       if x:
         x = 1 # c1
       elif y: # c2
         y = 1
       else:
         # c3
         z = 1
      r   r7   r    r9   r:   r    r!   r"   r=   )elifr%   r9   r>   )r%   r!   r"   elser9   rA   )zr!   r"   r'   r+   s      r   testIfElifElsez"PytreeUnwrapperTest.testIfElifElse|   s    ? 
  D -d33Gg	
	
###$	
&&&'	
OOO	
VSM	
VH	
OOO(     r   c                     t          j        d          }t          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           d S )Nzj
       if x:
         x = 1 # c1
         while t:
           # c2
           j = 1
         k = 1
      r   rD   r:   rE   )whiletr9      r>   )jr!   r"   )kr!   r"   r'   r+   s      r   testNestedCompoundTwoLevelz.PytreeUnwrapperTest.testNestedCompoundTwoLevel   s    ? 
  D -d33Gg	
	
###$	
 	
VH	
OOO	
OOO(     r   c                     t          j        d          }t          j        |          }|                     |dg dfddgfdg dfg           d S )Nz?
       while x > 1: # c1
          # c2
          x = 1
      r   )rK   r    >r"   r9   r=   r:   r>   r   r'   r+   s      r   testSimpleWhilez#PytreeUnwrapperTest.testSimpleWhile   sw    ? 
  D
 -d33Gg	
1112	
VH	
OOO(     r   c                     t          j        d          }t          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
           d S )
Nz
      try:
        pass
      except:
        pass
      except:
        pass
      else:
        pass
      finally:
        pass
      r   tryr9   r:   passexceptrG   finallyr'   r+   s      r   testSimpleTryz!PytreeUnwrapperTest.testSimpleTry   s    ? 
  D -d33Gg	
UCL	
VH	
XsO	
VH	
XsO	
VH	
VSM	
VH	
Y	
VH(     r   c                     t          j        d          }t          j        |          }|                     |dg dfddgfdddgfg           d S )Nz<
      def foo(x): # c1
        # c2
        return x
      r   defr8   r/   r    r1   r9   r=   r:   r>   returnr    r'   r+   s      r   testSimpleFuncdefz%PytreeUnwrapperTest.testSimpleFuncdef   sw    ? 
  D
 -d33Gg	
6667	
VH	
XsO(     r   c                     t          j        d          }t          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           d S )
Nzq
      def foo(x): # c1
        # c2
        return x

      def bar(): # c3
        # c4
        return x
      r   r[   r:   r>   r]   r    )r\   barr/   r1   r9   rA   z# c4r'   r+   s      r   testTwoFuncDefsz#PytreeUnwrapperTest.testTwoFuncDefs   s    ? 
  D -d33Gg	
6667	
VH	
XsO	
1112	
VH	
XsO(     r   c                     t          j        d          }t          j        |          }|                     |dg dfddgfdg dfg           d S )Nz:
      class Klass: # c1
        # c2
        p = 1
      r   )classKlassr9   r=   r:   r>   )pr!   r"   r'   r+   s      r   testSimpleClassDefz&PytreeUnwrapperTest.testSimpleClassDef   sw    ? 
  D
 -d33Gg	
+++,	
VH	
OOO(     r   c                     t          j        d          }t          j        |          }|                     |dg dfdddgfg           d S )Nz"
        def f(): return 37
      r   r\   fr/   r1   r9   r:   r]   37r'   r+   s      r   testSingleLineStmtInFuncz,PytreeUnwrapperTest.testSingleLineStmtInFunc   so    ? 
  D -d33Gg	
'''(	
Xt(     r   c                     t          j        d          }t          j        |          }|                     |ddgfddgfdg dfddgfg           d S )NzR
        # Comment #1

        # Comment #2
        def f():
          pass
      r   z# Comment #1z# Comment #2rh   r:   rV   r'   r+   s      r   testMultipleCommentsz(PytreeUnwrapperTest.testMultipleComments  s    ? 
  D -d33Gg	
^	
^	
'''(	
VH	(     r   c                     t          j        d          }t          j        |          }|                     |dg dfg           d S )NzW
      a = [
          'a',
          'b',
          'c',  # hello world
      ]
      r   )ar!   [z'a',z'b'rq   z'c'rq   z# hello world]r'   r+   s      r   testSplitListWithCommentz,PytreeUnwrapperTest.testSplitListWithComment  si    ? 
  D -d33Gg - - - ) ( 	 	 	 	 	r   N)__name__
__module____qualname__r   r-   r2   r4   r;   r?   rB   rI   rP   rS   rY   r^   ra   rf   rk   rm   rs   r	   r   r   r   r      s       / / /&    	 	 	      "  *  &    6    (       	 	 	 	 	r   r   c                   &    e Zd Zd Zd Zd Zd ZdS )MatchBracketsTestc                    g }|D ]*}d |j         D             }|r|                    |           +t          |          D ]\  }}||         }|s!|D ]}	|                     |	d                    0|D ]a\  }
}|                     ||
         d         ||         d                    |                     ||         d         ||
         d                    bdS )a-  Check that the tokens have the expected matching bracket.

    Arguments:
      uwlines: list of UnwrappedLine.
      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.
    c                 F    g | ]}|j         t          j        v||j        fS r	   )r
   r   r   matching_bracketr   s     r   r   z<MatchBracketsTest._CheckMatchingBrackets.<locals>.<listcomp>/  s=     L L LG<+JJJ b12JJJr   r:   r   N)r   r   	enumerateassertIsNoner   )r   r   r   r   r   r   indexbracket_listuwliner   open_bracketclose_brackets               r   _CheckMatchingBracketsz(MatchBracketsTest._CheckMatchingBrackets$  s;    F ' 'L L#&:L L Lo 
 'o&&&()9:: N N|e}f N 	& 	&E


E!H
%
%
%
%	& ,8 	N 	N'L-


6,/2F=4I!4L
M
M
M


6-03VL5I!5L
M
M
M
M	NN Nr   c                     t          j        d          }t          j        |          }|                     |g dg g           d S )NzD        def foo(a, b=['w','d'], c=[42, 37]):
          pass
        ))rM      )      )      r(   r)   r   r*   r   r+   s      r   testFunctionDefz!MatchBracketsTest.testFunctionDef?  s_    ?   D -d33G$$$
*     r   c                     t          j        d          }t          j        |          }|                     |dgdgg g           d S )Nz@        @bar()
        def foo(a, b, c):
          pass
        )rM      rM      r   r+   s      r   testDecoratorzMatchBracketsTest.testDecoratorJ  s`    ?   D
 -d33G		
*     r   c                     t          j        d          }t          j        |          }|                     |dgg g           d S )Nz1        class A(B, C, D):
          pass
        r   r   r+   s      r   testClassDefzMatchBracketsTest.testClassDefW  s[    ?   D -d33G	
*     r   N)rt   ru   rv   r   r   r   r   r	   r   r   rx   rx   "  sS        N N N6	 	 	  	 	 	 	 	r   rx   __main__)__doc__r(   unittestyapf.yapflibr   	yapftestsr   YAPFTestr   rx   rt   mainr	   r   r   <module>r      s    ' &   % % % % % % & & & & & &G	 G	 G	 G	 G	*3 G	 G	 G	T> > > > >(1 > > >B z(-///// r   