Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/simpletestlib/expectation.php
Go to the documentation of this file.
00001 <?php
00012 require_once(dirname(__FILE__) . '/dumper.php');
00013 require_once(dirname(__FILE__) . '/compatibility.php');
00023 class SimpleExpectation {
00024     var $_dumper = false;
00025     var $_message;
00026 
00032     function SimpleExpectation($message = '%s') {
00033         $this->_message = $message;
00034     }
00035 
00043     function test($compare) {
00044     }
00045 
00054     function testMessage($compare) {
00055     }
00056 
00066     function overlayMessage($compare, $dumper) {
00067         $this->_dumper = $dumper;
00068         return sprintf($this->_message, $this->testMessage($compare));
00069     }
00070 
00076     function &_getDumper() {
00077         if (! $this->_dumper) {
00078             $dumper = new SimpleDumper();
00079             return $dumper;
00080         }
00081         return $this->_dumper;
00082     }
00083 
00094     function isExpectation($expectation) {
00095         return is_object($expectation) &&
00096                 SimpleTestCompatibility::isA($expectation, 'SimpleExpectation');
00097     }
00098 }
00099 
00105 class AnythingExpectation extends SimpleExpectation {
00106 
00113     function test($compare) {
00114         return true;
00115     }
00116 
00124     function testMessage($compare) {
00125         $dumper = &$this->_getDumper();
00126         return 'Anything always matches [' . $dumper->describeValue($compare) . ']';
00127     }
00128 }
00129 
00135 class FailedExpectation extends SimpleExpectation {
00136 
00143     function test($compare) {
00144         return false;
00145     }
00146 
00153     function testMessage($compare) {
00154         $dumper = &$this->_getDumper();
00155         return 'Failed expectation never matches [' . $dumper->describeValue($compare) . ']';
00156     }
00157 }
00158 
00164 class TrueExpectation extends SimpleExpectation {
00165 
00172     function test($compare) {
00173         return (boolean)$compare;
00174     }
00175 
00183     function testMessage($compare) {
00184         $dumper = &$this->_getDumper();
00185         return 'Expected true, got [' . $dumper->describeValue($compare) . ']';
00186     }
00187 }
00188 
00194 class FalseExpectation extends SimpleExpectation {
00195 
00202     function test($compare) {
00203         return ! (boolean)$compare;
00204     }
00205 
00213     function testMessage($compare) {
00214         $dumper = &$this->_getDumper();
00215         return 'Expected false, got [' . $dumper->describeValue($compare) . ']';
00216     }
00217 }
00218 
00224 class EqualExpectation extends SimpleExpectation {
00225     var $_value;
00226 
00233     function EqualExpectation($value, $message = '%s') {
00234         $this->SimpleExpectation($message);
00235         $this->_value = $value;
00236     }
00237 
00245     function test($compare) {
00246         return (($this->_value == $compare) && ($compare == $this->_value));
00247     }
00248 
00256     function testMessage($compare) {
00257         if ($this->test($compare)) {
00258             return "Equal expectation [" . $this->_dumper->describeValue($this->_value) . "]";
00259         } else {
00260             return "Equal expectation fails " .
00261                     $this->_dumper->describeDifference($this->_value, $compare);
00262         }
00263     }
00264 
00270     function _getValue() {
00271         return $this->_value;
00272     }
00273 }
00274 
00280 class NotEqualExpectation extends EqualExpectation {
00281 
00288     function NotEqualExpectation($value, $message = '%s') {
00289         $this->EqualExpectation($value, $message);
00290     }
00291 
00299     function test($compare) {
00300         return ! parent::test($compare);
00301     }
00302 
00310     function testMessage($compare) {
00311         $dumper = &$this->_getDumper();
00312         if ($this->test($compare)) {
00313             return "Not equal expectation passes " .
00314                     $dumper->describeDifference($this->_getValue(), $compare);
00315         } else {
00316             return "Not equal expectation fails [" .
00317                     $dumper->describeValue($this->_getValue()) .
00318                     "] matches";
00319         }
00320     }
00321 }
00322 
00328 class WithinMarginExpectation extends SimpleExpectation {
00329     var $_upper;
00330     var $_lower;
00331 
00340     function WithinMarginExpectation($value, $margin, $message = '%s') {
00341         $this->SimpleExpectation($message);
00342         $this->_upper = $value + $margin;
00343         $this->_lower = $value - $margin;
00344     }
00345 
00353     function test($compare) {
00354         return (($compare <= $this->_upper) && ($compare >= $this->_lower));
00355     }
00356 
00364     function testMessage($compare) {
00365         if ($this->test($compare)) {
00366             return $this->_withinMessage($compare);
00367         } else {
00368             return $this->_outsideMessage($compare);
00369         }
00370     }
00371 
00377     function _withinMessage($compare) {
00378         return "Within expectation [" . $this->_dumper->describeValue($this->_lower) . "] and [" .
00379                 $this->_dumper->describeValue($this->_upper) . "]";
00380     }
00381 
00387     function _outsideMessage($compare) {
00388         if ($compare > $this->_upper) {
00389             return "Outside expectation " .
00390                     $this->_dumper->describeDifference($compare, $this->_upper);
00391         } else {
00392             return "Outside expectation " .
00393                     $this->_dumper->describeDifference($compare, $this->_lower);
00394         }
00395     }
00396 }
00397 
00403 class OutsideMarginExpectation extends WithinMarginExpectation {
00404 
00413     function OutsideMarginExpectation($value, $margin, $message = '%s') {
00414         $this->WithinMarginExpectation($value, $margin, $message);
00415     }
00416 
00424     function test($compare) {
00425         return ! parent::test($compare);
00426     }
00427 
00435     function testMessage($compare) {
00436         if (! $this->test($compare)) {
00437             return $this->_withinMessage($compare);
00438         } else {
00439             return $this->_outsideMessage($compare);
00440         }
00441     }
00442 }
00443 
00449 class ReferenceExpectation extends SimpleExpectation {
00450     var $_value;
00451 
00458     function ReferenceExpectation(&$value, $message = '%s') {
00459         $this->SimpleExpectation($message);
00460         $this->_value =& $value;
00461     }
00462 
00470     function test(&$compare) {
00471         return SimpleTestCompatibility::isReference($this->_value, $compare);
00472     }
00473 
00481     function testMessage($compare) {
00482         if ($this->test($compare)) {
00483             return "Reference expectation [" . $this->_dumper->describeValue($this->_value) . "]";
00484         } else {
00485             return "Reference expectation fails " .
00486                     $this->_dumper->describeDifference($this->_value, $compare);
00487         }
00488     }
00489 
00490     function _getValue() {
00491         return $this->_value;
00492     }
00493 }
00494 
00500 class IdenticalExpectation extends EqualExpectation {
00501 
00508     function IdenticalExpectation($value, $message = '%s') {
00509         $this->EqualExpectation($value, $message);
00510     }
00511 
00519     function test($compare) {
00520         return SimpleTestCompatibility::isIdentical($this->_getValue(), $compare);
00521     }
00522 
00530     function testMessage($compare) {
00531         $dumper = &$this->_getDumper();
00532         if ($this->test($compare)) {
00533             return "Identical expectation [" . $dumper->describeValue($this->_getValue()) . "]";
00534         } else {
00535             return "Identical expectation [" . $dumper->describeValue($this->_getValue()) .
00536                     "] fails with [" .
00537                     $dumper->describeValue($compare) . "] " .
00538                     $dumper->describeDifference($this->_getValue(), $compare, TYPE_MATTERS);
00539         }
00540     }
00541 }
00542 
00548 class NotIdenticalExpectation extends IdenticalExpectation {
00549 
00556     function NotIdenticalExpectation($value, $message = '%s') {
00557         $this->IdenticalExpectation($value, $message);
00558     }
00559 
00567     function test($compare) {
00568         return ! parent::test($compare);
00569     }
00570 
00578     function testMessage($compare) {
00579         $dumper = &$this->_getDumper();
00580         if ($this->test($compare)) {
00581             return "Not identical expectation passes " .
00582                     $dumper->describeDifference($this->_getValue(), $compare, TYPE_MATTERS);
00583         } else {
00584             return "Not identical expectation [" . $dumper->describeValue($this->_getValue()) . "] matches";
00585         }
00586     }
00587 }
00588 
00594 class PatternExpectation extends SimpleExpectation {
00595     var $_pattern;
00596 
00603     function PatternExpectation($pattern, $message = '%s') {
00604         $this->SimpleExpectation($message);
00605         $this->_pattern = $pattern;
00606     }
00607 
00613     function _getPattern() {
00614         return $this->_pattern;
00615     }
00616 
00624     function test($compare) {
00625         return (boolean)preg_match($this->_getPattern(), $compare);
00626     }
00627 
00635     function testMessage($compare) {
00636         if ($this->test($compare)) {
00637             return $this->_describePatternMatch($this->_getPattern(), $compare);
00638         } else {
00639             $dumper = &$this->_getDumper();
00640             return "Pattern [" . $this->_getPattern() .
00641                     "] not detected in [" .
00642                     $dumper->describeValue($compare) . "]";
00643         }
00644     }
00645 
00653     function _describePatternMatch($pattern, $subject) {
00654         preg_match($pattern, $subject, $matches);
00655         $position = strpos($subject, $matches[0]);
00656         $dumper = $this->_getDumper();
00657         return "Pattern [$pattern] detected at character [$position] in [" .
00658                 $dumper->describeValue($subject) . "] as [" .
00659                 $matches[0] . "] in region [" .
00660                 $dumper->clipString($subject, 100, $position) . "]";
00661     }
00662 }
00663 
00669 class WantedPatternExpectation extends PatternExpectation {
00670 }
00671 
00678 class NoPatternExpectation extends PatternExpectation {
00679 
00686     function NoPatternExpectation($pattern, $message = '%s') {
00687         $this->PatternExpectation($pattern, $message);
00688     }
00689 
00697     function test($compare) {
00698         return ! parent::test($compare);
00699     }
00700 
00708     function testMessage($compare) {
00709         if ($this->test($compare)) {
00710             $dumper = &$this->_getDumper();
00711             return "Pattern [" . $this->_getPattern() .
00712                     "] not detected in [" .
00713                     $dumper->describeValue($compare) . "]";
00714         } else {
00715             return $this->_describePatternMatch($this->_getPattern(), $compare);
00716         }
00717     }
00718 }
00719 
00725 class UnwantedPatternExpectation extends NoPatternExpectation {
00726 }
00727 
00733 class IsAExpectation extends SimpleExpectation {
00734     var $_type;
00735 
00742     function IsAExpectation($type, $message = '%s') {
00743         $this->SimpleExpectation($message);
00744         $this->_type = $type;
00745     }
00746 
00752     function _getType() {
00753         return $this->_type;
00754     }
00755 
00763     function test($compare) {
00764         if (is_object($compare)) {
00765             return SimpleTestCompatibility::isA($compare, $this->_type);
00766         } else {
00767             return (strtolower(gettype($compare)) == $this->_canonicalType($this->_type));
00768         }
00769     }
00770 
00777     function _canonicalType($type) {
00778         $type = strtolower($type);
00779         $map = array(
00780                 'bool' => 'boolean',
00781                 'float' => 'double',
00782                 'real' => 'double',
00783                 'int' => 'integer');
00784         if (isset($map[$type])) {
00785             $type = $map[$type];
00786         }
00787         return $type;
00788     }
00789 
00797     function testMessage($compare) {
00798         $dumper = &$this->_getDumper();
00799         return "Value [" . $dumper->describeValue($compare) .
00800                 "] should be type [" . $this->_type . "]";
00801     }
00802 }
00803 
00810 class NotAExpectation extends IsAExpectation {
00811     var $_type;
00812 
00819     function NotAExpectation($type, $message = '%s') {
00820         $this->IsAExpectation($type, $message);
00821     }
00822 
00830     function test($compare) {
00831         return ! parent::test($compare);
00832     }
00833 
00841     function testMessage($compare) {
00842         $dumper = &$this->_getDumper();
00843         return "Value [" . $dumper->describeValue($compare) .
00844                 "] should not be type [" . $this->_getType() . "]";
00845     }
00846 }
00847 
00853 class MethodExistsExpectation extends SimpleExpectation {
00854     var $_method;
00855 
00863     function MethodExistsExpectation($method, $message = '%s') {
00864         $this->SimpleExpectation($message);
00865         $this->_method = &$method;
00866     }
00867 
00874     function test($compare) {
00875         return (boolean)(is_object($compare) && method_exists($compare, $this->_method));
00876     }
00877 
00885     function testMessage($compare) {
00886         $dumper = &$this->_getDumper();
00887         if (! is_object($compare)) {
00888             return 'No method on non-object [' . $dumper->describeValue($compare) . ']';
00889         }
00890         $method = $this->_method;
00891         return "Object [" . $dumper->describeValue($compare) .
00892                 "] should contain method [$method]";
00893     }
00894 }
00895 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations