B
    `y?                 @   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   ^/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/_pytest/assertion/util.pyformat_explanation   s    
r   c             C   sb   | pd d}|d g}xD|dd D ]4}|rF|d dkrF|| q&|d  d| 7  < q&W |S )	zReturn 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   sN  t | dd }dg}dg}x| dd D ]}|dr|d rJd}nd}|t| |d  d7  < |d |dd	t|d   | |dd   q,|d
r|  |  ||d   |dd 7  < q,|d dkst|d  d7  < |drt|n
t|d }|d	| |dd   q,W t|dksJt|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 +z  r   )r   r    r   )list
startswithr#   lenpopAssertionError)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    r3   c             C   s
   t | tS )N)r/   r2   )r.   r   r   r   istexte   s    r4   c             C   s
   t | tS )N)r/   dict)r.   r   r   r   isdicti   s    r6   c             C   s   t | ttfS )N)r/   set	frozenset)r.   r   r   r   issetm   s    r9   )objr   c             C   s   t | tot| dd d k	S )N_fields)r/   tuplegetattr)r:   r   r   r   isnamedtupleq   s    r>   c             C   s   t | dd d k	S )N__dataclass_fields__)r=   )r:   r   r   r   	isdataclsu   s    r@   c             C   s   t | dd d k	S )N__attrs_attrs__)r=   )r:   r   r   r   isattrsy   s    rB   c             C   s,   yt |  t|  S  tk
r&   dS X d S )NF)iterr4   	TypeError)r:   r   r   r   
isiterable}   s
    
rE   )opleftrightr   c       
      C   s   |  d}|dkr$t|}t|}n,dt| d d }t||d}t||d}| d| d| }d}	y>|dkrt|||}	n$|d	krt|rt|rt|||}	W nD tjk
r    Y n. t	k
r   d

tjj  dg}	Y nX |	sd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_anyr4   _notin_textr
   ZExit	Exceptionformat_pytest_codeZExceptionInfoZfrom_currentZ_getreprcrash)
configrF   rG   rH   rI   Z	left_reprZ
right_reprrL   summaryr   r   r   r   assertrepr_compare   s2    

rV   )rG   rH   rI   r   c             C   s   g }t | r"t |r"t| ||}nt| t|krXt| sJt| sJt| rXt| ||}nlt| rvt|rvt| ||}nNt	| rt	|rt
| ||}n0t| rt|rt| ||}n|dkrt| |}t| rt|rt| ||}|| |S )Nr   )r4   
_diff_texttyper@   rB   r>   _compare_eq_clsr3   _compare_eq_sequencer9   _compare_eq_setr6   _compare_eq_dict_compare_eq_verboserE   _compare_eq_iterableextend)rG   rH   rI   r   Zexplr   r   r   rN      s$    

rN   c             C   sZ  ddl m} g }|dk rd}x0ttt| t|D ]}| | || kr4P q4W |dkr|d8 }d| g}| |d } ||d }t| t|krx*tt| D ]}| |  ||  krP qW |dkr|d8 }|d|g7 }| d|  } |d|  }d	}|  s| r2tt| } tt|}|d
g7 }|dd ||	|| 	|D 7 }|S )zReturn 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>)
difflibr`   rangeminr'   rQ   isspacereprr2   
splitlines)rG   rH   rI   r`   r   ikeependsr   r   r   rW      s@    


rW   )rG   rH   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   )rd   r+   r   r   r   re      s    z'_compare_eq_verbose.<locals>.<listcomp>c             S   s   g | ]}d | qS )-r   )rd   r+   r   r   r   re      s    )rj   rk   )rG   rH   rm   Z
left_linesZright_linesr   r   r   r   r]      s    r]   c             C   s   | d dd }|dkrBd| d dd  | d< |g|  | dd< | d dd }|dkr| d dd d | d< | |g | dd< dS )	z6Move opening/closing parenthesis/bracket to own lines.r   Nr   )([r   rM   r!   ))]r   ,r   )r   Zopeningclosingr   r   r    _surrounding_parens_on_own_lines  s    rv   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 )N)rstrip)rd   r+   r   r   r   	<genexpr>*  s    z'_compare_eq_iterable.<locals>.<genexpr>)	rf   pprintpformatrk   r'   r   rv   r_   r`   )	rG   rH   rI   rf   Zleft_formattingZright_formattingZ
lines_leftZlines_rightr   r   r   r   r^     s"    r^   c             C   s$  t | tot |t}g }t| }t|}xztt||D ]h}| | || kr8|rr| ||d  }|||d  }	n| | }|| }	|d| d|d|	g7 }P q8W |r|S || }
|
r |
dkrd}t| | }nd|
 }
