Fixing content type ordering when content_type is not defined.
[akelos.git] / vendor / simpletest / expectation.php
blob29a87e177c99505d8612da620fb2729b9b7707b1
1 <?php
2 /**
3 * base include file for SimpleTest
4 * @package SimpleTest
5 * @subpackage UnitTester
6 * @version $Id: expectation.php,v 1.43 2006/02/05 02:04:24 lastcraft Exp $
7 */
9 /**#@+
10 * include other SimpleTest class files
12 require_once(dirname(__FILE__) . '/dumper.php');
13 require_once(dirname(__FILE__) . '/compatibility.php');
14 /**#@-*/
16 /**
17 * Assertion that can display failure information.
18 * Also includes various helper methods.
19 * @package SimpleTest
20 * @subpackage UnitTester
21 * @abstract
23 class SimpleExpectation {
24 var $_dumper;
25 var $_message;
27 /**
28 * Creates a dumper for displaying values and sets
29 * the test message.
30 * @param string $message Customised message on failure.
32 function SimpleExpectation($message = '%s') {
33 $this->_dumper = &new SimpleDumper();
34 $this->_message = $message;
37 /**
38 * Tests the expectation. True if correct.
39 * @param mixed $compare Comparison value.
40 * @return boolean True if correct.
41 * @access public
42 * @abstract
44 function test($compare) {
47 /**
48 * Returns a human readable test message.
49 * @param mixed $compare Comparison value.
50 * @return string Description of success
51 * or failure.
52 * @access public
53 * @abstract
55 function testMessage($compare) {
58 /**
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
63 * or failure.
64 * @access public
66 function overlayMessage($compare) {
67 return sprintf($this->_message, $this->testMessage($compare));
70 /**
71 * Accessor for the dumper.
72 * @return SimpleDumper Current value dumper.
73 * @access protected
75 function &_getDumper() {
76 return $this->_dumper;
79 /**
80 * Test to see if a value is an expectation object.
81 * A useful utility method.
82 * @param mixed $expectation Hopefully an Epectation
83 * class.
84 * @return boolean True if descended from
85 * this class.
86 * @access public
87 * @static
89 function isExpectation($expectation) {
90 return is_object($expectation) &&
91 SimpleTestCompatibility::isA($expectation, 'SimpleExpectation');
95 /**
96 * Test for equality.
97 * @package SimpleTest
98 * @subpackage UnitTester
100 class EqualExpectation extends SimpleExpectation {
101 var $_value;
104 * Sets the value to compare against.
105 * @param mixed $value Test value to match.
106 * @param string $message Customised message on failure.
107 * @access public
109 function EqualExpectation($value, $message = '%s') {
110 $this->SimpleExpectation($message);
111 $this->_value = $value;
115 * Tests the expectation. True if it matches the
116 * held value.
117 * @param mixed $compare Comparison value.
118 * @return boolean True if correct.
119 * @access public
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
129 * or failure.
130 * @access public
132 function testMessage($compare) {
133 if ($this->test($compare)) {
134 return "Equal expectation [" . $this->_dumper->describeValue($this->_value) . "]";
135 } else {
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.
144 * @access protected
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.
162 * @access public
164 function NotEqualExpectation($value, $message = '%s') {
165 $this->EqualExpectation($value, $message);
169 * Tests the expectation. True if it differs from the
170 * held value.
171 * @param mixed $compare Comparison value.
172 * @return boolean True if correct.
173 * @access public
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
183 * or failure.
184 * @access public
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);
191 } else {
192 return "Not equal expectation fails [" .
193 $dumper->describeValue($this->_getValue()) .
194 "] matches";
200 * Test for being within a range.
201 * @package SimpleTest
202 * @subpackage UnitTester
204 class WithinMarginExpectation extends SimpleExpectation {
205 var $_upper;
206 var $_lower;
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.
214 * @access public
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
224 * held value.
225 * @param mixed $compare Comparison value.
226 * @return boolean True if correct.
227 * @access public
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
237 * or failure.
238 * @access public
240 function testMessage($compare) {
241 if ($this->test($compare)) {
242 return $this->_withinMessage($compare);
243 } else {
244 return $this->_outsideMessage($compare);
249 * Creates a the message for being within the range.
250 * @param mixed $compare Value being tested.
251 * @access private
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.
261 * @access private
263 function _outsideMessage($compare) {
264 if ($compare > $this->_upper) {
265 return "Outside expectation " .
266 $this->_dumper->describeDifference($compare, $this->_upper);
267 } else {
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.
287 * @access public
289 function OutsideMarginExpectation($value, $margin, $message = '%s') {
290 $this->WithinMarginExpectation($value, $margin, $message);
294 * Tests the expectation. True if it matches the
295 * held value.
296 * @param mixed $compare Comparison value.
297 * @return boolean True if correct.
298 * @access public
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
308 * or failure.
309 * @access public
311 function testMessage($compare) {
312 if (! $this->test($compare)) {
313 return $this->_withinMessage($compare);
314 } else {
315 return $this->_outsideMessage($compare);
321 * Test for identity.
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.
331 * @access public
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.
342 * @access public
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
352 * or failure.
353 * @access public
355 function testMessage($compare) {
356 $dumper = &$this->_getDumper();
357 if ($this->test($compare)) {
358 return "Identical expectation [" . $dumper->describeValue($this->_getValue()) . "]";
359 } else {
360 return "Identical expectation [" . $dumper->describeValue($this->_getValue()) .
361 "] fails with [" .
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.
379 * @access public
381 function NotIdenticalExpectation($value, $message = '%s') {
382 $this->IdenticalExpectation($value, $message);
386 * Tests the expectation. True if it differs from the
387 * held value.
388 * @param mixed $compare Comparison value.
389 * @return boolean True if correct.
390 * @access public
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
400 * or failure.
401 * @access public
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);
408 } else {
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 {
420 var $_pattern;
423 * Sets the value to compare against.
424 * @param string $pattern Pattern to search for.
425 * @param string $message Customised message on failure.
426 * @access public
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.
436 * @access protected
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.
447 * @access public
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
457 * or failure.
458 * @access public
460 function testMessage($compare) {
461 if ($this->test($compare)) {
462 return $this->_describePatternMatch($this->_getPattern(), $compare);
463 } else {
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.
478 * @access protected
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) . "]";
492 * @deprecated
494 class WantedPatternExpectation extends PatternExpectation {
498 * Fail if a pattern is detected within the
499 * comparison.
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.
509 * @access public
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.
520 * @access public
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
530 * or failure.
531 * @access public
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) . "]";
539 } else {
540 return $this->_describePatternMatch($this->_getPattern(), $compare);
546 * @package SimpleTest
547 * @subpackage UnitTester
548 * @deprecated
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 {
559 var $_type;
562 * Sets the type to compare with.
563 * @param string $type Type or class name.
564 * @param string $message Customised message on failure.
565 * @access public
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.
575 * @access protected
577 function _getType() {
578 return $this->_type;
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.
586 * @access public
588 function test($compare) {
589 if (is_object($compare)) {
590 return SimpleTestCompatibility::isA($compare, $this->_type);
591 } else {
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.
600 * @access private
602 function _canonicalType($type) {
603 $type = strtolower($type);
604 $map = array(
605 'bool' => 'boolean',
606 'float' => 'double',
607 'real' => 'double',
608 'int' => 'integer');
609 if (isset($map[$type])) {
610 $type = $map[$type];
612 return $type;
616 * Returns a human readable test message.
617 * @param mixed $compare Comparison value.
618 * @return string Description of success
619 * or failure.
620 * @access public
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 {
636 var $_type;
639 * Sets the type to compare with.
640 * @param string $type Type or class name.
641 * @param string $message Customised message on failure.
642 * @access public
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.
653 * @access public
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
663 * or failure.
664 * @access public
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 {
679 var $_method;
682 * Sets the value to compare against.
683 * @param string $method Method to check.
684 * @param string $message Customised message on failure.
685 * @access public
686 * @return void
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.
697 * @access public
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
707 * or failure.
708 * @access public
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]";