a
    /§»ay?  ã                   @   sê  U d Z ddlZddlZddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
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 ddlmZ ddlmZ dZeeeeegee f  ed< dZeeeeegdf  ed< eedœdd„Zee	e dœdd„Zee e	e dœdd„Zeedœdd„Zeedœdd„Zeedœdd„Z eedœd d!„Z!eed"œd#d$„Z"eed"œd%d&„Z#eed"œd'd(„Z$eed"œd)d*„Z%eeeee	e  d+œd,d-„Z&dEeeee	e d.œd/d0„Z'dFeeee	e d.œd1d2„Z(eee	e d3œd4d5„Z)e	e ddœd6d7„Z*dGee ee ee	e d.œd8d9„Z+dHee ee ee	e d.œd:d;„Z,dIee ee ee	e d.œd<d=„Z-dJe
eef e
eef ee	e d.œd>d?„Z.eeee	e d.œd@dA„Z/dKeeee	e dBœdCdD„Z0dS )Lz"Utilities for assertion debugging.é    N)ÚAbstractSet)ÚAny)ÚCallable)ÚIterable)ÚList)ÚMapping)ÚOptional)ÚSequence)Úoutcomes)Ú_pformat_dispatch)Ú
safeformat)ÚsafereprÚ_reprcompareÚ_assertion_pass)ÚexplanationÚreturnc                 C   s   t | ƒ}t|ƒ}d |¡S )aŠ  Format an explanation.

    Normally all embedded newlines are escaped, however there are
    three exceptions: \n{, \n} and \n~.  The first two are intended
    cover nested explanations, see function and attribute explanations
    for examples (.visit_Call(), visit_Attribute()).  The last one is
    for when one explanation needs to span multiple lines, e.g. when
    displaying diffs.
    Ú
)Ú_split_explanationÚ_format_linesÚjoin)r   ÚlinesÚresult© r   úP/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/_pytest/assertion/util.pyÚformat_explanation   s    
r   c                 C   s^   | pd  d¡}|d g}|dd… D ]4}|rD|d dv rD| |¡ q$|d  d| 7  < q$|S )	zãReturn a list of individual lines in the explanation.

    This will return a list of lines split on '\n{', '\n}' and '\n~'.
    Any other newlines will be escaped and appear in the line as the
    literal '\n' characters.
    Ú r   r   é   N)Ú{Ú}ú~ú>éÿÿÿÿz\n)ÚsplitÚappend)r   Z	raw_linesr   Úvaluesr   r   r   r   -   s    
r   )r   r   c                 C   sH  t | dd… ƒ}dg}dg}| dd… D �]}| d¡rž|d rFd}nd}| t|ƒ¡ |d  d7  < | d¡ | dd	t|ƒd   | |dd…  ¡ q(| d
¡rÖ| ¡  | ¡  ||d   |dd… 7  < q(|d dv sæJ ‚|d  d7  < | d¡�r
t|ƒn
t|ƒd }| d	| |dd…  ¡ q(t|ƒdk�sDJ ‚|S )a  Format the individual lines.

    This will replace the '{', '}' and '~' characters of our mini formatting
    language with the proper 'where ...', 'and ...' and ' + ...' text, taking
    care of indentation along the way.

    Return a list of formatted lines.
    Nr   r   r   r!   zand   zwhere z +ú  r   )r   r    r   )ÚlistÚ
startswithr#   ÚlenÚpop)r   r   ÚstackZstackcntÚlineÚsÚindentr   r   r   r   >   s,    	

,
 r   )Úxr   c                 C   s   t | tjjƒot | tƒ S ©N)Ú
