B
    >¶†\¯1  ã               @   sæ   d dl Z d dlZd dlmZ d dlmZmZmZ d dl	m
Z d dlm
Z d dlm
Z d dlmZmZ ye j dZW n ek
rŠ   dZY nX G dd	„ d	eƒZG d
d„ deƒZ
G dd„ deeƒZG dd„ deeƒZG dd„ deeƒZdS )é    N)Úmessages)ÚDoctestScopeÚFunctionScopeÚModuleScope)ÚTest)ÚTestCaseÚskipTFc                   s(   e Zd ZdZdd„ Z‡ fdd„Z‡  ZS )Ú_DoctestMixinTc             C   s¬   g }xŽt  |¡ ¡ D ]|}| ¡ dkr&n`| d¡st| d¡st| d¡st| d¡st| d¡st| d¡st|r~|d  d	¡r~d
| }nd| }| |¡ qW t  d¡}|d |¡ S )NÚ ú zexcept:zexcept zfinally:zelse:zelif éÿÿÿÿ)z>>> @z... @z... %sz>>> %szo            def doctest_something():
                """
                   %s
                """
            z
       )ÚtextwrapÚdedentÚ
splitlinesÚstripÚ
startswithÚappendÚjoin)ÚselfÚinputÚlinesÚlineZdoctestificator© r   ú:lib/python3.7/site-packages/pyflakes/test/test_doctests.pyÚ
doctestify   s"    






z_DoctestMixin.doctestifyc                s   t t| ƒj|  |¡f|ž|ŽS )N)Úsuperr	   Úflakesr   )r   r   ÚargsÚkw)Ú	__class__r   r   r   2   s    z_DoctestMixin.flakes)Ú__name__Ú
__module__Ú__qualname__ÚwithDoctestr   r   Ú__classcell__r   r   )r   r   r	      s   r	   c               @   s¸   e Zd 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edƒ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 ),r   Tc             C   sâ   |   d¡}|j}dd„ |D ƒ}dd„ |D ƒ}dd„ |D ƒ}|  t|ƒd¡ |  t|ƒd¡ |d }|d }|  |t¡ |  |t¡ |  |t¡ |  |t¡ |  	d|¡ |  	d	|¡ |  	d
|¡ |  t|ƒd¡ |  	d|d ¡ dS )z-Check that a doctest is given a DoctestScope.z§
        m = None

        def doctest_stuff():
            '''
                >>> d = doctest_stuff()
            '''
            f = m
            return f
        c             S   s   g | ]}|j tkr|‘qS r   )r   r   )Ú.0Úscoper   r   r   ú
<listcomp>I   s    z)Test.test_scope_class.<locals>.<listcomp>c             S   s   g | ]}|j tkr|‘qS r   )r   r   )r%   r&   r   r   r   r'   K   s    c             S   s   g | ]}|j tkr|‘qS r   )r   r   )r%   r&   r   r   r   r'   M   s    é   r   ÚmÚdoctest_stuffÚdÚfN)
r   Ú
deadScopesÚassertEqualÚlenZassertIsInstancer   r   ZassertNotIsInstancer   ÚassertIn)r   ÚcheckerÚscopesÚmodule_scopesÚdoctest_scopesÚfunction_scopesÚmodule_scopeÚdoctest_scoper   r   r   Útest_scope_class:   s&    	zTest.test_scope_classc             C   sÂ   |   d¡}|j}dd„ |D ƒ}dd„ |D ƒ}dd„ |D ƒ}|  t|ƒd¡ |  t|ƒd¡ |d }|d }|  d|¡ |  d	|¡ |  d
|¡ |  t|ƒd¡ |  d|d ¡ |  d|d ¡ dS )z'Check that nested doctests are ignored.a˜  
        m = None

        def doctest_stuff():
            '''
                >>> def function_in_doctest():
                ...     """
                ...     >>> ignored_undefined_name
                ...     """
                ...     df = m
                ...     return df
                ...
                >>> function_in_doctest()
            '''
            f = m
            return f
        c             S   s   g | ]}|j tkr|‘qS r   )r   r   )r%   r&   r   r   r   r'   x   s    z4Test.test_nested_doctest_ignored.<locals>.<listcomp>c             S   s   g | ]}|j tkr|‘qS r   )r   r   )r%   r&   r   r   r   r'   z   s    c             S   s   g | ]}|j tkr|‘qS r   )r   r   )r%   r&   r   r   r   r'   |   s    r(   r   r)   r*   Úfunction_in_doctesté   r,   ÚdfN)r   r-   r.   r/   r0   )r   r1   r2   r3   r4   r5   r6   r7   r   r   r   Útest_nested_doctest_ignoredb   s     z Test.test_nested_doctest_ignoredc             C   sÒ   |   d¡}|j}dd„ |D ƒ}dd„ |D ƒ}dd„ |D ƒ}|  t|ƒd¡ |  t|ƒd¡ |d }|d }|  d|¡ |  d	|¡ |  t|ƒd
¡ |  d|d ¡ |  d|d ¡ |  d|d ¡ |  d|¡ dS )z;Check that global in doctest does not pollute module scope.a[  
        def doctest_stuff():
            '''
                >>> def function_in_doctest():
                ...     global m
                ...     m = 50
                ...     df = 10
                ...     m = df
                ...
                >>> function_in_doctest()
            '''
            f = 10
            return f

        c             S   s   g | ]}|j tkr|‘qS r   )r   r   )r%   r&   r   r   r   r'   ¡   s    z;Test.test_global_module_scope_pollution.<locals>.<listcomp>c             S   s   g | ]}|j tkr|‘qS r   )r   r   )r%   r&   r   r   r   r'   £   s    c             S   s   g | ]}|j tkr|‘qS r   )r   r   )r%   r&   r   r   r   r'   ¥   s    r(   r   r*   r9   r:   r,   r;   r)   N)r   r-   r.   r/   r0   ZassertNotIn)r   r1   r2   r3   r4   r5   r6   r7   r   r   r   Ú"test_global_module_scope_pollution   s"    z'Test.test_global_module_scope_pollutionc             C   s   |   dtj¡ d S )Nzn
        global m

        def doctest_stuff():
            '''
                >>> m
            '''
        )r   r)   ÚUndefinedName)r   r   r   r   Útest_global_undefined¸   s    zTest.test_global_undefinedc             C   s   |   dtjtj¡ dS )z*Doctest within nested class are processed.a  
        class C:
            class D:
                '''
                    >>> m
                '''
                def doctest_stuff(self):
                    '''
                        >>> m
                    '''
                    return 1
        N)r   r)   r>   )r   r   r   r   Útest_nested_classÂ   s    zTest.test_nested_classc             C   s   |   d¡ dS )z<Doctest module does not process doctest in nested functions.a=  
        def doctest_stuff():
            def inner_function():
                '''
                    >>> syntax error
                    >>> inner_function()
                    1
                    >>> m
                '''
                return 1
            m = inner_function()
            return m
        N)r   )r   r   r   r   Útest_ignore_nested_functionÑ   s    z Test.test_ignore_nested_functionc             C   s   |   dtj¡ dS )z#Doctest may not access class scope.z¬
        class C:
            def doctest_stuff(self):
                '''
                    >>> m
                '''
                return 1
            m = 1
        N)r   r)   r>   )r   r   r   r   Útest_inaccessible_scope_classæ   s    z"Test.test_inaccessible_scope_classc             C   s   |   d¡ d S )Nzr
        import foo

        def doctest_stuff():
            '''
                >>> foo
            '''
        )r   )r   r   r   r   Útest_importBeforeDoctestò   s    zTest.test_importBeforeDoctestZtodoc             C   s   |   dtj¡ d S )Nzž
        import foo

        def doctest_stuff():
            """
                >>> import foo
                >>> foo
            """

        foo
        )r   r)   ZRedefinedWhileUnused)r   r   r   r   Útest_importBeforeAndInDoctestü   s    
z"Test.test_importBeforeAndInDoctestc             C   s   |   d¡ d S )NzŸ
        def doctest_stuff():
            """
                >>> import foo
                >>> foo
            """

        import foo
        foo()
        )r   )r   r   r   r   Útest_importInDoctestAndAfter
  s    	z!Test.test_importInDoctestAndAfterc             C   s4   |   dtj¡jd }|  |jd¡ |  |jd¡ d S )Nzg

        def doctest_stuff():
            """
                >>> x # line 5
            """

        r   é   é   )r   r)   r>   r   r.   ÚlinenoÚcol)r   Úexcr   r   r   Útest_offsetInDoctests  s    zTest.test_offsetInDoctestsc             C   s4   |   dtj¡jd }|  |jd¡ |  |jd¡ d S )Nzo

        def doctest_stuff():
            """
                >>> lambda: x # line 5
            """

        r   rF   é   )r   r)   r>   r   r.   rH   rI   )r   rJ   r   r   r   Útest_offsetInLambdasInDoctests"  s    z#Test.test_offsetInLambdasInDoctestsc             C   s4   |   dtj¡jd }|  |jd¡ |  |jd¡ d S )Nzm

        def doctest_stuff():
            """
                >>> x = 5
            """

        x

        r   é   )r   r)   r>   r   r.   rH   rI   )r   rJ   r   r   r   Útest_offsetAfterDoctests.  s    	zTest.test_offsetAfterDoctestsc             C   sÔ   |   dtjtjtj¡j}|d }|  |jd¡ tjdkrH|  |jd¡ n|  |jd¡ |d }|  |jd¡ t	r€|  |jd	¡ n|  |jd
¡ |d }|  |jd¡ t	s²tjdkrÂ|  |jd	¡ n|  |jd¡ d S )NzÐ
            def doctest_stuff():
                """
                    >>> from # line 4
                    >>>     fortytwo = 42
                    >>> except Exception:
                """
            r   é   )é   rN   é   é   r(   rF   é   é   r:   é   )
r   r)   ÚDoctestSyntaxErrorr   r.   rH   ÚsysÚversion_inforI   ÚPYPY)r   Ú
exceptionsrJ   r   r   r   Útest_syntaxErrorInDoctest<  s(    

zTest.test_syntaxErrorInDoctestc             C   sR   |   dtj¡jd }|  |jd¡ ts0tjdkr@|  |j	d¡ n|  |j	d¡ d S )Nz|
        def doctest_stuff():
            """
                >>> if True:
                ... pass
            """
        r   rF   )rQ   rN   rT   rU   )
r   r)   rW   r   r.   rH   rZ   rX   rY   rI   )r   rJ   r   r   r   Útest_indentationErrorInDoctest`  s    z#Test.test_indentationErrorInDoctestc             C   sT   |   dtjtj¡j\}}|  |jd¡ |  |jd¡ |  |jd¡ |  |jd¡ d S )NzÞ
            def doctest_stuff(arg1,
                              arg2,
                              arg3):
                """
                    >>> assert
                    >>> this
                """
            rV   é   é   rG   )r   r)   rW   r>   r   r.   rH   rI   )r   Zexc1Zexc2r   r   r   Útest_offsetWithMultiLineArgsn  s    	z!Test.test_offsetWithMultiLineArgsc             C   s   |   d¡ d S )NzT
        def foo():
            '''
                >>> foo
            '''
        )r   )r   r   r   r   Útest_doctestCanReferToFunction€  s    z#Test.test_doctestCanReferToFunctionc             C   s   |   d¡ d S )Nzµ
        class Foo():
            '''
                >>> Foo
            '''
            def bar(self):
                '''
                    >>> Foo
                '''
        )r   )r   r   r   r   Útest_doctestCanReferToClassˆ  s    	z Test.test_doctestCanReferToClassc             C   sD   |   dtjtj¡j}|d }|  |jd¡ |d }|  |jd¡ d S )NaF  
            def buildurl(base, *args, **kwargs):
                """
                >>> buildurl('/blah.php', ('a', '&'), ('b', '=')
                '/blah.php?a=%26&b=%3D'
                >>> buildurl('/blah.php', a='&', 'b'='=')
                '/blah.php?b=%3D&a=%26'
                """
                pass
            r   rP   r(   rV   )r   r)   rW   r   r.   rH   )r   r[   rJ   r   r   r   Ú!test_noOffsetSyntaxErrorInDoctest”  s    

z&Test.test_noOffsetSyntaxErrorInDoctestc             C   s   |   d¡ d S )Nz£
        def func():
            """A docstring

            >>> func()
            1
            >>> _
            1
            """
            return 1
        )r   )r   r   r   r   Útest_singleUnderscoreInDoctest§  s    
