B
    v9aV#                 @   sp   d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
 ddlmZmZ e	  G dd dejZdS )	z%Tests to cover the Tools/i18n package    N)dedent)assert_python_ok)skip_if_missingtoolsdir)temp_cwdtemp_dirc               @   s   e Zd ZdZejeddZdd Z	dd Z
dd	 Zd
d Zeejd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&d' Zd(d) Zd*d+ Z d,d- Z!d.S )/Test_pygettextzTests for the pygettext.py toolZi18nzpygettext.pyc             C   sR   i }xH| dD ]:}|r|dr$q|d}| dd\}}| ||< qW |S )z: utility: return the header of a .po file as a dictionary 
)#msgidZmsgstr":   )split
startswithstrip)selfdataZheaderslinekeyval r   /usr/lib/python3.7/test_i18n.py
get_header   s    
zTest_pygettext.get_headerc             C   s   g }d}g }x| dD ]l}|rV|dr<||d n|d| g }d}q|dr|tdd }||d d}qW |r|d| |S )z= utility: return all msgids in .po file as a list of strings Fr	   r   zmsgid NT)r   r   appendr   joinlen)r   r   msgidsZreading_msgidZ	cur_msgidr   r   r   r   
get_msgids    s$    

zTest_pygettext.get_msgidsc          
   C   sl   d}t dP}t|d}|| W dQ R X t| jd| td}| }W dQ R X W dQ R X | |S )z: utility: return all msgids extracted from module_content ztest_docstrings.pyNwz-Dzmessages.pot)r   openwriter   scriptreadr   )r   Zmodule_contentfilenamecwdfpr   r   r   r   extract_docstrings_from_str8   s    

z*Test_pygettext.extract_docstrings_from_strc          
   C   s   t d}t| j td}| }W dQ R X | |}| d| | d| | d| | d| | d| | d| | d	| | d
| | d| W dQ R X dS )zMake sure the required fields are in the header, according to:
           http://www.gnu.org/software/gettext/manual/gettext.html#Header-Entry
        Nzmessages.potzProject-Id-VersionzPOT-Creation-DatezPO-Revision-DatezLast-TranslatorzLanguage-TeamzMIME-VersionzContent-TypezContent-Transfer-EncodingzGenerated-By)r   r   r"   r    r#   r   assertIn)r   r%   r&   r   headerr   r   r   test_headerC   s    



zTest_pygettext.test_headerZaixzbpo-29972: broken test on AIXc          
   C   s   ddl m } tdf}t| j td}| }W dQ R X | |}|d }|drj|dtd  }|	|d W dQ R X dS )z; Match the date format from xgettext for POT-Creation-Date r   )datetimeNzmessages.potzPOT-Creation-Datez\nz%Y-%m-%d %H:%M%z)
r+   r   r   r"   r    r#   r   endswithr   Zstrptime)r   r+   r%   r&   r   r)   ZcreationDater   r   r   test_POT_Creation_Date]   s    




z%Test_pygettext.test_POT_Creation_Datec          
   C   sF   x@dD ]8}|  |$ | td| }| d| W d Q R X qW d S )N)z	"""doc"""z
r'''doc'''zR'doc'zu"doc"zE                def foo(bar):
                    %s
                doc)subTestr'   r   r(   )r   r.   r   r   r   r   test_funcdocstringp   s
    
z!Test_pygettext.test_funcdocstringc             C   s&   |  td}| dd |D  d S )Nz5        def foo(bar):
            b"""doc"""
        c             S   s   g | ]}d |kr|qS )r.   r   ).0r   r   r   r   
<listcomp>~   s    z;Test_pygettext.test_funcdocstring_bytes.<locals>.<listcomp>)r'   r   assertFalse)r   r   r   r   r   test_funcdocstring_bytesy   s    z'Test_pygettext.test_funcdocstring_bytesc             C   s&   |  td}| dd |D  d S )Nz5        def foo(bar):
            f"""doc"""
        c             S   s   g | ]}d |kr|qS )r.   r   )r1   r   r   r   r   r2      s    z=Test_pygettext.test_funcdocstring_fstring.<locals>.<listcomp>)r'   r   r3   )r   r   r   r   r   test_funcdocstring_fstring   s    z)Test_pygettext.test_funcdocstring_fstringc          
   C   sF   x@dD ]8}|  |$ | td| }| d| W d Q R X qW d S )N)z	"""doc"""z
r'''doc'''zR'doc'zu"doc"z@                class C:
                    %s
                r.   )r/   r'   r   r(   )r   r.   r   r   r   r   test_classdocstring   s
    