isinstanceÚcollectionsÚabcr	   Ústr©r.   r   r   r   Ú
issequencea   s    r5   c                 C   s
   t | tƒS r/   )r0   r3   r4   r   r   r   Úistexte   s    r6   c                 C   s
   t | tƒS r/   )r0   Údictr4   r   r   r   Úisdicti   s    r8   c                 C   s   t | ttfƒS r/   )r0   ÚsetÚ	frozensetr4   r   r   r   Úissetm   s    r;   )Úobjr   c                 C   s   t | tƒot| dd ƒd uS )NÚ_fields)r0   ÚtupleÚgetattr©r<   r   r   r   Úisnamedtupleq   s    rA   c                 C   s   t | dd ƒd uS )NÚ__dataclass_fields__©r?   r@   r   r   r   Ú	isdataclsu   s    rD   c                 C   s   t | dd ƒd uS )NÚ__attrs_attrs__rC   r@   r   r   r   Úisattrsy   s    rF   c                 C   s.   zt | ƒ t| ƒ W S  ty(   Y dS 0 d S )NF)Úiterr6   Ú	TypeErrorr@   r   r   r   Ú
isiterable}   s
    rI   )ÚopÚleftÚrightr   c           
      C   sú   |   d¡}|dkr$t|ƒ}t|ƒ}n,dt|ƒ d d }t||d�}t||d�}|› d|› d|› �}d}	z>|dkr€t|||ƒ}	n$|d	kr¤t|ƒr¤t|ƒr¤t|||ƒ}	W n@ tjy¼   ‚ Y n, t	yæ   d
 
tjj ¡  ¡ ¡dg}	Y n0 |	sðdS |g|	 S )z<Return specialised explanations for some operators/operands.Úverboser   éA   é   ©Úmaxsizeú Nz==znot inz?(pytest_assertion plugin: representation of details failed: {}.z+ Probably an object has a faulty __repr__.))Z	getoptionr   r(   r   Ú_compare_eq_anyr6   Ú_notin_textr
   ZExitÚ	ExceptionÚformatÚ_pytestÚ_codeZExceptionInfoZfrom_currentZ_getreprcrash)
ÚconfigrJ   rK   rL   rM   Z	left_reprZ
right_reprrQ   Úsummaryr   r   r   r   Úassertrepr_compare…   s8    

þÿü
r[   )rK   rL   rM   r   c                 C   sî   g }t | ƒr"t |ƒr"t| ||ƒ}nÈt| ƒt|ƒkrXt| ƒsJt| ƒsJt| ƒrXt| ||ƒ}nlt| ƒrvt|ƒrvt| ||ƒ}nNt	| ƒr”t	|ƒr”t
| ||ƒ}n0t| ƒr²t|ƒr²t| ||ƒ}n|dkrÄt| |ƒ}t| ƒrêt|ƒrêt| ||ƒ}| |¡ |S )Nr   )r6   Ú
_diff_textÚtyperD   rF   rA   Ú_compare_eq_clsr5   Ú_compare_eq_sequencer;   Ú_compare_eq_setr8   Ú_compare_eq_dictÚ_compare_eq_verboserI   Ú_compare_eq_iterableÚextend)rK   rL   rM   r   Zexplr   r   r   rS   ­   s.    ÿÿÿ

rS   c                 C   sV  ddl m} g }|dk rôd}ttt| ƒt|ƒƒƒD ]}| | || kr2 qLq2|dkr~|d8 }d| g}| |d… } ||d… }t| ƒt|ƒkrôtt| ƒƒD ]}| |  ||  krš q¸qš|dkrô|d8 }|d |¡g7 }| d| … } |d| … }d	}|  ¡ �s| ¡ �r.tt| ƒƒ} tt|ƒƒ}|d
g7 }|dd„ || 	|¡|  	|¡ƒD ƒ7 }|S )zµReturn the explanation for the diff between text.

    Unless --verbose is used this will skip leading and trailing
    characters which are identical to keep the diff minimal.
    r   )Úndiffr   é*   é
   z@Skipping %s identical leading characters in diff, use -v to showNzASkipping {} identical trailing characters in diff, use -v to showTz;Strings contain only whitespace, escaping them using repr()c                 S   s   g | ]}|  d ¡‘qS )r   )Ústrip©Ú.0r+   r   r   r   Ú
