B
    v9aí5  ã               @   sV  d Z ddlmZ ddlZddlmZ ddlmZ ddlZddlm	Z	 ddl
mZ G dd	„ d	ejƒZG d
d„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG d d!„ d!ejƒZed"krRejd#d$d% dS )&a½  Test query, coverage 93%).

Non-gui tests for Query, SectionName, ModuleName, and HelpSource use
dummy versions that extract the non-gui methods and add other needed
attributes.  GUI tests create an instance of each class and simulate
entries and button clicks.  Subclass tests only target the new code in
the subclass definition.

The appearance of the widgets is checked by the Query and
HelpSource htests.  These are run by running query.py.
é    )ÚqueryN)Úrequires)ÚTk)Úmock)ÚVarc               @   sF   e Zd ZdZG dd„ dƒZdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )Ú	QueryTestzTest Query base class.c               @   s<   e Zd ZejjZejjZejjZdd„ Zdd„ Z	dd„ Z
dS )zQueryTest.Dummy_Queryc             C   s&   t |d| _ddi| _d | _d| _d S )N)ÚvalueÚtextÚ F)r   ÚentryÚentry_errorÚresultÚ	destroyed)ÚselfÚdummy_entry© r   ú /usr/lib/python3.7/test_query.pyÚ__init__!   s    
zQueryTest.Dummy_Query.__init__c             C   s   || j d< d S )Nr	   )r   )r   Úmessager   r   r   Ú	showerror&   s    zQueryTest.Dummy_Query.showerrorc             C   s
   d| _ d S )NT)r   )r   r   r   r   Údestroy(   s    zQueryTest.Dummy_Query.destroyN)Ú__name__Ú
__module__Ú__qualname__r   ÚQueryÚentry_okÚokÚcancelr   r   r   r   r   r   r   ÚDummy_Query   s   r   c             C   sD   |   d¡}|  | ¡ d ¡ |  |j|jfd¡ |  d|jd ¡ d S )Nú )NFz
blank liner	   )r   ÚassertEqualr   r   r   ÚassertInr   )r   Údialogr   r   r   Útest_entry_ok_blank+   s    
zQueryTest.test_entry_ok_blankc             C   sD   |   d¡}| j}|| ¡ dƒ ||j|jfdƒ ||jd dƒ d S )Nz  good Úgood)NFr	   r
   )r   r    r   r   r   r   )r   r"   ÚEqualr   r   r   Útest_entry_ok_good1   s
    
