o
    KXh/                     @   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                   @   sD  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.d/ Z"d0d1 Z#d2d3 Z$d4d5 Z%d6d7 Z&d8d9 Z'd:d; Z(d<d= Z)d>d? Z*d@dA Z+dBdC Z,dDdE Z-dFdG Z.dHdI Z/dJS )K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   C/opt/python-3.10.19/usr/lib/python3.10/test/test_tools/test_i18n.py
get_header   s   
zTest_pygettext.get_headerc                 C   s   g }d}g }| dD ]6}|r*|dr||d n|d| g }d}q|drA|tdd }||d d}q|rL|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A}t|d}|| W d   n1 sw   Y  t| jd| td}| }W d   n1 s;w   Y  W d   n1 sJw   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   



z*Test_pygettext.extract_docstrings_from_strc              	   C   s   t da}t| j td}| }W d   n1 sw   Y  | |}| d| | d| | d| | d| | d| | d| | d	| | d
| | d| W d   dS 1 shw   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C}t| j td}| }W d   n1 s#w   Y  | |}|d }|dr?|dtd  }|	|d W d   dS 1 sPw   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   V   dD ]&}|  | | td| }| d| W d    n1 s#w   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   r3   r   r   r   r   test_funcdocstringp      z!Test_pygettext.test_funcdocstringc                 C   &   |  td}| dd |D  d S )Nz5        def foo(bar):
            b"""doc"""
        c                 S      g | ]}d |v r|qS r3   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      z'Test_pygettext.test_funcdocstring_bytesc                 C   r8   )Nz5        def foo(bar):
            f"""doc"""
        c                 S   r9   r:   r   r;   r   r   r   r=      r>   z=Test_pygettext.test_funcdocstring_fstring.<locals>.<listcomp>r?   rA   r   r   r   test_funcdocstring_fstring   rC   z)Test_pygettext.test_funcdocstring_fstringc              	   C   r1   )Nr2   z@                class C:
                    %s
                r3   r4   r5   r   r   r   test_classdocstring   r7   z"Test_pygettext.test_classdocstringc                 C   r8   )Nz0        class C:
            b"""doc"""
        c                 S   r9   r:   r   r;   r   r   r   r=      r>   z<Test_pygettext.test_classdocstring_bytes.<locals>.<listcomp>r?   rA   r   r   r   test_classdocstring_bytes   rC   z(Test_pygettext.test_classdocstring_bytesc                 C   r8   )Nz0        class C:
            f"""doc"""
        c                 S   r9   r:   r   r;   r   r   r   r=      r>   z>Test_pygettext.test_classdocstring_fstring.<locals>.<listcomp>r?   rA   r   r   r   test_classdocstring_fstring   rC   z*Test_pygettext.test_classdocstring_fstringc              	   C   r1   )Nr2   z#                %s
                r3   r4   r5   r   r   r   test_moduledocstring   s   z#Test_pygettext.test_moduledocstringc                 C   r8   )Nz        b"""doc"""
        c                 S   r9   r:   r   r;   r   r   r   r=      r>   z=Test_pygettext.test_moduledocstring_bytes.<locals>.<listcomp>r?   rA   r   r   r   test_moduledocstring_bytes      z)Test_pygettext.test_moduledocstring_bytesc                 C   r8   )Nz        f"""doc"""
        c                 S   r9   r:   r   r;   r   r   r   r=      r>   z?Test_pygettext.test_moduledocstring_fstring.<locals>.<listcomp>r?   rA   r   r   r   test_moduledocstring_fstring   rJ   z+Test_pygettext.test_moduledocstring_fstringc                 C   s   |  d}| d| d S )Nz_("""doc""" r'str' u"ing")Z	docstring)r(   r*   rA   r   r   r   
test_msgid   s   zTest_pygettext.test_msgidc                 C   "   |  d}| dd |D  d S )Nz_(b"""doc""")c                 S   r9   r:   r   r;   r   r   r   r=      r>   z3Test_pygettext.test_msgid_bytes.<locals>.<listcomp>r(   r@   rA   r   r   r   test_msgid_bytes      
zTest_pygettext.test_msgid_bytesc                 C   rM   )Nz_(f"""doc""")c                 S   r9   r:   r   r;   r   r   r   r=      r>   z5Test_pygettext.test_msgid_fstring.<locals>.<listcomp>rN   rA   r   r   r   test_msgid_fstring   rP   z!Test_pygettext.test_msgid_fstringc                 C      |  td}| d| dS )z3 Test docstrings for functions with annotated args z9        def foo(bar: str):
            """doc"""
        r3   Nr(   r   r*   rA   r   r   r   !test_funcdocstring_annotated_args      z0Test_pygettext.test_funcdocstring_annotated_argsc                 C   rR   )z: Test docstrings for functions with annotated return type z;        def foo(bar) -> str:
            """doc"""
        r3   NrS   rA   r   r   r   #test_funcdocstring_annotated_return   rU   z2Test_pygettext.test_funcdocstring_annotated_returnc                 C   rR   )z6 Test docstring for functions with default arg values z7        def foo(bar=()):
            """doc"""
        r3   NrS   rA   r   r   r    test_funcdocstring_defvalue_args   rU   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doc3NrS   rA   r   r   r   !test_funcdocstring_multiple_funcs   s   
z0Test_pygettext.test_funcdocstring_multiple_funcsc                 C   rR   )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"""
        r3   NrS   rA   r   r   r   test_classdocstring_early_colon   s   z.Test_pygettext.test_classdocstring_early_colonc                 C      |  td}| d| d S )Nz"        f"{_('foo bar')}"
        foo barrS   rA   r   r   r   test_calls_in_fstrings      z%Test_pygettext.test_calls_in_fstringsc                 C   rZ   )Nz#        rf"{_('foo bar')}"
        r[   rS   rA   r   r   r   test_calls_in_fstrings_raw   r]   z)Test_pygettext.test_calls_in_fstrings_rawc                 C   rZ   )Nz+        f"""{f'{_("foo bar")}'}"""
        r[   rS   rA   r   r   r   test_calls_in_fstrings_nested   r]   z,Test_pygettext.test_calls_in_fstrings_nestedc                 C   rZ   )Nz&        f"{obj._('foo bar')}"
        r[   rS   rA   r   r   r    test_calls_in_fstrings_attribute  r]   z/Test_pygettext.test_calls_in_fstrings_attributec                 C   rZ   )Nz*        f"{type(str)('foo bar')}"
        r[   r(   r   assertNotInrA   r   r   r   (test_calls_in_fstrings_with_call_on_call  r]   z7Test_pygettext.test_calls_in_fstrings_with_call_on_callc                 C   rZ   )Nz6        f"{_('foo {bar}').format(bar='baz')}"
        	foo {bar}rS   rA   r   r   r   "test_calls_in_fstrings_with_format  r]   z1Test_pygettext.test_calls_in_fstrings_with_formatc                 C   r8   )Nz%        f"{_(f'foo {bar}')}"
        c                 S   r9   )rd   r   r;   r   r   r   r=     r>   zLTest_pygettext.test_calls_in_fstrings_with_wrong_input_1.<locals>.<listcomp>r?   rA   r   r   r   )test_calls_in_fstrings_with_wrong_input_1  rJ   z8Test_pygettext.test_calls_in_fstrings_with_wrong_input_1c                 C   rZ   )Nz        f"{_(1)}"
        r   ra   rA   r   r   r   )test_calls_in_fstrings_with_wrong_input_2  r]   z8Test_pygettext.test_calls_in_fstrings_with_wrong_input_2c                 C   s*   |  td}| d| | d| d S )Nz%        f"{_('foo', 'bar')}"
        foobarra   rA   r   r   r   (test_calls_in_fstring_with_multiple_args#     z7Test_pygettext.test_calls_in_fstring_with_multiple_argsc                 C   s6   |  td}| d| | d| | d| d S )Nz)        f"{_('foo', bar='baz')}"
        rh   ri   Zbazra   rA   r   r   r   'test_calls_in_fstring_with_keyword_args*  s   z6Test_pygettext.test_calls_in_fstring_with_keyword_argsc                 C   s*   |  td}| d| | d| d S )Nz*        f"{_(f'foo') + _('bar')}"
        rh   ri   )r(   r   rb   r*   rA   r   r   r   5test_calls_in_fstring_with_partially_wrong_expression2  rk   zDTest_pygettext.test_calls_in_fstring_with_partially_wrong_expressionc              
   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 s9w   Y  ttj|d
 ttj|d
dd}|d|d	 W d   n1 sgw   Y  ttj|d ttj|ddd}|d|d	 W d   n1 sw   Y  t| j	| td}|
 }W d   n1 sw   Y  | d| d| | d| d| | || W d   n1 sw   Y  W d   dS W d   dS 1 sw   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*   rb   )r   Ztext1Ztext2Ztext3ZsdirZsfiler'   r   r   r   r   test_files_list9  s0   

PzTest_pygettext.test_files_listN)0__name__
__module____qualname____doc__rp   rr   r   r   r#   r   r   r(   r,   unittestZskipIfsysplatformr   r0   r6   rB   rD   rE   rF   rG   rH   rI   rK   rL   rO   rQ   rT   rV   rW   rX   rY   r\   r^   r_   r`   rc   re   rf   rg   rj   rl   rm   rs   r   r   r   r   r      sP    
		
r   )rw   rp   ry   rx   textwrapr   Ztest.support.script_helperr   Ztest.test_toolsr   r   Ztest.support.os_helperr   r   ZTestCaser   r   r   r   r   <module>   s    