d}t|| }|
dkr|| d| g7 }n|d	||
|f g7 }|S )
Nr   z	At index z diff: z != r   ZLeftZRightz contains one more item: z/%s contains %d more items, first extra item: %s)r/   bytesr'   rg   rh   r   )rG   rH   rI   Zcomparing_bytesr   Zlen_leftZ	len_rightrl   Z
left_valueZright_valueZlen_diffZdir_with_moreextrar   r   r   rZ   /  s:    	
rZ   c             C   sl   g }| | }||  }|r>| d x|D ]}| t| q(W |rh| d x|D ]}| t| qRW |S )NzExtra items in the left set:zExtra items in the right set:)r#   r   )rG   rH   rI   r   Z	diff_leftZ
diff_rightitemr   r   r   r[   d  s    



r[   c                s~  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 }x6|D ].}	|t|	 |	 id	 t|	|	 i g7 }qW || }
t|
}|r"|d
||dkrdndf  |t fdd|
D   || }t|}|rz|d||dkrNdndf  |tfdd|D   |S )Nc                s&   i | ]} | | kr | |qS r   r   )rd   k)rG   rH   r   r   
<dictcomp>|  s    z$_compare_eq_dict.<locals>.<dictcomp>rK   z,Omitting %s identical items, use -vv to showzCommon items:c                s    h | ]} | | kr|qS r   r   )rd   r~   )rG   rH   r   r   	<setcomp>  s    z#_compare_eq_dict.<locals>.<setcomp>zDiffering items:z != zLeft contains %d more item%s:r   r   r,   c                s   i | ]} | |qS r   r   )rd   r~   )rG   r   r   r     s    zRight contains %d more item%s:c                s   i | ]} | |qS r   r   )rd   r~   )rH   r   r   r     s    )	r7   intersectionr'   ry   rz   rk   r   r#   r_   )rG   rH   rI   r   Zset_leftZ	set_rightcommonsamediffr~   Z
extra_leftZlen_extra_leftZextra_rightZlen_extra_rightr   )rG   rH   r   r\   u  s@    



.r\   c          	      sx  t | r"| j}dd | D }n6t| r@| j}dd |D }nt| rP| j}ndsXtd g }g }x6|D ].}t| |t||kr|	| qj|	| qjW g }|s|r|dg7 }|r|dk r|	dt
|  n |r|d	g7 }|t| 7 }|rt|d
g7 }|t| 7 }x^|D ]V}t| |}	t||}
|dd| d ||	|
f g7 }| fddt|	|
|D 7 }qW |S )Nc             S   s   g | ]\}}|j r|qS r   )compare)rd   fieldinfor   r   r   re     s    z#_compare_eq_cls.<locals>.<listcomp>c             S   s   g | ]}t |d r|jqS )eq)r=   name)rd   r   r   r   r   re     s    Fz  r   rK   z,Omitting %s identical items, use -vv to showzMatching attributes:zDiffering attributes:z'Drill down into differing attribute %s:z%s%s: %r != %rc                s   g | ]} | qS r   r   )rd   r+   )r-   r   r   re     s   )r@   r?   itemsrB   rA   r>   r;   r)   r=   r#   r'   ry   rz   rk   rN   )rG   rH   rI   Z
all_fieldsZfields_to_checkr   r   r   r   Z
field_leftZfield_rightr   )r-   r   rY     sL    







rY   )termtextrI   r   c       
      C   s   | | }|d | }||t|  d  }|| }t|||}dt| dd g}xP|D ]H}	|	drfqV|	drrqV|	dr|d|	dd    qV||	 qVW |S )	Nz%s is contained here:ra   )rL   ZSkippingz- z+ z  rK   )findr'   rW   r   r&   r#   )
r   r   rI   indexheadtailZcorrect_textr   Znewdiffr+   r   r   r   rO     s    




rO   )r   )r   )r   )r   )r   )r   )r   )1__doc__collections.abcr0   ry   typingr   r   r   r   r   r   r   r	   Z_pytest._coderR   r
   Z_pytest._io.safereprr   r   r   r   r2   object__annotations__r   intr   r   r   boolr3   r4   r6   r9   r>   r@   rB   rE   rV   rN   rW   r]   rv   r^   rZ   r[   r\   rY   rO   r   r   r   r   <module>   sV   "#(04'/