B
    u9aq1  ć               @   s¦   d dl Z d dlZd dlmZ e d”ZejZdZdZdZ	G dd dej
ZG d	d
 d
ZG dd deej
ZG dd deej
ZG dd dej
Zedkr¢e ”  dS )é    N)ŚsupportŚturtlea_  width = 0.75
height = 0.8
canvwidth = 500
canvheight = 200
leftright = 100
topbottom = 100
mode = world
colormode = 255
delay = 100
undobuffersize = 10000
shape = circle
pencolor  = red
fillcolor  = blue
resizemode  = auto
visible  = None
language = english
exampleturtle = turtle
examplescreen = screen
title = Python Turtle Graphics
using_IDLE = ''
z# Comments!
# Testing comments!
pencolor  = red
fillcolor  = blue
visible  = False
language = english
# Some more
# comments
using_IDLE = False
z0
pencolor = red
fillcolor: blue
visible = False
c               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
ŚTurtleConfigTestc          	   C   s8   |   tjtj” ttjd}| |” W d Q R X tjS )NŚw)Z
addCleanupr   ŚunlinkZTESTFNŚopenŚwrite)ŚselfZcfg_strŚf© r   ś!/usr/lib/python3.7/test_turtle.pyŚget_cfg_file4   s    zTurtleConfigTest.get_cfg_filec             C   sR   |   t”}t |”}ddddddddddd	d
ddd dddddd}|  ||” d S )Ng      č?gé?iō  éČ   éd   Zworldé’   i'  ZcircleŚredŚblueŚautoŚenglishr   ZscreenzPython Turtle GraphicsŚ )ŚwidthZheightZ	canvwidthZ
canvheightZ	leftrightZ	topbottomŚmodeZ	colormodeZdelayZundobuffersizeŚshapeŚpencolorŚ	fillcolorZ
resizemodeŚvisibleŚlanguageZexampleturtleZexamplescreenŚtitleŚ
using_IDLE)r   Śtest_configr   Śconfig_dictŚassertEqual)r	   Ścfg_nameŚ
parsed_cfgŚexpectedr   r   r   Śtest_config_dict:   s.    

z!TurtleConfigTest.test_config_dictc             C   s4   |   t”}t |”}dddddd}|  ||” d S )Nr   r   Fr   )r   r   r   r   r   )r   Śtest_config_twor   r    r!   )r	   r"   r#   r$   r   r   r   Ś'test_partial_config_dict_with_commmentsX   s    

z8TurtleConfigTest.test_partial_config_dict_with_commmentsc          	   C   s^   |   t”}t ” }t |”}W d Q R X | ” }|  d|” |  d|” |  |ddd” d S )NzBad line in config-file zfillcolor: bluer   F)r   r   )	r   Śinvalid_test_configr   Zcaptured_stdoutr   r    ŚgetvalueZassertInr!   )r	   r"   Śstdoutr#   Zerr_msgr   r   r   Śtest_config_dict_invalidg   s    

z)TurtleConfigTest.test_config_dict_invalidN)Ś__name__Ś
__module__Ś__qualname__r   r%   r'   r+   r   r   r   r   r   2   s   r   c               @   s   e Zd Zdd ZdS )ŚVectorComparisonMixinc             C   sT   t |t |kr|  d” x4tt||D ]"\}\}}| j||d |”d q*W d S )NzTuples are not of equal sizezvalues at index {} do not match)Śmsg)ŚlenZfailŚ	enumerateŚzipŚassertAlmostEqualŚformat)r	   Śvec1Śvec2ŚidxŚiŚjr   r   r   ŚassertVectorsAlmostEqual{   s
    
