B
    sdYF                 @   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   E/home/dcms/DCMS/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   3   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   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    r6   c             C   s
   t | tS )N)r2   r5   )r1   r   r   r   istextk   s    r7   c             C   s
   t | tS )N)r2   dict)r1   r   r   r   isdicto   s    r9   c             C   s   t | ttfS )N)r2   set	frozenset)r1   r   r   r   issets   s    r<   )objr   c             C   s   t | tot| dd d k	S )N_fields)r2   tuplegetattr)r=   r   r   r   isnamedtuplew   s    rA   c             C   s   t | dd d k	S )N__dataclass_fields__)r@   )r=   r   r   r   	isdatacls{   s    rC   c             C   s   t | dd d k	S )N__attrs_attrs__)r@   )r=   r   r   r   isattrs   s    rE   c             C   s,   yt |  t|  S  tk
r&   dS X d S )NF)iterr7   	TypeError)r=   r   r   r   
isiterable   s
    
rH   c             C   s@   t | jdr<t | jjdr<| jjj}t| r4d|k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__rI   rJ   rE   )r=   Zcode_filenamer   r   r   has_default_eq   s    
rM   F)opleftright	use_asciir   c             C   s6  |  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	}
y>|d
krt|||}
n$|dkrt|rt|rt	|||}
W nF t
jk
r    Y n0 tk
r    dtjj  dg}
Y nX |
s,d	S |	g|
 S )z<Return specialised explanations for some operators/operands.verboseZNFDr   )rQ   A      )maxsizerQ    Nz==znot inz?(pytest_assertion plugin: representation of details failed: {}.z+ Probably an object has a faulty __repr__.))Z	getoptionr2   r5   r
   r   r*   r   _compare_eq_anyr7   _notin_textr   ZExit	Exceptionformat_pytest_codeZExceptionInfoZfrom_currentZ_getreprcrash)configrN   rO   rP   rQ   rR   Z	left_reprZ
right_reprrU   summaryr   r   r   r   assertrepr_compare   s8    


r_   )rO   rP   rR   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)r7   
_diff_textZ_pytest.python_apir`   r2   Z_repr_comparetyperC   rE   rA   _compare_eq_clsr6   _compare_eq_sequencer<   _compare_eq_setr9   _compare_eq_dictrH   _compare_eq_iterableextend)rO   rP   rR   r   r`   Zapprox_sideZ
other_sideZexplr   r   r   rW      s*    
rW   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>)
difflibri   rangeminr*   rZ   isspacereprr5   
splitlines)rO   rP   rR   ri   r   ikeependsr   r   r   ra      s@    


ra   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    rV   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 )N)rstrip)rm   r.   r   r   r   	<genexpr>M  s    z'_compare_eq_iterable.<locals>.<genexpr>)
running_on_ciro   pprintpformatrt   r*   r   r}   rh   ri   )	rO   rP   rR   ro   Zleft_formattingZright_formattingZ
lines_leftZlines_rightr   r   r   r   rg   3  s"    rg   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)r2   bytesr*   rp   rq   r   )rO   rP   rR   Zcomparing_bytesr   Zlen_leftZ	len_rightru   Z
left_valueZright_valueZlen_diffZdir_with_moreextrar   r   r   rd   R  s:    	
rd   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   )rO   rP   rR   r   Z	diff_leftZ
diff_rightitemr   r   r   re     s    



re   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   )rm   k)rO   rP   r   r   
<dictcomp>  s    z$_compare_eq_dict.<locals>.<dictcomp>rT   z,Omitting %s identical items, use -vv to showzCommon items:c                s    h | ]} | | kr|qS r   r   )rm   r   )rO   rP   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   )rm   r   )rO   r   r   r     s    zRight contains %d more item%s:c                s   i | ]} | |qS r   r   )rm   r   )rP   r   r   r     s    )	r:   intersectionr*   r   r   rt   r   r&   rh   )rO   rP   rR   r   Zset_leftZ	set_rightcommonsamediffr   Z
extra_leftZlen_extra_leftZextra_rightZlen_extra_rightr   )rO   rP   r   rf     s@    



.rf   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sltd g }g }x6|D ].}t	| |t	||kr|
| q~|
| q~W 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 }	x^|D ]V}t	| |}
t	||}|	dd| d ||
|f g7 }	|	 fddt|
||D 7 }	q0W |	S )Nr   c             S   s   g | ]}|j r|jqS r   )comparename)rm   infor   r   r   rn     s    z#_compare_eq_cls.<locals>.<listcomp>c             S   s   g | ]}t |d r|jqS )eq)r@   r   )rm   fieldr   r   r   rn     s    Fz  r   rT   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   )rm   r.   )r0   r   r   rn     s   )rM   rC   dataclassesfieldsrE   rD   rA   r>   r,   r@   r&   r*   r   r   rt   rW   )rO   rP   rR   r   Z
all_fieldsZfields_to_checkr   r   r   r   Z
field_leftZfield_rightr   )r0   r   rc     sR    








rc   )termtextrR   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:rj   )rU   ZSkippingz- z+ z  rT   )findr*   ra   r   r)   r&   )
r   r   rR   indexheadtailZcorrect_textr   Znewdiffr.   r   r   r   rX     s    




rX   )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kV  qd S )N)osenviron)rm   varr   r   r   r   
  s    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.abcr3   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   r5   object__annotations__r   intr   r   r   r   boolr6   r7   r9   r<   rA   rC   rE   rH   rM   r_   rW   ra   r}   rg   rd   re   rf   rc   rX   r   r   r   r   r   <module>   sd   "#	
2#04'3