Development

Changeset 5575

You must first sign up to be able to contribute.

Changeset 5575

Show
Ignore:
Timestamp:
10/17/07 22:18:08 (1 year ago)
Author:
fabien
Message:

lime: rewrote code coverage

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • tools/lime/trunk/lib/lime.php

    r4575 r5575  
    647647      $tmp = <<<EOF 
    648648<?php 
    649 xdebug_start_code_coverage(); 
     649xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE); 
    650650ob_start(); 
    651651include('$file'); 
     
    668668          if (!isset($coverage[$file])) 
    669669          { 
    670             $coverage[$file] = array(); 
     670            $coverage[$file] = $lines; 
     671            continue; 
    671672          } 
    672673 
    673674          foreach ($lines as $line => $count) 
    674675          { 
    675             if (!isset($coverage[$file][$line])
     676            if ($count == 1
    676677            { 
    677               $coverage[$file][$line] = 0
     678              $coverage[$file][$line] = 1
    678679            } 
    679             $coverage[$file][$line] = $coverage[$file][$line] + $count; 
    680680          } 
    681681        } 
     
    689689    foreach ($this->files as $file) 
    690690    { 
     691      $is_covered = isset($coverage[$file]); 
    691692      $cov = isset($coverage[$file]) ? $coverage[$file] : array(); 
    692  
    693       list($cov, $php_lines) = $this->compute(file_get_contents($file), $cov); 
     693      $covered_lines = array(); 
     694      $missing_lines = array(); 
     695 
     696      foreach ($cov as $line => $flag) 
     697      { 
     698        switch ($flag) 
     699        { 
     700          case 1: 
     701            $covered_lines[] = $line; 
     702            break; 
     703          case -1: 
     704            $missing_lines[] = $line; 
     705            break; 
     706        } 
     707      } 
     708      $total_lines = count($covered_lines) + count($missing_lines); 
    694709 
    695710      $output = $this->harness->output; 
    696       $percent = count($php_lines) ? count($cov) * 100 / count($php_lines) : 100; 
    697  
    698       $total_php_lines += count($php_lines)
    699       $total_covered_lines += count($cov); 
     711      $percent = $total_lines ? count($covered_lines) * 100 / $total_lines : 0; 
     712 
     713      $total_php_lines += $total_lines
     714      $total_covered_lines += count($covered_lines); 
    700715 
    701716      $relative_file = $this->get_relative_file($file); 
    702717      $output->echoln(sprintf("%-70s %3.0f%%", substr($relative_file, -min(70, strlen($relative_file))), $percent), $percent == 100 ? 'INFO' : ($percent > 90 ? 'PARAMETER' : ($percent < 20 ? 'ERROR' : ''))); 
    703       if ($this->verbose && $percent != 100) 
    704       { 
    705         $output->comment(sprintf("missing: %s", $this->format_range(array_keys(array_diff_key($php_lines, $cov))))); 
     718      if ($this->verbose && $is_covered && $percent != 100) 
     719      { 
     720        $output->comment(sprintf("missing: %s", $this->format_range($missing_lines))); 
    706721      } 
    707722    } 
    708723 
    709724    $output->echoln(sprintf("TOTAL COVERAGE: %3.0f%%", $total_covered_lines * 100 / $total_php_lines)); 
    710   } 
    711  
    712   static function get_php_lines($content) 
    713   { 
    714     if (is_file($content)) 
    715     { 
    716       $content = file_get_contents($content); 
    717     } 
    718  
    719     $tokens = token_get_all($content); 
    720     $php_lines = array(); 
    721     $current_line = 1; 
    722     $in_class = false; 
    723     $in_function = false; 
    724     $in_function_declaration = false; 
    725     $end_of_current_expr = true; 
    726     $open_braces = 0; 
    727     foreach ($tokens as $token) 
    728     { 
    729       if (is_string($token)) 
    730       { 
    731         switch ($token) 
    732         { 
    733           case '=': 
    734             if (false === $in_class || (false !== $in_function && !$in_function_declaration)) 
    735             { 
    736               $php_lines[$current_line] = true; 
    737             } 
    738             break; 
    739           case '{': 
    740             ++$open_braces; 
    741             $in_function_declaration = false; 
    742             break; 
    743           case ';': 
    744             $in_function_declaration = false; 
    745             $end_of_current_expr = true; 
    746             break; 
    747           case '}': 
    748             $end_of_current_expr = true; 
    749             --$open_braces; 
    750             if ($open_braces == $in_class) 
    751             { 
    752               $in_class = false; 
    753             } 
    754             if ($open_braces == $in_function) 
    755             { 
    756               $in_function = false; 
    757             } 
    758             break; 
    759         } 
    760  
    761         continue; 
    762       } 
    763  
    764       list($id, $text) = $token; 
    765  
    766       switch ($id) 
    767       { 
    768         case T_CURLY_OPEN: 
    769         case T_DOLLAR_OPEN_CURLY_BRACES: 
    770           ++$open_braces; 
    771           break; 
    772         case T_WHITESPACE: 
    773         case T_OPEN_TAG: 
    774         case T_CLOSE_TAG: 
    775           $end_of_current_expr = true; 
    776           $current_line += count(explode("\n", $text)) - 1; 
    777           break; 
    778         case T_COMMENT: 
    779         case T_DOC_COMMENT: 
    780           $current_line += count(explode("\n", $text)) - 1; 
    781           break; 
    782         case T_CLASS: 
    783           $in_class = $open_braces; 
    784           break; 
    785         case T_FUNCTION: 
    786           $in_function = $open_braces; 
    787           $in_function_declaration = true; 
    788           break; 
    789         case T_AND_EQUAL: 
    790         case T_CASE: 
    791         case T_CATCH: 
    792         case T_CLONE: 
    793         case T_CONCAT_EQUAL: 
    794         case T_CONTINUE: 
    795         case T_DEC: 
    796         case T_DECLARE: 
    797         case T_DEFAULT: 
    798         case T_DIV_EQUAL: 
    799         case T_DO: 
    800         case T_ECHO: 
    801         case T_ELSEIF: 
    802         case T_EMPTY: 
    803         case T_ENDDECLARE: 
    804         case T_ENDFOR: 
    805         case T_ENDFOREACH: 
    806         case T_ENDIF: 
    807         case T_ENDSWITCH: 
    808         case T_ENDWHILE: 
    809         case T_EVAL: 
    810         case T_EXIT: 
    811         case T_FOR: 
    812         case T_FOREACH: 
    813         case T_GLOBAL: 
    814         case T_IF: 
    815         case T_INC: 
    816         case T_INCLUDE: 
    817         case T_INCLUDE_ONCE: 
    818         case T_INSTANCEOF: 
    819         case T_ISSET: 
    820         case T_IS_EQUAL: 
    821         case T_IS_GREATER_OR_EQUAL: 
    822         case T_IS_IDENTICAL: 
    823         case T_IS_NOT_EQUAL: 
    824         case T_IS_NOT_IDENTICAL: 
    825         case T_IS_SMALLER_OR_EQUAL: 
    826         case T_LIST: 
    827         case T_LOGICAL_AND: 
    828         case T_LOGICAL_OR: 
    829         case T_LOGICAL_XOR: 
    830         case T_MINUS_EQUAL: 
    831         case T_MOD_EQUAL: 
    832         case T_MUL_EQUAL: 
    833         case T_NEW: 
    834         case T_OBJECT_OPERATOR: 
    835         case T_OR_EQUAL: 
    836         case T_PLUS_EQUAL: 
    837         case T_PRINT: 
    838         case T_REQUIRE: 
    839         case T_REQUIRE_ONCE: 
    840         case T_RETURN: 
    841         case T_SL: 
    842         case T_SL_EQUAL: 
    843         case T_SR: 
    844         case T_SR_EQUAL: 
    845         case T_THROW: 
    846         case T_TRY: 
    847         case T_UNSET: 
    848         case T_UNSET_CAST: 
    849         case T_USE: 
    850         case T_WHILE: 
    851         case T_XOR_EQUAL: 
    852           $php_lines[$current_line] = true; 
    853           $end_of_current_expr = false; 
    854           break; 
    855         default: 
    856           if (false === $end_of_current_expr) 
    857           { 
    858             $php_lines[$current_line] = true; 
    859           } 
    860           //print "$current_line: ".token_name($id)."\n"; 
    861       } 
    862     } 
    863  
    864     return $php_lines; 
    865   } 
    866  
    867   function compute($content, $cov) 
    868   { 
    869     $php_lines = self::get_php_lines($content); 
    870  
    871     // we remove from $cov non php lines 
    872     foreach (array_diff_key($cov, $php_lines) as $line => $tmp) 
    873     { 
    874       unset($cov[$line]); 
    875     } 
    876  
    877     return array($cov, $php_lines); 
    878725  } 
    879726