<listcomp>ñ   s   ÿz_diff_text.<locals>.<listcomp>)
Údifflibre   ÚrangeÚminr(   rV   ÚisspaceÚreprr3   Ú
splitlines)rK   rL   rM   re   r   ÚiÚkeependsr   r   r   r\   È   sF    ÿÿÿ
þr\   )rK   rL   r   c                 C   sL   d}t | ƒ |¡}t |ƒ |¡}g }|dd„ |D ƒ7 }|dd„ |D ƒ7 }|S )NTc                 S   s   g | ]}d | ‘qS )ú+r   ri   r   r   r   rk   þ   ó    z'_compare_eq_verbose.<locals>.<listcomp>c                 S   s   g | ]}d | ‘qS )ú-r   ri   r   r   r   rk   ÿ   ru   )rp   rq   )rK   rL   rs   Z
left_linesZright_linesr   r   r   r   rb   ø   s    rb   c                 C   sˆ   | d dd… }|dv rBd| d dd…  | d< |g|  | dd…< | d dd… }|dv r„| d dd… d | d< | |g | dd…< dS )	z6Move opening/closing parenthesis/bracket to own lines.r   Nr   )ú(ú[r   rR   r!   )ú)ú]r   ú,r   )r   ZopeningÚclosingr   r   r   Ú _surrounding_parens_on_own_lines  s    r}   c           	      C   s¤   |s
dgS dd l }t | ¡ ¡ }t |¡ ¡ }t|ƒ}t|ƒ}||kr^t| ƒ ¡ }t|ƒ ¡ }|dksn|dkr~t|ƒ t|ƒ dg}| dd„ | ||¡D ƒ¡ |S )NzUse -v to get the full diffr   r   z
Full diff:c                 s   s   | ]}|  ¡ V  qd S r/   )Úrstripri   r   r   r   Ú	<genexpr>)  s   z'_compare_eq_iterable.<locals>.<genexpr>)	rl   ÚpprintÚpformatrq   r(   r   r}   rd   re   )	rK   rL   rM   rl   Zleft_formattingZright_formattingZ
lines_leftZlines_rightr   r   r   r   rc     s$    

ÿrc   c                 C   s"  t | tƒot |tƒ}g }t| ƒ}t|ƒ}tt||ƒƒD ]j}| | || kr6|rp| ||d … }|||d … }	n| | }|| }	|d|› d|›d|	›�g7 } q¢q6|rª|S || }
|
�r|
dkrÒd}t| | ƒ}nd|
 }
d}t|| ƒ}|
dk�r
||› d|› �g7 }n|d	||
|f g7 }|S )
Nr   z	At index z diff: ú != r   ZLeftZRightz contains one more item: z/%s contains %d more items, first extra item: %s)r0   Úbytesr(   rm   rn   r   )rK   rL   rM   Zcomparing_bytesr   Zlen_leftZ	len_rightrr   Z
left_valueZright_valueZlen_diffZdir_with_moreÚextrar   r   r   r_   /  s>    	
ÿÿr_   c                 C   sd   g }| | }||  }|r:|  d¡ |D ]}|  t|ƒ¡ q&|r`|  d¡ |D ]}|  t|ƒ¡ qL|S )NzExtra items in the left set:zExtra items in the right set:)r#   r   )rK   rL   rM   r   Z	diff_leftZ
diff_rightÚitemr   r   r   r`   d  s    

