Development

Changeset 7312

You must first sign up to be able to contribute.

Changeset 7312

Show
Ignore:
Timestamp:
02/04/08 01:34:03 (7 months ago)
Author:
dwhittle
Message:

dwhittle: added back get_php_lines + cleaned up

Files:

Legend:

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

    r6862 r7312  
    11<?php 
    22 
    3 /* 
     3/** 
    44 * This file is part of the symfony package. 
    55 * (c) 2004-2006 Fabien Potencier <fabien.potencier@gmail.com> 
     
    1616 * @version    SVN: $Id$ 
    1717 */ 
    18  
    1918class lime_test 
    2019{ 
     
    2625  public $output = null; 
    2726 
    28   function __construct($plan = null, $output_instance = null) 
     27  public function __construct($plan = null, $output_instance = null) 
    2928  { 
    3029    $this->plan = $plan; 
     
    3433  } 
    3534 
    36   function __destruct() 
     35  public function __destruct() 
    3736  { 
    3837    $total = $this->passed + $this->failed + $this->skipped; 
     
    6160  } 
    6261 
    63   function ok($exp, $message = '') 
     62  public function ok($exp, $message = '') 
    6463  { 
    6564    if ($result = (boolean) $exp) 
     
    9089  } 
    9190 
    92   function is($exp1, $exp2, $message = '') 
     91  public function is($exp1, $exp2, $message = '') 
    9392  { 
    9493    if (is_object($exp1) || is_object($exp2)) 
     
    103102    if (!$result = $this->ok($value, $message)) 
    104103    { 
    105       $this->output->diag(sprintf("           got: %s", str_replace("\n", '', var_export($exp1, true))), sprintf("      expected: %s", str_replace("\n", '', var_export($exp2, true)))); 
     104      $this->output->diag(sprintf("           got: %s", var_export($exp1, true)), sprintf("      expected: %s", var_export($exp2, true))); 
    106105    } 
    107106 
     
    109108  } 
    110109 
    111   function isnt($exp1, $exp2, $message = '') 
     110  public function isnt($exp1, $exp2, $message = '') 
    112111  { 
    113112    if (!$result = $this->ok($exp1 != $exp2, $message)) 
    114113    { 
    115       $this->output->diag(sprintf("      %s", str_replace("\n", '', var_export($exp1, true))), '          ne', sprintf("      %s", str_replace("\n", '', var_export($exp2, true)))); 
     114      $this->output->diag(sprintf("      %s", var_export($exp2, true)), '          ne', sprintf("      %s", var_export($exp2, true))); 
    116115    } 
    117116 
     
    119118  } 
    120119 
    121   function like($exp, $regex, $message = '') 
     120  public function like($exp, $regex, $message = '') 
    122121  { 
    123122    if (!$result = $this->ok(preg_match($regex, $exp), $message)) 
     
    129128  } 
    130129 
    131   function unlike($exp, $regex, $message = '') 
     130  public function unlike($exp, $regex, $message = '') 
    132131  { 
    133132    if (!$result = $this->ok(!preg_match($regex, $exp), $message)) 
     
    139138  } 
    140139 
    141   function cmp_ok($exp1, $op, $exp2, $message = '') 
     140  public function cmp_ok($exp1, $op, $exp2, $message = '') 
    142141  { 
    143142    eval(sprintf("\$result = \$exp1 $op \$exp2;")); 
     
    150149  } 
    151150 
    152   function can_ok($object, $methods, $message = '') 
     151  public function can_ok($object, $methods, $message = '') 
    153152  { 
    154153    $result = true; 
     
    170169  } 
    171170 
    172   function isa_ok($var, $class, $message = '') 
     171  public function isa_ok($var, $class, $message = '') 
    173172  { 
    174173    $type = is_object($var) ? get_class($var) : gettype($var); 
     
    181180  } 
    182181 
    183   function is_deeply($exp1, $exp2, $message = '') 
     182  public function is_deeply($exp1, $exp2, $message = '') 
    184183  { 
    185184    if (!$result = $this->ok($this->test_is_deeply($exp1, $exp2), $message)) 
     
    191190  } 
    192191 
    193   function pass($message = '') 
     192  public function pass($message = '') 
    194193  { 
    195194    return $this->ok(true, $message); 
    196195  } 
    197196 
    198   function fail($message = '') 
     197  public function fail($message = '') 
    199198  { 
    200199    return $this->ok(false, $message); 
    201200  } 
    202201 
    203   function diag($message) 
     202  public function diag($message) 
    204203  { 
    205204    $this->output->diag($message); 
    206205  } 
    207206 
    208   function skip($message = '', $nb_tests = 1) 
     207  public function skip($message = '', $nb_tests = 1) 
    209208  { 
    210209    for ($i = 0; $i < $nb_tests; $i++) 
     
    215214  } 
    216215 
    217   function todo($message = '') 
     216  public function todo($message = '') 
    218217  { 
    219218    ++$this->skipped and --$this->passed; 
     
    221220  } 
    222221 
    223   function include_ok($file, $message = '') 
     222  public function include_ok($file, $message = '') 
    224223  { 
    225224    if (!$result = $this->ok((@include($file)) == 1, $message)) 
     
    264263  } 
    265264 
    266   function comment($message) 
     265  public function comment($message) 
    267266  { 
    268267    $this->output->comment($message); 
    269268  } 
    270269 
    271   static function get_temp_directory() 
     270  public static function get_temp_directory() 
    272271  { 
    273272    if ('\\' == DIRECTORY_SEPARATOR) 
     
    295294class lime_output 
    296295{ 
    297   function diag() 
     296  public function diag() 
    298297  { 
    299298    $messages = func_get_args(); 
     
    304303  } 
    305304 
    306   function comment($message) 
     305  public function comment($message) 
    307306  { 
    308307    echo "# $message\n"; 
    309308  } 
    310309 
    311   function echoln($message) 
     310  public function echoln($message) 
    312311  { 
    313312    echo "$message\n"; 
    314313  } 
    315314 
    316   function green_bar($message) 
     315  public function green_bar($message) 
    317316  { 
    318317    echo "$message\n"; 
    319318  } 
    320319 
    321   function red_bar($message) 
     320  public function red_bar($message) 
    322321  { 
    323322    echo "$message\n"; 
     
    329328  public $colorizer = null; 
    330329 
    331   function __construct() 
     330  public function __construct() 
    332331  { 
    333332    $this->colorizer = new lime_colorizer(); 
    334333  } 
    335334 
    336   function diag() 
     335  public function diag() 
    337336  { 
    338337    $messages = func_get_args(); 
     
    343342  } 
    344343 
    345   function comment($message) 
     344  public function comment($message) 
    346345  { 
    347346    echo $this->colorizer->colorize(sprintf('# %s', $message), 'COMMENT')."\n"; 
    348347  } 
    349348 
    350   function echoln($message, $colorizer_parameter = null) 
     349  public function echoln($message, $colorizer_parameter = null) 
    351350  { 
    352351    $message = preg_replace('/(?:^|\.)((?:not ok|dubious) *\d*)\b/e', '$this->colorizer->colorize(\'$1\', \'ERROR\')', $message); 
     
    358357  } 
    359358 
    360   function green_bar($message) 
     359  public function green_bar($message) 
    361360  { 
    362361    echo $this->colorizer->colorize($message.str_repeat(' ', 71 - min(71, strlen($message))), 'GREEN_BAR')."\n"; 
    363362  } 
    364363 
    365   function red_bar($message) 
     364  public function red_bar($message) 
    366365  { 
    367366    echo $this->colorizer->colorize($message.str_repeat(' ', 71 - min(71, strlen($message))), 'RED_BAR')."\n"; 
     
    373372  static public $styles = array(); 
    374373 
    375   static function style($name, $options = array()) 
     374  public static function style($name, $options = array()) 
    376375  { 
    377376    self::$styles[$name] = $options; 
    378377  } 
    379378 
    380   static function colorize($text = '', $parameters = array()) 
     379  public static function colorize($text = '', $parameters = array()) 
    381380  { 
    382381    // disable colors if not supported (windows or non tty console) 
     
    418417  public $output = null; 
    419418 
    420   function __construct($output_instance, $php_cli = null) 
     419  public function __construct($output_instance, $php_cli = null) 
    421420  { 
    422421    if (getenv('PHP_PATH')) 
     
    462461  } 
    463462 
    464   function run() 
     463  public function run() 
    465464  { 
    466465    if (!count($this->files)) 
     
    614613  public $verbose = false; 
    615614 
    616   function __construct($harness) 
     615  public function __construct($harness) 
    617616  { 
    618617    $this->harness = $harness; 
    619618  } 
    620619 
    621   function run() 
     620  public function run() 
    622621  { 
    623622    if (!function_exists('xdebug_start_code_coverage')) 
     
    655654      passthru(sprintf('%s "%s" 2>&1', $this->harness->php_cli, $tmp_file), $return); 
    656655      $retval = ob_get_clean(); 
    657       if (0 == $return) 
    658       { 
    659         if (false === $cov = unserialize(substr($retval, strpos($retval, '<PHP_SER>') + 9, strpos($retval, '</PHP_SER>') - 9))) 
    660         { 
     656 
     657      if (0 != $return) // test exited without success 
     658      { 
     659        // something may have gone wrong, we should warn the user so they know 
     660        // it's a bug in their code and not symfony's 
     661 
     662        $this->harness->output->echoln(sprintf('Warning: %s returned status %d, results may be inaccurate', $file, $return), 'ERROR'); 
     663      } 
     664 
     665      if (false === $cov = unserialize(substr($retval, strpos($retval, '<PHP_SER>') + 9, strpos($retval, '</PHP_SER>') - 9))) 
     666      { 
     667        if (0 == $return) 
     668        { 
     669          // failed to serialize, but PHP said it should of worked. 
     670          // something is seriously wrong, so abort with exception 
    661671          throw new Exception(sprintf('Unable to unserialize coverage for file "%s"', $file)); 
    662672        } 
    663  
    664         foreach ($cov as $file => $lines) 
    665         { 
    666           if (!in_array($file, $this->files)) 
     673        else 
     674        { 
     675          // failed to serialize, but PHP warned us that this might have happened. 
     676          // so we should ignore and move on 
     677          continue; // continue foreach loop through $this->harness->files 
     678        } 
     679      } 
     680 
     681      foreach ($cov as $file => $lines) 
     682      { 
     683        if (!in_array($file, $this->files)) 
     684        { 
     685          continue; 
     686        } 
     687 
     688        if (!isset($coverage[$file])) 
     689        { 
     690          $coverage[$file] = $lines; 
     691          continue; 
     692        } 
     693 
     694        foreach ($lines as $line => $flag) 
     695        { 
     696          if ($flag == 1) 
    667697          { 
    668             continue; 
    669           } 
    670  
    671           if (!isset($coverage[$file])) 
    672           { 
    673             $coverage[$file] = $lines; 
    674             continue; 
    675           } 
    676  
    677           foreach ($lines as $line => $flag) 
    678           { 
    679             if ($flag == 1) 
    680             { 
    681               $coverage[$file][$line] = 1; 
    682             } 
     698            $coverage[$file][$line] = 1; 
    683699          } 
    684700        } 
     
    728744  } 
    729745 
    730   function format_range($lines) 
     746  public static function get_php_lines($content) 
     747  { 
     748    if (is_readable($content)) 
     749    { 
     750      $content = file_get_contents($content); 
     751    } 
     752 
     753    $tokens = token_get_all($content); 
     754    $php_lines = array(); 
     755    $current_line = 1; 
     756    $in_class = false; 
     757    $in_function = false; 
     758    $in_function_declaration = false; 
     759    $end_of_current_expr = true; 
     760    $open_braces = 0; 
     761    foreach ($tokens as $token) 
     762    { 
     763      if (is_string($token)) 
     764      { 
     765        switch ($token) 
     766        { 
     767          case '=': 
     768            if (false === $in_class || (false !== $in_function && !$in_function_declaration)) 
     769            { 
     770              $php_lines[$current_line] = true; 
     771            } 
     772            break; 
     773          case '{': 
     774            ++$open_braces; 
     775            $in_function_declaration = false; 
     776            break; 
     777          case ';': 
     778            $in_function_declaration = false; 
     779            $end_of_current_expr = true; 
     780            break; 
     781          case '}': 
     782            $end_of_current_expr = true; 
     783            --$open_braces; 
     784            if ($open_braces == $in_class) 
     785            { 
     786              $in_class = false; 
     787            } 
     788            if ($open_braces == $in_function) 
     789            { 
     790              $in_function = false; 
     791            } 
     792            break; 
     793        } 
     794 
     795        continue; 
     796      } 
     797 
     798      list($id, $text) = $token; 
     799 
     800      switch ($id) 
     801      { 
     802        case T_CURLY_OPEN: 
     803        case T_DOLLAR_OPEN_CURLY_BRACES: 
     804          ++$open_braces; 
     805          break; 
     806        case T_WHITESPACE: 
     807        case T_OPEN_TAG: 
     808        case T_CLOSE_TAG: 
     809          $end_of_current_expr = true; 
     810          $current_line += count(explode("\n", $text)) - 1; 
     811          break; 
     812        case T_COMMENT: 
     813        case T_DOC_COMMENT: 
     814          $current_line += count(explode("\n", $text)) - 1; 
     815          break; 
     816        case T_CLASS: 
     817          $in_class = $open_braces; 
     818          break; 
     819        case T_FUNCTION: 
     820          $in_function = $open_braces; 
     821          $in_function_declaration = true; 
     822          break; 
     823        case T_AND_EQUAL: 
     824        case T_BREAK: 
     825        case T_CASE: 
     826        case T_CATCH: 
     827        case T_CLONE: 
     828        case T_CONCAT_EQUAL: 
     829        case T_CONTINUE: 
     830        case T_DEC: 
     831        case T_DECLARE: 
     832        case T_DEFAULT: 
     833        case T_DIV_EQUAL: 
     834        case T_DO: 
     835        case T_ECHO: 
     836        case T_ELSEIF: 
     837        case T_EMPTY: 
     838        case T_ENDDECLARE: 
     839        case T_ENDFOR: 
     840        case T_ENDFOREACH: 
     841        case T_ENDIF: 
     842        case T_ENDSWITCH: 
     843        case T_ENDWHILE: 
     844        case T_EVAL: 
     845        case T_EXIT: 
     846        case T_FOR: 
     847        case T_FOREACH: 
     848        case T_GLOBAL: 
     849        case T_IF: 
     850        case T_INC: 
     851        case T_INCLUDE: 
     852        case T_INCLUDE_ONCE: 
     853        case T_INSTANCEOF: 
     854        case T_ISSET: 
     855        case T_IS_EQUAL: 
     856        case T_IS_GREATER_OR_EQUAL: 
     857        case T_IS_IDENTICAL: 
     858        case T_IS_NOT_EQUAL: 
     859        case T_IS_NOT_IDENTICAL: 
     860        case T_IS_SMALLER_OR_EQUAL: 
     861        case T_LIST: 
     862        case T_LOGICAL_AND: 
     863        case T_LOGICAL_OR: 
     864        case T_LOGICAL_XOR: 
     865        case T_MINUS_EQUAL: 
     866        case T_MOD_EQUAL: 
     867        case T_MUL_EQUAL: 
     868        case T_NEW: 
     869        case T_OBJECT_OPERATOR: 
     870        case T_OR_EQUAL: 
     871        case T_PLUS_EQUAL: 
     872        case T_PRINT: 
     873        case T_REQUIRE: 
     874        case T_REQUIRE_ONCE: 
     875        case T_RETURN: 
     876        case T_SL: 
     877        case T_SL_EQUAL: 
     878        case T_SR: 
     879        case T_SR_EQUAL: 
     880        case T_SWITCH: 
     881        case T_THROW: 
     882        case T_TRY: 
     883        case T_UNSET: 
     884        case T_UNSET_CAST: 
     885        case T_USE: 
     886        case T_WHILE: 
     887        case T_XOR_EQUAL: 
     888          $php_lines[$current_line] = true; 
     889          $end_of_current_expr = false; 
     890          break; 
     891        default: 
     892          if (false === $end_of_current_expr) 
     893          { 
     894            $php_lines[$current_line] = true; 
     895          } 
     896      } 
     897    } 
     898 
     899    return $php_lines; 
     900  } 
     901 
     902  public function compute($content, $cov) 
     903  { 
     904    $php_lines = self::get_php_lines($content); 
     905 
     906    // we remove from $cov non php lines 
     907    foreach (array_diff_key($cov, $php_lines) as $line => $tmp) 
     908    { 
     909      unset($cov[$line]); 
     910    } 
     911 
     912    return array($cov, $php_lines); 
     913  } 
     914 
     915  public function format_range($lines) 
    731916  { 
    732917    sort($lines); 
     
    765950  public $base_dir = ''; 
    766951 
    767   function register($files_or_directories) 
     952  public function register($files_or_directories) 
    768953  { 
    769954    foreach ((array) $files_or_directories as $f_or_d) 
     
    784969  } 
    785970 
    786   function register_glob($glob) 
     971  public function register_glob($glob) 
    787972  { 
    788973    if ($dirs = glob($glob)) 
     
    795980  } 
    796981 
    797   function register_dir($directory) 
     982  public function register_dir($directory) 
    798983  { 
    799984    if (!is_dir($directory))