B
    u9afK  ã               @   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mZmZm	Z	 d dl
Z
edƒ edƒZedƒ edƒ yd dlZW n ek
rˆ   Y nX dd„ Ze j d¡Ze e p®ed	kd
e ¡e ejdkd¡G dd„ dejƒƒƒZG dd„ dejƒZG dd„ dejƒZedkre ¡  dS )é    N)ÚrequiresÚimport_moduleÚverboseÚSaveSignalsÚcurseszcurses.asciizcurses.textpadc             C   s   t  tt| ƒd|  ¡S )Nzrequires curses.%s)ÚunittestZ
skipUnlessÚhasattrr   )Úname© r
   ú!/usr/lib/python3.7/test_curses.pyÚrequires_curses_func#   s    r   ZTERMÚunknownz*$TERM=%r, calling initscr() may cause exitÚcygwinz!cygwin's curses mostly just hangsc               @   s@  e Zd Zedd„ ƒZedd„ ƒZdd„ Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ Zedƒdd„ ƒZedƒdd„ ƒZedƒdd„ ƒZedƒdd„ ƒZedƒdd„ ƒZedƒdd „ ƒZedƒd!d"„ ƒZed#ƒd$d%„ ƒZed&ƒd'd(„ ƒZed)ƒd*d+„ ƒZd,d-„ Zed.ƒe ej d/¡d0¡d1d2„ ƒƒZd3d4„ Zd5d6„ Zd7d8„ Z d9d:„ Z!d;S )<Ú
TestCursesc             C   sJ   t j ¡ s*t d¡‚t ¡ | _| j ¡ }nd | _t j ¡ }t	j
|d d S )Nzsys.__stdout__ is not a tty)Úfd)ÚsysÚ
__stdout__Úisattyr   ÚSkipTestÚtempfileÚTemporaryFileÚtmpÚfilenor   Z	setupterm)Úclsr   r
   r
   r   Ú
setUpClass0   s    



