B
    `/                 @   s   d Z ddlZddlmZ ddlmZ ddlmZmZm	Z	 G dd de
ZG dd	 d	eZd
d ZdddZeddeeddd ZdS )z Results of coverage measurement.    N)iitems)SimpleReprMixin)contractCoverageException	nice_pairc               @   s   e Zd ZdZdd ZdddZdd Zed	d
dd Zed	d
dd Z	ed	d
dd Z
ed	d
dd Zdd Zdd Zedd
dd Zedd
dd ZdS )Analysisz(The results of analyzing a FileReporter.c       	   	      s&  | _ | _| jj _ j  _ j  _ j  jpBg } j|}| _ j j  _	 j 
 rt j  _ j  _ j  _  }  }t fddt|D }tdd t|D }n g  _i  _t  _d } }}tdt jt jt j	|||d _d S )Nc             3   s$   | ]\}}| j krt|V  qd S )N)missinglen).0kv)self X/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/coverage/results.py	<genexpr>#   s    z$Analysis.__init__.<locals>.<genexpr>c             s   s   | ]\}}t |V  qd S )N)r	   )r
   r   r   r   r   r   r   $   s    r      )n_filesn_statements
n_excluded	n_missing
n_branchesn_partial_branchesn_missing_branches)datafile_reporterfilenamelines
statementsZexcluded_linesZexcludedZtranslate_linesexecutedr   has_arcssortedarcs_arc_possibilitiesexit_countsZno_branch_lines	no_branch_total_branchesmissing_branch_arcssumr   setNumbersr	   Znumbers)	r   r   r   Zfile_mapperr   r   mbar   r   r   )r   r   __init__   s:    
zAnalysis.__init__Fc             C   s0   |r|   rt|  }nd}t| j| j|dS )zThe missing line numbers, formatted nicely.

        Returns a string like "1-2, 5-11, 13-14".

        If `branches` is true, includes the missing branch arcs also.

        N)r!   )r   r   r&   format_linesr   r   )r   Zbranchesr!   r   r   r   missing_formatted5   s    zAnalysis.missing_formattedc             C   s
   | j  S )z"Were arcs measured in this result?)r   r   )r   r   r   r   r   D   s    zAnalysis.has_arcszlist(tuple(int, int)))returnsc             C   s   | j S )z.Returns a sorted list of the arcs in the code.)r"   )r   r   r   r   arc_possibilitiesH   s    zAnalysis.arc_possibilitiesc             C   s&   | j | jpg }| j|}t|S )z@Returns a sorted list of the arcs actually executed in the code.)r   r!   r   r   Ztranslate_arcsr    )r   r   r   r   r   arcs_executedM   s    zAnalysis.arcs_executedc                s,     }   fdd|D }t|S )z;Returns a sorted list of the arcs in the code not executed.c             3   s(   | ] }| kr|d  j kr|V  qdS )r   N)r$   )r
   p)r   r   r   r   r   Z   s    z(Analysis.arcs_missing.<locals>.<genexpr>)r/   r0   r    )r   possibler   r   )r   r   r   arcs_missingT   s    zAnalysis.arcs_missingc                s*   |    |  } fdd|D }t|S )zAReturns a sorted list of the executed arcs missing from the code.c             3   sB   | ]:}| kr|d  |d kr|d  d ks6|d d kr|V  qdS )r   r   Nr   )r
   e)r2   r   r   r   k   s    z,Analysis.arcs_unpredicted.<locals>.<genexpr>)r/   r0   r    )r   r   Zunpredictedr   )r2   r   arcs_unpredicted`   s    zAnalysis.arcs_unpredictedc             C   s   dd t | jD S )z<Returns a list of line numbers that have more than one exit.c             S   s   g | ]\}}|d kr|qS )r   r   )r
   l1countr   r   r   
<listcomp>t   s    z*Analysis._branch_lines.<locals>.<listcomp>)r   r#   )r   r   r   r   _branch_linesr   s    zAnalysis._branch_linesc             C   s   t dd | j D S )z"How many total branches are there?c             s   s   | ]}|d kr|V  qdS )r   Nr   )r
   r7   r   r   r   r   x   s    z+Analysis._total_branches.<locals>.<genexpr>)r'   r#   values)r   r   r   r   r%   v   s    zAnalysis._total_brancheszdict(int: list(int))c             C   sJ   |   }t|  }tt}x&|D ]\}}||kr$|| | q$W |S )zgReturn arcs that weren't executed from branch lines.

        Returns {l1:[l2a,l2b,...], ...}

        )r3   r(   r9   collectionsdefaultdictlistappend)r   r   Zbranch_linesr*   r6   l2r   r   r   r&   z   s    
zAnalysis.missing_branch_arcszdict(int: tuple(int, int))c             C   sH   |   }i }x6|  D ]*}| j| }t|| }||| f||< qW |S )zGet stats about branches.

        Returns a dict mapping line numbers to a tuple:
        (total_exits, taken_exits).
        )r&   r9   r#   r	   )r   Zmissing_arcsstatslnumexitsr   r   r   r   branch_stats   s    
zAnalysis.branch_statsN)F)__name__
__module____qualname____doc__r+   r-   r   r   r/   r0   r3   r5   r9   r%   r&   rC   r   r   r   r   r      s   %
r   c               @   s   e Zd ZdZdZdZdZdddZdd Ze	d	d
 Z
