Skip to content

Commit

Permalink
Colorized console output
Browse files Browse the repository at this point in the history
  • Loading branch information
jaz303 committed May 17, 2009
1 parent 95355ef commit 2bbdd31
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
6 changes: 5 additions & 1 deletion examples/run_tests.php
Expand Up @@ -7,5 +7,9 @@

$suite->require_all(dirname(__FILE__) . '/test');
$suite->auto_fill();
$suite->run(new ztest\ConsoleReporter);

$reporter = new ztest\ConsoleReporter;
$reporter->enable_color();

$suite->run($reporter);
?>
45 changes: 40 additions & 5 deletions inc/ConsoleReporter.php
Expand Up @@ -3,16 +3,30 @@

class ConsoleReporter extends Reporter
{
private static $colors = array(
'red' => "\033[31m",
'green' => "\033[32m",
'blue' => "\033[34m",
'yellow' => "\033[33m",
'default' => "\033[0m"
);

private $use_color = false;

public function enable_color() {
$this->use_color = true;
}

protected function report_test_pass() {
echo ".";
echo $this->wrap('.', 'green');
}

protected function report_test_fail(AssertionFailed $eaf) {
echo "F";
echo $this->wrap('F', 'red');
}

protected function report_test_error(\Exception $e) {
echo "E";
echo $this->wrap('E', 'red');
}

protected function report_summary() {
Expand All @@ -31,9 +45,30 @@ protected function report_summary() {
}

echo "\nSummary: ";
echo "{$this->test_passes}/{$this->test_total} tests passed";
echo ", {$this->assert_passes}/{$this->assert_total} assertions";

echo $this->wrap_by_success(
"{$this->test_passes}/{$this->test_total} tests passed",
$this->test_passes,
$this->test_total);
echo ", ";
echo $this->wrap_by_success(
"{$this->assert_passes}/{$this->assert_total} assertions",
$this->assert_passes,
$this->assert_total);

echo sprintf("\n%fs\n", $this->exec_time);
}

private function wrap($string, $color) {
if ($this->use_color) {
return self::$colors[$color] . $string . self::$colors['default'];
} else {
return $string;
}
}

private function wrap_by_success($string, $success, $total) {
return $this->wrap($string, $success == $total ? 'green' : 'yellow');
}
}
?>
6 changes: 5 additions & 1 deletion template/run_tests.php
Expand Up @@ -18,6 +18,10 @@
// Add non-abstract subclasses of ztest\TestCase as test-cases to be run
$suite->auto_fill();

// Create a reporter and enable color output
$reporter = new ztest\ConsoleReporter;
$reporter->enable_color();

// Go, go, go
$suite->run(new ztest\ConsoleReporter);
$suite->run($reporter);
?>

0 comments on commit 2bbdd31

Please sign in to comment.