a
    e>                     @   s   d Z 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 )zTest codecontext, coverage 100%    )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   3lib/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sideZfillexpand)rowcolumnZsticky)r   r   r   Zwithdrawr   r   r   r   insertcode_sampleZ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   r.   r/   r,   r   deleter   r   Zupdate_idletasksdestroy)r0   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
configTypeZsectionr   r   r   mock_idleconf_GetFont`   s    z4CodeContextTest.setUp.<locals>.mock_idleconf_GetFontZGetFont)r   yviewr   CodeContextr,   ccr<   r.   r@   unittestr	   patchobjectstartZ
addCleanupstoprA   )r   r?   ZGetHighlight_patcherrC   ZGetFont_patcherr   r>   r   setUpN   s(    
zCodeContextTest.setUpc                 C   s,   | j jr| j j  | j   | j `| ` d S r   )rF   r;   r5   __del__rB   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 )Nr7   r   r   Fr&   )
assertEqualr,   rF   editwinr   assertIsNoner;   info
topvisiblet1)r   eqZedrF   r   r   r   	test_initn   s    zCodeContextTest.test_initc                 C   s   | j   d S r   )rF   rM   rB   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>afterrT   zdoesn't exist)r   r\   rF   rV   rM   assertRaisesr   r   tkcallZassertInstrZ	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   rE   reloadrQ   rF   context_depthrB   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 )Nr7   Zfgr:   Zbgr9   r$   end-1cr   zHide Code Contextr\   rT   r&   ra   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   )rQ   rF   toggle_code_context_eventr;   ZassertIsNotNoner   r<   getrR   r   r   r^   r_   rV   rS   joinget_contextrD   )r   rW   rF   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   rj   r   	class C1:class      rx   rx       def __init__(self, a, b):def
   )ru      rx   z    def compare(self):r|      r           if a > b:ifrn   )ru   r~   r   r}   r   z        elif a < b:elifrj   )r~   r   r   r   r   r   )Z
stopindent)rQ   rF   rr   r]   AssertionError)r   rW   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 )NrO   r&   rj   r$   rh   r   ru   rt   rv   rx   )rO   ru   rz   ry   z'class C1:
    def __init__(self, a, b):rn   )rO   ru   r~   r   r      zFclass C1:
    def compare(self):
        if a > b:
        elif a < b:   r{   )rQ   rF   r;   ro   rS   update_code_contextrT   rU   r   rD   rp   rf   )r   rW   rF   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.0rj   r*   z1.5r&   z12.0r   z3.0r   r$   z5.0Zselry   )rQ   rF   Z
jumptoliner;   ro   r   rD   r   rU   Zmark_setrf   Ztag_add)r   rW   rF   Zjumpr   r   r   test_jumptoline.  s<    zCodeContextTest.test_jumptoliner   c                 C   sD   | j jr| j   | j   |  | j   | j   |  d S r   )rF   r;   ro   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 )Nr7   Z
TkTextFont)rQ   rF   r   ZassertNotEqualr;   ro   rA   Zupdate_font)r   rW   rF   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#ffff00r8   c                    s,    j d | d   j d | d  d S )Nr9   r:   )r;   )ZcolorsrF   rW   r   r   assert_colors_are_equal  s    zFCodeContextTest.test_highlight_colors.<locals>.assert_colors_are_equal)rQ   rF   dictr<   r;   ro   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"   classmethodr1   r6   rL   rN   rX   rY   rc   rg   rs   r   r   r   r	   rH   rI   r   rE   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   Zno)r   r   r   )z# TEST COMMENTr   )    (continuation))r   r   r   z^(\s*)([^\s]*))c)r   z(continuation))r   Zget_spaces_firstwordrQ   recompile)r   rp   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   rv   rw   rj   z    # Class comment.rt   )rx   r{   r|   r   )r   r   r   	if a == b:)r&   r   r   )rQ   r   Zget_line_infor+   
splitlinesZINFINITY)r   rW   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__rj   )	verbosity)__doc__Zidlelibr   rG   Zunittest.mockZtest.supportr   Ztkinterr   r   r   r   r   r	   r   r
   r.   r/   r3   ZIdleUserConfParserr-   r+   r   ZTestCaser#   r   r    r   r   r   r   r   <module>   s,     k(