z"Test_pygettext.test_classdocstringc             C   s&   |  td}| dd |D  d S )Nz0        class C:
            b"""doc"""
        c             S   s   g | ]}d |kr|qS )r.   r   )r1   r   r   r   r   r2      s    z<Test_pygettext.test_classdocstring_bytes.<locals>.<listcomp>)r'   r   r3   )r   r   r   r   r   test_classdocstring_bytes   s    z(Test_pygettext.test_classdocstring_bytesc             C   s&   |  td}| dd |D  d S )Nz0        class C:
            f"""doc"""
        c             S   s   g | ]}d |kr|qS )r.   r   )r1   r   r   r   r   r2      s    z>Test_pygettext.test_classdocstring_fstring.<locals>.<listcomp>)r'   r   r3   )r   r   r   r   r   test_classdocstring_fstring   s    z*Test_pygettext.test_classdocstring_fstringc             C   s   |  d}| d| d S )Nz_("""doc""" r'str' u"ing")Z	docstring)r'   r(   )r   r   r   r   r   
test_msgid   s    zTest_pygettext.test_msgidc             C   s"   |  d}| dd |D  d S )Nz_(b"""doc""")c             S   s   g | ]}d |kr|qS )r.   r   )r1   r   r   r   r   r2      s    z3Test_pygettext.test_msgid_bytes.<locals>.<listcomp>)r'   r3   )r   r   r   r   r   test_msgid_bytes   s    
zTest_pygettext.test_msgid_bytesc             C   s"   |  d}| dd |D  d S )Nz_(f"""doc""")c             S   s   g | ]}d |kr|qS )r.   r   )r1   r   r   r   r   r2      s    z5Test_pygettext.test_msgid_fstring.<locals>.<listcomp>)r'   r3   )r   r   r   r   r   test_msgid_fstring   s    
z!Test_pygettext.test_msgid_fstringc             C   s   |  td}| d| dS )z3 Test docstrings for functions with annotated args z9        def foo(bar: str):
            """doc"""
        r.   N)r'   r   r(   )r   r   r   r   r   !test_funcdocstring_annotated_args   s    z0Test_pygettext.test_funcdocstring_annotated_argsc             C   s   |  td}| d| dS )z: Test docstrings for functions with annotated return type z;        def foo(bar) -> str:
            """doc"""
        r.   N)r'   r   r(   )r   r   r   r   r   #test_funcdocstring_annotated_return   s    z2Test_pygettext.test_funcdocstring_annotated_returnc             C   s   |  td}| d| dS )z6 Test docstring for functions with default arg values z7        def foo(bar=()):
            """doc"""
        r.   N)r'   r   r(   )r   r   r   r   r    test_funcdocstring_defvalue_args   s    z/Test_pygettext.test_funcdocstring_defvalue_argsc             C   s6   |  td}| d| | d| | d| dS )z Test docstring extraction for multiple functions combining
        annotated args, annotated return types and default arg values
        z        def foo1(bar: tuple=()) -> str:
            """doc1"""

        def foo2(bar: List[1:2]) -> (lambda x: x):
            """doc2"""

        def foo3(bar: 'func'=lambda x: x) -> {1: 2}:
            """doc3"""
        Zdoc1Zdoc2Zdoc3N)r'   r   r(   )r   r   r   r   r   !test_funcdocstring_multiple_funcs   s
    	z0Test_pygettext.test_funcdocstring_multiple_funcsc             C   s   |  td}| d| dS )ze Test docstring extraction for a class with colons occurring within
        the parentheses.
        z\        class D(L[1:2], F({1: 2}), metaclass=M(lambda x: x)):
            """doc"""
        r.   N)r'   r   r(   )r   r   r   r   r   test_classdocstring_early_colon   s    z.Test_pygettext.test_classdocstring_early_colonc             C   sp  d}d}d}t dP td:}ttj|d ttj|ddd}|d|d	 W dQ R X ttj|d
 ttj|d
dd}|d|d	 W dQ R X ttj|d ttj|ddd}|d|d	 W dQ R X t| j	| td}|
 }W dQ R X | d| d| | d| d| | || W dQ R X W dQ R X dS )zVMake sure the directories are inspected for source files
           bpo-31920
        zText to translate1zText to translate2zText to ignoreNZpypkgzpymod.pyr   z_()zpkg.pyz	pymod2.pyZCVSz	pymod3.pyzmessages.potzmsgid "r   )r   r   osmkdirpathr   r    r!   r   r"   r#   r(   ZassertNotIn)r   Ztext1Ztext2Ztext3ZsdirZsfiler&   r   r   r   r   test_files_list   s&    
zTest_pygettext.test_files_listN)"__name__
__module____qualname____doc__rB   rD   r   r   r"   r   r   r'   r*   unittestZskipIfsysplatformr   r-   r0   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rE   r   r   r   r   r      s.   		
r   )rI   rB   rK   rJ   textwrapr   Ztest.support.script_helperr   Ztest.test_toolsr   r   Ztest.supportr   r   ZTestCaser   r   r   r   r   <module>   s   