o
    JAf>                     @   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rrejdd dS 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   9/usr/lib/python3.10/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      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   r1   r2   r/   r   deleter   r   Zupdate_idletasksdestroy)r3   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?   r1   rC   unittestr	   patchobjectstartZ
addCleanupstoprD   )r   rB   ZGetHighlight_patcherrG   ZGetFont_patcherr   rA   r   setUpN   s(   
zCodeContextTest.setUpc                 C   s,   | j jr
| j j  | j   | j `| ` d S r   )rJ   r>   r8   __del__rF   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 )Nr:   r   r   Fr'   )
assertEqualr/   rJ   editwinr   assertIsNoner>   info
topvisiblet1)r   eqZedrJ   r   r   r   	test_initn   s   zCodeContextTest.test_initc                 C   s   | j   d S r   )rJ   rQ   rF   r   r   r   test_del{   r    zCodeContextTest.test_delc                 C   sv   | j ddd  }| j_| j  | t}| jj	dd| W d    n1 s+w   Y  | 
dt|j d S )Ni'  c                   S   s   d S r   r   r   r   r   r   <lambda>   s    z5CodeContextTest.test_del_with_timer.<locals>.<lambda>afterrX   zdoesn't exist)r   r_   rJ   rZ   rQ   assertRaisesr   r   tkcallZassertInstr	exception)r   timercmr   r   r   test_del_with_timer~   s   
z#CodeContextTest.test_del_with_timerc                 C   s   t j  | | jjd d S )N   )r   rI   reloadrU   rJ   context_depthrF   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 )Nr:   Zfgr=   bgr<   r%   end-1cr   zHide Code Contextr_   rX   r'   re   zShow Code Context
c                 s   s    | ]}|d  V  qdS )   Nr   ).0xr   r   r   	<genexpr>   s    zACodeContextTest.test_toggle_code_context_event.<locals>.<genexpr>   r   )rU   rJ   toggle_code_context_eventr>   ZassertIsNotNoner   r?   getrV   r   r   ra   rb   rZ   rW   joinget_contextrH   )r   r[   rJ   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w   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)rU   rJ   rw   r`   AssertionError)r   r[   gcr   r   r   test_get_context   sJ   

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 )NrS   r'   ro   r%   rm   r   rz   ry   r{   r}   )rS   rz   r   r~   z'class C1:
    def __init__(self, a, b):rs   )rS   rz   r   r   r      zFclass C1:
    def compare(self):
        if a > b:
        elif a < b:   r   )rU   rJ   r>   rt   rW   update_code_contextrX   rY   r   rH   ru   rj   )r   r[   rJ   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~   )rU   rJ   Z
jumptoliner>   rt   r   rH   r   rY   Zmark_setrj   Ztag_add)r   r[   rJ   Zjumpr   r   r   test_jumptoline.  s<   zCodeContextTest.test_jumptoliner   c                 C   sD   | j jr	| j   | j   |  | j   | j   |  d S r   )rJ   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r|  || _|  |  ||jd | || _|  ||jd | d S )Nr:   Z
TkTextFont)rU   rJ   r   ZassertNotEqualr>   rt   rD   Zupdate_font)r   r[   rJ   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rJ   r[   r   r   assert_colors_are_equal  s   zFCodeContextTest.test_highlight_colors.<locals>.assert_colors_are_equal)rU   rJ   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#   classmethodr4   r9   rP   rR   r\   r]   rg   rk   rx   r   r   r   r	   rL   rM   r   rI   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_firstwordrU   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   )rU   r   Zget_line_infor-   
splitlinesZINFINITY)r   r[   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)__doc__Zidlelibr   rK   Zunittest.mockZtest.supportr   tkinterr   r   r   r   r   r	   r   r
   r1   r2   r6   ZIdleUserConfParserr0   r-   r   ZTestCaser$   r   r!   r   r   r   r   r   <module>   s0      k(