a
    !fYF                     @   s&  U d Z ddl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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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< dZee 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#d%d.d/Z,dKeeee#ee
e  d1d2d3Z-dLeeee
e d4d5d6Z.dMeeee
e d4d7d8Z/e
e ddd9d:Z0dNe	e e	e ee
e d4d;d<Z1dOee ee ee
e d4d=d>Z2dPee ee ee
e d4d?d@Z3dQeeef eeef ee
e d4dAdBZ4eeee
e d4dCdDZ5dReeee
e dEdFdGZ6e#dHdIdJZ7dS )Sz"Utilities for assertion debugging.    N)AbstractSet)Any)Callable)Iterable)List)Mapping)Optional)Sequence)	normalize)outcomes)_pformat_dispatch)saferepr)saferepr_unlimited)Config_reprcompare_assertion_pass_config)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   W/var/www/html/python-backend/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 )	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   3   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 sJ |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   D   s,    	

,
 r   )xr   c                 C   s   t | tjjot | t S N)
isinstancecollectionsabcr	   strr1   r   r   r   
issequenceg   s    r8   c                 C   s
   t | tS r2   )r3   r6   r7   r   r   r   istextk   s    r9   c                 C   s
   t | tS r2   )r3   dictr7   r   r   r   isdicto   s    r;   c                 C   s   t | ttfS r2   )r3   set	frozensetr7   r   r   r   issets   s    r>   )objr   c                 C   s   t | tot| dd d uS )N_fields)r3   tuplegetattrr?   r   r   r   isnamedtuplew   s    rD   c                 C   s   t | dd d uS )N__dataclass_fields__rB   rC   r   r   r   	isdatacls{   s    rG   c                 C   s   t | dd d uS )N__attrs_attrs__rF   rC   r   r   r   isattrs   s    rI   c                 C   s.   zt |  t|  W S  ty(   Y dS 0 d S )NF)iterr9   	ExceptionrC   r   r   r   
isiterable   s
    rL   c                 C   s@   t | jdr<t | jjdr<| jjj}t| r4d|v S |dkS dS )a  Check if an instance of an object contains the default eq

    First, we check if the object's __eq__ attribute has __code__,
    if so, we check the equally of the method code filename (__code__.co_filename)
    to the default one generated by the dataclass and attr module
    for dataclasses the default co_filename is <string>, for attrs class, the __eq__ should contain "attrs eq generated"
    __code__co_filenamezattrs generated eqz<string>T)hasattr__eq__rM   rN   rI   )r?   Zcode_filenamer   r   r   has_default_eq   s    
rQ   F)opleftright	use_asciir   c                 C   s2  |  d}t|to0t|to0td|td|k}|dkrTt||d}t||d}n0dt| d d }t|||d}t|||d}| d| d| }	d	}
z>|d
krt|||}
n$|dkrt|rt|rt	|||}
W nB 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.verboseZNFDr   )rU   A      )maxsizerU    Nz==znot inz?(pytest_assertion plugin: representation of details failed: {}.z+ Probably an object has a faulty __repr__.))Z	getoptionr3   r6   r
   r   r+   r   _compare_eq_anyr9   _notin_textr   ZExitrK   format_pytest_codeZExceptionInfoZfrom_currentZ_getreprcrash)configrR   rS   rT   rU   rV   Z	left_reprZ
right_reprrY   summaryr   r   r   r   assertrepr_compare   sB    


rb   )rS   rT   rV   r   c                 C   s4  g }t | r$t |r$t| ||}nddlm} t| |sDt||rtt| |rR| n|}t| |rd|n| }||}nt| t|krt| st| st	| rt
| ||}n\t| rt|rt| ||}n>t| rt|rt| ||}n t| rt|rt| ||}t| r0t|r0t| ||}|| |S )Nr   )
ApproxBase)r9   
_diff_textZ_pytest.python_apirc   r3   Z_repr_comparetyperG   rI   rD   _compare_eq_clsr8   _compare_eq_sequencer>   _compare_eq_setr;   _compare_eq_dictrL   _compare_eq_iterableextend)rS   rT   rV   r   rc   Zapprox_sideZ
other_sideZexplr   r   r   r[      s4    
r[   c                 C   sV  ddl m} g }|dk rd}ttt| t|D ]}| | || kr2 qLq2|dkr~|d8 }d| g}| |d } ||d }t| t|krtt| D ]}| |  ||  kr qq|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 )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>)
difflibrl   rangeminr+   r]   isspacereprr6   
splitlines)rS   rT   rV   rl   r   ikeependsr   r   r   rd      sF    
rd   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    rZ   r$   ))]r!   ,r   )r   Zopeningclosingr   r   r    _surrounding_parens_on_own_lines'  s    r   c           	      C   s   |dkrt  sdgS dd l}t|  }t| }t|}t|}||krht|  }t| }|dksx|dkrt| t| dg}|dd |	||D  |S )Nr   zUse -v to get more diffr   z
Full diff:c                 s   s   | ]}|  V  qd S r2   )rstriprp   r   r   r   	<genexpr>L  s   z'_compare_eq_iterable.<locals>.<genexpr>)
running_on_cirs   pprintpformatrx   r+   r   r   rk   rl   )	rS   rT   rV   rs   Zleft_formattingZright_formattingZ
lines_leftZlines_rightr   r   r   r   rj   3  s$    