edd Zedd Zedd Zedd Ze	dd Zedd Zdd Zdd ZdS )r)   zThe numerical results of measuring coverage.

    This holds the basic statistics from `Analysis`, and is used to roll
    up statistics across files.

    r   g      ?g     X@c             C   s.   || _ || _|| _|| _|| _|| _|| _d S )N)r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r+      s    zNumbers.__init__c             C   s    | j | j| j| j| j| j| jgS )z:Return a list for __init__(*args) to recreate this object.)r   r   r   r   r   r   r   )r   r   r   r   	init_args   s    zNumbers.init_argsc             C   s>   d|  krdk sn t || _dd|  | _d| j | _dS )z<Set the number of decimal places used to report percentages.r   
   g      ?g      Y@N)AssertionError
_precision_near0_near100)cls	precisionr   r   r   set_precision   s    zNumbers.set_precisionc             C   s   | j | j S )z*Returns the number of executed statements.)r   r   )r   r   r   r   
n_executed   s    zNumbers.n_executedc             C   s   | j | j S )z(Returns the number of executed branches.)r   r   )r   r   r   r   n_executed_branches   s    zNumbers.n_executed_branchesc             C   s*   | j dkr"| j\}}d| | }nd}|S )z/Returns a single percentage value for coverage.r   g      Y@)r   ratio_covered)r   	numeratordenominatorZpc_covr   r   r   
pc_covered   s
    

zNumbers.pc_coveredc             C   sd   | j }d|  k r| jk r(n n| j}n.| j|  k r>dk rJn n| j}nt|| j}d| j|f S )a  Returns the percent covered, as a string, without a percent sign.

        Note that "0" is only returned when the value is truly zero, and "100"
        is only returned when the value is truly 100.  Rounding can never
        result in either "0" or "100".

        r   d   z%.*f)rV   rL   rM   roundrK   )r   Zpcr   r   r   pc_covered_str   s    	zNumbers.pc_covered_strc             C   s    d}| j dkr|d| j  7 }|S )z/How many characters wide can pc_covered_str be?   r   r   )rK   )rN   widthr   r   r   pc_str_width   s    
zNumbers.pc_str_widthc             C   s    | j | j }| j| j }||fS )z:Return a numerator and denominator for the coverage ratio.)rQ   rR   r   r   )r   rT   rU   r   r   r   rS      s    zNumbers.ratio_coveredc             C   sl   t  }| j|j |_| j|j |_| j|j |_| j|j |_| j|j |_| j|j |_| j|j |_|S )N)r)   r   r   r   r   r   r   r   )r   othernumsr   r   r   __add__   s    zNumbers.__add__c             C   s   |dkr| S t S )Nr   )NotImplemented)r   r]   r   r   r   __radd__  s    zNumbers.__radd__N)r   r   r   r   r   r   r   )rD   rE   rF   rG   rK   rL   rM   r+   rH   classmethodrP   propertyrQ   rR   rV   rY   r\   rS   r_   ra   r   r   r   r   r)      s     


r)   c             C   s   t | } t |}g }d}d}xR| D ]J}|t|kr4P ||| krV|d7 }|sP|}|}q"|r"|||f d}q"W |r|||f |S )z,Produce a list of ranges for `format_lines`.Nr   r   )r    r	   r>   )r   r   pairsstartZlidxZstmtendr   r   r   _line_ranges  s&    
rg   c       
      C   s   dd t | |D }|rzt|}xX|D ]P\}}xFt|D ]:}||kr8||kr8|dkrX|nd}||d||f f q8W q&W ddd t|D }	|	S )	a  Nicely format a list of line numbers.

    Format a list of line numbers for printing by coalescing groups of lines as
    long as the lines represent consecutive statements.  This will coalesce
    even if there are gaps between statements.

    For example, if `statements` is [1,2,3,4,5,10,11,12,13,14] and
    `lines` is [1,2,5,10,11,13,14] then the result will be "1-2, 5-11, 13-14".

    Both `lines` and `statements` can be any iterable. All of the elements of
    `lines` must be in `statements`, and all of the values must be positive
    integers.

    If `arcs` is provided, they are (start,[end,end,end]) pairs that will be
    included in the output as long as start isn't in `lines`.

    c             S   s   g | ]}|d  t |fqS )r   )r   )r
   pairr   r   r   r8   6  s    z format_lines.<locals>.<listcomp>r   exitz%d->%sz, c             s   s   | ]}|d  V  qdS )Nr   )r
   tr   r   r   r   ?  s    zformat_lines.<locals>.<genexpr>)rg   r    r>   join)
r   r   r!   Z
line_itemsZ
line_exitslinerB   exdestretr   r   r   r,   $  s    r,   number)total
fail_underrO   r.   c             C   sJ   d|  krdks(n d |}t||dkr<| dkr<dS t| ||k S )a'  Determine if a total should fail due to fail-under.

    `total` is a float, the coverage measurement total. `fail_under` is the
    fail_under setting to compare with. `precision` is the number of digits
    to consider after the decimal point.

    Returns True if the total should fail.

    r   g      Y@z4fail_under={} is invalid. Must be between 0 and 100.T)formatr   rX   )rr   rs   rO   msgr   r   r   should_fail_underC  s    
rv   )N)rG   r;   Zcoverage.backwardr   Zcoverage.debugr   Zcoverage.miscr   r   r   objectr   r)   rg   r,   intboolrv   r   r   r   r   <module>   s    r
