a
    ze>                     @   s   d dl mZ d dlZd dlZd dlmZ d dlmZmZm	Z	m
Z
mZ d dlmZ d dlZd dl mZ ejjZededededdZd	ZG d
d dZG dd dejZG dd dejZedkrejdd dS )    )codecontextN)requires)NSEWTkFrameTextTclError)mock)config )mainZ	highlightkeys
extensionsz
class C1:
    # Class comment.
    def __init__(self, a, b):
        self.a = a
        self.b = b
    def compare(self):
        if a > b:
            return a
        elif a < b:
            return b
        else:
            return None
c                   @   s$   e Zd Zdd Zdd Zdd ZdS )DummyEditwinc                 C   s"   || _ || _|| _|| _d| _d S )Nr   )roottopZ
text_frametextlabel)selfr   framer    r   8/usr/lib/python3.9/idlelib/idle_test/test_codecontext.py__init__'   s
    zDummyEditwin.__init__c                 C   s   t t| j|S N)intfloatr   index)r   r   r   r   r   	getlineno.   s    zDummyEditwin.getlinenoc                 K   s   |d | _ d S )Nr   )r   )r   kwargsr   r   r   update_menu_label1   s    zDummyEditwin.update_menu_labelN)__name__
__module____qualname__r   r   r   r   r   r   r   r   &   s   r   c                   @   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dd Zdd Zdd Zdd Zejejddd Zdd Zdd Zd S )!CodeContextTestc                 C   s~   t d t  }| _|  t| }| _t| }| _|dt	 |j
dddd |jddtd t|||| _ttj_d S )NZgui1.0leftZboth   )Zsidefillexpand)rowcolumnZsticky)r   r   r   Zwithdrawr   r   r   r   insertcode_samplepackZgridr   r   editortestcfgr   idleConfuserCfg)clsr   r   r   r   r   r   
setUpClass7   s    zCodeContextTest.setUpClassc                 C   s@   t tj_| jjdd | `| `| `| j	  | j
  | `d S )Nr$   end)usercfgr   r0   r1   r.   r   deleter   r   Zupdate_idletasksdestroy)r2   r   r   r   tearDownClassE   s    

zCodeContextTest.tearDownClassc                    s   j d dj d< tj_ddd_tjj  fdd}t	j
jtjd	|}|  |j d_fd
d}t	j
jtjd|}|  |j d S )Nr   ZTkFixedFontfontz#abcdefz#123456
background
foregroundc                    s   |dkrj S  | |S )Ncontext)highlight_cfg)ZthemeelementZorig_idleConf_GetHighlightr   r   r   mock_idleconf_GetHighlightV   s    z9CodeContextTest.setUp.<locals>.mock_idleconf_GetHighlightGetHighlightc                    s    j S r   )font_override)r   Z
configTypesectionr   r   r   mock_idleconf_GetFont`   s    z4CodeContextTest.setUp.<locals>.mock_idleconf_GetFontZGetFont)r   yviewr   CodeContextr.   ccr>   r0   rB   unittestr	   patchobjectstartZ
addCleanupstoprC   )r   rA   ZGetHighlight_patcherrF   ZGetFont_patcherr   r@   r   setUpN   s(    
zCodeContextTest.setUpc                 C   s,   | j jr| j j  | j   | j `| ` d S r   )rI   r=   r7   __del__rE   r   r   r   tearDowng   s    
zCodeContextTest.tearDownc                 C   sz   | j }| j}| j}||j| ||j|j ||jd |jd  | |j ||jdg ||jd | | jj	 d S )Nr9   r   r   Fr&   )
assertEqualr.   rI   editwinr   assertIsNoner=   info
topvisiblet1)r   eqZedrI   r   r   r   	test_initn   s    zCodeContextTest.test_initc                 C   s   | j   d S r   )rI   rP   rE   r   r   r   test_del{   s    zCodeContextTest.test_delc                 C   sv   | j ddd  }| j_| j  | t"}| jj	dd| W d    n1 sV0    Y  | 
dt|j d S )Ni'  c                   S   s   d S r   r   r   r   r   r   <lambda>       z5CodeContextTest.test_del_with_timer.<locals>.<lambda>afterrW   zdoesn't exist)r   r_   rI   rY   rP   assertRaisesr   r   tkcallZassertInstr	exception)r   timercmr   r   r   test_del_with_timer~   s
    
0z#CodeContextTest.test_del_with_timerc                 C   s   t j  | | jjd d S )N   )r   rH   reloadrT   rI   context_depthrE   r   r   r   test_reload   s    
zCodeContextTest.test_reloadc                 C   sJ  | j }| j}|j}|jr|  |  | |j ||jd | jd  ||jd | jd  ||jd | jd  ||jddd ||jj	d	 || j
jd
d| jjd d |  | |j ||jj	d | | jj ddd |dd D }|jd |  ||jdd| |  |  ||jdd| d S )Nr9   Zfgr<   bgr;   r$   end-1cr   zHide Code Contextr_   rW   r&   re   zShow Code Context
c                 s   s   | ]}|d  V  qdS )   Nr   ).0xr   r   r   	<genexpr>   r^   zACodeContextTest.test_toggle_code_context_event.<locals>.<genexpr>   r   )rT   rI   toggle_code_context_eventr=   ZassertIsNotNoner   r>   getrU   r   r   ra   rb   rY   rV   joinget_contextrG   )r   rZ   rI   ZtoggleZline11_contextr   r   r   test_toggle_code_context_event   s0     z.CodeContextTest.test_toggle_code_context_eventc                 C   s4  | j }| jj}| t |ddd W d    n1 s:0    Y  ||ddgdf ||ddgdf ||dddgdf ||d	g d
df ||dg ddf ||dddg ddf ||dddg ddf ||dddddgdf ||dddg ddf ||dddddgdf d S )Nr&   r   )Zstopline   ro   r   	class C1:class      r}   r}       def __init__(self, a, b):def
   )rz      r}   z    def compare(self):r      r           if a > b:ifrs   )rz   r   r   r   r   z        elif a < b:elifro   )r   r   r   r   r   r   )Z
stopindent)rT   rI   rw   r`   AssertionError)r   rZ   gcr   r   r   test_get_context   sH    *
z CodeContextTest.test_get_contextc                 C   s  | j }| j}|js|  | |  ||jdg ||jd |j	d |  ||jdg ||jd ||j
ddd |j	d |  ||jddg ||jd ||j
ddd	 |j	d |  ||jddg ||jd
 ||j
