From ffc0e93c4eb0555a08f0b58bed0735416e6ba41f Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Sun, 20 Apr 2008 18:34:43 +0000 Subject: - 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 ... :) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 ... --- modules/simpletest/unit_tester.php | 178 +++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 modules/simpletest/unit_tester.php (limited to 'modules/simpletest/unit_tester.php') diff --git a/modules/simpletest/unit_tester.php b/modules/simpletest/unit_tester.php new file mode 100644 index 000000000..cf4ea16ba --- /dev/null +++ b/modules/simpletest/unit_tester.php @@ -0,0 +1,178 @@ +SimpleTestCase($label); + } + + /** + * Called from within the test methods to register + * passes and failures. + * @param boolean $result Pass on true. + * @param string $message Message to display describing + * the test state. + * @return boolean True on pass + * @access public + */ + function assertTrue($result, $message = FALSE, $group = 'Other') { + return $this->assert(new TrueExpectation(), $result, $message, $group); + } + + /** + * Will be true on false and vice versa. False + * is the PHP definition of false, so that null, + * empty strings, zero and an empty array all count + * as false. + * @param boolean $result Pass on false. + * @param string $message Message to display. + * @return boolean True on pass + * @access public + */ + function assertFalse($result, $message = '%s', $group = 'Other') { + $dumper = &new SimpleDumper(); + $message = sprintf($message, 'Expected false, got ['. $dumper->describeValue($result) .']'); + return $this->assertTrue(!$result, $message, $group); + } + + /** + * Will be true if the value is null. + * @param null $value Supposedly null value. + * @param string $message Message to display. + * @return boolean True on pass + * @access public + */ + function assertNull($value, $message = '%s', $group = 'Other') { + $dumper = &new SimpleDumper(); + $message = sprintf($message, '['. $dumper->describeValue($value) .'] should be null'); + return $this->assertTrue(!isset($value), $message, $group); + } + + /** + * Will be true if the value is set. + * @param mixed $value Supposedly set value. + * @param string $message Message to display. + * @return boolean True on pass. + * @access public + */ + function assertNotNull($value, $message = '%s', $group = 'Other') { + $dumper = &new SimpleDumper(); + $message = sprintf($message, '['. $dumper->describeValue($value) .'] should not be null'); + return $this->assertTrue(isset($value), $message, $group); + } + + /** + * Will trigger a pass if the two parameters have + * the same value only. Otherwise a fail. + * @param mixed $first Value to compare. + * @param mixed $second Value to compare. + * @param string $message Message to display. + * @return boolean True on pass + * @access public + */ + function assertEqual($first, $second, $message = '%s', $group = 'Other') { + $dumper = &new SimpleDumper(); + $message = sprintf($message, 'Expected '. $dumper->describeValue($first) .', got ['. $dumper->describeValue($second) .']'); + $this->assertTrue($first == $second, $message, $group); + } + + /** + * Will trigger a pass if the two parameters have + * a different value. Otherwise a fail. + * @param mixed $first Value to compare. + * @param mixed $second Value to compare. + * @param string $message Message to display. + * @return boolean True on pass + * @access public + */ + function assertNotEqual($first, $second, $message = '%s', $group = 'Other') { + $dumper = &new SimpleDumper(); + $message = sprintf($message, 'Expected '. $dumper->describeValue($first) .', not equal to '. $dumper->describeValue($second)); + $this->assertTrue($first != $second, $message, $group); + } + + /** + * Will trigger a pass if the two parameters have + * the same value and same type. Otherwise a fail. + * @param mixed $first Value to compare. + * @param mixed $second Value to compare. + * @param string $message Message to display. + * @return boolean True on pass + * @access public + */ + function assertIdentical($first, $second, $message = '%s', $group = 'Other') { + $dumper = &new SimpleDumper(); + $message = sprintf($message, 'Expected '. $dumper->describeValue($first) .', got ['. $dumper->describeValue($second) .']'); + $this->assertTrue($first === $second, $message, $group); + } + + /** + * Will trigger a pass if the two parameters have + * the different value or different type. + * @param mixed $first Value to compare. + * @param mixed $second Value to compare. + * @param string $message Message to display. + * @return boolean True on pass + * @access public + */ + function assertNotIdentical($first, $second, $message = '%s', $group = 'Other') { + $dumper = &new SimpleDumper(); + $message = sprintf($message, 'Expected '. $dumper->describeValue($first) .', not identical to '. $dumper->describeValue($second)); + $this->assertTrue($first !== $second, $message, $group); + } + + /** + * Will trigger a pass if the Perl regex pattern + * is found in the subject. Fail otherwise. + * @param string $pattern Perl regex to look for including + * the regex delimiters. + * @param string $subject String to search in. + * @param string $message Message to display. + * @return boolean True on pass + * @access public + */ + function assertPattern($pattern, $subject, $message = '%s', $group = 'Other') { + $dumper = &new SimpleDumper(); + $replace = 'Pattern '. $pattern .' detected in ['. $dumper->describeValue($subject) .']'; + $found = preg_match($pattern, $subject, $matches); + if ($found) { + $position = strpos($subject, $matches[0]); + $replace .= ' in region ['. $dumper->clipString($subject, 100, $position) .']'; + } + $message = sprintf($message, $replace); + $this->assertTrue($found, $message, $group); + } + + /** + * Will trigger a pass if the perl regex pattern + * is not present in subject. Fail if found. + * @param string $pattern Perl regex to look for including + * the regex delimiters. + * @param string $subject String to search in. + * @param string $message Message to display. + * @return boolean True on pass + * @access public + */ + function assertNoPattern($pattern, $subject, $message = '%s', $group = 'Other') { + $dumper = &new SimpleDumper(); + $found = preg_match($pattern, $subject); + $message = sprintf($message, 'Pattern '. $pattern .' not detected in ['. $dumper->describeValue($subject) .']'); + $this->assertFalse($found, $message, $group = 'Other'); + } +} \ No newline at end of file -- cgit v1.2.3