diff options
author | Dries Buytaert <dries@buytaert.net> | 2008-04-20 18:34:43 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2008-04-20 18:34:43 +0000 |
commit | ffc0e93c4eb0555a08f0b58bed0735416e6ba41f (patch) | |
tree | 7f741d0b40124633c5b337dc7210711bfcd4b876 /modules/simpletest/expectation.php | |
parent | af474609e3e80db9ba1d16b9ad2eae89775f51c8 (diff) | |
download | brdo-ffc0e93c4eb0555a08f0b58bed0735416e6ba41f.tar.gz brdo-ffc0e93c4eb0555a08f0b58bed0735416e6ba41f.tar.bz2 |
- Added a test framework to Drupal along with a first batch of tests for
Drupal core! This is an important milestone for the project so enable
the module and check it out ... :)
Thanks to Rok Žlender, Károly Négyesi, Jimmy Berry, Kevin Bridges, Charlie
Gordon, Douglas Hubler, Miglius Alaburda, Andy Kirkham, Dimitri13, Kieran
Lal, Moshe Weitzman, and the many other people that helped with testing
over the past years and that drove this home.
It all works but it is still rough around the edges (i.e. documentation
is still being written, the coding style is not 100% yet, a number of
tests still fail) but we spent the entire weekend working on it in Paris
and made a ton of progress. The best way to help and to get up to speed,
is to start writing and contributing some tests ... as well as fixing
some of the failures.
For those willing to help with improving the test framework, here are
some next steps and issues to resolve:
- How to best approach unit tests and mock functions?
- How to test drupal_mail() and drupal_http_request()?
- How to improve the admin UI so we have a nice progress bar?
- How best to do code coverage?
- See http://g.d.o/node/10099 for more ...
Diffstat (limited to 'modules/simpletest/expectation.php')
-rw-r--r-- | modules/simpletest/expectation.php | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/modules/simpletest/expectation.php b/modules/simpletest/expectation.php new file mode 100644 index 000000000..1dd85c9ac --- /dev/null +++ b/modules/simpletest/expectation.php @@ -0,0 +1,144 @@ +<?php +// $Id$ + +/** + * Assertion that can display failure information. + * Also includes various helper methods. + * @package SimpleTest + * @subpackage UnitTester + * @abstract + */ +class SimpleExpectation { + var $_dumper = false; + var $_message; + + /** + * Creates a dumper for displaying values and sets + * the test message. + * @param string $message Customised message on failure. + */ + function SimpleExpectation($message = '%s') { + $this->_message = $message; + } + + /** + * Tests the expectation. True if correct. + * @param mixed $compare Comparison value. + * @return boolean True if correct. + * @access public + * @abstract + */ + function test($compare) {} + + /** + * Returns a human readable test message. + * @param mixed $compare Comparison value. + * @return string Description of success + * or failure. + * @access public + * @abstract + */ + function testMessage($compare) {} + + /** + * Overlays the generated message onto the stored user + * message. An additional message can be interjected. + * @param mixed $compare Comparison value. + * @param SimpleDumper $dumper For formatting the results. + * @return string Description of success + * or failure. + * @access public + */ + function overlayMessage($compare, $dumper) { + $this->_dumper = $dumper; + return sprintf($this->_message, $this->testMessage($compare)); + } + + /** + * Accessor for the dumper. + * @return SimpleDumper Current value dumper. + * @access protected + */ + function &_getDumper() { + if (!$this->_dumper) { + $dumper = &new SimpleDumper(); + return $dumper; + } + return $this->_dumper; + } + + /** + * Test to see if a value is an expectation object. + * A useful utility method. + * @param mixed $expectation Hopefully an Epectation + * class. + * @return boolean True if descended from + * this class. + * @access public + * @static + */ + function isExpectation($expectation) { + return is_object($expectation) && is_a($expectation, 'SimpleExpectation'); + } +} + +/** + * A wildcard expectation always matches. + * @package SimpleTest + * @subpackage MockObjects + */ +class AnythingExpectation extends SimpleExpectation { + + /** + * Tests the expectation. Always true. + * @param mixed $compare Ignored. + * @return boolean True. + * @access public + */ + function test($compare) { + return true; + } + + /** + * Returns a human readable test message. + * @param mixed $compare Comparison value. + * @return string Description of success + * or failure. + * @access public + */ + function testMessage($compare) { + $dumper = &$this->_getDumper(); + return 'Anything always matches ['. $dumper->describeValue($compare) .']'; + } +} + +/** + * An expectation that passes on boolean true. + * @package SimpleTest + * @subpackage MockObjects + */ +class TrueExpectation extends SimpleExpectation { + + /** + * Tests the expectation. + * @param mixed $compare Should be true. + * @return boolean True on match. + * @access public + */ + function test($compare) { + return (boolean)$compare; + } + + /** + * Returns a human readable test message. + * @param mixed $compare Comparison value. + * @return string Description of success + * or failure. + * @access public + */ + function testMessage($compare) { + $dumper = &$this->_getDumper(); + return 'Expected true, got ['. $dumper->describeValue($compare) .']'; + } +} + |