assertions are working

This commit is contained in:
tmont 2009-06-13 22:28:46 +00:00
parent 7742285162
commit b519542d76
9 changed files with 93 additions and 11 deletions

View File

@ -4,8 +4,14 @@
private function __construct() {}
public static function that($expected, $value, $message = '') {
protected static function evaluate(Constraint $constraint, $message) {
if (!$constraint->evaluate()) {
$constraint->fail($message);
}
}
public static function equals($expected, $actual, $message = '') {
self::evaluate(new EqualsConstraint($expected, $actual), $message);
}
}

View File

@ -0,0 +1,29 @@
<?php
abstract class Constraint {
protected $expected;
protected $actual;
public function __construct($expected, $actual) {
$this->expected = $expected;
$this->actual = $actual;
}
public abstract function evaluate();
protected abstract function getFailureMessage();
public function fail($message = '') {
throw new FailedTest($this->toString($message));
}
public final function toString($message) {
$message = !empty($message) ? $message . "\n" : '';
$message .= "Failed asserting that\n" . $this->getFailureMessage();
return $message;
}
}
?>

View File

@ -0,0 +1,15 @@
<?php
class EqualsConstraint extends Constraint {
public function evaluate() {
return $this->expected == $this->actual;
}
protected function getFailureMessage() {
return Util::export($this->actual) . "\nis equal to\n" . Util::export($this->expected);
}
}
?>

View File

@ -26,11 +26,12 @@
private function writeTestMethodResult($text) {
if ($this->currentLineLength >= self::LINE_LENGTH) {
$this->out("\n ");
$this->out("\n");
$this->currentLineLength = 0;
}
$this->out($text);
$This->currentLineLength = 4 + strlen($text);
$this->currentLineLength = strlen($text);
}
public function beforeTestSuite(TestSuite $suite) {
@ -110,7 +111,6 @@
case self::VERBOSITY_HIGH:
$this->out(' ' . $method->getName() . ': ');
break;
case self::VERBOSITY_LOW:
case self::VERBOSITY_MEDIUM:
default:
break;

View File

@ -3,6 +3,6 @@
class TestFailure extends Exception {}
class ErredTest extends FailedTest {}
class IgnoredTest extends FailedTest {}
class FailedTest extends FailedTest {}
class FailedTest extends TestFailure {}
?>

View File

@ -25,7 +25,7 @@
$listener->onTestMethodPassed($this);
}
} catch (TestFailure $failure) {
$this->handleTestFailure($failure);
$this->handleTestFailure($failure, $listeners);
} catch (Exception $e) {
//test for expected exception
foreach ($listeners as $listener) {
@ -57,7 +57,7 @@
throw new InvalidArgumentException('Unknown test failure type: ' . get_class($failure));
}
protected function handleTestFailure(TestFailure $failure) {
protected function handleTestFailure(TestFailure $failure, array $listeners) {
if ($failure instanceof FailedTest) {
foreach ($listeners as $listener) {
$listener->onTestMethodFailed($this);

View File

@ -3,7 +3,7 @@
/**
* Autoload manifest
*
* Autogenerated by manifester.php on 2009-06-13 12:58:13
* Autogenerated by manifester.php on 2009-06-13 15:28:20
*
* @package TUnit
* @version 0.1.0
@ -18,6 +18,8 @@
'CliSwitchCollection' => 'TUnit/util/cli.php',
'CombinedTestResult' => 'TUnit/framework/result/CombinedTestResult.php',
'ConsoleListener' => 'TUnit/framework/listeners/ConsoleListener.php',
'Constraint' => 'TUnit/framework/constraints/Constraint.php',
'EqualsConstraint' => 'TUnit/framework/constraints/EqualsConstraint.php',
'ErredTest' => 'TUnit/framework/result/FailedTest.php',
'ErredTestResult' => 'TUnit/framework/result/ErredTestResult.php',
'FailedTest' => 'TUnit/framework/result/FailedTest.php',
@ -27,13 +29,14 @@
'PassedTestResult' => 'TUnit/framework/result/PassedTestResult.php',
'TestCase' => 'TUnit/framework/test/TestCase.php',
'TestFailure' => 'TUnit/framework/result/FailedTest.php',
'TestListener' => 'TUnit/framework/test/TestListener.php',
'TestListener' => 'TUnit/framework/listeners/TestListener.php',
'TestMethod' => 'TUnit/framework/test/TestMethod.php',
'TestResult' => 'TUnit/framework/result/TestResult.php',
'TestRunner' => 'TUnit/framework/TestRunner.php',
'TestSuite' => 'TUnit/framework/test/TestSuite.php',
'Testable' => 'TUnit/framework/test/Testable.php',
'Usage' => 'TUnit/util/cli.php'
'Usage' => 'TUnit/util/cli.php',
'Util' => 'TUnit/util/Util.php'
);
?>

29
src/TUnit/util/Util.php Normal file
View File

@ -0,0 +1,29 @@
<?php
class Util {
private function __construct() {}
public static function export($var) {
switch (strtolower(gettype($var))) {
case 'object':
return get_class($var);
case 'string':
if (strlen($var) > 47) {
return substr($var, 0, 20) . '...' . substr($var, floor(strlen($var) / 2) - 3, 7) . '...' . substr($var, -20);
}
return $var;
case 'double':
case 'null':
case 'boolean':
case 'integer':
return var_export($var, true);
case 'resource':
return 'resource of type ' . get_resource_type($var);
}
}
}
?>