zQueryTest.test_entry_ok_goodc             C   sT   |   d¡}t ¡ |j_|  | ¡ d ¡ |  |jjj¡ |j`|  |j	|j
fd¡ d S )Nr
   )NF)r   r   ÚMockr   Z	focus_setr    r   Ú
assertTrueÚcalledr   r   )r   r"   r   r   r   Útest_ok_blank8   s    
zQueryTest.test_ok_blankc             C   s2   |   d¡}|  | ¡ d ¡ |  |j|jfd¡ d S )Nr$   )r$   T)r   r    r   r   r   )r   r"   r   r   r   Útest_ok_good@   s    
zQueryTest.test_ok_goodc             C   s2   |   d¡}|  | ¡ d ¡ |  |j|jfd¡ d S )Nzdoes not matter)NT)r   r    r   r   r   )r   r"   r   r   r   Útest_cancelE   s    
zQueryTest.test_cancelN)
r   r   r   Ú__doc__r   r#   r&   r*   r+   r,   r   r   r   r   r      s   r   c               @   s>   e Zd ZdZG dd„ dƒZdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚSectionNameTestz#Test SectionName subclass of Query.c               @   s*   e Zd ZejjZdgZdd„ Zdd„ ZdS )z!SectionNameTest.Dummy_SectionNameÚusedc             C   s   t |d| _ddi| _d S )N)r   r	   r
   )r   r   r   )r   r   r   r   r   r   Q   s    z*SectionNameTest.Dummy_SectionName.__init__c             C   s   || j d< d S )Nr	   )r   )r   r   r   r   r   r   T   s    z+SectionNameTest.Dummy_SectionName.showerrorN)	r   r   r   r   ÚSectionNamer   Ú
used_namesr   r   r   r   r   r   ÚDummy_SectionNameN   s   r2   c             C   s0   |   d¡}|  | ¡ d ¡ |  d|jd ¡ d S )Nr   zno namer	   )r2   r    r   r!   r   )r   r"   r   r   r   Útest_blank_section_nameW   s    
z'SectionNameTest.test_blank_section_namec             C   s0   |   d¡}|  | ¡ d ¡ |  d|jd ¡ d S )Nr/   Zuser	   )r2   r    r   r!   r   )r   r"   r   r   r   Útest_used_section_name\   s    
z&SectionNameTest.test_used_section_namec             C   s0   |   d¡}|  | ¡ d ¡ |  d|jd ¡ d S )NZ goodgoodgoodgoodgoodgoodgoodgoodzlonger than 30r	   )r2   r    r   r!   r   )r   r"   r   r   r   Útest_long_section_namea   s    
z&SectionNameTest.test_long_section_namec             C   s0   |   d¡}|  | ¡ d¡ |  |jd d¡ d S )Nz  good r$   r	   r
   )r2   r    r   r   )r   r"   r   r   r   Útest_good_section_namef   s    
z&SectionNameTest.test_good_section_nameN)	r   r   r   r-   r2   r3   r4   r5   r6   r   r   r   r   r.   K   s   	r.   c               @   s>   e Zd ZdZG dd„ dƒZdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚModuleNameTestz"Test ModuleName subclass of Query.c               @   s(   e Zd ZejjZdZdd„ Zdd„ ZdS )zModuleNameTest.Dummy_ModuleNamer
   c             C   s   t |d| _ddi| _d S )N)r   r	   r
   )r   r   r   )r   r   r   r   r   r   r   s    z(ModuleNameTest.Dummy_ModuleName.__init__c             C   s   || j d< d S )Nr	   )r   )r   r   r   r   r   r   u   s    z)ModuleNameTest.Dummy_ModuleName.showerrorN)	r   r   r   r   Ú
ModuleNamer   Útext0r   r   r   r   r   r   ÚDummy_ModuleNameo   s   r:   c             C   s0   |   d¡}|  | ¡ d ¡ |  d|jd ¡ d S )Nr   zno namer	   )r:   r    r   r!   r   )r   r"   r   r   r   Útest_blank_module_namex   s    
z%ModuleNameTest.test_blank_module_namec             C   s0   |   d¡}|  | ¡ d ¡ |  d|jd ¡ d S )NZ __name_xyz123_should_not_exist__z	not foundr	   )r:   r    r   r!   r   )r   r"   r   r   r   Útest_bogus_module_name}   s    
z%ModuleNameTest.test_bogus_module_namec             C   s0   |   d¡}|  | ¡ d ¡ |  d|jd ¡ d S )NÚ	itertoolszsource-basedr	   )r:   r    r   r!   r   )r   r"   r   r   r   Útest_c_source_name‚   s    
z!ModuleNameTest.test_c_source_namec             C   s4   |   d¡}|  | ¡  d¡¡ |  |jd d¡ d S )NÚidlelibz__init__.pyr	   r
   )r:   r(   r   Úendswithr    r   )r   r"   r   r   r   Útest_good_module_name‡   s    
z$ModuleNameTest.test_good_module_nameN)	r   r   r   r-   r:   r;   r<   r>   rA   r   r   r   r   r7   l   s   	r7   c               @   s&   e Zd ZdZG dd„ dƒZdd„ ZdS )ÚHelpsourceBrowsefileTestz8Test browse_file method of ModuleName subclass of Query.c               @   s   e Zd ZejjZeƒ ZdS )z)HelpsourceBrowsefileTest.Dummy_HelpSourceN)r   r   r   r   Ú
HelpSourceÚbrowse_filer   Úpathvarr   r   r   r   ÚDummy_HelpSource’   s   rF   c          
   C   s   |   ¡ }x‚ddd„ dfddd„ tfddd„ dfddd„ tffD ]J\}}}|  ¡ 2 |j |¡ ||_| ¡  |  |j ¡ |¡ W d Q R X q>W d S )Nr
   c             S   s   dS )Nr
   r   )ÚaÚbÚcr   r   r   Ú<lambda>   ó    zBHelpsourceBrowsefileTest.test_file_replaces_path.<locals>.<lambda>c             S   s   t S )N)Ú__file__)rG   rH   rI   r   r   r   rJ   ž   rK   Zhtestc             S   s   dS )Nr
   r   )rG   rH   rI   r   r   r   rJ   Ÿ   rK   c             S   s   t S )N)rL   )rG   rH   rI   r   r   r   rJ       rK   )	rF   rL   ÚsubTestrE   ÚsetZaskfilenamerD   r    Úget)r   r"   ÚpathÚfuncr   r   r   r   Útest_file_replaces_path–   s    
z0HelpsourceBrowsefileTest.test_file_replaces_pathN)r   r   r   r-   rF   rR   r   r   r   r   rB      s   rB   c               @   sP   e Zd ZdZG dd„ dƒZejZedd„ ƒZ	dd„ Z
dd	„ Zd
d„ Zdd„ ZdS )ÚHelpsourcePathokTestz4Test path_ok method of HelpSource subclass of Query.c               @   s&   e Zd ZejjZdd„ Zddd„ZdS )z%HelpsourcePathokTest.Dummy_HelpSourcec             C   s   t |d| _ddi| _d S )N)r   r	   r
   )r   rP   Ú
path_error)r   Z
dummy_pathr   r   r   r   ­   s    z.HelpsourcePathokTest.Dummy_HelpSource.__init__Nc             C   s   || j d< d S )Nr	   )rT   )r   r   Zwidgetr   r   r   r   °   s    z/HelpsourcePathokTest.Dummy_HelpSource.showerror)N)r   r   r   r   rC   Úpath_okr   r   r   r   r   r   rF   «   s   rF   c             C   s   | j t_d S )N)Úorig_platformr   Úplatform)Úclsr   r   r   ÚtearDownClass´   s    z"HelpsourcePathokTest.tearDownClassc             C   s0   |   d¡}|  | ¡ d ¡ |  d|jd ¡ d S )Nr   zno help filer	   )rF   r    rU   r!   rT   )r   r"   r   r   r   Útest_path_ok_blank¸   s    
z'HelpsourcePathokTest.test_path_ok_blankc             C   s4   |   td ¡}|  | ¡ d ¡ |  d|jd ¡ d S )Nzbad-bad-badz	not existr	   )rF   rL   r    rU   r!   rT   )r   r"   r   r   r   Útest_path_ok_bad½   s    z%HelpsourcePathokTest.test_path_ok_badc          
   C   sd   |   d¡}| j}xNdD ]F}|  ¡ 4 |j |¡ |  | ¡ |¡ |  |jd d¡ W d Q R X qW d S )Nr
   )z
www.py.orgzhttp://py.orgr	   )rF   r    rM   rP   rN   rU   rT   )r   r"   r%   Zurlr   r   r   Útest_path_ok_webÂ   s    


z%HelpsourcePathokTest.test_path_ok_webc          
   C   sl   |   d¡}x\dD ]T\}}|  ¡ > |t_|j t¡ |  | ¡ |t ¡ |  |j	d d¡ W d Q R X qW d S )Nr
   ))Údarwinzfile://)Úotherr
   r	   )
rF   rM   r   rW   rP   rN   rL   r    rU   rT   )r   r"   rW   Úprefixr   r   r   Útest_path_ok_fileË   s    

z&HelpsourcePathokTest.test_path_ok_fileN)r   r   r   r-   rF   r   rW   rV   ÚclassmethodrY   rZ   r[   r\   r`   r   r   r   r   rS   ¨   s   	rS   c               @   s&   e Zd ZdZG dd„ dƒZdd„ ZdS )ÚHelpsourceEntryokTestz5Test entry_ok method of HelpSource subclass of Query.c               @   s,   e Zd ZejjZi Zi Zdd„ Zdd„ Z	dS )z&HelpsourceEntryokTest.Dummy_HelpSourcec             C   s   | j S )N)Úname)r   r   r   r   Úitem_okÜ   s    z.HelpsourceEntryokTest.Dummy_HelpSource.item_okc             C   s   | j S )N)rP   )r   r   r   r   rU   Þ   s    z.HelpsourceEntryokTest.Dummy_HelpSource.path_okN)
r   r   r   r   rC   r   r   rT   rd   rU   r   r   r   r   rF   Ø   s
   rF   c          
   C   sR   |   ¡ }xDdD ]<\}}}|  ¡ $ || |_|_|  | ¡ |¡ W d Q R X qW d S )N))NNN)Nzdoc.txtN)ÚdocNN)re   zdoc.txt)re   zdoc.txt)rF   rM   rc   rP   r    r   )r   r"   rc   rP   r   r   r   r   Útest_entry_ok_helpsourceá   s
    