ddd	 |j	d
 |  ||jg d ||jd ||j
ddd |j	d |  ||jg d ||jd ||j
ddd |  d|_||jg d ||jd ||j
ddd |j	d |  ||jg d ||jd ||j
ddd d S )NrR   r&   ro   r$   rm   r   rz   ry   r{   r}   )rR   rz   r   r~   z'class C1:
    def __init__(self, a, b):rs   )rR   rz   r   r   r      zFclass C1:
    def compare(self):
        if a > b:
        elif a < b:   r   )rT   rI   r=   rt   rV   update_code_contextrW   rX   r   rG   ru   rj   )r   rZ   rI   r   r   r   test_update_code_context   sT    z(CodeContextTest.test_update_code_contextc                 C   s.  | j }| j}|j}|js |  |jd |  ||jd |j	dd |  ||jd |jd |  ||jd |j	dd |  ||jd	 d|_
|jd |  ||jd |j	dd
 |  ||jd	 |jd |  |jdd
d |j	dd
 |  ||jd d S )Nz2.0ro   r+   z1.5r&   z12.0r   z3.0r   r$   z5.0Zselr~   )rT   rI   Z
jumptoliner=   rt   r   rG   r   rX   Zmark_setrj   Ztag_add)r   rZ   rI   Zjumpr   r   r   test_jumptoline.  s<    zCodeContextTest.test_jumptoliner   c                 C   sD   | j jr| j   | j   |  | j   | j   |  d S r   )rI   r=   rt   Ztimer_eventZassert_not_calledZassert_called)r   Zmock_updater   r   r   test_timer_eventW  s    



z CodeContextTest.test_timer_eventc                 C   s   | j }| j}|jd }d}| || |jd ur8|  || _|  |  ||jd | || _|  ||jd | d S )Nr9   Z
TkTextFont)rT   rI   r   ZassertNotEqualr=   rt   rC   Zupdate_font)r   rZ   rI   Z	orig_font	test_fontr   r   r   r   d  s    

zCodeContextTest.test_fontc                    s   | j | j t| j}ddd} fdd} jr<   || _      ||    || || _   || d S )Nz#222222z#ffff00r:   c                    s,    j d | d   j d | d  d S )Nr;   r<   )r=   )ZcolorsrI   rZ   r   r   assert_colors_are_equal  s    zFCodeContextTest.test_highlight_colors.<locals>.assert_colors_are_equal)rT   rI   dictr>   r=   rt   Zupdate_highlight_colors)r   Zorig_colorsZtest_colorsr   r   r   r   test_highlight_colors}  s     

z%CodeContextTest.test_highlight_colorsN)r    r!   r"   classmethodr3   r8   rO   rQ   r[   r\   rg   rk   rx   r   r   r   r	   rK   rL   r   rH   r   r   r   r   r   r   r   r#   5   s$   

$0Q)
r#   c                   @   s   e Zd Zdd Zdd ZdS )HelperFunctionTextc                 C   sF   t j}d}|D ]\}}| ||| q| |dtddd d S )N))z    first word)    first)z	first word)	r   )u
     ᧔᧒: )z  u   ᧔᧒)z	no spaces)r   no)r   r   r   )z# TEST COMMENTr   )    (continuation))r   r   r   z^(\s*)([^\s]*))c)r   z(continuation))r   Zget_spaces_firstwordrT   recompile)r   ru   Z
test_lineslineZexpected_outputr   r   r   test_get_spaces_firstword  s    	z,HelperFunctionText.test_get_spaces_firstwordc                 C   s   | j }tj}t }|||d tjddf |||d d |||d tjddf |||d d	 |||d
 d ||dd d S )Nr   r   Fr&   )r   r{   r|   ro   z    # Class comment.ry   )r}   r   r   r   )r   r   r   	if a == b:)r&   r   r   )rT   r   Zget_line_infor,   
splitlinesZINFINITY)r   rZ   Zglilinesr   r   r   test_get_line_info  s    z%HelperFunctionText.test_get_line_infoN)r    r!   r"   r   r   r   r   r   r   r     s   r   __main__ro   )	verbosity)Zidlelibr   rJ   Zunittest.mockZtest.supportr   tkinterr   r   r   r   r   r	   r   r
   r0   r1   r5   ZIdleUserConfParserr/   r,   r   ZTestCaser#   r   r    r   r   r   r   r   <module>   s*     k(