r`   c                    sz  g }t ˆ ƒ}t ˆƒ}| |¡}‡ ‡fdd„|D ƒ}|rR|dk rR|dt|ƒ g7 }n |rr|dg7 }|t |¡ ¡ 7 }‡ ‡fdd„|D ƒ}|rÈ|dg7 }|D ].}	|t|	ˆ |	 iƒd	 t|	ˆ|	 iƒ g7 }q˜|| }
t|
ƒ}|�r| d
||dkròdndf ¡ | t ‡ fdd„|
D ƒ¡ ¡ ¡ || }t|ƒ}|�rv| d||dk�rJdndf ¡ | t ‡fdd„|D ƒ¡ ¡ ¡ |S )Nc                    s&   i | ]}ˆ | ˆ| kr|ˆ | “qS r   r   ©rj   Úk©rK   rL   r   r   Ú
<dictcomp>|  ru   z$_compare_eq_dict.<locals>.<dictcomp>rO   ú,Omitting %s identical items, use -vv to showzCommon items:c                    s    h | ]}ˆ | ˆ| kr|’qS r   r   r†   rˆ   r   r   Ú	<setcomp>‚  ru   z#_compare_eq_dict.<locals>.<setcomp>zDiffering items:r‚   zLeft contains %d more item%s:r   r   r,   c                    s   i | ]}|ˆ | “qS r   r   r†   )rK   r   r   r‰   �  ru   zRight contains %d more item%s:c                    s   i | ]}|ˆ | “qS r   r   r†   )rL   r   r   r‰   ™  ru   )	r9   Úintersectionr(   r€   r�   rq   r   r#   rd   )rK   rL   rM   r   Zset_leftZ	set_rightÚcommonÚsameÚdiffr‡   Z
extra_leftZlen_extra_leftZextra_rightZlen_extra_rightr   rˆ   r   ra   u  sL    


,ÿÿÿÿÿÿra   c              	      sp  t | ƒr"| j}dd„ | ¡ D ƒ}n6t| ƒr@| j}dd„ |D ƒ}nt| ƒrP| j}ndsXJ ‚d‰ g }g }|D ].}t| |ƒt||ƒkrŒ| |¡ qh| |¡ qhg }|s¤|r®|dg7 }|rÎ|dk rÎ| dt	|ƒ ¡ n |rî|d	g7 }|t
 |¡ ¡ 7 }|�rl|d
g7 }|t
 |¡ ¡ 7 }|D ]V}t| |ƒ}	t||ƒ}
|dd| dˆ ||	|
f g7 }|‡ fdd„t|	|
|ƒD ƒ7 }�q|S )Nc                 S   s   g | ]\}}|j r|‘qS r   )Úcompare)rj   ÚfieldÚinfor   r   r   rk   ¡  ru   z#_compare_eq_cls.<locals>.<listcomp>c                 S   s   g | ]}t |d ƒr|j‘qS )Úeq)r?   Úname)rj   r‘   r   r   r   rk   ¤  ru   Fr%   r   rO   rŠ   zMatching attributes:zDiffering attributes:z'Drill down into differing attribute %s:z%s%s: %r != %rc                    s   g | ]}ˆ | ‘qS r   r   ri   ©r-   r   r   rk   Æ  s   ÿ)rD   rB   ÚitemsrF   rE   rA   r=   r?   r#   r(   r€   r�   rq   rS   )rK   rL   rM   Z
all_fieldsZfields_to_checkrŽ   r�   r‘   r   Z
field_leftZfield_rightr   r•   r   r^   ž  sN    




ý
þr^   )ÚtermÚtextrM   r   c           
      C   s¢   |  | ¡}|d |… }||t| ƒ d … }|| }t|||ƒ}dt| dd� g}|D ]H}	|	 d¡rdqT|	 d¡rpqT|	 d¡r’| d|	dd …  ¡ qT| |	¡ qT|S )	Nz%s is contained here:rf   rP   ZSkippingz- z+ r%   rO   )Úfindr(   r\   r   r'   r#   )
r—   r˜   rM   ÚindexÚheadÚtailZcorrect_textr�   Znewdiffr+   r   r   r   rT   Í  s    



rT   )r   )r   )r   )r   )r   )r   )r   )1Ú__doc__Úcollections.abcr1   r€   Útypingr   r   r   r   r   r   r   r	   Z_pytest._coderW   r
   Z_pytest._io.safereprr   r   r   r   r3   ÚobjectÚ__annotations__r   Úintr   r   r   Úboolr5   r6   r8   r;   rA   rD   rF   rI   r[   rS   r\   rb   r}   rc   r_   r`   ra   r^   rT   r   r   r   r   Ú<module>   sn   "#(0 ÿþ  ÿþ6 ÿþ ÿþ)/