B
    u9a0O                 @   sp   d dl Z d dlZd dlmZ G dd de jZG dd dZG dd dZG d	d
 d
e jZedkrle 	  dS )    N)Templatec               @   s|   e 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
dd Zdd Zdd Zdd Zdd Zdd ZdS )
ModuleTestc             C   s   |  tjd |  tjd |  tjd |  tjtjtj  |  tjd |  tjtjd  |  tjd |  tj	d |  tj
tjtj tj tj	 tj  d S )Nz 	
ZabcdefghijklmnopqrstuvwxyzZABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789ZabcdefABCDEFZ01234567z !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~)assertEqualstringZ
whitespaceZascii_lowercaseZascii_uppercaseascii_lettersdigitsZ	hexdigitsZ	octdigitsZpunctuationZ	printable)self r
   !/usr/lib/python3.7/test_string.py
test_attrs   s    zModuleTest.test_attrsc             C   s   |  tdd |  tdd |  tdd |  tdd |  tddd |  td	d
 |  tdd |  tdd |  tddd d S )Nzabc def ghizAbc Def Ghizabc	def
ghizabc	   def  
ghizABC DEF GHIzABC-DEF-GHI-zAbc-Def-GhizABC-def DEF-ghi GHIzAbc-def Def-ghi Ghiz   aBc  DeF   zAbc Defz		aBc	DeF		z		Abc	Def	)r   r   Zcapwords)r	   r
   r
   r   test_capwords   s    zModuleTest.test_capwordsc             C   sf   t  }| |dd | |ddd | |dddd | t|j | tt jj d S )Nfoozfoo{0}barZfoobarzfoo{1}{0}-{1}   z	foo6bar-6)r   	Formatterr   formatassertRaises	TypeError)r	   fmtr
   r
   r   test_basic_formatter"   s    zModuleTest.test_basic_formatterc          	   C   s   t  }| |jdddd | t|jd | |jdddd | t|jd | |jdddd | t|jd | td	 |jddd
 W d Q R X d S )Nz-{arg}-test)argz-test-z-{self}-)r	   z-{format_string}-)format_stringr   )r   r   )r   r   r   r   r   KeyErrorZassertRaisesRegexr   )r	   r   r
   r
   r   test_format_keyword_arguments*   s    z(ModuleTest.test_format_keyword_argumentsc          	   C   s   t  }| |dddddd | |jdd ddddjd ddd | |dddddd | |dddddddd | |jd	d
dddd	jd
ddd | t |ddd W d Q R X | t |ddd W d Q R X d S )Nzfoo{}{}r   r   zfoo{1}{num}{1})Znumz{:^{}}z	{:^{}} {}Xz{:^{pad}}{}r   )Zpadzfoo{1}{}zfoo{}{1})r   r   r   r   r   
ValueError)r	   r   r
   r
   r   test_auto_numbering6   s    zModuleTest.test_auto_numberingc             C   s   t  }| |jdddd | |ddd | t|jdd | |ddd	 | |dt jd
 | |dtdd | |dtdd d S )Nz	-{arg!r}-r   )r   z-'test'-z{0!s}z{0!h}z{0!a}*   Z42z6'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'   z'\xff'   z'\u0100')r   r   r   r   r   r   r   chr)r	   r   r
   r
   r   test_conversion_specifiersI   s    z%ModuleTest.test_conversion_specifiersc          	   C   sV   t  }G dd d}| }| |d|d | t |dd W d Q R X d S )Nc               @   s   e Zd Zdd ZdS )z,ModuleTest.test_name_lookup.<locals>.AnyAttrc             S   s   |S )Nr
   )r	   attrr
   r
   r   __getattr__X   s    z8ModuleTest.test_name_lookup.<locals>.AnyAttr.__getattr__N)__name__
__module____qualname__r'   r
   r
   r
   r   AnyAttrW   s   r+   z{0.lumber}{0.jack}Z
lumberjack )r   r   r   r   r   AttributeError)r	   r   r+   xr
   r
   r   test_name_lookupU   s    zModuleTest.test_name_lookupc          	   C   sn   t  }dddg}| |d|d | t |dg  W d Q R X | t |di  W d Q R X d S )NZeggsandZspamz{0[2]}{0[0]}Zspameggs)r   r   r   r   r   
IndexErrorr   )r	   r   lookupr
   r
   r   test_index_lookup_   s    
