a
    zeK                     @   s`   d Z ddlmZ ddlZddlmZ G dd dejZG dd dejZe	d	kr\ej
d
d dS )zTest pyparse, coverage 96%.    )pyparseN)
namedtuplec                   @   s   e Zd Zdd Zdd ZdS )ParseMapTestc                 C   s`   dd dD }t |}| |td td | |td td | |d td d S )Nc                 S   s   i | ]}t |t |qS  )ord).0cr   r   4/usr/lib/python3.9/idlelib/idle_test/test_pyparse.py
<dictcomp>       z.ParseMapTest.test_parsemap.<locals>.<dictcomp>z 	
	axi  )r   ZParseMapassertEqualr   )selfZ	keepwhitemappingr   r   r	   test_parsemap
   s
    
zParseMapTest.test_parsemapc                 C   s$   t dd}| dt jd d S )N   z	 a([{b}])b"c'd
zxxx(((x)))x"x'x
)r   Parserr   	translateZtrans)r   parserr   r   r	   
test_trans   s    zParseMapTest.test_transN)__name__
__module____qualname__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dd Zdd Zdd Zdd  Zd!S )"PyParseTestc                 C   s   t jddd| _d S )Nr   )indentwidthtabwidth)r   r   r   clsr   r   r	   
setUpClass   s    zPyParseTest.setUpClassc                 C   s   | ` d S N)r   r   r   r   r	   tearDownClass   s    zPyParseTest.tearDownClassc                 C   s$   |  | jjd |  | jjd d S )Nr   )r   r   r   r   )r   r   r   r	   	test_init"   s    zPyParseTest.test_initc              	   C   s   | j }| j}|j}| t |d W d    n1 s:0    Y  d}|D ]P}| j|d0 || ||j| ||jd W d    qL1 s0    Y  qLd S )Nr   ) a
stringr   )r   r   set_codeassertRaisesAssertionErrorsubTestcodestudy_level)r   eqpsetcodetestsr'   r   r   r	   test_set_code&   s    &zPyParseTest.test_set_codec                    s  | j }| j}|j}|j}dd }|d |||d |d |||d |d d\} | t |  W d    n1 s0    Y  | t |d W d    n1 s0    Y  | |d	d
 d |||  || fdd
d  || fdd
d| || fdd
dd  |d |||  || fdd
d  || fdd
d| || fdd
d  d S )Nc                 S   s   dS )NFr   indexr   r   r	   char_in_string_false=   r   zDPyParseTest.test_find_good_parse_start.<locals>.char_in_string_falsezdef spam():
r   zclass spam(  
k"""This is a module docstring"""
class C:
    def __init__(self, a,
                 b=True):
        pass
)!   *   Fc                 S   s   dS )NTr   r3   r   r   r	   <lambda>[   r   z8PyParseTest.test_find_good_parse_start.<locals>.<lambda>)Zis_char_in_stringc                    s   |  kS r!   r   r3   posr   r	   r9   c   r   c                    s   |  kS r!   r   r3   r:   r   r	   r9   f   r   c                    s   |  k S r!   r   r3   r:   r   r	   r9   i   r   zZ"""This is a module docstring"""
class C:
    def __init__(self, a, b=True):
        pass
c                    s   |  kS r!   r   r3   r:   r   r	   r9   s   r   c                    s   |  kS r!   r   r3   r:   r   r	   r9   t   r   c                    s   |  k S r!   r   r3   r:   r   r	   r9   w   r   )r   r   r(   Zfind_good_parse_startr)   	TypeErrorassertIsNone)r   r.   r/   r0   startr5   Zpos0r   r:   r	   test_find_good_parse_start8   s2    $&z&PyParseTest.test_find_good_parse_startc                 C   s   d}d}| j }|| | t |d W d    n1 sB0    Y  |d | |j| || | |j||d   d S )Nr6   r8      r   )r   r(   r)   r*   Zset_lor   r,   )r   r,   r;   r/   r   r   r	   test_set_loy   s    
(

zPyParseTest.test_set_loc                 C   s  | j }| j}|j}|j}td\}}}}}	tdg d}
|
ddg||
dddg||
dddg||
d	ddg||
d
ddg||
dddg||
dddg||
dddg||
dddg||
dddg||
dg d||
dddg||
dddg|	|
dddg||
dddg||
dg d||
dg d|	|
dg d|	|
dddg||
dddg|	|
dddg|f}|D ]n}| j|jdH ||j |  ||jd ||j	|j	 ||j
|j
 W d    n1 s0    Y  qd| |  d S )Nr@   TestInfo)r'   	goodlinescontinuationr$   r   z$"""This is a complete docstring."""
   z$'''This is a complete docstring.'''
z""""This is a continued docstring.
z"'''This is a continued docstring.
z""""Closing quote does not match."
z"""Bracket in docstring [
z#'''Incomplete two line docstring.

   z"This is a complete string."
z"This is an incomplete string.
z'This is more incomplete.

)r   rE   rF   z# Comment\
z"("""Complete string in bracket"""
z("""Open string in bracket
a = (1 + 2) - 5 *\
0
   def function1(self, a,
                 b):
)r   rE      
   def function1(self, a,\

   def function1(self, a,
())
)(
{)(]
r&   )r   r   r(   Z_study1ranger   r+   r'   r-   rC   rD   r=   )r   r.   r/   r0   studyNONE	BACKSLASHFIRSTNEXTBRACKETrB   r1   testr   r   r	   test_study1   sL    
2zPyParseTest.test_study1c              	   C   s   | j }| j}|j}|j}td\}}}}}	tdddg}
|
d||
d||
d||
d||
d	|	f}|D ]J}| j|jd
( ||j || |j W d    qh1 s0    Y  qhd S )Nr@   rB   r'   rD   r$   z%"""This is a continuation docstring.
z-'''This is a multiline-continued docstring.

rG   rJ   r&   )	r   r   r(   Zget_continuation_typerO   r   r+   r'   rD   )r   r.   r/   r0   ZgettyperQ   rR   rS   rT   rU   rB   r1   rV   r   r   r	   test_get_continuation_type   s     
z&PyParseTest.test_get_continuation_typec                 C   s  | j }| j}|j}|j}tdg d}|ddddd d|ddddd d	|d
dddd d|ddddd d|ddddd d|ddddd d|dddddd|dddddd|dddddd|d dd!dd"d#|d$dd%d&d d'|d(dd)d*dd+|d,dd-d.d d/|d0dd%d1d d|d2dddd df}|D ]}| j|jd3r ||j |  ||jd4 ||j|j	 ||j
|j ||j|j ||j|j ||j|j W d    n1 s0    Y  q| |  d S )5NrB   )r'   r>   endlastchopenbracket
bracketingr$   r   r   r   z0'''This is a multiline continuation docstring.

0   ')r^   r   rE   )r_   r   z # Comment\
   )r^   rE   rE   )rb   r   z #Comment\
rG      *)r^   r   rE   )   r   rH   rE   :)rE   r      rE   ).   r   rJ      ,rk   )ri   rj   rK      z3
   def function1(self, a,  # End of line comment.
3   )ri   rj   )rm   rF   )rp   rE   z7  a = ["first item",
  # Comment line
    "next item",
7      )r^   )rr   rE   )   rF   )rd   rE   )   rF   )&   rE   )r8   rF   )5   rE   rL   r   ))r^   ra   rF   r   rI   r   rM   rI   ()r^   ri   rc   rN   r@   ]r^   ra   rx   rF   rE   r   r   z:\a
z\a
r&   rF   )r   r   r(   Z_study2r   r+   r'   r-   Z
stmt_startr>   Zstmt_endrY   rZ   Zlastopenbracketposr[   Zstmt_bracketingr\   r=   )r   r.   r/   r0   rP   rB   r1   rV   r   r   r	   test_study2   sl    










'
2zPyParseTest.test_study2c              
   C   s   | j }| j}|j}|j}tdddg}|dd|dd|dd|d	d
|dd|dd|dd|ddf}|d | t |  W d    n1 s0    Y  |D ]J}| j|jd( ||j || |j	 W d    q1 s0    Y  qd S )NrB   r'   linesz[x for x in a]
rE   z[x
for x in a
rF   z[x\
for x in a\
z[x
for x in a
]
rI   z%
"""Docstring comment L1"""
L2
L3
L4
z%
"""Docstring comment L1
L2"""
L3
L4
z&
"""Docstring comment L1\
L2\
L3\
L4\
r   z+

"""Docstring comment L1\
L2\
L3\
L4\
"""
r@   r$   r&   )
r   r   r(   Zget_num_lines_in_stmtr   r)   
IndexErrorr+   r'   r   )r   r.   r/   r0   getlinesrB   r1   rV   r   r   r	   test_get_num_lines_in_stmt  s*    $
z&PyParseTest.test_get_num_lines_in_stmtc                 C   s   | j }| j}|j}|j}tdddg}|dd|dd|dd|d	d
|dd
|dd
|dd|dd|dd|dd|ddf}|d | t |  W d    n1 s0    Y  |D ]}||j || |j qd S )NrB   r'   spaceszdef function1(self, a,
   z
    def function1(self, a,
   z
	def function1(self, a,
z
    def function1(
   z
	def function1(
z
    def function1(  
z2[
"first item",
  # Comment line
    "next item",
r   z4[
  "first item",
  # Comment line
    "next item",
rF   z1["first item",
  # Comment line
    "next item",
rE   z(
r   z(a
zdef function1(self, a, b):
)	r   r   r(   Zcompute_bracket_indentr   r)   r*   r'   r   )r   r.   r/   r0   indentrB   r1   rV   r   r   r	   test_compute_bracket_indent1  s.    $
z'PyParseTest.test_compute_bracket_indentc           
      C   s`  | j }| j}|j}|j}d}|D ]h}| j|dH || | t |  W d    n1 s`0    Y  W d    q 1 s~0    Y  q tdd}|dd|dd|dd	|d
d|dd|dd|dd|dd|dd|dd|dd|dd|dd|ddf}|D ]N}	| j|	jd( ||	j || |	j	 W d    n1 sN0    Y  qd S )N)zdef function1(self, a, b\
z    """ (\
za = #\
r&   rB   )r'   r   rG   r   za = 1 + 2 - 5 *\
z    a = 1 + 2 - 5 *\
r   z  a = "spam"\
rr   z  a = \
"a"\
z  a = #\
"a"\
r@   za == \
rF   za != \
z\
z    \
z	\
za\
rI   z{}\
z(1 + 2) - 5 *\
)
r   r   r(   Zcompute_backslash_indentr+   r)   r*   r   r'   r   )
r   r.   r/   r0   r   errorsr'   rB   r1   rV   r   r   r	   test_compute_backslash_indentQ  s<    D

z)PyParseTest.test_compute_backslash_indentc              	   C   s   | j }| j}|j}|j}tdddg}|dd|dd|dd|dd	|d
d	|dd	|dd	f}|D ]J}| j|jd( ||j || |j W d    qf1 s0    Y  qfd S )NrB   r'   r   r$   	def a():
z
	def a():
r   z    def a():
z    z    def a(
z	
    def a(
z	
    # Comment.
r&   )r   r   r(   Zget_base_indent_stringr   r+   r'   r   )r   r.   r/   r0   Z
baseindentrB   r1   rV   r   r   r	   test_get_base_indent_stringx  s"    	
z'PyParseTest.test_get_base_indent_stringc           	      C   s   | j }| j}| j}|j}|j}tdddg}|d||d||d||d||d||d	||d
||d||d||d||d||d|f}|D ]H}| j|jd& ||j ||  W d    q1 s0    Y  qd S )NrB   r'   assert_r   rH   z:
za:
z):
z(:
z":
rK   !def function1(self, a):
    pass
z# A comment:
z"""A docstring:
r&   )	
assertTrueassertFalser   r(   Zis_block_openerr   r+   r'   r   )	r   yesnor/   r0   openerrB   r1   rV   r   r   r	   test_is_block_opener  s.    
z PyParseTest.test_is_block_openerc           	      C   s   | j }| j}| j}|j}|j}tdddg}|d||d||d||d||d||d	||d
||d||d||d||d||d||d|f}|D ]H}| j|jd& ||j ||  W d    q1 s0    Y  qd S )NrB   r'   r   zreturn
z	break
z  continue
z     raise
z	pass    
zpass	
z	return #
zraised
z
returning
z	# return
z	"""break
z
"continue
r   r&   )	r   r   r   r(   Zis_block_closerr   r+   r'   r   )	r   r   r   r/   r0   ZcloserrB   r1   rV   r   r   r	   test_is_block_closer  s0    
z PyParseTest.test_is_block_closerc                 C   s   | j }| j}|j}|j}tdddg}|dd|dd|dd|d	d
|dd|dd|dd|dd|dd|ddf
}|D ]J}| j|jd( ||j || |j W d    q~1 s0    Y  q~d S )NrB   r'   bracketr$   r]   r%   z()()
r|   z(
)()
)r^   ra   ry   rI   rE   r@   r   z()
()
)ry   r   r   z()(
)
)r^   ra   rx   r}   r   z(())
)r^   ra   rE   rF   r   r~   z(
())
)r^   ra   )rF   rF   rf   r   rN   z(((())
)r^   ra   r   )rF   rI   )rI   r   )r@   rI   )rr   rF   r&   )r   r   r(   Zget_last_stmt_bracketingr   r+   r'   r   )r   r.   r/   r0   r\   rB   r1   rV   r   r   r	   test_get_last_stmt_bracketing  s,    
z)PyParseTest.test_get_last_stmt_bracketingN)r   r   r   classmethodr    r"   r#   r2   r?   rA   rW   rX   r   r   r   r   r   r   r   r   r   r   r   r	   r      s$   

A2= 'r   __main__rF   )	verbosity)__doc__Zidlelibr   Zunittestcollectionsr   ZTestCaser   r   r   mainr   r   r   r	   <module>   s      M