B
    >¶†\Ÿ  ã               @   sP   d dl Z d dlZd dlmZ d dlmZmZ G dd„ deƒZG dd„ deƒZdS )é    N)Úchecker)ÚTestCaseÚskipIfc               @   sL   e Zd ZdZedd„ ƒZdd„ Zdd„ Zee	j
dk d	ƒd
d„ ƒZdd„ ZdS )ÚTypeableVisitorTestsz&
    Tests of L{_TypeableVisitor}
    c             C   s    t  | ¡}t ¡ }| |¡ |S )zV
        Run L{_TypeableVisitor} on the parsed source and return the visitor.
        )ÚastÚparser   Z_TypeableVisitorZvisit)ÚsÚtreeÚvisitor© r   ú9lib/python3.7/site-packages/pyflakes/test/test_checker.pyÚ_run_visitor   s    

z!TypeableVisitorTests._run_visitorc             C   st   |   d¡}|  |jddddg¡ |  |jd tj¡ |  |jd tj¡ |  |jd tj¡ |  |jd tj	¡ dS )zA
        Test that the typeable node types are collected
        zx = 1  # assignment
for x in range(1): pass  # for loop
def f(): pass  # function definition
with a as b: pass  # with statement
é   é   é   é   N)
r   ÚassertEqualÚtypeable_linesÚassertIsInstanceÚtypeable_nodesr   ÚAssignZForÚFunctionDefZWith)Úselfr
   r   r   r   Útest_node_types   s    z$TypeableVisitorTests.test_node_typesc             C   sH   |   d¡}|  |jddg¡ |  |jd tj¡ |  |jd tj¡ dS )zƒ
        Test the common pitfall of missing `generic_visit` in visitors by
        ensuring that nested nodes are reported
        zdef f():
    x = 1
r   r   N)r   r   r   r   r   r   r   r   )r   r
   r   r   r   Útest_visitor_recurses)   s
    z*TypeableVisitorTests.test_visitor_recurses)r   é   zasync syntax introduced in py35c             C   s^   |   d¡}|  |jdddg¡ |  |jd tj¡ |  |jd tj¡ |  |jd tj¡ dS )z@
        Test that the PEP 492 node types are collected
        zoasync def f():  # async def
    async for x in y:  pass  # async for
    async with a as b: pass  # async with
r   r   r   N)	r   r   r   r   r   r   ZAsyncFunctionDefZAsyncForZ	AsyncWith)r   r
   r   r   r   Útest_py35_node_types8   s    z)TypeableVisitorTests.test_py35_node_typesc             C   s:   |   d¡}|  |jddg¡ |  |jd jd jd¡ dS )zn
        Test that when two typeable nodes are present on a line, the last
        typeable one wins.
        zx = 1; y = 1r   r   ÚyN)r   r   r   r   ZtargetsÚid)r   r
   r   r   r   Útest_last_node_winsI   s    
z(TypeableVisitorTests.test_last_node_winsN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústaticmethodr   r   r   r   ÚsysÚversion_infor   r   r   r   r   r   r      s   
r   c               @   st   e Zd ZdZe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 )ÚCollectTypeCommentsTestsz,
    Tests of L{_collect_type_comments}
    c             C   s2   t  | ¡}t | ¡}t ||¡}dd„ | ¡ D ƒS )zÒ
        Run L{_collect_type_comments} on the parsed source and return the
        mapping from nodes to comments.  The return value is converted to
        a set: {(node_type, tuple of comments), ...}
        c             S   s*   h | ]"\}}t |ƒtd d„ |D ƒƒf’qS )c             s   s   | ]\}}|V  qd S )Nr   )Ú.0Ú_r   r   r   r   ú	<genexpr>d   s    z>CollectTypeCommentsTests._collect.<locals>.<setcomp>.<genexpr>)ÚtypeÚtuple)r(   ÚkÚvr   r   r   ú	<setcomp>d   s    z4CollectTypeCommentsTests._collect.<locals>.<setcomp>)r   r   r   Zmake_tokensZ_collect_type_commentsÚitems)r   r	   ÚtokensÚretr   r   r   Ú_collectZ   s    

