rearranged constraint hierarchy and added some simple constraints

This commit is contained in:
tmont 2009-06-14 03:03:46 +00:00
parent 6b078208b2
commit ca2237671a
8 changed files with 154 additions and 21 deletions

View File

@ -22,6 +22,46 @@
self::evaluate(self::negate(new EqualConstraint($expected, $actual)), $message);
}
public static function identical($expected, $actual, $message = '') {
self::evaluate(new IdenticalConstraint($expected, $actual), $message);
}
public static function notIdentical($expected, $actual, $message = '') {
self::evaluate(self::negate(new IdenticalConstraint($expected, $actual)), $message);
}
public static function isTrue($value, $message = '') {
self::evaluate(new TrueConstraint($value), $message);
}
public static function isFalse($value, $message = '') {
self::evaluate(new FalseConstraint($value), $message);
}
public static function set($value, $message = '') {
self::evaluate(new IssetConstraint($value), $message);
}
public static function notSet($value, $message = '') {
self::evaluate(self::negate(new IssetConstraint($value)), $message);
}
public static function isEmpty($value, $message = '') {
self::evaluate(new EmptyConstraint($value), $message);
}
public static function isNotEmpty($value, $message = '') {
self::evaluate(self::negate(new EmptyConstraint($value)), $message);
}
public static function isNull($value, $message = '') {
self::evaluate(new NullConstraint($value), $message);
}
public static function isNotNull($value, $message = '') {
self::evaluate(self::negate(new NullConstraint($value)), $message);
}
}
?>

View File

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

View File

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

View File

@ -7,7 +7,7 @@
}
protected function getFailureMessage() {
return Util::export($this->actual) . "\nis equal to\n" . Util::export($this->expected);
return Util::export($this->actual) . ' is equal to ' . Util::export($this->expected);
}
}

View File

@ -0,0 +1,15 @@
<?php
class IdenticalConstraint extends DefaultConstraint {
public function evaluate() {
return $this->expected === $this->actual;
}
protected function getFailureMessage() {
return Util::export($this->actual) . ' is identical to ' . Util::export($this->expected);
}
}
?>

View File

@ -1,6 +1,6 @@
<?php
class NotConstraint implements Constraint {
class NotConstraint extends Constraint {
protected $constraint;
@ -17,7 +17,7 @@
}
public function toString($message) {
return $this->negateString($constraint->toString($message));
return $this->negateString($this->constraint->toString($message));
}
protected function negateString($string) {

View File

@ -0,0 +1,73 @@
<?php
abstract class SimpleConstraint extends Constraint {
protected $value;
public function __construct($value) {
$this->value = $value;
}
}
class IssetConstraint extends SimpleConstraint {
public function evaluate() {
return isset($this->value);
}
protected function getFailureMessage() {
return Util::export($this->value) . ' is set';
}
}
class EmptyConstraint extends SimpleConstraint {
public function evaluate() {
return empty($this->value);
}
protected function getFailureMessage() {
return Util::export($this->value) . ' is empty';
}
}
class NullConstraint extends SimpleConstraint {
public function evaluate() {
return $this->value === null;
}
protected function getFailureMessage() {
return Util::export($this->value) . ' is null';
}
}
class TrueConstraint extends SimpleConstraint {
public function evaluate() {
return $this->value === true;
}
protected function getFailureMessage() {
return Util::export($this->value) . ' is true';
}
}
class FalseConstraint extends SimpleConstraint {
public function evaluate() {
return $this->value === false;
}
protected function getFailureMessage() {
return Util::export($this->value) . ' is false';
}
}
?>

View File

@ -3,7 +3,7 @@
/**
* Autoload manifest
*
* Autogenerated by manifester.php on 2009-06-13 17:44:11
* Autogenerated by manifester.php on 2009-06-13 19:56:43
*
* @package TUnit
* @version 0.1.0
@ -20,17 +20,23 @@
'ConsoleListener' => 'TUnit/framework/listeners/ConsoleListener.php',
'Constraint' => 'TUnit/framework/constraints/Constraint.php',
'DefaultConstraint' => 'TUnit/framework/constraints/DefaultConstraint.php',
'EmptyConstraint' => 'TUnit/framework/constraints/SimpleConstraints.php',
'EqualConstraint' => 'TUnit/framework/constraints/EqualConstraint.php',
'ErredTest' => 'TUnit/framework/result/FailedTest.php',
'ErredTestResult' => 'TUnit/framework/result/ErredTestResult.php',
'FailedTest' => 'TUnit/framework/result/FailedTest.php',
'FailedTestResult' => 'TUnit/framework/result/FailedTestResult.php',
'FalseConstraint' => 'TUnit/framework/constraints/SimpleConstraints.php',
'IdenticalConstraint' => 'TUnit/framework/constraints/IdenticalConstraint.php',
'IgnoredTest' => 'TUnit/framework/result/FailedTest.php',
'IgnoredTestResult' => 'TUnit/framework/result/IgnoredTestResult.php',
'IssetConstraint' => 'TUnit/framework/constraints/SimpleConstraints.php',
'NotConstraint' => 'TUnit/framework/constraints/NotConstraint.php',
'NullConstraint' => 'TUnit/framework/constraints/SimpleConstraints.php',
'PassedTestResult' => 'TUnit/framework/result/PassedTestResult.php',
'Product' => 'TUnit/util/Product.php',
'RecursiveTestIterator' => 'TUnit/util/RecursiveTestIterator.php',
'SimpleConstraint' => 'TUnit/framework/constraints/SimpleConstraints.php',
'SingleTestResult' => 'TUnit/framework/result/SingleTestResult.php',
'TestCase' => 'TUnit/framework/test/TestCase.php',
'TestFailure' => 'TUnit/framework/result/FailedTest.php',
@ -40,6 +46,7 @@
'TestRunner' => 'TUnit/framework/TestRunner.php',
'TestSuite' => 'TUnit/framework/test/TestSuite.php',
'Testable' => 'TUnit/framework/test/Testable.php',
'TrueConstraint' => 'TUnit/framework/constraints/SimpleConstraints.php',
'Usage' => 'TUnit/util/cli.php',
'Util' => 'TUnit/util/Util.php'
);