z.HelpsourceEntryokTest.test_entry_ok_helpsourceN)r   r   r   r-   rF   rf   r   r   r   r   rb   Õ   s   	rb   c               @   s6   e Zd ZdZG dd„ dƒZdd„ Zdd„ Zdd	„ Zd
S )ÚCustomRunCLIargsokTestz6Test cli_ok method of the CustomRun subclass of Query.c               @   s$   e Zd ZejjZdd„ Zdd„ ZdS )z&CustomRunCLIargsokTest.Dummy_CustomRunc             C   s   t |d| _ddi| _d S )N)r   r	   r
   )r   r   r   )r   r   r   r   r   r   ó   s    z/CustomRunCLIargsokTest.Dummy_CustomRun.__init__c             C   s   || j d< d S )Nr	   )r   )r   r   r   r   r   r   ö   s    z0CustomRunCLIargsokTest.Dummy_CustomRun.showerrorN)r   r   r   r   Ú	CustomRunÚcli_args_okr   r   r   r   r   r   ÚDummy_CustomRunñ   s   rj   c             C   s   |   d¡}|  | ¡ g ¡ d S )Nr   )rj   r    ri   )r   r"   r   r   r   Útest_blank_argsù   s    
z&CustomRunCLIargsokTest.test_blank_argsc             C   s0   |   d¡}|  | ¡ d ¡ |  d|jd ¡ d S )Nz'no-closing-quotez
No closingr	   )rj   r    ri   r!   r   )r   r"   r   r   r   Útest_invalid_argsý   s    
z(CustomRunCLIargsokTest.test_invalid_argsc             C   sP   ddddddg}|   d |¡d ¡}|  | ¡ |d	g ¡ |  |jd
 d¡ d S )Nz-nZ10z	--verbosez-pz/pathz--namer   z
 "my name"zmy namer	   r
   )rj   Újoinr    ri   r   )r   Úargsr"   r   r   r   Útest_good_args  s    z%CustomRunCLIargsokTest.test_good_argsN)r   r   r   r-   rj   rk   rl   ro   r   r   r   r   rg   î   s
   rg   c               @   s&   e Zd ZdZG dd„ dƒZdd„ ZdS )ÚCustomRunEntryokTestz8Test entry_ok method of the CustomRun subclass of Query.c               @   s&   e Zd ZejjZi Zeƒ Zdd„ Z	dS )z$CustomRunEntryokTest.Dummy_CustomRunc             C   s   | j S )N)Úcli_args)r   r   r   r   ri     s    z0CustomRunEntryokTest.Dummy_CustomRun.cli_args_okN)