z.VectorComparisonMixin.assertVectorsAlmostEqualN)r,   r-   r.   r;   r   r   r   r   r/   y   s   r/   c               @   sd   e Z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dd ZdS )Ś	TestVec2Dc             C   s   t dd}|  |d d” |  |d d” |  |t ” |  tt ” |  tt d” |  tt d” |  tt |” |  tt ddd” d S )Ng      ą?é   r   é   )r   r>   )ŚVec2Dr!   ŚassertIsInstanceŚassertRaisesŚ	TypeError)r	   Śvecr   r   r   Śtest_constructor   s    
zTestVec2D.test_constructorc             C   s   t dd}|  t|d” d S )Ng¾/Ż$ā?gX9“Čv¾ó?z(0.57,1.23))r?   r!   Śrepr)r	   rC   r   r   r   Ś	test_repr   s    
zTestVec2D.test_reprc             C   sf   t dd}t dd}t dd}|  ||” |  |t|” |  t||” |  ||” |  ||” d S )Nr   r>   g        é*   )r?   r!   ŚtupleZassertNotEqual)r	   r6   r7   Zvec3r   r   r   Śtest_equality   s    


zTestVec2D.test_equalityc          
   C   sn   t dd}x^ttjd D ]L}| j|d6 tj||d}t |”}|  ||” |  |t ” W d Q R X qW d S )Ng      ą?r=   r>   )Śproto)Zprotocol)	r?   ŚrangeŚpickleZHIGHEST_PROTOCOLŚsubTestŚdumpsŚloadsr!   r@   )r	   rC   rJ   ZpickledZ	unpickledr   r   r   Śtest_pickling   s    

zTestVec2D.test_picklingc       
   
   C   sd   x^|D ]V}| j |d@ |\\}}}t| }t| }|||}	t| }|  |	|” W d Q R X qW d S )N)Ścase)rM   r?   r;   )
r	   Ś
test_casesZlambda_operatorZ	test_caseŚfirstŚsecondr$   Zop1Zop2Śresultr   r   r   Ś_assert_arithmetic_casesØ   s    

z"TestVec2D._assert_arithmetic_casesc             C   s   dddg}|   |dd ” d S )N)))r   r   )r>   r>   )g      š?g      š?)))é’’’’r   )r=   r=   )r>   r=   )))g      ų?r   )r>   r>   )g      @r>   c             S   s   | | S )Nr   )ŚxŚyr   r   r   Ś<lambda>æ   ó    z0TestVec2D.test_vector_addition.<locals>.<lambda>)rV   )r	   rR   r   r   r   Śtest_vector_addition·   s    zTestVec2D.test_vector_additionc             C   s   ddg}|   |dd ” d S )N)))r   r   )r>   r>   )rW   rW   )))g     @%@g      Ą?)é
   r   )g      ä?g      Ą?c             S   s   | | S )Nr   )rX   rY   r   r   r   rZ   Č   r[   z3TestVec2D.test_vector_subtraction.<locals>.<lambda>)rV   )r	   rR   r   r   r   Śtest_vector_subtractionĮ   s    z!TestVec2D.test_vector_subtractionc             C   sX   t dd}t dd}|| }d}|  ||” t dd}|d }t dd}|  ||” d S )Nr]   g      ą?é   é#   é   é   )r?   r4   r;   )r	   r6   r7   Zanswerr$   rC   r   r   r   Śtest_vector_multiplyŹ   s    



zTestVec2D.test_vector_multiplyc             C   s    t dd}d}|  | |” d S )Nr]   iö’’’)iö’’’r]   )r?   r;   )r	   rC   r$   r   r   r   Śtest_vector_negative×   s    
zTestVec2D.test_vector_negativec             C   s^   t dd}d}|  t||” t dd}d}|  t||” t dd}d}|  t||” d S )Né   é   r]   r   g      @g      @)r?   r!   Śabs)r	   rC   r$   r   r   r   Śtest_distanceÜ   s    