zTestCurses.setUpClassc             C   s   | j r| j  ¡  | ` d S )N)r   Úclose)r   r
   r
   r   ÚtearDownClass>   s    
zTestCurses.tearDownClassc             C   s2   t ƒ | _| j ¡  trtƒ  t ¡ | _t ¡  d S )N)	r   Úsave_signalsZsaver   Úprintr   ZinitscrÚstdscrZsavetty)Úselfr
   r
   r   ÚsetUpD   s    

zTestCurses.setUpc             C   s   t  ¡  t  ¡  | j ¡  d S )N)r   ZresettyZendwinr   Zrestore)r    r
   r
   r   ÚtearDownM   s    zTestCurses.tearDownc             C   sþ  | j }t dd¡}t dddd¡}t dddd¡}x^|j|jgD ]N}xHddtjfddddtjfgD ](}| j|j|d ||Ž  W d Q R X qbW q@W x–|j|j	|j
|j|j|j|j|j|j|j|j|j|j|j|j|j|j|j|j|j|j|j|j|j|j |j!gD ]&}| j|jd |ƒ  W d Q R X q W | "d	d
¡ | "d	d
tj¡ | "ddd	d
¡ | "ddd	d
tj¡ | #tj¡ | $tj¡ | %tj¡ | &d¡ | &dtj'¡ | (d¡ | (dtj'¡ | )dddddddd¡ | )dddddddd¡ | j*t+dd | )dddddg dd¡ W d Q R X | ,dd¡ | ,dd¡ | ,dd ¡ |  *t+|j,ddd¡ |  *t+|j,d¡ | ,¡  | -d!¡ | .d"d"¡}| .d!d!dd¡}| /d#d#¡ | 0d¡ | 0dtj¡ | 1dd¡ | 1ddtj¡ | 1d!d!dd¡ | 1d!d!ddtj¡ | 2d!¡ | 3d!¡ t4|d$ƒr.| 5d!¡ | 5d%¡ | 6d&¡ | 7d!¡ | 8d'd
¡ | 8d'd
tj¡ | 8ddd'd
¡ | 8ddd'd
tj¡ | 9d(¡ | 9d(tj¡ | 9ddd(¡ | 9ddd(tj¡ | :d%¡ | ;d!¡ | <d!¡ | =d
d
¡ | >d"d"¡ | ?d!¡ | @d!¡ | A|¡ | B|¡ | A|d!d"d"d!d
d
¡ | B|d!d"d"d!d
d
¡ | Cd!d"¡ | Dd!¡ | E¡  | Ed"¡ | Ed)¡ | =d*d"¡ | Fdd¡ | Gdd¡}| Gdddd¡}t4|d+ƒrÎtHjI Jd,¡sÎ| Kd!¡ | Ld¡ | Mdd¡ | Mddd%¡ | Ndd
¡ | Ndd
tjO¡ t4|d-ƒrV| Pdd"d
tjQ¡ | Pd
tj¡ | Pdd.tjR¡ | PtjQ¡ | ¡  | Nd!d!dd
¡ | Nd!d!dd
tjO¡ t4|d/ƒrš| Sd0d1¡ t4|d2ƒr²| Tdd¡ |  *tU|jVd3¡ |  *tU|jVd"d
d3¡ |  *tU|jd4¡ |  *tU|jd"d
d4¡ d S )5Né
   é   é   )Úar&   )é   r'   r&   )ÚmethÚargs)r(   Z1234é   r'   ú éA   éB   éC   éD   éE   éF   éG   éH   ú|ú!ú-Ú_ú+ú\ú#ú/z(Expected win.border() to raise TypeError)Úmsgó   :ó   ~é   é   é	   Úimmedokr   ÚcÚabcÚdeféýÿÿÿé   ÚsyncokZsunosÚchgaté   Úresizeé   éP   Úencloseipþÿÿéþÿÿÿ)Wr   r   ÚnewwinÚaddchÚaddstrZA_BOLDÚsubTestÚ__qualname__ÚclearZclrtobotZclrtoeolZ	cursyncupZdelchZdeletelnZeraseZgetbegyxZgetbkgdÚgetkeyÚgetmaxyxZgetparyxZgetyxZinchZinsertlnZinstrZis_wintouchedZnoutrefreshZ	redrawwinZrefreshZstandoutZstandendZsyncdownZsyncupZtouchwinZ
untouchwinÚaddnstrZattronZattroffZattrsetZbkgdZ	A_REVERSEZbkgdsetZborderÚassertRaisesÚ	TypeErrorÚboxZclearokZderwinZmvderwinZechocharZhlineZidcokZidlokr   rB   ZinschZinsdellnÚinsnstrÚinsstrZis_linetouchedZkeypadZleaveokZmoveZmvwinZnodelayZ	notimeoutZoverlayZ	overwriteZredrawlnZscrollokZscrollZ	setscrregZsubwinr   ÚplatformÚ
startswithrH   ZtimeoutZ	touchlineZvlineZ
A_STANDOUTrI   ZA_BLINKZA_UNDERLINErK   rN   Ú
ValueErrorZgetstr)r    r   ÚwinZwin2r(   r)   Zwin4Zwin3r
   r
   r   Útest_window_funcsR   sâ    
























zTestCurses.test_window_funcsc          
   C   st   | j }xhdD ]`}| j|dJ |  t|jd¡ |  t|jdd¡ |  t|jd¡ |  t|jdd¡ W d Q R X qW d S )N)r&   ó   a)Úargza r?   )r   rS   rY   r`   rR   rX   r]   r\   )r    r   rd   r
   r
   r   Útest_embedded_null_charsÖ   s    
z#TestCurses.test_embedded_null_charsc             C   s"  xˆt jt jt jt jt jt jt jt jt j	t j
t jt jt jt jt jt jt jt jt jt jt jt jt jgD ]$}| j|jd |ƒ  W d Q R X qbW tt dƒrœt  ¡  tt dƒr®t  ¡  t  d¡rÂt  d¡ t  d¡ t   ¡  t   d¡ t! "¡ &}| j# $|¡ | %d¡ t  &|¡ W d Q R X t  'd¡ t  (d¡ t  )d¡ t  *d¡ t  +dd¡ t  ,d	d	¡}t  ,d	d	dd¡}t  -¡  t  -d¡ t  .d
¡ t  /¡  t  0¡  t  0d¡ tt dƒr°t  1d	d	¡ t  2d¡ t  3d¡ t  d¡ t  4d¡ tt dƒrôt  5t6j7 8¡ ¡ t  9d¡ t  :d¡ tt dƒrt  ;d¡ d S )N)ÚfuncÚfilterÚgetsyxZcnormr?   r   éd   é2   r$   s   abcÚsetsyxZhcÚcoZcrs   crÚ	typeaheadr&   Úuse_env)<r   ZbaudrateZbeepZcan_change_colorZcbreakZdef_prog_modeZdoupdateZflashZflushinpÚ
has_colorsZhas_icZhas_ilZisendwinZkillcharZlongnameZnocbreakZnoechoZnonlZ	noqiflushZnorawZreset_prog_modeZ	termattrsZtermnameZ	erasecharrS   rT   r   rg   rh   ÚtigetstrZcurs_setZdelay_outputZechor   r   r   ZputwinÚseekZgetwinZ	halfdelayZ	intrflushÚmetaZnapmsZnewpadrP   ÚnlZputpZqiflushÚrawrk   Z	tigetflagZtigetnumÚtparmrm   r   Ú	__stdin__r   ÚunctrlÚungetchrn   )r    rf   Úfra   r
   r
   r   Útest_module_funcsà   sb    




 






 

 






zTestCurses.test_module_funcsc             C   sl   t  ¡ s|  d¡ t  ¡  t  ddd¡ t  d¡ t  d¡ t  t jd ¡ t  	d¡ t
t dƒrht  ¡  d S )Nzrequires colors supportr@   r?   r   Úuse_default_colors)r   ro   ÚskipTestZstart_colorZ	init_pairZcolor_contentZ
color_pairZpair_contentZCOLOR_PAIRSZpair_numberr   r{   )r    r
   r
   r   Útest_colors_funcs  s    




zTestCurses.test_colors_funcsÚkeynamec             C   s   t  d¡ d S )Né   )r   r~   )r    r
   r
   r   Útest_keyname#  s    zTestCurses.test_keynameÚhas_keyc             C   s   t  d¡ d S )Nr   )r   r   )r    r
   r
   r   Útest_has_key'  s    zTestCurses.test_has_keyÚgetmousec             C   sL   t  t j¡\}}|dkr"|  d¡ t  d¡ t  ddddt j¡ t  ¡ }d S )Nr   zmouse stuff not availabler#   )r   Z	mousemaskZBUTTON1_PRESSEDr|   ZmouseintervalZ
ungetmouserƒ   )r    Z	availmaskZoldmaskÚmr
   r
   r   Útest_getmouse+  s    

zTestCurses.test_getmouseÚpanelc          	   C   sB   t  dd¡}t j |¡}| jt jjdd | ¡  W d Q R X d S )Nr#   z!userptr should fail since not set)r<   )r   rP   r†   Ú	new_panelrY   ÚerrorZuserptr)r    ÚwÚpr
   r
   r   Útest_userptr_without_set5  s
    

z#TestCurses.test_userptr_without_setc             C   sf   t  dd¡}t j |¡}tƒ }t |¡}xtdƒD ]}| |¡ q2W | d ¡ |  	t |¡|d¡ d S )Nr#   ri   zset_userptr leaked references)
r   rP   r†   r‡   Úobjectr   ÚgetrefcountÚrangeÚset_userptrÚassertEqual)r    r‰   rŠ   ÚobjZnrefsÚir
   r
   r   Útest_userptr_memory_leak>  s    

z#TestCurses.test_userptr_memory_leakc                sD   t  dd¡}t j |¡‰ G ‡ fdd„dƒ}ˆ  |ƒ ¡ ˆ  d ¡ d S )Nr#   c                   s   e Zd Z‡ fdd„ZdS )z+TestCurses.test_userptr_segfault.<locals>.Ac                s   ˆ   d ¡ d S )N)r   )r    )r†   r
   r   Ú__del__P  s    z3TestCurses.test_userptr_segfault.<locals>.A.__del__N)Ú__name__Ú
__module__rT   r”   r
   )r†   r
   r   ÚAO  s   r—   )r   rP   r†   r‡   r   )r    r‰   r—   r
   )r†   r   Útest_userptr_segfaultK  s
    z TestCurses.test_userptr_segfaultc             C   s,   t  dd¡}t j |¡}|  tt|ƒ¡ d S )Nr#   )r   rP   r†   r‡   rY   rZ   Útype)r    r‰   r†   r
   r
   r   Útest_new_curses_panelU  s    z TestCurses.test_new_curses_panelÚis_term_resizedc             C   s   t j| j ¡ Ž  d S )N)r   r›   r   rW   )r    r
   r
   r   Útest_is_term_resized[  s    zTestCurses.test_is_term_resizedÚresize_termc             C   s   t j| j ¡ Ž  d S )N)r   r   r   rW   )r    r
   r
   r   Útest_resize_term_  s    zTestCurses.test_resize_termÚ
resizetermc             C   sP   | j }tjtj }}|d }|d }t ||¡ |  tj|¡ |  tj|¡ d S )Nr?   )r   r   ZLINESZCOLSrŸ   r   )r    r   ÚlinesÚcolsZ	new_linesZnew_colsr
   r
   r   Útest_resizetermc  s    zTestCurses.test_resizetermc             C   s   t  d¡ | j ¡  d S )Ni  )r   rx   r   rV   )r    r
   r
   r   Útest_issue6243n  s    
zTestCurses.test_issue6243Ú	unget_wchZopenbsdz!OpenBSD's curses (v.5.7) has bugsc             C   sÆ   | j }|j}x´dD ]¬}y| |¡ W n tk
r:   wY nX yt |¡ W n8 tk
r‚ } z|  d||j|f ¡ W d d }~X Y nX | ¡ }|  	||¡ t
|ƒ}t |¡ | ¡ }|  	||¡ qW d S )N)r&   õ   Ã©u   â‚¬u   ô¿¿z)unget_wch(%a) failed with encoding %s: %s)r   ÚencodingÚencodeÚUnicodeEncodeErrorr   r¤   Ú	ExceptionZfailZget_wchr   Úord)r    r   r¦   ÚchÚerrÚreadÚcoder
   r
   r   Útest_unget_wchr  s$    
"
zTestCurses.test_unget_wchc             C   s(   t  t  d¡dd¡}|  t|ƒt¡ d S )NZcupr$   r*   )r   ru   rp   ZassertIsr™   Úbytes)r    Úbr
   r
   r   Útest_issue10570‹  s    zTestCurses.test_issue10570c          	   C   s^   | j }dd l}|j}| |¡ |  t¡ d|_W d Q R X ||_|  t¡
 |`W d Q R X d S )Nr   r#   )r   Úcodecsr¦   ÚlookuprY   rZ   )r    r   r³   r¦   r
   r
   r   Útest_encoding  s    
