B
    u9as                 @   s  d dl Z d dlZd dlZd dlZd dlZd dlmZ edZd dl	Z	d dl	m
Z
 d dlmZ yd dlmZmZ W n ek
r   ej ZZY nX eeeejdZdadd	 ZG d
d de jZG dd de jZG dd de jZdd Zdd Zedkr
e  dS )    N)support_tkinter)Tcl)TclError)INT_MAXPY_SSIZE_T_MAX.c              C   s   t d krt } | dd}td|}| \}}}}t|t|t|  }}}dddd| }|dkrz||||dfa n||d||fa t S )	Ninfo
patchlevelz(\d+)\.(\d+)([ab.])(\d+)ZalphaZbetafinal)abr   r   )_tk_patchlevelr   callre	fullmatchgroupsint)tclr
   mmajorminorreleaselevelserial r   /usr/lib/python3.7/test_tcl.pyget_tk_patchlevel   s    r   c               @   s   e Zd Zdd ZdS )TkinterTestc             C   s   |  ttjd d S )NT)assertRaises	TypeErrorr   Z_flatten)selfr   r   r   testFlattenLen)   s    zTkinterTest.testFlattenLenN)__name__
__module____qualname__r!   r   r   r   r   r   '   s   r   c               @   sf  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-d. Zd/d0 Zd1d2 Zeejd3kd4d5d6 Z d7d8 Z!d9d: Z"d;d< Z#d=d> Z$ee%d?kd@dAdB Z&dCdD Z'dEdF Z(dGdH Z)dIdJ Z*dKdL Z+dMdN Z,dOdP Z-dQdR Z.dSS )TTclTestc             C   s   t  | _| jj | _d S )N)r   interptkwantobjects)r    r   r   r   setUp0   s    zTclTest.setUpc             C   s&   | j }|d | |dd d S )Nzset a 1zset a1)r&   evalassertEqual)r    r   r   r   r   testEval4   s    
zTclTest.testEvalc             C   s   | j }| |dd d S )Nzset a "a\0b"za b)r&   r,   r+   )r    r   r   r   r   test_eval_null_in_result9   s    z TclTest.test_eval_null_in_resultc             C   s   | j }| t|jd d S )Nzset a)r&   r   r   r+   )r    r   r   r   r   testEvalException=   s    zTclTest.testEvalExceptionc             C   s   | j }| t|jd d S )Nzthis is wrong)r&   r   r   r+   )r    r   r   r   r   testEvalException2A   s    zTclTest.testEvalException2c             C   s,   | j }|ddd | |ddd d S )Nsetr   r*   )r&   r   r,   )r    r   r   r   r   testCallE   s    zTclTest.testCallc             C   s   | j }| t|jdd d S )Nr1   r   )r&   r   r   r   )r    r   r   r   r   testCallExceptionJ   s    zTclTest.testCallExceptionc             C   s   | j }| t|jddd d S )NthisisZwrong)r&   r   r   r   )r    r   r   r   r   testCallException2N   s    zTclTest.testCallException2c             C   s(   | j }|dd | |dd d S )Nr   r*   zset a)r&   setvarr,   r+   )r    r   r   r   r   
testSetVarR   s    zTclTest.testSetVarc             C   s(   | j }|dd | |dd d S )Nza(1)r*   zset a(1))r&   r7   r,   r+   )r    r   r   r   r   testSetVarArrayW   s    zTclTest.testSetVarArrayc             C   s&   | j }|d | |dd d S )Nzset a 1r   r*   )r&   r+   r,   getvar)r    r   r   r   r   
testGetVar\   s    
zTclTest.testGetVarc             C   s&   | j }|d | |dd d S )Nz
set a(1) 1za(1)r*   )r&   r+   r,   r:   )r    r   r   r   r   testGetVarArraya   s    
zTclTest.testGetVarArrayc             C   s   | j }| t|jd d S )Nr   )r&   r   r   r:   )r    r   r   r   r   testGetVarExceptionf   s    zTclTest.testGetVarExceptionc             C   s   | j }| t|jd d S )Nza(1))r&   r   r   r:   )r    r   r   r   r   testGetVarArrayExceptionj   s    z TclTest.testGetVarArrayExceptionc             C   sD   | j }|dd | |dd |d | |dd d S )Nr      zinfo exists ar*   0)r&   r7   r,   r+   unsetvar)r    r   r   r   r   testUnsetVarn   s
    