zTestVec2D.test_distancec          
   C   sb   dddddg}xN|D ]F}| j |d0 |\\}}}t| }| |”}|  ||” W d Q R X qW d S )N)))r   r   r   )r   r   )))r   r>   éZ   )rW   r   )))r   r>   i¦’’’)r>   r   )))r>   r   é“   )rW   r   )))r>   r   ih  )r>   r   )rQ   )rM   r?   Śrotater;   )r	   ZcasesrQ   rC   Zrotr$   Zgotr   r   r   Śtest_rotateé   s    

zTestVec2D.test_rotateN)r,   r-   r.   rD   rF   rI   rP   rV   r\   r^   rc   rd   rh   rl   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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d#S )$ŚTestTNavigatorc             C   s   t  ” | _d S )N)r   Ś
TNavigatorŚnav)r	   r   r   r   ŚsetUpż   s    zTestTNavigator.setUpc             C   s6   | j  dd” |  | j  ” d” |  | j  ” d” d S )Nr   i’’’)ro   Śgotor4   ŚxcorŚycor)r	   r   r   r   Ś	test_goto   s    zTestTNavigator.test_gotoc             C   s>   |   | j ” | jj” | j dd” |   | j ” | jj” d S )Nr   i’’’)r!   ro   ŚposZ	_positionrq   )r	   r   r   r   Śtest_pos  s    zTestTNavigator.test_posc             C   s0   |   | jjd” | j d” |  | jjd” d S )N)g      š?r   ri   )g        g      š?)r!   ro   Ś_orientŚleftr;   )r	   r   r   r   Ś	test_left
  s    zTestTNavigator.test_leftc             C   s0   |   | jjd” | j d” |  | jjd” d S )N)g      š?r   ri   )r   g      šæ)r!   ro   rw   Śrightr;   )r	   r   r   r   Ś
test_right  s    zTestTNavigator.test_rightc             C   sd   | j  dd” |  | j  ” d” |  | j  ” d” | j  ”  |  | j  ” d” |  | j  ” d” d S )Nr   i’’’r   )ro   rq   r4   rr   rs   Śreset)r	   r   r   r   Ś
test_reset  s    
zTestTNavigator.test_resetc             C   s|   | j  d” tdd}|  | j  ” |” | j  ”  | j  d” | j  d” tdd}|  | j  ” |” |  t| j jd” d S )Né   r   ri   Z
skldjfldsk)	ro   Śforwardr?   r;   Śpositionr|   rx   rA   rB   )r	   r$   r   r   r   Śtest_forward  s    


zTestTNavigator.test_forwardc             C   sj   | j  d” tdd}|  | j  ” |” | j  ”  | j  d” | j  d” tdd}|  | j  ” |” d S )Nr   i8’’’r   ri   )ro   Zbackr?   r;   r   r|   rz   )r	   r$   r   r   r   Śtest_backwards)  s    


zTestTNavigator.test_backwardsc             C   s.   | j  d” d}|  | j  tdd”|” d S )Nr   r   )ro   r   r4   Śdistancer?   )r	   r$   r   r   r   rh   4  s    zTestTNavigator.test_distancec             C   sZ   | j  d” |  | j  ” d” | j  ”  |  | j  ” d” | j  ”  |  | j  ” d” d S )Nri   gßm Uū!ł?)ro   rx   r4   ŚheadingZradiansZdegrees)r	   r   r   r   Śtest_radians_and_degrees9  s    

z'TestTNavigator.test_radians_and_degreesc             C   sv   ddddddddg}x\|D ]T\\}}}|   | j ||”|” |   | j ||f”|” |   | j t||”|” qW d S )	N))r   r   g        ))r   r   g     F@))r   r   g     V@))i’’’r   g     ą`@))i’’’r   g     f@))i’’’i’’’g      l@))r   i’’’g     ąp@))r   i’’’g     °s@)r!   ro   Ztowardsr?   )r	   ZcoordinatesrX   rY   r$   r   r   r   Śtest_towardsA  s    zTestTNavigator.test_towardsc          
   C   s  | j  d” |  | j  ” d” | j  d” |  | j  ” d” | j  d” |  | j  ” d” |  t| j jd” | j  ”  ddd	d
