o
    ?n0jG$  ã                   @   sh   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G dd„ dejƒZ	e
d	kr2e ¡  dS dS )
z Tests for yapf.pytree_unwrapper.é    N)Úpytree_utils)Úyapf_test_helperc                   @   s”   e Z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!d"„ Zd#S )$ÚPytreeUnwrapperTestc                 C   s>   g }|D ]}dd„ |j D ƒ}| |j|f¡ q|  ||¡ dS )zåCheck 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.
    c                 S   s   g | ]}|j tjvr|j‘qS © )Únamer   ÚNONSEMANTIC_TOKENSÚvalue©Ú.0Úftr   r   úf/home/wildlama/miniconda3/envs/lam_a2e/lib/python3.10/site-packages/yapftests/pytree_unwrapper_test.pyÚ
<listcomp>$   s
    þz:PytreeUnwrapperTest._CheckLogicalLines.<locals>.<listcomp>N)ÚtokensÚappendÚdepthÚassertEqual)ÚselfÚllinesÚlist_of_expectedÚactualÚllineÚfiltered_valuesr   r   r   Ú_CheckLogicalLines   s   þz&PytreeUnwrapperTest._CheckLogicalLinesc                 C   s@   t  d¡}t |¡}|  |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   r   r   r   ÚtestSimpleFileScope-   ó   




ýz'PytreeUnwrapperTest.testSimpleFileScopec                 C   ó.   t  d¡}t |¡}|  |dg d¢fg¡ d S )Nz$
      y = (1 +
           x)
      r   )r   r   ú(r   ú+r   ú)r!   r%   r   r   r   ÚtestSimpleMultilineStatement:   s
   



ÿz0PytreeUnwrapperTest.testSimpleMultilineStatementc                 C   s8   t  d¡}t |¡}|  |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%   r   r   r   ÚtestFileScopeWithInlineCommentD   s   




þz2PytreeUnwrapperTest.testFileScopeWithInlineCommentc                 C   sB   t  d¡}t |¡}|  |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%   r   r   r   ÚtestSimpleIfO   s   





ýz PytreeUnwrapperTest.testSimpleIfc              	   C   sJ   t  d¡}t |¡}|  |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)r0   r1   r2   ú# c2r3   r   r   r!   r%   r   r   r   ÚtestSimpleIfWithComments\   s   





üz,PytreeUnwrapperTest.testSimpleIfWithCommentsc              
   C   sR   t  d¡}t |¡}|  |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   r/   r3   r5   )r   r   r   r6   ú# c3r   r!   r%   r   r   r   ÚtestIfWithCommentsInsidek   s   





ûz,PytreeUnwrapperTest.testIfWithCommentsInsidec                 C   sh   t  d¡}t |¡}|  |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   ©r0   r   r2   r3   ©r   r   r   r5   )Úelifr   r2   r6   )r   r   r   Úelser2   r8   )Úzr   r   r!   r%   r   r   r   ÚtestIfElifElse|   s   

	






ùz"PytreeUnwrapperTest.testIfElifElsec                 C   s^   t  d¡}t |¡}|  |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   r:   r3   r;   )ÚwhileÚtr2   é   r6   )Újr   r   )Úkr   r   r!   r%   r   r   r   ÚtestNestedCompoundTwoLevel‘   s   







úz.PytreeUnwrapperTest.testNestedCompoundTwoLevelc                 C   ó@   t  d¡}t |¡}|  |dg d¢fddgfdg d¢fg¡ d S )Nz?
       while x > 1: # c1
          # c2
          x = 1
      r   )r@   r   ú>r   r2   r5   r3   r6   r   r!   r%   r   r   r   ÚtestSimpleWhile¤   r(   z#PytreeUnwrapperTest.testSimpleWhilec                 C   s~   t  d¡}t |¡}|  |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   Útryr2   r3   ÚpassÚexceptr=   Úfinallyr!   r%   r   r   r   ÚtestSimpleTry±   s   







öz!PytreeUnwrapperTest.testSimpleTryc                 C   s@   t  d¡}t |¡}|  |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   ©Údefr1   r*   r   r,   r2   r5   r3   r6   Úreturnr   r!   r%   r   r   r   ÚtestSimpleFuncdefÌ   r(   z%PytreeUnwrapperTest.testSimpleFuncdefc                 C   s\   t  d¡}t |¡}|  |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   rN   r3   r6   rP   r   )rO   Úbarr*   r,   r2   r8   z# c4r!   r%   r   r   r   ÚtestTwoFuncDefsÙ   s   

	