z!CollectTypeCommentsTests._collectc             C   s"   |   d¡}|  |tjdfh¡ dS )z@
        Test that the function works for binary source
        s   x = 1  # type: int)z# type: intN)r3   ÚassertSetEqualr   r   )r   r2   r   r   r   Ú
test_bytesf   s    
z#CollectTypeCommentsTests.test_bytesc             C   s"   |   d¡}|  |tjdfh¡ dS )z>
        Test that the function works for text source
        zx = 1  # type: int)z# type: intN)r3   r   r   r   )r   r2   r   r   r   Ú	test_textm   s    
z"CollectTypeCommentsTests.test_textc             C   s   |   d¡}|  |tƒ ¡ dS )z9
        Test that a non-type comment is ignored
        zx = 1  # noqaN)r3   r4   Úset)r   r2   r   r   r   Útest_non_type_comment_ignoredt   s    
z6CollectTypeCommentsTests.test_non_type_comment_ignoredc             C   s   |   d¡}|  |tƒ ¡ dS )zP
        Test that a type comment before something typeable is ignored.
        z# type: int
x = 1N)r3   r4   r7   )r   r2   r   r   r   Ú!test_type_comment_before_typeable{   s    
z:CollectTypeCommentsTests.test_type_comment_before_typeablec             C   s   |   d¡}|  |tƒ ¡ dS )zH
        Test that `# type: ignore` comments are not collected.
        zx = 1  # type: ignoreN)r3   r4   r7   )r   r2   r   r   r   Ú test_type_ignore_comment_ignored‚   s    
z9CollectTypeCommentsTests.test_type_ignore_comment_ignoredc             C   s4   |   d¡}|  |tƒ ¡ |   d¡}|  |tƒ ¡ dS )zg
        Test that `# type: ignore` comments with more content are also not
        collected.
        zx = 1  # type: ignore # noqazx = 1  #type:ignore#noqaN)r3   r4   r7   )r   r2   r   r   r   Ú*test_type_ignore_with_other_things_ignored‰   s    

zCCollectTypeCommentsTests.test_type_ignore_with_other_things_ignoredc             C   s"   |   d¡}|  |tjdfh¡ d S )Nzx = 1  # type: int  # noqa)z# type: int  # noqa)r3   r4   r   r   )r   r2   r   r   r   Ú,test_type_comment_with_extra_still_collected“   s    
zECollectTypeCommentsTests.test_type_comment_with_extra_still_collectedc             C   s"   |   d¡}|  |tjdfh¡ d S )Nzx = 1 #type:int)z	#type:int)r3   r4   r   r   )r   r2   r   r   r   Ú$test_type_comment_without_whitespace—   s    
z=CollectTypeCommentsTests.test_type_comment_without_whitespacec             C   s"   |   d¡}|  |tjdfh¡ d S )Nzx = 1 # type: ignore[T])z# type: ignore[T])r3   r4   r   r   )r   r2   r   r   r   Ú)test_type_comment_starts_with_word_ignore›   s    
zBCollectTypeCommentsTests.test_type_comment_starts_with_word_ignorec             C   s"   |   d¡}|  |tjdfh¡ dS )zn
        Test that when two typeable nodes are present on a line, the last
        typeable one wins.
        zdef f(): x = 1  # type: int)z# type: intN)r3   r4   r   r   )r   r2   r   r   r   r   Ÿ   s    
z,CollectTypeCommentsTests.test_last_node_winsc             C   s&   |   d¡}tjdfh}|  ||¡ dS )zw
        Test that type comments for function arguments are all attributed to
        the function definition.
        z]def f(
        a,  # type: int
        b,  # type: str
):
    # type: (...) -> None
    pass
)z# type: intz# type: strz# type: (...) -> NoneN)r3   r   r   r4   )r   r2   Zexpectedr   r   r   Ú#test_function_def_assigned_comments§   s
    z<CollectTypeCommentsTests.test_function_def_assigned_commentsN)r    r!   r"   r#   r$   r3   r5   r6   r8   r9   r:   r;   r<   r=   r>   r   r?   r   r   r   r   r'   U   s   
r'   )	r   r%   Zpyflakesr   Zpyflakes.test.harnessr   r   r   r'   r   r   r   r   Ú<module>   s
   M