zModuleTest.test_index_lookupc             C   s4   G dd dt j}|ddi}| |dd d S )Nc               @   s    e Zd Zi fddZdd ZdS )z>ModuleTest.test_override_get_value.<locals>.NamespaceFormatterc             S   s   t j|  || _d S )N)r   r   __init__	namespace)r	   r5   r
   r
   r   r4   j   s    zGModuleTest.test_override_get_value.<locals>.NamespaceFormatter.__init__c             S   sD   t |tr0y|| S  tk
r,   | j| S X ntj||| d S )N)
isinstancestrr   r5   r   r   	get_value)r	   keyargskwdsr
   r
   r   r8   n   s    
zHModuleTest.test_override_get_value.<locals>.NamespaceFormatter.get_valueN)r(   r)   r*   r4   r8   r
   r
   r
   r   NamespaceFormatteri   s   r<   ZgreetingZhelloz{greeting}, world!zhello, world!)r   r   r   r   )r	   r<   r   r
   r
   r   test_override_get_valueh   s    z"ModuleTest.test_override_get_valuec             C   s4   G dd dt j}| }| |ddd d d S )Nc               @   s   e Zd Zdd ZdS )z<ModuleTest.test_override_format_field.<locals>.CallFormatterc             S   s   t | |S )N)r   )r	   valueformat_specr
   r
   r   format_field~   s    zIModuleTest.test_override_format_field.<locals>.CallFormatter.format_fieldN)r(   r)   r*   r@   r
   r
   r
   r   CallFormatter}   s   rA   z*{0}*c               S   s   dS )Nresultr
   r
   r
   r
   r   <lambda>       z7ModuleTest.test_override_format_field.<locals>.<lambda>z*result*)r   r   r   r   )r	   rA   r   r
   r
   r   test_override_format_field|   s    z%ModuleTest.test_override_format_fieldc             C   s2   G dd dt j}| }| |dddd d S )Nc                   s   e Zd Z fddZ  ZS )z:ModuleTest.test_override_convert_field.<locals>.XFormatterc                s   |dkrd S t  ||S )Nr.   )superconvert_field)r	   r>   Z
conversion)	__class__r
   r   rG      s    zHModuleTest.test_override_convert_field.<locals>.XFormatter.convert_field)r(   r)   r*   rG   __classcell__r
   r
   )rH   r   
XFormatter   s   rJ   z{0!r}:{0!x}r   z
'foo':None)r   r   r   r   )r	   rJ   r   r
   r
   r   test_override_convert_field   s    z&ModuleTest.test_override_convert_fieldc             C   s0   G dd dt j}| }| |ddd d S )Nc               @   s   e Zd Zdd ZdS )z4ModuleTest.test_override_parse.<locals>.BarFormatterc             s   sZ   xT| dD ]F}|d dkrD|dd  d\}}}d||d fV  q|d d d fV  qW d S )N|r   +   :r,   )split	partition)r	   r   ZfieldZ
field_name_r?   r
   r
   r   parse   s
    z:ModuleTest.test_override_parse.<locals>.BarFormatter.parseN)r(   r)   r*   rS   r
   r
   r
   r   BarFormatter   s   rT   z*|+0:^10s|*r   z*   foo    *)r   r   r   r   )r	   rT   r   r
   r
   r   test_override_parse   s    zModuleTest.test_override_parsec          	   C   s   G dd dt j}| }| |ddd | |jddddd	 | |jd
ddddd | jt|jd
ddddd | t|jddd | jt|jddddd | jt|jddddd d S )Nc               @   s   e Zd Zdd ZdS )z@ModuleTest.test_check_unused_args.<locals>.CheckAllUsedFormatterc             S   sH   t | }|tdt| x|D ]}|| q&W |rDtdd S )Nr   zunused arguments)setkeysupdaterangelenremover   )r	   Z	used_argsr:   kwargsZunused_argsr   r
   r
   r   check_unused_args   s    
