a
    Ø
þdG$  ã                   @   sd   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r`e ¡  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   úh/var/www/html/stable-diffusion-webui/venv/lib/python3.9/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-   s    



ýz'PytreeUnwrapperTest.testSimpleFileScopec                 C   s.   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)r.   r/   r0   ú# c2r1   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-   r1   r3   )r   r   r   r4   ú# 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   ©r.   r   r0   r1   ©r   r   r   r3   )Úelifr   r0   r4   )r   r   r   Úelser0   r6   )Ú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   r8   r1   r9   )ÚwhileÚtr0   é   r4   )Újr   r   )Úkr   r   r!   r%   r   r   r   ÚtestNestedCompoundTwoLevel‘   s    






úz.PytreeUnwrapperTest.testNestedCompoundTwoLevelc                 C   s@   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   r0   r3   r1   r4   r   r!   r%   r   r   r   ÚtestSimpleWhile¤   s    



ý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   Útryr0   r1   Ú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   ©Údefr/   r(   r   r*   r0   r3   r1   r4   Úreturnr   r!   r%   r   r   r   ÚtestSimpleFuncdefÌ   s    



ý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   rK   r1   r4   rM   r   )rL   Úbarr(   r*   r0   r6   z# c4r!   r%   r   r   r   ÚtestTwoFuncDefsÙ   s    
	




úz#PytreeUnwrapperTest.testTwoFuncDefsc                 C   s@   t  d¡}t |¡}|  |dg d¢fddgfdg d¢fg¡ d S )Nz:
      class Klass: # c1
        # c2
        p = 1
      r   )ÚclassZKlassr0   r3   r1   r4   )Úpr   r   r!   r%   r   r   r   ÚtestSimpleClassDefí   s    



ý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   ©rL   Úfr(   r*   r0   r1   rM   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 #2rT   r1   rG   r!   r%   r   r   r   ÚtestMultipleComments  s    


üz(PytreeUnwrapperTest.testMultipleCommentsc                 C   s.   t  d¡}t |¡}|  |dg d¢fg¡ d S )NzW
      a = [
          'a',
          'b',
          'c',  # hello world
      ]
      r   )Úar   ú[z'a'ú,z'b'rZ   z'c'rZ   z# hello worldú]r!   r%   r   r   r   ÚtestSplitListWithComment  s    

z,PytreeUnwrapperTest.testSplitListWithCommentN)Ú__name__Ú
__module__Ú__qualname__r   r'   r+   r,   r2   r5   r7   r=   rC   rE   rJ   rN   rP   rS   rV   rW   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 ]t\}}|| }|sb|D ]}|  |d ¡ qLq4|D ]@\}	}
|  ||	 d ||
 d ¡ |  ||
 d ||	 d ¡ qfq4d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>r1   r   N)r   r   Ú	enumerateZassertIsNoner   )r   r   r   r   r   r   ÚindexZbracket_listr   Z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
        ))r@   é   )é   é   )é   é   ©r"   r#   r   r$   rc   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
        )r@   é   ©r@   é   ri   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
        rl   ri   r%   r   r   r   ÚtestClassDefW  s    

þzMatchBracketsTest.testClassDefN)r]   r^   r_   rc   rj   rn   ro   r   r   r   r   r`   "  s   r`   Ú__main__)Ú__doc__r"   ZunittestZyapf.pytreer   Z	yapftestsr   ZYAPFTestr   r`   r]   Úmainr   r   r   r   Ú<module>   s     A