3 * base include file for SimpleTest
5 * @subpackage UnitTester
6 * @version $Id: expectation.php,v 1.31 2004/08/10 00:05:26 lastcraft Exp $
10 * include other SimpleTest class files
12 require_once(dirname(__FILE__
) . '/dumper.php');
13 require_once(dirname(__FILE__
) . '/options.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
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
;
83 * @subpackage UnitTester
85 class EqualExpectation
extends SimpleExpectation
{
89 * Sets the value to compare against.
90 * @param mixed $value Test value to match.
91 * @param string $message Customised message on failure.
94 function EqualExpectation($value, $message = '%s') {
95 $this->SimpleExpectation($message);
96 $this->_value
= $value;
100 * Tests the expectation. True if it matches the
102 * @param mixed $compare Comparison value.
103 * @return boolean True if correct.
106 function test($compare) {
107 return (($this->_value
== $compare) && ($compare == $this->_value
));
111 * Returns a human readable test message.
112 * @param mixed $compare Comparison value.
113 * @return string Description of success
117 function testMessage($compare) {
118 if ($this->test($compare)) {
119 return "Equal expectation [" . $this->_dumper
->describeValue($this->_value
) . "]";
121 return "Equal expectation fails " .
122 $this->_dumper
->describeDifference($this->_value
, $compare);
127 * Accessor for comparison value.
128 * @return mixed Held value to compare with.
131 function _getValue() {
132 return $this->_value
;
137 * Test for inequality.
138 * @package SimpleTest
139 * @subpackage UnitTester
141 class NotEqualExpectation
extends EqualExpectation
{
144 * Sets the value to compare against.
145 * @param mixed $value Test value to match.
146 * @param string $message Customised message on failure.
149 function NotEqualExpectation($value, $message = '%s') {
150 $this->EqualExpectation($value, $message);
154 * Tests the expectation. True if it differs from the
156 * @param mixed $compare Comparison value.
157 * @return boolean True if correct.
160 function test($compare) {
161 return ! parent
::test($compare);
165 * Returns a human readable test message.
166 * @param mixed $compare Comparison value.
167 * @return string Description of success
171 function testMessage($compare) {
172 $dumper = &$this->_getDumper();
173 if ($this->test($compare)) {
174 return "Not equal expectation passes " .
175 $dumper->describeDifference($this->_getValue(), $compare);
177 return "Not equal expectation fails [" .
178 $dumper->describeValue($this->_getValue()) .
186 * @package SimpleTest
187 * @subpackage UnitTester
189 class IdenticalExpectation
extends EqualExpectation
{
192 * Sets the value to compare against.
193 * @param mixed $value Test value to match.
194 * @param string $message Customised message on failure.
197 function IdenticalExpectation($value, $message = '%s') {
198 $this->EqualExpectation($value, $message);
202 * Tests the expectation. True if it exactly
203 * matches the held value.
204 * @param mixed $compare Comparison value.
205 * @return boolean True if correct.
208 function test($compare) {
209 return SimpleTestCompatibility
::isIdentical($this->_getValue(), $compare);
213 * Returns a human readable test message.
214 * @param mixed $compare Comparison value.
215 * @return string Description of success
219 function testMessage($compare) {
220 $dumper = &$this->_getDumper();
221 if ($this->test($compare)) {
222 return "Identical expectation [" . $dumper->describeValue($this->_getValue()) . "]";
224 return "Identical expectation [" . $dumper->describeValue($this->_getValue()) .
226 $this->_dumper
->describeValue($compare) . "] " .
227 $this->_dumper
->describeDifference(
236 * Test for non-identity.
237 * @package SimpleTest
238 * @subpackage UnitTester
240 class NotIdenticalExpectation
extends IdenticalExpectation
{
243 * Sets the value to compare against.
244 * @param mixed $value Test value to match.
245 * @param string $message Customised message on failure.
248 function NotIdenticalExpectation($value, $message = '%s') {
249 $this->IdenticalExpectation($value, $message);
253 * Tests the expectation. True if it differs from the
255 * @param mixed $compare Comparison value.
256 * @return boolean True if correct.
259 function test($compare) {
260 return ! parent
::test($compare);
264 * Returns a human readable test message.
265 * @param mixed $compare Comparison value.
266 * @return string Description of success
270 function testMessage($compare) {
271 $dumper = &$this->_getDumper();
272 if ($this->test($compare)) {
273 return "Not identical expectation passes " .
274 $dumper->describeDifference($this->_getValue(), $compare, TYPE_MATTERS
);
276 return "Not identical expectation [" . $dumper->describeValue($this->_getValue()) . "] matches";
282 * Test for a pattern using Perl regex rules.
283 * @package SimpleTest
284 * @subpackage UnitTester
286 class WantedPatternExpectation
extends SimpleExpectation
{
290 * Sets the value to compare against.
291 * @param string $pattern Pattern to search for.
292 * @param string $message Customised message on failure.
295 function WantedPatternExpectation($pattern, $message = '%s') {
296 $this->SimpleExpectation($message);
297 $this->_pattern
= $pattern;
301 * Accessor for the pattern.
302 * @return string Perl regex as string.
305 function _getPattern() {
306 return $this->_pattern
;
310 * Tests the expectation. True if the Perl regex
311 * matches the comparison value.
312 * @param string $compare Comparison value.
313 * @return boolean True if correct.
316 function test($compare) {
317 return (boolean
)preg_match($this->_getPattern(), $compare);
321 * Returns a human readable test message.
322 * @param mixed $compare Comparison value.
323 * @return string Description of success
327 function testMessage($compare) {
328 if ($this->test($compare)) {
329 return $this->_decribePatternMatch($this->_getPattern(), $compare);
331 $dumper = &$this->_getDumper();
332 return "Pattern [" . $this->_getPattern() .
333 "] not detected in [" .
334 $dumper->describeValue($compare) . "]";
339 * Describes a pattern match including the string
340 * found and it's position.
341 * @param string $pattern Regex to match against.
342 * @param string $subject Subject to search.
345 function _decribePatternMatch($pattern, $subject) {
346 preg_match($pattern, $subject, $matches);
347 $position = strpos($subject, $matches[0]);
348 $dumper = &$this->_getDumper();
349 return "Pattern [$pattern] detected at [$position] in [" .
350 $dumper->describeValue($subject) . "] as [" .
351 $matches[0] . "] in region [" .
352 $dumper->clipString($subject, 40, $position) . "]";
357 * Fail if a pattern is detected within the
359 * @package SimpleTest
360 * @subpackage UnitTester
362 class UnwantedPatternExpectation
extends WantedPatternExpectation
{
365 * Sets the reject pattern
366 * @param string $pattern Pattern to search for.
367 * @param string $message Customised message on failure.
370 function UnwantedPatternExpectation($pattern, $message = '%s') {
371 $this->WantedPatternExpectation($pattern, $message);
375 * Tests the expectation. False if the Perl regex
376 * matches the comparison value.
377 * @param string $compare Comparison value.
378 * @return boolean True if correct.
381 function test($compare) {
382 return ! parent
::test($compare);
386 * Returns a human readable test message.
387 * @param string $compare Comparison value.
388 * @return string Description of success
392 function testMessage($compare) {
393 if ($this->test($compare)) {
394 $dumper = &$this->_getDumper();
395 return "Pattern [" . $this->_getPattern() .
396 "] not detected in [" .
397 $dumper->describeValue($compare) . "]";
399 return $this->_decribePatternMatch($this->_getPattern(), $compare);
405 * Tests either type or class name if it's an object.
406 * @package SimpleTest
407 * @subpackage UnitTester
409 class IsAExpectation
extends SimpleExpectation
{
413 * Sets the type to compare with.
414 * @param string $type Type or class name.
415 * @param string $message Customised message on failure.
418 function IsAExpectation($type, $message = '%s') {
419 $this->SimpleExpectation($message);
420 $this->_type
= $type;
424 * Accessor for type to check against.
425 * @return string Type or class name.
433 * Tests the expectation. True if the type or
434 * class matches the string value.
435 * @param string $compare Comparison value.
436 * @return boolean True if correct.
439 function test($compare) {
440 if (is_object($compare)) {
441 return SimpleTestCompatibility
::isA($compare, $this->_type
);
443 return (strtolower(gettype($compare)) == $this->_canonicalType($this->_type
));
448 * Coerces type name into a gettype() match.
449 * @param string $type User type.
450 * @return string Simpler type.
453 function _canonicalType($type) {
454 $type = strtolower($type);
460 if (isset($map[$type])) {
467 * Returns a human readable test message.
468 * @param mixed $compare Comparison value.
469 * @return string Description of success
473 function testMessage($compare) {
474 $dumper = &$this->_getDumper();
475 return "Value [" . $dumper->describeValue($compare) .
476 "] should be type [" . $this->_type
. "]";
481 * Tests either type or class name if it's an object.
482 * Will succeed if the type does not match.
483 * @package SimpleTest
484 * @subpackage UnitTester
486 class NotAExpectation
extends IsAExpectation
{
490 * Sets the type to compare with.
491 * @param string $type Type or class name.
492 * @param string $message Customised message on failure.
495 function NotAExpectation($type, $message = '%s') {
496 $this->IsAExpectation($type, $message);
500 * Tests the expectation. False if the type or
501 * class matches the string value.
502 * @param string $compare Comparison value.
503 * @return boolean True if different.
506 function test($compare) {
507 return ! parent
::test($compare);
511 * Returns a human readable test message.
512 * @param mixed $compare Comparison value.
513 * @return string Description of success
517 function testMessage($compare) {
518 $dumper = &$this->_getDumper();
519 return "Value [" . $dumper->describeValue($compare) .
520 "] should not be type [" . $this->_getType() . "]";
525 * Tests for existance of a method in an object
526 * @package SimpleTest
527 * @subpackage UnitTester
529 class MethodExistsExpectation
extends SimpleExpectation
{
533 * Sets the value to compare against.
534 * @param string $method Method to check.
535 * @param string $message Customised message on failure.
539 function MethodExistsExpectation($method, $message = '%s') {
540 $this->SimpleExpectation($message);
541 $this->_method
= &$method;
545 * Tests the expectation. True if the method exists in the test object.
546 * @param string $compare Comparison method name.
547 * @return boolean True if correct.
550 function test($compare) {
551 return (boolean
)(is_object($compare) && method_exists($compare, $this->_method
));
555 * Returns a human readable test message.
556 * @param mixed $compare Comparison value.
557 * @return string Description of success
561 function testMessage($compare) {
562 $dumper = &$this->_getDumper();
563 if (! is_object($compare)) {
564 return 'No method on non-object [' . $dumper->describeValue($compare) . ']';
566 $method = $this->_method
;
567 return "Object [" . $dumper->describeValue($compare) .
568 "] should contain method [$method]";