zRModuleTest.test_check_unused_args.<locals>.CheckAllUsedFormatter.check_unused_argsN)r(   r)   r*   r]   r
   r
   r
   r   CheckAllUsedFormatter   s   r^   z{0}
   Z10z{0}{i}d   )iZ10100z	{0}{i}{1}   Z1010020r   )ra   jz{i})r   r   r   r   r   r   )r	   r^   r   r
   r
   r   test_check_unused_args   s    z!ModuleTest.test_check_unused_argsc          	   C   sV   t  }d}tdd}| t}|d||t d W d Q R X | dt|j	 d S )Nr
   r`   )ra   z{i}	recursion)
r   r   dictr   r   Z_vformatrV   assertInr7   	exception)r	   r   r:   r\   errr
   r
   r   test_vformat_recursion_limit   s    
z'ModuleTest.test_vformat_recursion_limitN)r(   r)   r*   r   r   r   r   r    r%   r/   r3   r=   rE   rK   rU   rd   rk   r
   r
   r
   r   r      s   
		r   c               @   s   e Zd ZdS )BagN)r(   r)   r*   r
   r
   r
   r   rl      s   rl   c               @   s   e Zd Zdd ZdS )Mappingc          	   C   sH   | }x>| dD ]0}yt||}W q tk
r>   t|Y qX qW |S )N.)rP   getattrr-   r   )r	   nameobjpartr
   r
   r   __getitem__   s    zMapping.__getitem__N)r(   r)   r*   rs   r
   r
   r
   r   rm      s   rm   c               @   s   e 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
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 ).TestTemplatec             C   sJ   t d}| |tdddd | t|jtdd | tt j d S )Nz,$who likes to eat a bag of $what worth $$100timham)whowhatz(tim likes to eat a bag of ham worth $100)rw   )r   r   
substituterg   r   r   r   )r	   sr
   r
   r   test_regular_templates   s
    z#TestTemplate.test_regular_templatesc             C   sD   t d}tdddd}| ||d | t|jtddd d S )Nz$who likes ${what} for ${meal}ru   rv   dinner)rw   rx   mealztim likes ham for dinner)rw   rx   )r   rg   r   ry   r   r   )r	   rz   dr
   r
   r   "test_regular_templates_with_braces   s
    
z/TestTemplate.test_regular_templates_with_bracesc             C   s,   t d}tdddd}| ||d d S )Nz$WHO likes ${WHAT} for ${MEAL}ru   rv   r|   )ZWHOWHATZMEALztim likes ham for dinner)r   rg   r   ry   )r	   rz   r~   r
   r
   r   &test_regular_templates_with_upper_case   s    z3TestTemplate.test_regular_templates_with_upper_casec             C   s,   t d}tdddd}| ||d d S )Nz%$_wh0_ likes ${_w_h_a_t_} for ${mea1}ru   rv   r|   )Z_wh0_Z	_w_h_a_t_Zmea1ztim likes ham for dinner)r   rg   r   ry   )r	   rz   r~   r
   r
   r   'test_regular_templates_with_non_letters   s    z4TestTemplate.test_regular_templates_with_non_lettersc             C   sJ   | j }td}||tdddd td}||tdddd d S )Nz-$who likes to eat a bag of $$what worth $$100ru   rv   )rw   rx   z*tim likes to eat a bag of $what worth $100z$who likes $$ztim likes $)r   r   ry   rg   )r	   eqrz   r
   r
   r   test_escapes   s    zTestTemplate.test_escapesc             C   s<   | j }td}tdd}|||d |||d d S )Nz%(foo)s $foo ${foo}Zbaz)r   z%(foo)s baz baz)r   r   rg   ry   safe_substitute)r	   r   rz   r~   r
   r
   r   test_percents   s
    
zTestTemplate.test_percentsc             C   sT   | j }td}tdd}|||d |||d td}|||d d S )Nz&tim has eaten $count bags of ham today   )countz!tim has eaten 7 bags of ham todayz(tim has eaten ${count} bags of ham today)r   r   rg   ry   r   )r	   r   rz   r~   r
   r
   r   test_stringification   s    
z!TestTemplate.test_stringificationc             C   s>   | j }td}tddd}|||d |||d d S )Nz$who ate ${meal})ru   fred)rv   zkung pao)rw   r}   z'('tim', 'fred') ate ('ham', 'kung pao'))r   r   rg   ry   r   )r	   r   rz   r~   r
   r
   r   test_tupleargs   s
    zTestTemplate.test_tupleargsc             C   s   | j }td}||tddd ||tddd ||tddd	d
 ||tdddd ||tddddd d S )Nz$who likes ${what} for ${meal}ru   )rw   ztim likes ${what} for ${meal}rv   )rx   z$who likes ham for ${meal}r|   )rx   r}   z$who likes ham for dinner)rw   rx   ztim likes ham for ${meal})rw   rx   r}   ztim likes ham for dinner)r   r   r   rg   )r	   r   rz   r
   r
   r   test_SafeTemplate  s    zTestTemplate.test_SafeTemplatec             C   s   | j }td}|t|jtdd td}|t|jtdd td}|t|jtdd td}|t|jtdd td}|t|jtdd d S )Nz$who likes $ru   )rw   z$who likes ${what)z$who likes $100u   $who likes $ıu   $who likes $İ)r   r   r   ry   rg   )r	   raisesrz   r
   r
   r   test_invalid_placeholders  s    z&TestTemplate.test_invalid_placeholdersc             C   sX   G dd dt }t }t |_t |j_d|jj_d|j_|d}| ||d d S )Nc               @   s   e Zd ZdZdS )z9TestTemplate.test_idpattern_override.<locals>.PathPatternz[_a-z][._a-z0-9]*N)r(   r)   r*   	idpatternr
   r
   r
   r   PathPattern#  s   r   ru   rv   z,$bag.foo.who likes to eat a bag of $bag.whatztim likes to eat a bag of ham)	r   rm   rl   bagr   rw   rx   r   ry   )r	   r   mrz   r
   r
   r   test_idpattern_override"  s    

z$TestTemplate.test_idpattern_overridec             C   sN   G dd dt }|d}tddddd}| t|j| | ||d	 d S )
Nc               @   s   e Zd ZdZdS )z3TestTemplate.test_flags_override.<locals>.MyPatternr   N)r(   r)   r*   flagsr
   r
   r
   r   	MyPattern.  s   r   z$wHO likes ${WHAT} for ${meal}ru   rv   r|   r   )ZwHOr   r}   wzfredHO likes ${WHAT} for dinner)r   rg   r   r   ry   r   r   )r	   r   rz   r~   r
   r
   r   test_flags_override-  s
    z TestTemplate.test_flags_overridec             C   s:   G dd dt }tddd}|d}| ||d d S )Nc               @   s   e Zd ZdZdZdZdS )zFTestTemplate.test_idpattern_override_inside_outside.<locals>.MyPatternz[a-z]+z[A-Z]+r   N)r(   r)   r*   r   braceidpatternr   r
   r
   r
   r   r   8  s   r   r   BAR)r   r   z$foo ${BAR}zfoo BAR)r   rg   r   ry   )r	   r   r   rz   r
   r
   r   &test_idpattern_override_inside_outside5  s    z3TestTemplate.test_idpattern_override_inside_outsidec             C   sP   G dd dt }tddd}|d}| t|j| |d}| t|j| d S )Nc               @   s   e Zd ZdZdZdZdS )zWTestTemplate.test_idpattern_override_inside_outside_invalid_unbraced.<locals>.MyPatternz[a-z]+z[A-Z]+r   N)r(   r)   r*   r   r   r   r
   r
   r
   r   r   C  s   r   r   r   )r   r   z$FOOz${bar})r   rg   r   r   ry   )r	   r   r   rz   r
   r
   r   7test_idpattern_override_inside_outside_invalid_unbraced@  s    zDTestTemplate.test_idpattern_override_inside_outside_invalid_unbracedc             C   s   G dd dt }t }t |_t |j_d|jj_d|j_|d}| ||d G dd dt }|d}| 	t
|ji  | 	t
|ji  d S )	Nc               @   s   e Zd ZdZdS )z5TestTemplate.test_pattern_override.<locals>.MyPatternz
            (?P<escaped>@{2})                   |
            @(?P<named>[_a-z][._a-z0-9]*)       |
            @{(?P<braced>[_a-z][._a-z0-9]*)}    |
            (?P<invalid>@)
            N)r(   r)   r*   patternr
   r
   r
   r   r   N  s   r   ru   rv   z,@bag.foo.who likes to eat a bag of @bag.whatztim likes to eat a bag of hamc               @   s   e Zd ZdZdS )z6TestTemplate.test_pattern_override.<locals>.BadPatterna  
            (?P<badname>.*)                     |
            (?P<escaped>@{2})                   |
            @(?P<named>[_a-z][._a-z0-9]*)       |
            @{(?P<braced>[_a-z][._a-z0-9]*)}    |
            (?P<invalid>@)                      |
            N)r(   r)   r*   r   r
   r
   r
   r   
BadPattern]  s   r   )r   rm   rl   r   r   rw   rx   r   ry   r   r   r   )r	   r   r   rz   r   r
   r
   r   test_pattern_overrideM  s    

z"TestTemplate.test_pattern_overridec             C   sJ   G dd dt }d}||}| t|ji  |ddi}| |d d S )Nc               @   s   e Zd ZdZdS )z5TestTemplate.test_braced_override.<locals>.MyTemplatez
            \$(?:
              (?P<escaped>$)                     |
              (?P<named>[_a-z][_a-z0-9]*)        |
              @@(?P<braced>[_a-z][_a-z0-9]*)@@   |
              (?P<invalid>)                      |
           )
           N)r(   r)   r*   r   r
   r
   r
   r   
MyTemplatej  s   r   zPyCon in $@@location@@location	ClevelandzPyCon in Cleveland)r   r   r   ry   r   )r	   r   tmpltvalr
   r
   r   test_braced_overridei  s    
z!TestTemplate.test_braced_overridec             C   sJ   G dd dt }d}||}| | | |ddi}| |d d S )Nc               @   s   e Zd ZdZdS )z:TestTemplate.test_braced_override_safe.<locals>.MyTemplatez
            \$(?:
              (?P<escaped>$)                     |
              (?P<named>[_a-z][_a-z0-9]*)        |
              @@(?P<braced>[_a-z][_a-z0-9]*)@@   |
              (?P<invalid>)                      |
           )
           N)r(   r)   r*   r   r
   r
   r
   r   r   {  s   r   zPyCon in $@@location@@r   r   zPyCon in Cleveland)r   r   r   )r	   r   r   r   r   r
   r
   r   test_braced_override_safez  s    
z&TestTemplate.test_braced_override_safec          	   C   sN   G dd dt }|d}| t}|i  W d Q R X | dt|j d S )Nc               @   s   e Zd ZdZdS )z;TestTemplate.test_invalid_with_no_lines.<locals>.MyTemplatez
              (?P<invalid>) |
              unreachable(
                (?P<named>)   |
                (?P<braced>)  |
                (?P<escaped>)
              )
            N)r(   r)   r*   r   r
   r
   r
   r   r     s   r   r,   zline 1, col 1)r   r   r   ry   rh   r7   ri   )r	   r   rz   rj   r
   r
   r   test_invalid_with_no_lines  s
    	z'TestTemplate.test_invalid_with_no_linesc             C   s*   t d}tddd}| ||d d S )Nz$who likes $whatu   tÿmu   fþed)rw   rx   u   tÿm likes fþed)r   rg   r   ry   )r	   rz   r~   r
   r
   r   test_unicode_values  s    z TestTemplate.test_unicode_valuesc             C   s   | j }td}||jdddd ||jtddddd ||jtdd	ddddd td
}||jtddddd ||jtddddd td}||jddd d S )Nz$who likes $whatru   rv   )rw   rx   ztim likes ham)rw   )rx   r   zkung paozthe mapping is $mappingnone)r   bozo)mappingzthe mapping is bozoonetwozthe mapping is twozthe self is $self)r	   zthe self is bozo)r   r   ry   rg   )r	   r   rz   r
   r
   r   test_keyword_arguments  s    z#TestTemplate.test_keyword_argumentsc             C   s   | j }| j}td}||jdddd ||jtddddd ||jtdd	ddddd td
}||jtddddd ||jtddddd tdd}|t|j|i  |t|j|i  td}||jddd d S )Nz$who likes $whatru   rv   )rw   rx   ztim likes ham)rw   )rx   r   zkung paozthe mapping is $mappingr   )r   r   )r   zthe mapping is bozor   r   zthe mapping is twozthe self is $self)r	   zthe self is bozo)r   r   r   r   rg   r   ry   )r	   r   r   rz   r~   r
   r
   r   test_keyword_arguments_safe  s$    
z(TestTemplate.test_keyword_arguments_safec             C   s   | j }| j}G dd dt}|d}||jdddd |t|j ||jdddd || d |d}|t|jtddd || d G d	d
 d
t}|d}|  |tdddd d S )Nc               @   s   e Zd ZdZdS )z?TestTemplate.test_delimiter_override.<locals>.AmpersandTemplate&N)r(   r)   r*   	delimiterr
   r
   r
   r   AmpersandTemplate  s   r   zthis &gift is for &{who} &&ZbudZyou)Zgiftrw   zthis bud is for you &zthis &gift is for &{who} &c               @   s   e Zd ZdZdS )z7TestTemplate.test_delimiter_override.<locals>.PieDelims@N)r(   r)   r*   r   r
   r
   r
   r   	PieDelims  s   r   z-@who likes to eat a bag of @{what} worth $100ru   rv   )rw   rx   z(tim likes to eat a bag of ham worth $100)r   r   r   ry   r   r   r   rg   )r	   r   r   r   rz   r   r
   r
   r   test_delimiter_override  s    z$TestTemplate.test_delimiter_overrideN)r(   r)   r*   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r
   r
   r
   r   rt      s,   	rt   __main__)
Zunittestr   r   ZTestCaser   rl   rm   rt   r(   mainr
   r
   r
   r   <module>   s    <  