g}t|d }x|D ]}| j  |” qW |  	| j  ” |” | j  ”  dt| d }x|D ]}| j  |” qŚW |  	| j  ” |” | j  ”  dddd
ddddddg
}d}xT|D ]L}|dk rL| j  t
|” n| j  |” ||7 }|  | j  ” |d ” q,W d S )Nri   é-   é   gł?gĶĢĢĢĢ¬`@Zsdkfjdsfr]   é   éŖ   i,  ih  iV’’’i.’’’gfffff&A@gIĄiö’’’g{®Gįś=Ąiō  r   )ro   rx   r4   r   rz   rA   rB   r|   Śsumr!   rg   )r	   Z	rotationsrU   ZnumZ
sum_so_farr   r   r   Śtest_headingT  s6    






zTestTNavigator.test_headingc             C   s|   | j  d” |  | j  ” d” | j  d” |  | j  ” d” | j  d” |  | j  ” d” | j  d” |  | j  ” d” d S )Ng®GįzY@gėQøĪ^Ągp=
×£m@gėQøBĄg×£p=źS@ią éx   )ro   Z
setheadingr4   r   )r	   r   r   r   Śtest_setheadingv  s    zTestTNavigator.test_setheadingc             C   s:   | j  d” | j  d” | j  d” |  | j  ” d” d S )Nr   ri   i8’’’)g      Y@g      iĄ)ro   r   rx   r;   ru   )r	   r   r   r   Śtest_positions  s    zTestTNavigator.test_positionsc             C   s.   | j  d” | j  d” |  | j  ” d” d S )Ng·Ń ŽłĄgZd;ßYA)g·Ń ŽłĄgZd;ßYA)ro   ZsetxZsetyr;   ru   )r	   r   r   r   Śtest_setx_and_sety  s    z!TestTNavigator.test_setx_and_setyc             C   sJ   | j  d” | j  d” | j  ”  |  | j  ” d” |  | j  ” d” d S )Nrb   i`yž’)r   r   r   )ro   rx   r   Śhomer;   ru   r4   r   )r	   r   r   r   Ś	test_home  s
    
zTestTNavigator.test_homec             C   sh   |   | j dd”d” tdd}|   | j |”d” t ” }| d” | d” |   | j |”d” d S )	Nrb   é(   é2   g)\Āõ(Ģ?gü©ńŅMbP?gš-Ó)Ģ?ri   i'  )r4   ro   r   r?   r   rn   rx   r   )r	   rC   Zanother_turtler   r   r   Śtest_distance_method  s    


z#TestTNavigator.test_distance_methodN)r,   r-   r.   rp   rt   rv   ry   r{   r}   r   r   rh   r   r   r   r   r   r   r   r   r   r   r   r   rm   ū   s"   "
rm   c               @   s   e Zd Zdd Zdd ZdS )ŚTestTPenc             C   sF   t  ” }|  | ” ” | ”  |  | ” ” | ”  |  | ” ” d S )N)r   ŚTPenŚ
assertTrueZisdownZpenupŚassertFalseZpendown)r	   Śtpenr   r   r   Śtest_pendown_and_penup  s    zTestTPen.test_pendown_and_penupc             C   sF   t  ” }|  | ” ” | ”  |  | ” ” | ”  |  | ” ” d S )N)r   r   r   Z	isvisibleZ
hideturtler   Z
showturtle)r	   r   r   r   r   Ś(test_showturtle_hideturtle_and_isvisibleØ  s    z1TestTPen.test_showturtle_hideturtle_and_isvisibleN)r,   r-   r.   r   r   r   r   r   r   r     s   
r   Ś__main__)rL   ZunittestZtestr   Śimport_moduler   r?   r   r&   r(   ZTestCaser   r/   r<   rm   r   r,   Śmainr   r   r   r   Ś<module>   s   
G
x "