rj   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 } qq6|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:  != r   ZLeftZRightz contains one more item: z/%s contains %d more items, first extra item: %s)r3   bytesr+   rt   ru   r   )rS   rT   rV   Zcomparing_bytesr   Zlen_leftZ	len_rightry   Z
left_valueZright_valueZlen_diffZdir_with_moreextrar   r   r   rg   R  s>    	
rg   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   )rS   rT   rV   r   Z	diff_leftZ
diff_rightitemr   r   r   rh     s    

rh   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rd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   rq   krS   rT   r   r   
<dictcomp>      z$_compare_eq_dict.<locals>.<dictcomp>rX   ,Omitting %s identical items, use -vv to showzCommon items:c                    s    h | ]} | | kr|qS r   r   r   r   r   r   	<setcomp>  r   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   )rS   r   r   r     r   zRight contains %d more item%s:c                    s   i | ]}| | qS r   r   r   )rT   r   r   r     r   )	r<   intersectionr+   r   r   rx   r   r&   rk   )rS   rT   rV   r   Zset_leftZ	set_rightcommonsamediffr   Z
extra_leftZlen_extra_leftZextra_rightZlen_extra_rightr   r   r   ri     sL    


,ri   c              	      s  t | sg S t| r6dd l}|| }dd |D }n6t| rT| j}dd |D }nt| rd| j}ndslJ d g }g }|D ].}t| |t||kr|	| q||	| q|g }	|s|r|	dg7 }	|r|dk r|		d	t
|  n"|r|	d
g7 }	|	t| 7 }	|r|	dg7 }	|	t| 7 }	|D ]V}t| |}
t||}|	dd| d ||
|f g7 }	|	 fddt|
||D 7 }	q*|	S )Nr   c                 S   s   g | ]}|j r|jqS r   )comparename)rq   infor   r   r   rr     r   z#_compare_eq_cls.<locals>.<listcomp>c                 S   s   g | ]}t |d r|jqS )eq)rB   r   )rq   fieldr   r   r   rr     r   Fr(   r   rX   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   rp   r0   r   r   rr     s   )rQ   rG   dataclassesfieldsrI   rH   rD   r@   rB   r&   r+   r   r   rx   r[   )rS   rT   rV   r   Z
all_fieldsZfields_to_checkr   r   r   r   Z
field_leftZfield_rightr   r   r   rf     sT    






rf   )termtextrV   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:rm   )rY   ZSkippingz- z+ r(   rX   )findr+   rd   r   r*   r&   )
r   r   rV   indexheadtailZcorrect_textr   Znewdiffr.   r   r   r   r\     s    



r\   )r   c                  C   s   ddg} t dd | D S )z0Check if we're currently running on a CI system.CIZBUILD_NUMBERc                 s   s   | ]}|t jv V  qd S r2   )osenviron)rq   varr   r   r   r   
  r   z running_on_ci.<locals>.<genexpr>)any)Zenv_varsr   r   r   r     s    r   )F)r   )r   )r   )r   )r   )r   )r   )8__doc__collections.abcr4   r   r   typingr   r   r   r   r   r   r   r	   unicodedatar
   Z_pytest._coder^   r   Z_pytest._io.safereprr   r   r   Z_pytest.configr   r   r6   object__annotations__r   intr   r   r   r   boolr8   r9   r;   r>   rD   rG   rI   rL   rQ   rb   r[   rd   r   rj   rg   rh   ri   rf   r\   r   r   r   r   r   <module>   s   "#	 
4#0   6  )3