z#Test.test_singleUnderscoreInDoctestN)r    r!   r"   r#   r8   r<   r=   r?   r@   rA   rB   rC   r   rD   rE   rK   rM   rO   r\   r]   r`   ra   rb   rc   rd   r   r   r   r   r   6   s*   (++

$r   c               @   s   e Zd ZdZdS )Ú	TestOtherz2Run TestOther with each test wrapped in a doctest.N)r    r!   r"   Ú__doc__r   r   r   r   re   µ  s   re   c               @   s   e Zd ZdZdS )ÚTestImportsz4Run TestImports with each test wrapped in a doctest.N)r    r!   r"   rf   r   r   r   r   rg   ¹  s   rg   c               @   s   e Zd ZdZdS )ÚTestUndefinedNamesz;Run TestUndefinedNames with each test wrapped in a doctest.N)r    r!   r"   rf   r   r   r   r   rh   ½  s   rh   )rX   r   Zpyflakesr   r)   Zpyflakes.checkerr   r   r   Zpyflakes.test.test_otherr   re   Zpyflakes.test.test_importsrg   Z"pyflakes.test.test_undefined_namesrh   Zpyflakes.test.harnessr   r   Zpypy_version_inforZ   ÚAttributeErrorÚobjectr	   r   r   r   r   Ú<module>   s(   
    