zTclTest.testUnsetVarc             C   st   | j }|dd |dd | |dd | |dd |d | |dd | |dd d S )	Nza(1)r?   za(2)   zinfo exists a(1)r*   zinfo exists a(2)r@   )r&   r7   r,   r+   rA   )r    r   r   r   r   testUnsetVarArrayu   s    
zTclTest.testUnsetVarArrayc             C   s   | j }| t|jd d S )Nr   )r&   r   r   rA   )r    r   r   r   r   testUnsetVarException   s    zTclTest.testUnsetVarExceptionc             C   sF   d}t  dks(dt    kr$dk rBn n|dddd dd  f7 }|S )	N)	r   r?   ii   l        l    l    l         )      r   r   )rG      rG   )rG   rH   l            l        rC   i  )r   )r    Zintegersr   r   r   get_integers   s
    
zTclTest.get_integersc          	   C   s4  | j j}xx|  D ]l}| |d| | tdkrJ| |d| | | |d| dd| | |d| | qW tdk r| t|jt	dd  | |d	d	 | t
|j | t
|jd
d | t
|jd | t
|jd | t|jd | t
ttf|jd | tttf|jd d S )Nz %d )rG   rI   z %#o o z %#x rC   i  *   Z4210s   42g      E@r   z42 u   42)r&   r'   rJ   r,   getinttcl_versionreplacer   r   strr   
ValueErrorUnicodeEncodeError)r    r   ir   r   r   test_getint   s&    
zTclTest.test_getintc             C   s   | j j}| |dd | |dd | |dd | |dd | t|j | t|jdd | t|jd | t|jd	 | tttf|jd
 | tttf|jd d S )Nz 42 g      E@z 42.5 g     @E@rM   z42.5rN   s   42.5r   z42.5 u   42.5)	r&   r'   r,   	getdoubler   r   r   rS   rT   )r    r   r   r   r   test_getdouble   s    
zTclTest.test_getdoublec             C   s   | j j}| |dd | |dd | |dd | |dd | t|j | t|jdd | t|jd | t|jd | t|jd	 | tttf|jd
 | tttf|jd d S )NonTr*   rM   r   Fs   ong      ?r   zon u   on)	r&   r'   ZassertIs
getbooleanr   r   r   rS   rT   )r    r   r   r   r   test_getboolean   s    
zTclTest.test_getbooleanc          	   C   s~   | j }ttjd }| tjtj |d W d Q R X |tj | |	dd | |	dd | |	dd d S )	NwzKset a 1
            set b 2
            set c [ expr $a + $b ]
            zset ar*   zset b2zset c3)
r&   openr   TESTFN
addCleanupunlinkwriteevalfiler,   r+   )r    r   fr   r   r   testEvalFile   s    zTclTest.testEvalFilec          	   C   sl   | j }ttjd }| tjtj |d W d Q R X |tj | |	dd | |	dd d S )Nr\   z>
            set a "a b"
            set b "a\0b"
            zset aza bzset b)
