Changeset 7312
- Timestamp:
- 02/04/08 01:34:03 (7 months ago)
- Files:
-
- tools/lime/trunk/lib/lime.php (modified) (32 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
tools/lime/trunk/lib/lime.php
r6862 r7312 1 1 <?php 2 2 3 /* 3 /** 4 4 * This file is part of the symfony package. 5 5 * (c) 2004-2006 Fabien Potencier <fabien.potencier@gmail.com> … … 16 16 * @version SVN: $Id$ 17 17 */ 18 19 18 class lime_test 20 19 { … … 26 25 public $output = null; 27 26 28 function __construct($plan = null, $output_instance = null)27 public function __construct($plan = null, $output_instance = null) 29 28 { 30 29 $this->plan = $plan; … … 34 33 } 35 34 36 function __destruct()35 public function __destruct() 37 36 { 38 37 $total = $this->passed + $this->failed + $this->skipped; … … 61 60 } 62 61 63 function ok($exp, $message = '')62 public function ok($exp, $message = '') 64 63 { 65 64 if ($result = (boolean) $exp) … … 90 89 } 91 90 92 function is($exp1, $exp2, $message = '')91 public function is($exp1, $exp2, $message = '') 93 92 { 94 93 if (is_object($exp1) || is_object($exp2)) … … 103 102 if (!$result = $this->ok($value, $message)) 104 103 { 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))); 106 105 } 107 106 … … 109 108 } 110 109 111 function isnt($exp1, $exp2, $message = '')110 public function isnt($exp1, $exp2, $message = '') 112 111 { 113 112 if (!$result = $this->ok($exp1 != $exp2, $message)) 114 113 { 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))); 116 115 } 117 116 … … 119 118 } 120 119 121 function like($exp, $regex, $message = '')120 public function like($exp, $regex, $message = '') 122 121 { 123 122 if (!$result = $this->ok(preg_match($regex, $exp), $message)) … … 129 128 } 130 129 131 function unlike($exp, $regex, $message = '')130 public function unlike($exp, $regex, $message = '') 132 131 { 133 132 if (!$result = $this->ok(!preg_match($regex, $exp), $message)) … … 139 138 } 140 139 141 function cmp_ok($exp1, $op, $exp2, $message = '')140 public function cmp_ok($exp1, $op, $exp2, $message = '') 142 141 { 143 142 eval(sprintf("\$result = \$exp1 $op \$exp2;")); … … 150 149 } 151 150 152 function can_ok($object, $methods, $message = '')151 public function can_ok($object, $methods, $message = '') 153 152 { 154 153 $result = true; … … 170 169 } 171 170 172 function isa_ok($var, $class, $message = '')171 public function isa_ok($var, $class, $message = '') 173 172 { 174 173 $type = is_object($var) ? get_class($var) : gettype($var); … … 181 180 } 182 181 183 function is_deeply($exp1, $exp2, $message = '')182 public function is_deeply($exp1, $exp2, $message = '') 184 183 { 185 184 if (!$result = $this->ok($this->test_is_deeply($exp1, $exp2), $message)) … … 191 190 } 192 191 193 function pass($message = '')192 public function pass($message = '') 194 193 { 195 194 return $this->ok(true, $message); 196 195 } 197 196 198 function fail($message = '')197 public function fail($message = '') 199 198 { 200 199 return $this->ok(false, $message); 201 200 } 202 201 203 function diag($message)202 public function diag($message) 204 203 { 205 204 $this->output->diag($message); 206 205 } 207 206 208 function skip($message = '', $nb_tests = 1)207 public function skip($message = '', $nb_tests = 1) 209 208 { 210 209 for ($i = 0; $i < $nb_tests; $i++) … … 215 214 } 216 215 217 function todo($message = '')216 public function todo($message = '') 218 217 { 219 218 ++$this->skipped and --$this->passed; … … 221 220 } 222 221 223 function include_ok($file, $message = '')222 public function include_ok($file, $message = '') 224 223 { 225 224 if (!$result = $this->ok((@include($file)) == 1, $message)) … … 264 263 } 265 264 266 function comment($message)265 public function comment($message) 267 266 { 268 267 $this->output->comment($message); 269 268 } 270 269 271 static function get_temp_directory()270 public static function get_temp_directory() 272 271 { 273 272 if ('\\' == DIRECTORY_SEPARATOR) … … 295 294 class lime_output 296 295 { 297 function diag()296 public function diag() 298 297 { 299 298 $messages = func_get_args(); … … 304 303 } 305 304 306 function comment($message)305 public function comment($message) 307 306 { 308 307 echo "# $message\n"; 309 308 } 310 309 311 function echoln($message)310 public function echoln($message) 312 311 { 313 312 echo "$message\n"; 314 313 } 315 314 316 function green_bar($message)315 public function green_bar($message) 317 316 { 318 317 echo "$message\n"; 319 318 } 320 319 321 function red_bar($message)320 public function red_bar($message) 322 321 { 323 322 echo "$message\n"; … … 329 328 public $colorizer = null; 330 329 331 function __construct()330 public function __construct() 332 331 { 333 332 $this->colorizer = new lime_colorizer(); 334 333 } 335 334 336 function diag()335 public function diag() 337 336 { 338 337 $messages = func_get_args(); … … 343 342 } 344 343 345 function comment($message)344 public function comment($message) 346 345 { 347 346 echo $this->colorizer->colorize(sprintf('# %s', $message), 'COMMENT')."\n"; 348 347 } 349 348 350 function echoln($message, $colorizer_parameter = null)349 public function echoln($message, $colorizer_parameter = null) 351 350 { 352 351 $message = preg_replace('/(?:^|\.)((?:not ok|dubious) *\d*)\b/e', '$this->colorizer->colorize(\'$1\', \'ERROR\')', $message); … … 358 357 } 359 358 360 function green_bar($message)359 public function green_bar($message) 361 360 { 362 361 echo $this->colorizer->colorize($message.str_repeat(' ', 71 - min(71, strlen($message))), 'GREEN_BAR')."\n"; 363 362 } 364 363 365 function red_bar($message)364 public function red_bar($message) 366 365 { 367 366 echo $this->colorizer->colorize($message.str_repeat(' ', 71 - min(71, strlen($message))), 'RED_BAR')."\n"; … … 373 372 static public $styles = array(); 374 373 375 static function style($name, $options = array())374 public static function style($name, $options = array()) 376 375 { 377 376 self::$styles[$name] = $options; 378 377 } 379 378 380 static function colorize($text = '', $parameters = array())379 public static function colorize($text = '', $parameters = array()) 381 380 { 382 381 // disable colors if not supported (windows or non tty console) … … 418 417 public $output = null; 419 418 420 function __construct($output_instance, $php_cli = null)419 public function __construct($output_instance, $php_cli = null) 421 420 { 422 421 if (getenv('PHP_PATH')) … … 462 461 } 463 462 464 function run()463 public function run() 465 464 { 466 465 if (!count($this->files)) … … 614 613 public $verbose = false; 615 614 616 function __construct($harness)615 public function __construct($harness) 617 616 { 618 617 $this->harness = $harness; 619 618 } 620 619 621 function run()620 public function run() 622 621 { 623 622 if (!function_exists('xdebug_start_code_coverage')) … … 655 654 passthru(sprintf('%s "%s" 2>&1', $this->harness->php_cli, $tmp_file), $return); 656 655 $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 661 671 throw new Exception(sprintf('Unable to unserialize coverage for file "%s"', $file)); 662 672 } 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) 667 697 { 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; 683 699 } 684 700 } … … 728 744 } 729 745 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) 731 916 { 732 917 sort($lines); … … 765 950 public $base_dir = ''; 766 951 767 function register($files_or_directories)952 public function register($files_or_directories) 768 953 { 769 954 foreach ((array) $files_or_directories as $f_or_d) … … 784 969 } 785 970 786 function register_glob($glob)971 public function register_glob($glob) 787 972 { 788 973 if ($dirs = glob($glob)) … … 795 980 } 796 981 797 function register_dir($directory)982 public function register_dir($directory) 798 983 { 799 984 if (!is_dir($directory))