zTestCurses.test_encodingc             C   sX   | j }yt |j¡}|  |¡ W n tk
r4   Y nX |jj d¡d }|  d|¡ d S )NÚ
r   z[y, x,])	r   ÚinspectÚ	signaturerQ   ÚassertFalser`   Ú__doc__ÚsplitZassertIn)r    r   r¸   Zhuman_readable_signaturer
   r
   r   Útest_issue21088š  s    
	zTestCurses.test_issue21088c             C   sX   | j }t|dƒst d¡‚tjj|dd}| ¡ \}}| |d |d ¡ | 	d¡ d S )NrK   zrequires curses.window.resizeT)Zinsert_moder@   r&   )
r   r   r   r   r   ZtextpadZTextboxrW   rK   Z_insert_printable_char)r    r   r[   r    r¡   r
   r
   r   Útest_issue13051¶  s    

zTestCurses.test_issue13051N)"r•   r–   rT   Úclassmethodr   r   r!   r"   rb   re   rz   r}   r   r€   r‚   r…   r‹   r“   r˜   rš   rœ   rž   r¢   r£   r   ÚskipIfr   r^   r_   r¯   r²   rµ   r¼   r½   r
   r
   r
   r   r   *   s6   	 
6
	
r   c               @   s   e Zd Zedƒdd„ ƒZdS )Ú	MiscTestsÚupdate_lines_colsc             C   s   t  ¡  d S )N)r   rÁ   )r    r
   r
   r   Útest_update_lines_colsÃ  s    z MiscTests.test_update_lines_colsN)r•   r–   rT   r   rÂ   r
   r
   r
   r   rÀ   Á  s   rÀ   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S )Ú	TestAsciic             C   s*   x$t jjD ]}|  tt j|ƒ|¡ q
W d S )N)r   ÚasciiZcontrolnamesZ
assertTruer   )r    r	   r
   r
   r   Útest_controlnamesÎ  s    zTestAscii.test_controlnamesc                s®  ‡ ‡‡fdd„}xbt dƒD ]T‰tˆƒ‰ tˆgƒ}|tjj| ¡ ƒ |tjj| ¡ ƒ |tjj| ¡ ƒ |tjj| ¡ ƒ |tjj	| 	¡ ƒ |tjj
| 
¡ ƒ |tjjˆdk ƒ |tjjˆdkƒ |tjjˆdk ƒ |tjjˆdk pìˆdkƒ |tjjˆ dkƒ |tjjdˆ  k odkn  ƒ |tjjdˆ  koBdkn  ƒ |tjjˆ tjkƒ |tjjˆ tjkƒ qW xd	d
dtjtjd fD ]‰ˆ tj ˆ¡¡ ˆ tj ˆ¡¡ ˆ tj ˆ¡¡ ˆ tj ˆ¡¡ ˆ tj 	ˆ¡¡ ˆ tj 
ˆ¡¡ ˆ tj ˆ¡¡ ˆ tj ˆ¡¡ ˆ tj ˆ¡¡ ˆ tj ˆ¡¡ ˆ tj ˆ¡¡ ˆ tj ˆ¡¡ ˆ tj ˆ¡¡ ˆ tj ˆ¡¡ qW ˆ tj d
¡¡ d S )Nc          	      s>   ˆj ˆ | d& ˆ | ˆƒ|¡ ˆ | ˆ ƒ|¡ W d Q R X d S )N)r«   rf   )rS   r   )rf   Zexpected)rC   r’   r    r
   r   ÚcheckÓ  s    z$TestAscii.test_ctypes.<locals>.checké   é€   é    é   z 	é~   rO   éÿÿÿÿr?   )rŽ   Úchrr°   r   rÄ   ÚisalnumÚisalphaÚisdigitÚislowerÚisspaceÚisupperÚisasciiZismetaZisctrlZiscntrlZisblankZisgraphZisprintZispunctÚstringZpunctuationZisxdigitZ	hexdigitsr   Ú
maxunicoder¹   )r    rÆ   r±   r
   )rC   r’   r    r   Útest_ctypesÒ  sF    
$$ zTestAscii.test_ctypesc             C   sD   t jj}|  |dƒd¡ |  |dƒd¡ |  |tdƒƒtdƒ¡ d S )Nõ   Ãr—   )r   rÄ   r   rª   )r    rÄ   r
   r
   r   Ú
test_asciiÿ  s    zTestAscii.test_asciic             C   sT   t jj}|  |dƒd¡ |  |dƒd¡ |  |dƒd¡ |  |tdƒƒtdƒ¡ d S )NÚJr¶   ú@ú )r   rÄ   Úctrlr   rª   )r    rÝ   r
   r
   r   Ú	test_ctrl  s
    zTestAscii.test_ctrlc             C   s@   t jj}|  |dƒd¡ |  |dƒd¡ |  |tdƒƒd¡ d S )Nr¶   õ   ÂŠr—   rØ   éÁ   )r   rÄ   Úaltr   rª   )r    rá   r
   r
   r   Útest_alt  s    zTestAscii.test_altc             C   sì   t jj}|  |dƒd¡ |  |dƒd¡ |  |dƒd¡ |  |dƒd¡ |  |dƒd¡ |  |dƒd¡ |  |d	ƒd
¡ |  |tdƒƒd¡ |  |tdƒƒd¡ |  |dƒd¡ |  |dƒd¡ |  |tdƒƒd¡ |  |tdƒƒd¡ d S )Nr&   r—   ú;r+   úz^?r¶   z^JrÜ   z^@rß   z!^JrØ   z!A)r   rÄ   rw   r   rª   )r    rw   r
   r
   r   Útest_unctrl  s    zTestAscii.test_unctrlN)	r•   r–   rT   rÅ   r×   rÙ   rÞ   râ   rå   r
   r
   r
   r   rÃ   Ì  s   -rÃ   Ú__main__)ÚosrÕ   r   r   r   Ztest.supportr   r   r   r   r·   r   Zcurses.panelÚImportErrorr   ÚenvironÚgetZtermr¿   r^   ZTestCaser   rÀ   rÃ   r•   Úmainr
   r
   r
   r   Ú<module>   s8      X