r&   r_   r   r`   ra   rb   rc   rd   r,   r+   )r    r   re   r   r   r   test_evalfile_null_in_result   s    z$TclTest.test_evalfile_null_in_resultc          
   C   sP   | j }d}yt| W n" tk
r: } zW d d }~X Y nX | t|j| d S )NZdoesnotexists)r&   osremove	Exceptionr   r   rd   )r    r   filenameer   r   r   testEvalFileException   s    zTclTest.testEvalFileExceptionc             C   s   | j }| t|jd d S )Nzpackage require DNE)r&   r   r   r+   )r    r   r   r   r   testPackageRequireException   s    z#TclTest.testPackageRequireExceptionwin32zRequires Windowsc          	   C   s   t jtj}|d dkr$tddt jd |d |dd  f }t j|sZtdt	
  }|d	 t|d
dg}W d Q R X | d| d S )Nr?   :z$Absolute path should have drive partz\\%s\%s$\%sZCOMPUTERNAMEr      zCannot connect to UNC PathZTCL_LIBRARYz-czimport tkinter; print(tkinter)s   tkinter)rh   pathabspathsys
executableunittestZSkipTestenvironexistsr   ZEnvironmentVarGuardZunset
subprocessZcheck_outputassertIn)r    fullnameZunc_nameenvstdoutr   r   r   testLoadWithUNC   s    




zTclTest.testLoadWithUNCc                sJ   j ddd ddd  fdd} tj  tjdd	  tjd
  tjd |dd |dd |dd |dd |dd |dd |dd |dd |dd |dd |dd |dd  |d!d |d"d# |d$d# |d%d& |d'd( |d)d( |d*d+ td,krF|d-td. d S )/Nr1   r   rq   r   rH   c                s&    | } ||  |t d S )N)
exprstringr,   assertIsInstancerR   )exprexpectedresult)r    r   r   r   check   s    
z&TclTest.test_exprstring.<locals>.checkz8.2z+6s   8.2 + 6spamrL   r@   z8.2 + 6z14.2z3.1 + $az6.1z2 + "$a.$b"z5.6z4*[llength "6 2"]8z{word one} < "word $a"z4*2 < 7zhypot($a, 4)z5.0z5 / 4r*   z5 / 4.0z1.25z$5 / ( [string length "abcd"] + 0.0 )z20.0/5.0z4.0z"0x03" > "2"u   [string length "a½€"]r^   z[string length "a\xbd\u20ac"]z"abc"abcu   "a½€"u   a½€z"a\xbd\u20ac"z"a\0b"za b)rG   rI   z2**64l            )r&   r   r   r   r   r   rP   rR   )r    r   r   )r    r   r   test_exprstring   s:    



















zTclTest.test_exprstringc                s2   j ddd ddd  fdd} tj  tjdd	  tjd
  tjd |dd |dd |dd |dd |dd |dd |dd |dd |dd |dd |dd |dd  |d!d |d"d# |d$d#  tjd% td&kr.|d'td( d S ))Nr1   r   rq   r   rH   c                s&    | } ||  |t d S )N)
exprdoubler,   r   float)r   r   r   )r    r   r   r   r   "  s    
z&TclTest.test_exprdouble.<locals>.checkz8.2z+6s   8.2 + 6r   rL   g        z8.2 + 6gffffff,@z3.1 + $agffffff@z2 + "$a.$b"gffffff@z4*[llength "6 2"]g       @z{word one} < "word $a"z4*2 < 7zhypot($a, 4)g      @z5 / 4g      ?z5 / 4.0g      ?z$5 / ( [string length "abcd"] + 0.0 )z20.0/5.0g      @z"0x03" > "2"u   [string length "a½€"]g      @z[string length "a\xbd\u20ac"]z"abc")rG   rI   z2**64l            )r&   r   r   r   r   r   rP   r   )r    r   r   )r    r   r   test_exprdouble  s4    















zTclTest.test_exprdoublec                s4   j ddd ddd  fdd} tj  tjdd	  tjd
  tjd |dd |dd |dd |dd |dd |dd |dd |dd |dd |dd |dd |dd |dd |dd |d d  tjd! td"kr0 tjd# d S )$Nr1   r   rq   r   rH   c                s&    | } ||  |t d S )N)exprlongr,   r   r   )r   r   r   )r    r   r   r   r   B  s    
z$TclTest.test_exprlong.<locals>.checkz8.2z+6s   8.2 + 6r   rL   r   z8.2 + 6   z3.1 + $az2 + "$a.$b"rI   z4*[llength "6 2"]rG   z{word one} < "word $a"z4*2 < 7zhypot($a, 4)z5 / 4r?   z5 / 4.0z$5 / ( [string length "abcd"] + 0.0 )z20.0/5.0   z"0x03" > "2"u   [string length "a½€"]z[string length "a\xbd\u20ac"]z"abc")rG   rI   z2**64)r&   r   r   r   r   r   rP   )r    r   r   )r    r   r   test_exprlong>  s4    















zTclTest.test_exprlongc                s   j ddd ddd  fdd} tj  tjdd	  tjd
  tjd |dd x2dD ]*}||d |d| d |d| d qW x2dD ]*}||d |d| d |d| d qW |dd |dd |dd |dd |dd |dd |dd |dd |dd |dd |dd |dd |dd |d d  tjd! td"kr|d#d d S )$Nr1   r   rq   r   rH   c                s2    | } ||  |t  |t d S )N)exprbooleanr,   r   r   ZassertNotIsInstancebool)r   r   r   )r    r   r   r   r   b  s    
z'TclTest.test_exprboolean.<locals>.checkz8.2z+6s   8.2 + 6r   rL   F)r@   falsenooffz"%s"z{%s})r*   trueyesrY   Tz8.2 + 6z3.1 + $az2 + "$a.$b"z4*[llength "6 2"]z{word one} < "word $a"z4*2 < 7zhypot($a, 4)z5 / 4z5 / 4.0z$5 / ( [string length "abcd"] + 0.0 )z20.0/5.0z"0x03" > "2"u   [string length "a½€"]z[string length "a\xbd\u20ac"]z"abc")rG   rI   z2**64)r&   r   r   r   r   r   rP   )r    r   valuer   )r    r   r   test_exprboolean^  sD    



















zTclTest.test_exprboolean)rG   rI   zrequires Tcl version >= 8.5c                sh    j  fdd}|dd |dd |dd |dd |d	d |d
d |dd |dd d S )Nc                sV    d| } r. ||  |t n$ || tt|f  |t d S )Nr   )r   r(   r,   r   r   rz   rR   )r   r   r   )r    r   r   r   r     s    z$TclTest.test_booleans.<locals>.checkr   Tr   rY   r   Fr   r   z1 < 2z1 > 2)r&   )r    r   r   )r    r   r   test_booleans  s    






zTclTest.test_booleansc             C   s   | j }x\|  D ]P}|dt|}| jrD| || | |t q| |t| | |t qW tdk r| 	t
|jdtdd  d S )Nr   )rG   rI   rC   i  )r&   rJ   r   rR   r(   r,   r   r   rP   r   r   )r    r   rU   r   r   r   r   test_expr_bignum  s    zTclTest.test_expr_bignumc                sn   fdd}  |d jr dnd   |d jr:dnd   |dd   |dd   |d	d	   |d
d
   |dd   |d jrdnd	   |d jrdnd   |d jrdnd x0  D ]$}  || jr|nt| qW tdk r6  |dd tdd  x`ddddtjjtjjtjj tjj fD ]4} jr~  ||| n  t	||| q`W  jr|t	d} 
||   |t	dt	d   |t	d t	d  n<  t	|t	dt	d   t	|t	d t	d    |d jr@dnd   |dddgg jrddnd  d S )!Nc                s    j dd| S )Nr1   _)r&   r   )r   )r    r   r   	passValue  s    z.TclTest.test_passing_values.<locals>.passValueTr*   Fr@   stringu	   string€zstr ingu	   str ing½u
   str ing€s   str ings   stringu
   strÀings   stringu   str½ing)rG   rI   rC   i  g        g      ?g      gUUUUUU?naninf)r?   r]   )g333333@z1 2 3.4r   r   c)r   )r   r   za {b c})r,   r(   rJ   rR   rP   rt   
float_infominmaxr   ZassertNotEqual)r    r   rU   re   r   )r    r   test_passing_values  sF    


$

 
zTclTest.test_passing_valuesc                s  d   fdd}j d| j jjd d+jd fdd}fdd}|d	d
 |dd |d |d |d |d |dd |dd |dd |dd |d |d |d |dd |dd |dd x D ]}||t| qW tdk r&|dd  tdd   xd!D ]}||t	| q,W x8d"t
jjt
jjt
jj t
jj fD ]}|||d qhW |td#|d |td# |d |d$d |d%d& |d'dgd(d)gd*g gd& d S ),Nc                s   |  | S )Nr   )arg)r   r   r   testfunc  s    z+TclTest.test_user_command.<locals>.testfuncr   )eqc               sN   |d kr| }d  j d| } t | | |t ||| d S )Nr   )r&   r   r   rR   )r   r   r   r)r   r    r   r   r     s    
z(TclTest.test_user_command.<locals>.checkc                s     j t| |t|d d d S )Ng|=)Zdelta)ZassertAlmostEqualr   abs)actualr   )r    r   r   float_eq  s    z+TclTest.test_user_command.<locals>.float_eqTr*   Fr@   r   u   string½u	   string€rL   s   strings	   string€u   stringâ¬s   string    zstr ingu	   str ing½u
   str ing€s   str ings   stringu
   strÀings   string€u   strÀingâ¬)rG   rI   rC   i  )g        g      ?g      gUUUUUU?r   r   )r?   )rC   )rq   r   z5 6r   z1 2 {3 4} {5 6} {}r?   rq   r   z5 6)N)r&   createcommandra   r'   deletecommandr,   rJ   rR   rP   reprrt   r   r   r   r   )r    r   r   r   rU   re   r   )r   r    r   test_user_command  sH    













zTclTest.test_user_commandc       	      C   s*  | j jj}| j jj}| t| | t|dd | t|d dddddd	d
ddddddddddg dfdddggdddgff|dddd| jrdndfg}t }tdkr| jr|dk rd}nd }||d!d"d#d$d%d|fg7 }d&| jt|f }x*|D ]"\}}| 	|||d'||f  qW | t
|d( d S ))Nr   r   rC   )r]   )r]   )rL   r   )z{})rL   )z"")rL   )z
a
 b	 c
 )r   r   r   )s
   a
 b	 c
 )r   r   r   )u   a €)r   u   €)s   a €)r   u   €)s	   ab cd)za bzc d)za {b c})r   zb c)za b\ c)r   zb c))r   zb c)r   zb c)za 2)r   r]   ))r   rC   )r   rC   )za 3.4)r   z3.4))r   g333333@)r   g333333@)r   r   r   r   listr?   r]   )g333333@)r?   r]   )g333333@)r*   r]   z3.4)rG   rI   )rG   rI   rI   )12u   €u   â¬z3.4)   u   €s   €)g333333@dictcreater   u   €s   €z4want objects? %s, Tcl version: %s, Tk patchlevel: %sz
arg=%a, %s{)r&   r'   	splitlistr   r   r   r(   r   rP   r,   r   )	r    r   r   	testcasesZtk_patchlevelr   Zdbg_infor   resr   r   r   test_splitlist  sR    



zTclTest.test_splitlistc             C   s  | j jj}| j jj}| t| | t|dd | t|d dddddd	d
dddddddddddddddg dfddgdfdddggdf|ddd d!| jrd"nd#fg}td$kr| jrt d%k rd&}nd'}||d(d)d*d+d,d!|fg7 }x$|D ]\}}| j	||||d- qW d S ).Nr   r   rC   )r]   r]   )rL   rL   )z{}rL   )z""rL   )r   r   )z
a
 b	 c
 )r   r   r   )s
   a
 b	 c
 )r   r   r   )u   a €)r   u   €)s   a €)r   u   €)s   abza b)s	   ab cd)za bzc d)s
   {ab cdz{a b c d)za {b c})r   )r   r   )za b\ c)r   )r   r   ))r   s   b c)r   )r   r   ))r   zb c)r   )r   r   )za 2)r   r]   ))r   rC   )r   rC   )za 3.4)r   z3.4))r   g333333@)r   g333333@))r   )rC   g333333@)r   )rC   g333333@)r   r   r   zb c)r   )r   r   r   r   r?   r]   )g333333@)r?   r]   )g333333@)r*   r]   z3.4)rG   rI   )rG   rI   rI   )r   u   €u   â¬z3.4)r   u   €s   €)g333333@r   r   r   u   €s   €)msg)
r&   r'   splitr   r   r   r(   rP   r   r,   )r    r   r   r   r   r   r   r   r   r   
test_split8  sT    




zTclTest.test_splitc          
   C   sB  t j}| jj}d}| |||ddddd | |||dddd d}| |||dd	dd
d | |||d	dd
d | t||d | t||d |ddd	dddd}| |||| jrd	ndddd t	dkr>|dddd	dddd}| jrt
 dk r dddd}nd	ddd}| |||| d S )Nz#-a {1 2 3} -something foo status {}Fz1 2 3foorL   )z-az
-somethingstatus)r   Z	somethingr   )z-a)r?   rC   rq   z
-somethingr   r   z{})r?   rC   rq   z{}z-a b -c )z-ar   z-cr   z-az
-somethingr   r   )rG   rI   r   r   )rG   rI   rI   )tkinterZ
_splitdictr&   r'   r,   r   RuntimeErrorr   r(   rP   r   )r    Z	splitdictr   r   r   r   r   r   test_splitdicti  s4    
zTclTest.test_splitdictc                s   t j jjfdd fdd}|d |d |d |d |d	 |d
 |d |d |d |d |d |d |d |d |d |d |d |d |d |d |d |d |d |d d S )Nc                s     d| dS )NZlindexr   )r   )s)r   r   r   unpack  s    z!TclTest.test_join.<locals>.unpackc                s     | g|    | dg|    | gg|    | dgg|    | gdg|    | dgdg|  d S )Nr   )r,   )r   )joinr    r   r   r   r     s    z TclTest.test_join.<locals>.checkrL   r   zsp amzsp	amzsp
amz 	
z{spam}z{sp am}z"spam"z"sp am"z{"spam"}z"{spam}"zsp\amz"sp\am"z	"{}" "{}"z"\z"{z"}z
\z
{z
}z\
z{
z}
)r   Z_joinr&   r'   )r    r   r   )r   r    r   r   r   	test_join  s8    zTclTest.test_joinc             C   s   |  ttj d S )N)r   r   r   ZTcl_Obj)r    r   r   r   test_new_tcl_obj  s    zTclTest.test_new_tcl_objN)/r"   r#   r$   r)   r-   r.   r/   r0   r2   r3   r6   r8   r9   r;   r<   r=   r>   rB   rD   rE   rJ   rV   rX   r[   rf   rg   rm   rn   rv   
skipUnlessrt   platformr~   r   r   r   r   rP   r   r   r   r   r   r   r   r   r   r   r   r   r   r%   .   sN   
	#  )*601#%r%   c               @   s   e Zd Zdd Zejeee	k dej
ed ddddd	 Zejeee	k dej
ed d
dddd Zejeee	k dej
ed ddddd ZdS )BigmemTclTestc             C   s   t  | _d S )N)r   r&   )r    r   r   r   r)     s    zBigmemTclTest.setUpzneeds UINT_MAX < SIZE_MAXr?   rI   F)sizeZmemuseZdry_runc             C   s$   d| }|  t| jjdd|d d S )N r   indexr   )r   OverflowErrorr&   r   )r    r   r   r   r   r   test_huge_string_call  s    z#BigmemTclTest.test_huge_string_callrC   c             C   sT  | j j}dd|  }| t|j| | t|j| | t|j| | t|j| | t|j| | t|j	| | t|j
| | t|j|dd | t|jd|d | t|j| | t|jd| | t|j
| | t|j| | t|j| | t|j| | t|j| | t|j| | t|j|t | t|j| d S )Nr*   r   xr   )r&   r'   r   r   rO   rW   rZ   r+   rd   recordZadderrorinfor7   rA   r   r   r   r   r   r   r   r   )r    r   r'   r   r   r   r   test_huge_string_builtins  s*    z'BigmemTclTest.test_huge_string_builtinsrH   c             C   sJ   | j j}dd|  }| t|j| | t|j| | t|jd| d S )Nr*   r   r   )r&   r'   r   r   rd   rA   )r    r   r'   r   r   r   r   test_huge_string_builtins2  s
    z(BigmemTclTest.test_huge_string_builtins2N)r"   r#   r$   r)   r   Zcpython_onlyrv   r   r   r   Z
bigmemtestr   r   r   r   r   r   r   r     s     r   c              C   s"   t jrt } td| dd d S )Nzpatchlevel =r	   r
   )r   verboser   printr   )r   r   r   r   setUpModule  s    r   c               C   s   t ttt d S )N)r   Zrun_unittestr%   r   r   r   r   r   r   	test_main  s    r   __main__) rv   r   ry   rt   rh   Ztestr   import_moduler   r   r   r   Z	_testcapir   r   ImportErrormaxsizetuplemapr   ZTCL_VERSIONr   rP   r   r   ZTestCaser   r%   r   r   r   r"   r   r   r   r   <module>   s8   
     2