úz#PytreeUnwrapperTest.testTwoFuncDefsc                 C   rF   )Nz:
      class Klass: # c1
        # c2
        p = 1
      r   )ÚclassZKlassr2   r5   r3   r6   )Úpr   r   r!   r%   r   r   r   ÚtestSimpleClassDefí   r(   z&PytreeUnwrapperTest.testSimpleClassDefc                 C   s8   t  d¡}t |¡}|  |dg d¢fdddgfg¡ d S )Nz"
        def f(): return 37
      r   ©rO   Úfr*   r,   r2   r3   rP   Z37r!   r%   r   r   r   ÚtestSingleLineStmtInFuncú   s   




þz,PytreeUnwrapperTest.testSingleLineStmtInFuncc                 C   sF   t  d¡}t |¡}|  |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 #2rW   r3   rJ   r!   r%   r   r   r   ÚtestMultipleComments  s   



üz(PytreeUnwrapperTest.testMultipleCommentsc                 C   r)   )NzW
      a = [
          'a',
          'b',
          'c',  # hello world
      ]
      r   )Úar   ú[z'a'ú,z'b'r]   z'c'r]   z# hello worldú]r!   r%   r   r   r   ÚtestSplitListWithComment  s   

z,PytreeUnwrapperTest.testSplitListWithCommentN)Ú__name__Ú
__module__Ú__qualname__r   r'   r-   r.   r4   r7   r9   r?   rE   rH   rM   rQ   rS   rV   rY   rZ   r_   r   r   r   r   r      s$    

r   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚMatchBracketsTestc                 C   s®   g }|D ]}dd„ |j D ƒ}|r| |¡ qt|ƒD ]:\}}|| }|s1|D ]	}|  |d ¡ q&q|D ] \}	}
|  ||	 d ||
 d ¡ |  ||
 d ||	 d ¡ q3qdS )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.
    c                 S   s"   g | ]}|j tjvr||jf‘qS r   )r   r   r   Zmatching_bracketr	   r   r   r   r   /  s
    þz<MatchBracketsTest._CheckMatchingBrackets.<locals>.<listcomp>r3   r   N)r   r   Ú	enumerateZassertIsNoner   )r   r   r   r   r   r   ÚindexZbracket_listr   Úopen_bracketZclose_bracketr   r   r   Ú_CheckMatchingBrackets$  s&   	ÿ
€ÿþúz(MatchBracketsTest._CheckMatchingBracketsc                 C   s,   t  d¡}t |¡}|  |g d¢g g¡ d S )NzD        def foo(a, b=['w','d'], c=[42, 37]):
          pass
        ))rB   é   )é   é   )é   é   ©r"   r#   r   r$   rg   r%   r   r   r   ÚtestFunctionDef?  s   


þz!MatchBracketsTest.testFunctionDefc                 C   s.   t  d¡}t |¡}|  |dgdgg g¡ d S )Nz@        @bar()
        def foo(a, b, c):
          pass
        )rB   é   ©rB   é   rm   r%   r   r   r   ÚtestDecoratorJ  s   


ýzMatchBracketsTest.testDecoratorc                 C   s*   t  d¡}t |¡}|  |dgg g¡ d S )Nz1        class A(B, C, D):
          pass
        rp   rm   r%   r   r   r   ÚtestClassDefW  s   


þzMatchBracketsTest.testClassDefN)r`   ra   rb   rg   rn   rr   rs   r   r   r   r   rc   "  s
    rc   Ú__main__)Ú__doc__r"   ZunittestZyapf.pytreer   Z	yapftestsr   ZYAPFTestr   rc   r`   Úmainr   r   r   r   Ú<module>   s     Aÿ