r   r   r   r   rh   r   r   r   Ú
restartvarri   r   r   r   r   rj     s   rj   c             C   sx   |   ¡ }xjdD ]b}|j |¡ xPddgdg|fffD ]8\}}| j||d ||_|  | ¡ |¡ W d Q R X q4W qW d S )N>   FT)NNzmy arg)Úrestartrq   )rj   rr   rN   rM   rq   r    r   )r   r"   rs   rq   r   r   r   r   Útest_entry_ok_customrun  s    
z,CustomRunEntryokTest.test_entry_ok_customrunN)r   r   r   r-   rj   rt   r   r   r   r   rp   	  s   rp   c               @   sD   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S )ÚQueryGuiTestc             C   sB   t dƒ tƒ  | _}| j ¡  tj|dddd| _t ¡ | j_	d S )NÚguiZTESTZtestT)Ú_utest)
r   r   ÚrootÚwithdrawr   r   r"   r   r'   r   )rX   rx   r   r   r   Ú
setUpClass"  s
    
zQueryGuiTest.setUpClassc             C   s   | j `| ` | j ¡  | `d S )N)r"   r   rx   )rX   r   r   r   rY   *  s    
zQueryGuiTest.tearDownClassc             C   s(   | j j dd¡ d | j _| j j ¡  d S )Nr   Úend)r"   r   Údeleter   r   Z
reset_mock)r   r   r   r   ÚsetUp1  s    zQueryGuiTest.setUpc             C   s>   | j }|j dd¡ |j ¡  |  |jd¡ |  |jj	¡ d S )Nr   Úabc)
r"   r   ÚinsertÚ	button_okÚinvoker    r   r(   r   r)   )r   r"   r   r   r   Útest_click_ok6  s
    
zQueryGuiTest.test_click_okc             C   s0   | j }|j ¡  |  |jd ¡ |  |jj¡ d S )N)r"   r€   r   r    r   ZassertFalser   r)   )r   r"   r   r   r   Útest_click_blank=  s    
zQueryGuiTest.test_click_blankc             C   s>   | j }|j dd¡ |j ¡  |  |jd ¡ |  |jj	¡ d S )Nr   r~   )
r"   r   r   Zbutton_cancelr   r    r   r(   r   r)   )r   r"   r   r   r   Útest_click_cancelC  s
    
zQueryGuiTest.test_click_cancelN)
r   r   r   ra   rz   rY   r}   r‚   rƒ   r„   r   r   r   r   ru      s   ru   c               @   s    e Zd Zedd„ ƒZdd„ ZdS )ÚSectionnameGuiTestc             C   s   t dƒ d S )Nrv   )r   )rX   r   r   r   rz   M  s    zSectionnameGuiTest.setUpClassc             C   sl   t ƒ }| ¡  tj|dddhdd}| j}|  |jdh¡ |j dd¡ |j 	¡  |  |j
d¡ | ¡  d S )NÚTÚtr~   T)rw   r   Úokay)r   ry   r   r0   r    r1   r   r   r€   r   r   r   )r   rx   r"   r%   r   r   r   Útest_click_section_nameQ  s    
z*SectionnameGuiTest.test_click_section_nameN)r   r   r   ra   rz   r‰   r   r   r   r   r…   K  s   r…   c               @   s    e Zd Zedd„ ƒZdd„ ZdS )ÚModulenameGuiTestc             C   s   t dƒ d S )Nrv   )r   )rX   r   r   r   rz   _  s    zModulenameGuiTest.setUpClassc             C   sj   t ƒ }| ¡  tj|ddddd}|  |jd¡ |  |j ¡ d¡ |j 	¡  |  
|j d¡¡ | ¡  d S )Nr†   r‡   r?   T)rw   z__init__.py)r   ry   r   r8   r    r9   r   rO   r€   r   r(   r   r@   r   )r   rx   r"   r   r   r   Útest_click_module_namec  s    
z(ModulenameGuiTest.test_click_module_nameN)r   r   r   ra   rz   r‹   r   r   r   r   rŠ   ]  s   rŠ   c               @   s    e Zd Zedd„ ƒZdd„ ZdS )ÚHelpsourceGuiTestc             C   s   t dƒ d S )Nrv   )r   )rX   r   r   r   rz   p  s    zHelpsourceGuiTest.setUpClassc             C   s„   t ƒ }| ¡  tj|ddtdd}| j}||j ¡ dƒ ||j ¡ tƒ |j	 
¡  tjdkr`dnd}||jd|t fƒ | ¡  d S )Nr†   Z__test__T)ZmenuitemÚfilepathrw   r]   zfile://r
   )r   ry   r   rC   rL   r    r   rO   rP   r€   r   ÚsysrW   r   r   )r   rx   r"   r%   r_   r   r   r   Útest_click_help_sourcet  s    


z(HelpsourceGuiTest.test_click_help_sourceN)r   r   r   ra   rz   r   r   r   r   r   rŒ   n  s   rŒ   c               @   s    e Zd Zedd„ ƒZdd„ ZdS )ÚCustomRunGuiTestc             C   s   t dƒ d S )Nrv   )r   )rX   r   r   r   rz   „  s    zCustomRunGuiTest.setUpClassc             C   sV   t ƒ }| ¡  tj|ddd}|j dd¡ |j ¡  |  |j	dgdf¡ | 
¡  d S )NZTitleT)rw   r   rˆ   )r   ry   r   rh   r   r   r€   r   r    r   r   )r   rx   r"   r   r   r   Útest_click_argsˆ  s    
z CustomRunGuiTest.test_click_argsN)r   r   r   ra   rz   r‘   r   r   r   r   r   ‚  s   r   Ú__main__é   F)Ú	verbosityÚexit)r-   r?   r   ZunittestZtest.supportr   Ztkinterr   rŽ   r   Zidlelib.idle_test.mock_tkr   ZTestCaser   r.   r7   rB   rS   rb   rg   rp   ru   r…   rŠ   rŒ   r   r   Úmainr   r   r   r   Ú<module>   s,   3!#-+
