3 * base include file for SimpleTest
5 * @subpackage UnitTester
6 * @version $Id: expectation.php,v 1.43 2006/02/05 02:04:24 lastcraft Exp $
10 * include other SimpleTest class files
12 require_once(dirname(__FILE__
) . '/dumper.php');
13 require_once(dirname(__FILE__
) . '/compatibility.php');
17 * Assertion that can display failure information.
18 * Also includes various helper methods.
20 * @subpackage UnitTester
23 class SimpleExpectation
{
28 * Creates a dumper for displaying values and sets
30 * @param string $message Customised message on failure.
32 function SimpleExpectation($message = '%s') {
33 $this->_dumper
= &new SimpleDumper();
34 $this->_message
= $message;
38 * Tests the expectation. True if correct.
39 * @param mixed $compare Comparison value.
40 * @return boolean True if correct.
44 function test($compare) {
48 * Returns a human readable test message.
49 * @param mixed $compare Comparison value.
50 * @return string Description of success
55 function testMessage($compare) {
59 * Overlays the generated message onto the stored user
60 * message. An additional message can be interjected.
61 * @param mixed $compare Comparison value.
62 * @return string Description of success
66 function overlayMessage($compare) {
67 return sprintf($this->_message
, $this->testMessage($compare));
71 * Accessor for the dumper.
72 * @return SimpleDumper Current value dumper.
75 function &_getDumper() {
76 return $this->_dumper
;
80 * Test to see if a value is an expectation object.
81 * A useful utility method.
82 * @param mixed $expectation Hopefully an Epectation
84 * @return boolean True if descended from
89 function isExpectation($expectation) {
90 return is_object($expectation) &&
91 SimpleTestCompatibility
::isA($expectation, 'SimpleExpectation');
98 * @subpackage UnitTester
100 class EqualExpectation
extends SimpleExpectation
{
104 * Sets the value to compare against.
105 * @param mixed $value Test value to match.
106 * @param string $message Customised message on failure.
109 function EqualExpectation($value, $message = '%s') {
110 $this->SimpleExpectation($message);
111 $this->_value
= $value;
115 * Tests the expectation. True if it matches the
117 * @param mixed $compare Comparison value.
118 * @return boolean True if correct.
121 function test($compare) {
122 return (($this->_value
== $compare) && ($compare == $this->_value
));
126 * Returns a human readable test message.
127 * @param mixed $compare Comparison value.
128 * @return string Description of success
132 function testMessage($compare) {
133 if ($this->test($compare)) {
134 return "Equal expectation [" . $this->_dumper
->describeValue($this->_value
) . "]";
136 return "Equal expectation fails " .
137 $this->_dumper
->describeDifference($this->_value
, $compare);
142 * Accessor for comparison value.
143 * @return mixed Held value to compare with.
146 function _getValue() {
147 return $this->_value
;
152 * Test for inequality.
153 * @package SimpleTest
154 * @subpackage UnitTester
156 class NotEqualExpectation
extends EqualExpectation
{
159 * Sets the value to compare against.
160 * @param mixed $value Test value to match.
161 * @param string $message Customised message on failure.
164 function NotEqualExpectation($value, $message = '%s') {
165 $this->EqualExpectation($value, $message);
169 * Tests the expectation. True if it differs from the
171 * @param mixed $compare Comparison value.
172 * @return boolean True if correct.
175 function test($compare) {
176 return ! parent
::test($compare);
180 * Returns a human readable test message.
181 * @param mixed $compare Comparison value.
182 * @return string Description of success
186 function testMessage($compare) {
187 $dumper = &$this->_getDumper();
188 if ($this->test($compare)) {
189 return "Not equal expectation passes " .
190 $dumper->describeDifference($this->_getValue(), $compare);
192 return "Not equal expectation fails [" .
193 $dumper->describeValue($this->_getValue()) .
200 * Test for being within a range.
201 * @package SimpleTest
202 * @subpackage UnitTester
204 class WithinMarginExpectation
extends SimpleExpectation
{
209 * Sets the value to compare against and the fuzziness of
210 * the match. Used for comparing floating point values.
211 * @param mixed $value Test value to match.
212 * @param mixed $margin Fuzziness of match.
213 * @param string $message Customised message on failure.
216 function WithinMarginExpectation($value, $margin, $message = '%s') {
217 $this->SimpleExpectation($message);
218 $this->_upper
= $value +
$margin;
219 $this->_lower
= $value - $margin;
223 * Tests the expectation. True if it matches the
225 * @param mixed $compare Comparison value.
226 * @return boolean True if correct.
229 function test($compare) {
230 return (($compare <= $this->_upper
) && ($compare >= $this->_lower
));
234 * Returns a human readable test message.
235 * @param mixed $compare Comparison value.
236 * @return string Description of success
240 function testMessage($compare) {
241 if ($this->test($compare)) {
242 return $this->_withinMessage($compare);
244 return $this->_outsideMessage($compare);
249 * Creates a the message for being within the range.
250 * @param mixed $compare Value being tested.
253 function _withinMessage($compare) {
254 return "Within expectation [" . $this->_dumper
->describeValue($this->_lower
) . "] and [" .
255 $this->_dumper
->describeValue($this->_upper
) . "]";
259 * Creates a the message for being within the range.
260 * @param mixed $compare Value being tested.
263 function _outsideMessage($compare) {
264 if ($compare > $this->_upper
) {
265 return "Outside expectation " .
266 $this->_dumper
->describeDifference($compare, $this->_upper
);
268 return "Outside expectation " .
269 $this->_dumper
->describeDifference($compare, $this->_lower
);
275 * Test for being outside of a range.
276 * @package SimpleTest
277 * @subpackage UnitTester
279 class OutsideMarginExpectation
extends WithinMarginExpectation
{
282 * Sets the value to compare against and the fuzziness of
283 * the match. Used for comparing floating point values.
284 * @param mixed $value Test value to not match.
285 * @param mixed $margin Fuzziness of match.
286 * @param string $message Customised message on failure.
289 function OutsideMarginExpectation($value, $margin, $message = '%s') {
290 $this->WithinMarginExpectation($value, $margin, $message);
294 * Tests the expectation. True if it matches the
296 * @param mixed $compare Comparison value.
297 * @return boolean True if correct.
300 function test($compare) {
301 return ! parent
::test($compare);
305 * Returns a human readable test message.
306 * @param mixed $compare Comparison value.
307 * @return string Description of success
311 function testMessage($compare) {
312 if (! $this->test($compare)) {
313 return $this->_withinMessage($compare);
315 return $this->_outsideMessage($compare);
322 * @package SimpleTest
323 * @subpackage UnitTester
325 class IdenticalExpectation
extends EqualExpectation
{
328 * Sets the value to compare against.
329 * @param mixed $value Test value to match.
330 * @param string $message Customised message on failure.
333 function IdenticalExpectation($value, $message = '%s') {
334 $this->EqualExpectation($value, $message);
338 * Tests the expectation. True if it exactly
339 * matches the held value.
340 * @param mixed $compare Comparison value.
341 * @return boolean True if correct.
344 function test($compare) {
345 return SimpleTestCompatibility
::isIdentical($this->_getValue(), $compare);
349 * Returns a human readable test message.
350 * @param mixed $compare Comparison value.
351 * @return string Description of success
355 function testMessage($compare) {
356 $dumper = &$this->_getDumper();
357 if ($this->test($compare)) {
358 return "Identical expectation [" . $dumper->describeValue($this->_getValue()) . "]";
360 return "Identical expectation [" . $dumper->describeValue($this->_getValue()) .
362 $dumper->describeValue($compare) . "] " .
363 $dumper->describeDifference($this->_getValue(), $compare, TYPE_MATTERS
);
369 * Test for non-identity.
370 * @package SimpleTest
371 * @subpackage UnitTester
373 class NotIdenticalExpectation
extends IdenticalExpectation
{
376 * Sets the value to compare against.
377 * @param mixed $value Test value to match.
378 * @param string $message Customised message on failure.
381 function NotIdenticalExpectation($value, $message = '%s') {
382 $this->IdenticalExpectation($value, $message);
386 * Tests the expectation. True if it differs from the
388 * @param mixed $compare Comparison value.
389 * @return boolean True if correct.
392 function test($compare) {
393 return ! parent
::test($compare);
397 * Returns a human readable test message.
398 * @param mixed $compare Comparison value.
399 * @return string Description of success
403 function testMessage($compare) {
404 $dumper = &$this->_getDumper();
405 if ($this->test($compare)) {
406 return "Not identical expectation passes " .
407 $dumper->describeDifference($this->_getValue(), $compare, TYPE_MATTERS
);
409 return "Not identical expectation [" . $dumper->describeValue($this->_getValue()) . "] matches";
415 * Test for a pattern using Perl regex rules.
416 * @package SimpleTest
417 * @subpackage UnitTester
419 class PatternExpectation
extends SimpleExpectation
{
423 * Sets the value to compare against.
424 * @param string $pattern Pattern to search for.
425 * @param string $message Customised message on failure.
428 function PatternExpectation($pattern, $message = '%s') {
429 $this->SimpleExpectation($message);
430 $this->_pattern
= $pattern;
434 * Accessor for the pattern.
435 * @return string Perl regex as string.
438 function _getPattern() {
439 return $this->_pattern
;
443 * Tests the expectation. True if the Perl regex
444 * matches the comparison value.
445 * @param string $compare Comparison value.
446 * @return boolean True if correct.
449 function test($compare) {
450 return (boolean
)preg_match($this->_getPattern(), $compare);
454 * Returns a human readable test message.
455 * @param mixed $compare Comparison value.
456 * @return string Description of success
460 function testMessage($compare) {
461 if ($this->test($compare)) {
462 return $this->_describePatternMatch($this->_getPattern(), $compare);
464 $dumper = &$this->_getDumper();
465 return "Pattern [" . $this->_getPattern() .
466 "] not detected in [" .
467 $dumper->describeValue($compare) . "]";
472 * Describes a pattern match including the string
473 * found and it's position.
474 * @package SimpleTest
475 * @subpackage UnitTester
476 * @param string $pattern Regex to match against.
477 * @param string $subject Subject to search.
480 function _describePatternMatch($pattern, $subject) {
481 preg_match($pattern, $subject, $matches);
482 $position = strpos($subject, $matches[0]);
483 $dumper = &$this->_getDumper();
484 return "Pattern [$pattern] detected at character [$position] in [" .
485 $dumper->describeValue($subject) . "] as [" .
486 $matches[0] . "] in region [" .
487 $dumper->clipString($subject, 100, $position) . "]";
494 class WantedPatternExpectation
extends PatternExpectation
{
498 * Fail if a pattern is detected within the
500 * @package SimpleTest
501 * @subpackage UnitTester
503 class NoPatternExpectation
extends PatternExpectation
{
506 * Sets the reject pattern
507 * @param string $pattern Pattern to search for.
508 * @param string $message Customised message on failure.
511 function NoPatternExpectation($pattern, $message = '%s') {
512 $this->PatternExpectation($pattern, $message);
516 * Tests the expectation. False if the Perl regex
517 * matches the comparison value.
518 * @param string $compare Comparison value.
519 * @return boolean True if correct.
522 function test($compare) {
523 return ! parent
::test($compare);
527 * Returns a human readable test message.
528 * @param string $compare Comparison value.
529 * @return string Description of success
533 function testMessage($compare) {
534 if ($this->test($compare)) {
535 $dumper = &$this->_getDumper();
536 return "Pattern [" . $this->_getPattern() .
537 "] not detected in [" .
538 $dumper->describeValue($compare) . "]";
540 return $this->_describePatternMatch($this->_getPattern(), $compare);
546 * @package SimpleTest
547 * @subpackage UnitTester
550 class UnwantedPatternExpectation
extends NoPatternExpectation
{
554 * Tests either type or class name if it's an object.
555 * @package SimpleTest
556 * @subpackage UnitTester
558 class IsAExpectation
extends SimpleExpectation
{
562 * Sets the type to compare with.
563 * @param string $type Type or class name.
564 * @param string $message Customised message on failure.
567 function IsAExpectation($type, $message = '%s') {
568 $this->SimpleExpectation($message);
569 $this->_type
= $type;
573 * Accessor for type to check against.
574 * @return string Type or class name.
577 function _getType() {
582 * Tests the expectation. True if the type or
583 * class matches the string value.
584 * @param string $compare Comparison value.
585 * @return boolean True if correct.
588 function test($compare) {
589 if (is_object($compare)) {
590 return SimpleTestCompatibility
::isA($compare, $this->_type
);
592 return (strtolower(gettype($compare)) == $this->_canonicalType($this->_type
));
597 * Coerces type name into a gettype() match.
598 * @param string $type User type.
599 * @return string Simpler type.
602 function _canonicalType($type) {
603 $type = strtolower($type);
609 if (isset($map[$type])) {
616 * Returns a human readable test message.
617 * @param mixed $compare Comparison value.
618 * @return string Description of success
622 function testMessage($compare) {
623 $dumper = &$this->_getDumper();
624 return "Value [" . $dumper->describeValue($compare) .
625 "] should be type [" . $this->_type
. "]";
630 * Tests either type or class name if it's an object.
631 * Will succeed if the type does not match.
632 * @package SimpleTest
633 * @subpackage UnitTester
635 class NotAExpectation
extends IsAExpectation
{
639 * Sets the type to compare with.
640 * @param string $type Type or class name.
641 * @param string $message Customised message on failure.
644 function NotAExpectation($type, $message = '%s') {
645 $this->IsAExpectation($type, $message);
649 * Tests the expectation. False if the type or
650 * class matches the string value.
651 * @param string $compare Comparison value.
652 * @return boolean True if different.
655 function test($compare) {
656 return ! parent
::test($compare);
660 * Returns a human readable test message.
661 * @param mixed $compare Comparison value.
662 * @return string Description of success
666 function testMessage($compare) {
667 $dumper = &$this->_getDumper();
668 return "Value [" . $dumper->describeValue($compare) .
669 "] should not be type [" . $this->_getType() . "]";
674 * Tests for existance of a method in an object
675 * @package SimpleTest
676 * @subpackage UnitTester
678 class MethodExistsExpectation
extends SimpleExpectation
{
682 * Sets the value to compare against.
683 * @param string $method Method to check.
684 * @param string $message Customised message on failure.
688 function MethodExistsExpectation($method, $message = '%s') {
689 $this->SimpleExpectation($message);
690 $this->_method
= &$method;
694 * Tests the expectation. True if the method exists in the test object.
695 * @param string $compare Comparison method name.
696 * @return boolean True if correct.
699 function test($compare) {
700 return (boolean
)(is_object($compare) && method_exists($compare, $this->_method
));
704 * Returns a human readable test message.
705 * @param mixed $compare Comparison value.
706 * @return string Description of success
710 function testMessage($compare) {
711 $dumper = &$this->_getDumper();
712 if (! is_object($compare)) {
713 return 'No method on non-object [' . $dumper->describeValue($compare) . ']';
715 $method = $this->_method
;
716 return "Object [" . $dumper->describeValue($compare) .
717 "] should contain method [$method]";