a
    zeV#                     @   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   sN   i }| dD ]:}|r|dr"q|d}| dd\}}| ||< q|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.9/test/test_tools/test_i18n.py
get_header   s    
zTest_pygettext.get_headerc                 C   s   g }d}g }| dD ]l}|rT|dr:||d n|d| g }d}q|dr|tdd }||d d}q|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   s   d}t d}t|d}|| W d   n1 s80    Y  t| jd| td}| }W d   n1 sv0    Y  W d   n1 s0    Y  | |S )z: utility: return all msgids extracted from module_content ztest_docstrings.pyNwz-Dmessages.pot)r   openwriter   scriptreadr   )r   Zmodule_contentfilenamecwdfpr   r   r   r   extract_docstrings_from_str8   s    
(
Dz*Test_pygettext.extract_docstrings_from_strc              	   C   s   t d}t| j td}| }W d   n1 s:0    Y  | |}| d| | d| | d| | d| | d| | d| | d	| | d
| | d| W d   n1 s0    Y  dS )zMake sure the required fields are in the header, according to:
           http://www.gnu.org/software/gettext/manual/gettext.html#Header-Entry
        Nr    zProject-Id-Version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}t| j td}| }W d   n1 sF0    Y  | |}|d }|dr~|dtd  }|	|d W d   n1 s0    Y  dS )z; Match the date format from xgettext for POT-Creation-Date r   )datetimeNr    r)   z\nz%Y-%m-%d %H:%M%z)
r-   r   r   r#   r!   r$   r   endswithr   strptime)r   r-   r&   r'   r   r+   ZcreationDater   r   r   test_POT_Creation_Date]   s    


&

z%Test_pygettext.test_POT_Creation_Datec              	   C   sV   dD ]L}|  |. | td| }| d| W d    q1 sF0    Y  qd S )Nz	"""doc"""z
r'''doc'''zR'doc'zu"doc"zE                def foo(bar):
                    %s
                docZsubTestr(   r   r*   r   r2   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 |v r|qS r2   r   .0r   r   r   r   
<listcomp>~       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 |v r|qS r6   r   r7   r   r   r   r9      r:   z=Test_pygettext.test_funcdocstring_fstring.<locals>.<listcomp>r;   r=   r   r   r   test_funcdocstring_fstring   s    z)Test_pygettext.test_funcdocstring_fstringc              	   C   sV   dD ]L}|  |. | td| }| d| W d    q1 sF0    Y  qd S )Nr1   z@                class C:
                    %s
                r2   r3   r4   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 |v r|qS r6   r   r7   r   r   r   r9      r:   z<Test_pygettext.test_classdocstring_bytes.<locals>.<listcomp>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 |v r|qS r6   r   r7   r   r   r   r9      r:   z>Test_pygettext.test_classdocstring_fstring.<locals>.<listcomp>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   
test_msgid   s    zTest_pygettext.test_msgidc                 C   s"   |  d}| dd |D  d S )Nz_(b"""doc""")c                 S   s   g | ]}d |v r|qS r6   r   r7   r   r   r   r9      r:   z3Test_pygettext.test_msgid_bytes.<locals>.<listcomp>r(   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 |v r|qS r6   r   r7   r   r   r   r9      r:   z5Test_pygettext.test_msgid_fstring.<locals>.<listcomp>rD   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"""
        r2   N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"""
        r2   NrG   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"""
        r2   NrG   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doc3NrG   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"""
        r2   NrG   r=   r   r   r   test_classdocstring_early_colon   s    z.Test_pygettext.test_classdocstring_early_colonc              
   C   s  d}d}d}t d td}ttj|d ttj|ddd"}|d|d	 W d   n1 sv0    Y  ttj|d
 ttj|d
dd"}|d|d	 W d   n1 s0    Y  ttj|d ttj|ddd"}|d|d	 W d   n1 s00    Y  t| j	| td}|
 }W d   n1 sn0    Y  | d| d| | d| d| | || W d   n1 s0    Y  W d   n1 s0    Y  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.pyCVSz	pymod3.pyr    z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&    002
(zTest_pygettext.test_files_listN)"__name__
__module____qualname____doc__rO   rQ   r   r   r#   r   r   r(   r,   unittestZskipIfsysplatformr   r0   r5   r>   r?   r@   rA   rB   rC   rE   rF   rH   rI   rJ   rK   rL   rR   r   r   r   r   r      s2   
		
r   )rV   rO   rX   rW   textwrapr   Ztest.support.script_helperr   Ztest.test_toolsr   r   Ztest.supportr   r   ZTestCaser   r   r   r   r   <module>   s   