cvs: pear /PHPUnit2/Framework Assert.php
| From: | Sebastian Bergmann | Date: | Mon, 25 Oct 2004 20:04:03 +0000 |
| Subject: | cvs: pear /PHPUnit2/Framework Assert.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-25222@lists.php.net to get a copy of this message | ||
sebastian Mon Oct 25 16:04:03 2004 EDT
Modified files:
/pear/PHPUnit2/Framework Assert.php
Log:
Factor out common code from assertContains() and assertNotContains() into doAssertContains(), from
assertType() and assertNotType() into doAssertType(), and from assertRegExp() and assertNotRegExp()
into doAssertRegExp().
http://cvs.php.net/diff.php/pear/PHPUnit2/Framework/Assert.php?r1=1.21&r2=1.22&ty=u Index: pear/PHPUnit2/Framework/Assert.php diff -u pear/PHPUnit2/Framework/Assert.php:1.21 pear/PHPUnit2/Framework/Assert.php:1.22 --- pear/PHPUnit2/Framework/Assert.php:1.21 Mon Oct 25 11:15:19 2004 +++ pear/PHPUnit2/Framework/Assert.php Mon Oct 25 16:04:00 2004 @@ -12,7 +12,7 @@ // | license@php.net so we can mail you a copy immediately. | // +------------------------------------------------------------------------+ // -// $Id: Assert.php,v 1.21 2004/10/25 15:15:19 sebastian Exp $ +// $Id: Assert.php,v 1.22 2004/10/25 20:04:00 sebastian Exp $ // require_once 'PHPUnit2/Framework/AssertionFailedError.php'; @@ -63,31 +63,7 @@ * @since 2.1.0 */ public static function assertContains($needle, $haystack, $message = '') { - if (is_array($haystack) || - (is_object($haystack) && $haystack instanceof Iterator)) { - $found = FALSE; - - foreach ($haystack as $straw) { - if ($straw === $needle) { - $found = TRUE; - break; - } - } - - if (!$found) { - self::failNotContains($needle, $haystack, $message); - } - } - - else if (is_string($needle) && is_string($haystack)) { - if (strpos($haystack, $needle) === FALSE) { - self::failNotContains($needle, $haystack, $message); - } - } - - else { - throw new Exception('Unsupported parameter passed to assertContains().'); - } + self::doAssertContains($needle, $haystack, TRUE, $message); } // }}} @@ -104,31 +80,7 @@ * @since 2.1.0 */ public static function assertNotContains($needle, $haystack, $message = '') { - if (is_array($haystack) || - (is_object($haystack) && $haystack instanceof Iterator)) { - $found = FALSE; - - foreach ($haystack as $straw) { - if ($straw === $needle) { - $found = TRUE; - break; - } - } - - if ($found) { - self::failNotContains($needle, $haystack, $message); - } - } - - else if (is_string($needle) && is_string($haystack)) { - if (strpos($haystack, $needle) !== FALSE) { - self::failNotContains($needle, $haystack, $message); - } - } - - else { - throw new Exception('Unsupported parameter passed to assertContains().'); - } + self::doAssertContains($needle, $haystack, FALSE, $message); } // }}} @@ -303,19 +255,7 @@ * @static */ public static function assertType($expected, $actual, $message = '') { - if (is_object($actual)) { - $actual = get_class($actual); - } else { - $actual = gettype($actual); - } - - if ($expected != $actual) { - self::failNotEquals( - $expected, - $actual, - $message - ); - } + self::doAssertType($expected, $actual, TRUE, $message); } // }}} @@ -332,19 +272,7 @@ * @since 2.2.0 */ public static function assertNotType($expected, $actual, $message = '') { - if (is_object($actual)) { - $actual = get_class($actual); - } else { - $actual = gettype($actual); - } - - if ($expected == $actual) { - self::failEquals( - $expected, - $actual, - $message - ); - } + self::doAssertType($expected, $actual, FALSE, $message); } // }}} @@ -360,18 +288,7 @@ * @static */ public static function assertRegExp($pattern, $string, $message = '') { - if (!preg_match($pattern, $string)) { - self::fail( - sprintf( - '%s%s"%s" does not match pattern "%s"', - - $message, - ($message != '') ? ' ' : '', - $string, - $pattern - ) - ); - } + self::doAssertRegExp($pattern, $string, TRUE, $message); } // }}} @@ -388,18 +305,7 @@ * @since 2.1.0 */ public static function assertNotRegExp($pattern, $string, $message = '') { - if (preg_match($pattern, $string)) { - self::fail( - sprintf( - '%s%s"%s" matches pattern "%s"', - - $message, - ($message != '') ? ' ' : '', - $string, - $pattern - ) - ); - } + self::doAssertRegExp($pattern, $string, FALSE, $message); } // }}} @@ -477,119 +383,211 @@ } // }}} - // {{{ private static function failSame($message) + // {{{ private static function doAssertContains($needle, $haystack, $condition, $message) /** + * @param mixed $needle + * @param mixed $haystack + * @param boolean $condition * @param string $message - * @throws PHPUnit2_Framework_AssertionFailedError * @access private * @static + * @since 2.2.0 */ - private static function failSame($message) { - self::fail( - sprintf( - '%s%sexpected not same', + private static function doAssertContains($needle, $haystack, $condition, $message) { + $found = FALSE; - $message, - ($message != '') ? ' ' : '' - ) - ); + if (is_array($haystack) || + (is_object($haystack) && $haystack instanceof Iterator)) { + foreach ($haystack as $straw) { + if ($straw === $needle) { + $found = TRUE; + break; + } + } + } + + else if (is_string($needle) && is_string($haystack)) { + if (strpos($haystack, $needle) !== FALSE) { + $found = TRUE; + } + } + + else { + throw new Exception('Unsupported parameter passed to assertContains().'); + } + + if ($condition && !$found) { + self::fail( + sprintf( + '%s%s"%s" does not contain "%s"', + + $message, + ($message != '') ? ' ' : '', + self::objectToString($haystack), + self::objectToString($needle) + ) + ); + } + + else if (!$condition && $found) { + self::fail( + sprintf( + '%s%s"%s" contains "%s"', + + $message, + ($message != '') ? ' ' : '', + self::objectToString($haystack), + self::objectToString($needle) + ) + ); + } } // }}} - // {{{ private static function failNotSame($expected, $actual, $message) + // {{{ private static function doAssertType($expected, $actual, $condition, $message) /** - * @param mixed $expected + * @param string $expected * @param mixed $actual + * @param boolean $condition * @param string $message - * @throws PHPUnit2_Framework_AssertionFailedError * @access private * @static + * @since 2.2.0 */ - private static function failNotSame($expected, $actual, $message) { - if (is_string($expected) && is_string($actual)) { - throw new PHPUnit2_Framework_ComparisonFailure($expected, $actual, $message); + private static function doAssertType($expected, $actual, $condition, $message) { + if (is_object($actual)) { + $actual = get_class($actual); + } else { + $actual = gettype($actual); } - self::fail( - sprintf( - '%s%sexpected same: <%s> was not: <%s>', + $result = ($expected == $actual); - $message, - ($message != '') ? ' ' : '', - self::objectToString($expected), - self::objectToString($actual) - ) - ); + if ($condition && !$result) { + self::failNotSame( + $expected, + $actual, + $message + ); + } + + else if (!$condition && $result) { + self::failSame( + $expected, + $actual, + $message + ); + } } // }}} - // {{{ private static function failNotEquals($expected, $actual, $message) + // {{{ private static function doAssertRegExp($pattern, $string, $condition, $message) /** - * @param mixed $expected - * @param mixed $actual + * @param mixed $pattern + * @param mixed $string + * @param boolean $condition * @param string $message - * @throws PHPUnit2_Framework_AssertionFailedError * @access private * @static + * @since 2.2.0 */ - private static function failNotEquals($expected, $actual, $message) { - self::fail(self::format($expected, $actual, $message)); + private static function doAssertRegExp($pattern, $string, $condition, $message) { + $result = preg_match($pattern, $string); + + if ($condition && !$result) { + self::fail( + sprintf( + '%s%s"%s" does not match pattern "%s"', + + $message, + ($message != '') ? ' ' : '', + $string, + $pattern + ) + ); + } + + else if (!$condition && $result) { + self::fail( + sprintf( + '%s%s"%s" matches pattern "%s"', + + $message, + ($message != '') ? ' ' : '', + $string, + $pattern + ) + ); + } } // }}} - // {{{ private static function failContains($needle, $haystack, $message) + // {{{ private static function failSame($message) /** - * @param mixed $needle - * @param mixed $haystack * @param string $message * @throws PHPUnit2_Framework_AssertionFailedError * @access private * @static - * @since 2.1.2 */ - private static function failContains($needle, $haystack, $message) { + private static function failSame($message) { self::fail( sprintf( - '%s%s"%s" contains "%s"', + '%s%sexpected not same', $message, - ($message != '') ? ' ' : '', - self::objectToString($haystack), - self::objectToString($needle) + ($message != '') ? ' ' : '' ) ); } // }}} - // {{{ private static function failNotContains($needle, $haystack, $message) + // {{{ private static function failNotSame($expected, $actual, $message) /** - * @param mixed $needle - * @param mixed $haystack + * @param mixed $expected + * @param mixed $actual * @param string $message * @throws PHPUnit2_Framework_AssertionFailedError * @access private * @static - * @since 2.1.2 */ - private static function failNotContains($needle, $haystack, $message) { + private static function failNotSame($expected, $actual, $message) { + if (is_string($expected) && is_string($actual)) { + throw new PHPUnit2_Framework_ComparisonFailure($expected, $actual, $message); + } + self::fail( sprintf( - '%s%s"%s" does not contain "%s"', + '%s%sexpected same: <%s> was not: <%s>', $message, ($message != '') ? ' ' : '', - self::objectToString($haystack), - self::objectToString($needle) + self::objectToString($expected), + self::objectToString($actual) ) ); } // }}} + // {{{ private static function failNotEquals($expected, $actual, $message) + + /** + * @param mixed $expected + * @param mixed $actual + * @param string $message + * @throws PHPUnit2_Framework_AssertionFailedError + * @access private + * @static + */ + private static function failNotEquals($expected, $actual, $message) { + self::fail(self::format($expected, $actual, $message)); + } + + // }}} // {{{ private static function objectToString($object) /** @@ -624,7 +622,7 @@ if (is_array($v)) { self::sortArrayRecursively($array[$k]); } - } + } } // }}}
http://cvs.php.net/diff.php/pear/PHPUnit2/Framework/Assert.php?r1=1.21&r2=1.22&ty=u Index: pear/PHPUnit2/Framework/Assert.php diff -u pear/PHPUnit2/Framework/Assert.php:1.21 pear/PHPUnit2/Framework/Assert.php:1.22 --- pear/PHPUnit2/Framework/Assert.php:1.21 Mon Oct 25 11:15:19 2004 +++ pear/PHPUnit2/Framework/Assert.php Mon Oct 25 16:04:00 2004 @@ -12,7 +12,7 @@ // | license@php.net so we can mail you a copy immediately. | // +------------------------------------------------------------------------+ // -// $Id: Assert.php,v 1.21 2004/10/25 15:15:19 sebastian Exp $ +// $Id: Assert.php,v 1.22 2004/10/25 20:04:00 sebastian Exp $ // require_once 'PHPUnit2/Framework/AssertionFailedError.php'; @@ -63,31 +63,7 @@ * @since 2.1.0 */ public static function assertContains($needle, $haystack, $message = '') { - if (is_array($haystack) || - (is_object($haystack) && $haystack instanceof Iterator)) { - $found = FALSE; - - foreach ($haystack as $straw) { - if ($straw === $needle) { - $found = TRUE; - break; - } - } - - if (!$found) { - self::failNotContains($needle, $haystack, $message); - } - } - - else if (is_string($needle) && is_string($haystack)) { - if (strpos($haystack, $needle) === FALSE) { - self::failNotContains($needle, $haystack, $message); - } - } - - else { - throw new Exception('Unsupported parameter passed to assertContains().'); - } + self::doAssertContains($needle, $haystack, TRUE, $message); } // }}} @@ -104,31 +80,7 @@ * @since 2.1.0 */ public static function assertNotContains($needle, $haystack, $message = '') { - if (is_array($haystack) || - (is_object($haystack) && $haystack instanceof Iterator)) { - $found = FALSE; - - foreach ($haystack as $straw) { - if ($straw === $needle) { - $found = TRUE; - break; - } - } - - if ($found) { - self::failNotContains($needle, $haystack, $message); - } - } - - else if (is_string($needle) && is_string($haystack)) { - if (strpos($haystack, $needle) !== FALSE) { - self::failNotContains($needle, $haystack, $message); - } - } - - else { - throw new Exception('Unsupported parameter passed to assertContains().'); - } + self::doAssertContains($needle, $haystack, FALSE, $message); } // }}} @@ -303,19 +255,7 @@ * @static */ public static function assertType($expected, $actual, $message = '') { - if (is_object($actual)) { - $actual = get_class($actual); - } else { - $actual = gettype($actual); - } - - if ($expected != $actual) { - self::failNotEquals( - $expected, - $actual, - $message - ); - } + self::doAssertType($expected, $actual, TRUE, $message); } // }}} @@ -332,19 +272,7 @@ * @since 2.2.0 */ public static function assertNotType($expected, $actual, $message = '') { - if (is_object($actual)) { - $actual = get_class($actual); - } else { - $actual = gettype($actual); - } - - if ($expected == $actual) { - self::failEquals( - $expected, - $actual, - $message - ); - } + self::doAssertType($expected, $actual, FALSE, $message); } // }}} @@ -360,18 +288,7 @@ * @static */ public static function assertRegExp($pattern, $string, $message = '') { - if (!preg_match($pattern, $string)) { - self::fail( - sprintf( - '%s%s"%s" does not match pattern "%s"', - - $message, - ($message != '') ? ' ' : '', - $string, - $pattern - ) - ); - } + self::doAssertRegExp($pattern, $string, TRUE, $message); } // }}} @@ -388,18 +305,7 @@ * @since 2.1.0 */ public static function assertNotRegExp($pattern, $string, $message = '') { - if (preg_match($pattern, $string)) { - self::fail( - sprintf( - '%s%s"%s" matches pattern "%s"', - - $message, - ($message != '') ? ' ' : '', - $string, - $pattern - ) - ); - } + self::doAssertRegExp($pattern, $string, FALSE, $message); } // }}} @@ -477,119 +383,211 @@ } // }}} - // {{{ private static function failSame($message) + // {{{ private static function doAssertContains($needle, $haystack, $condition, $message) /** + * @param mixed $needle + * @param mixed $haystack + * @param boolean $condition * @param string $message - * @throws PHPUnit2_Framework_AssertionFailedError * @access private * @static + * @since 2.2.0 */ - private static function failSame($message) { - self::fail( - sprintf( - '%s%sexpected not same', + private static function doAssertContains($needle, $haystack, $condition, $message) { + $found = FALSE; - $message, - ($message != '') ? ' ' : '' - ) - ); + if (is_array($haystack) || + (is_object($haystack) && $haystack instanceof Iterator)) { + foreach ($haystack as $straw) { + if ($straw === $needle) { + $found = TRUE; + break; + } + } + } + + else if (is_string($needle) && is_string($haystack)) { + if (strpos($haystack, $needle) !== FALSE) { + $found = TRUE; + } + } + + else { + throw new Exception('Unsupported parameter passed to assertContains().'); + } + + if ($condition && !$found) { + self::fail( + sprintf( + '%s%s"%s" does not contain "%s"', + + $message, + ($message != '') ? ' ' : '', + self::objectToString($haystack), + self::objectToString($needle) + ) + ); + } + + else if (!$condition && $found) { + self::fail( + sprintf( + '%s%s"%s" contains "%s"', + + $message, + ($message != '') ? ' ' : '', + self::objectToString($haystack), + self::objectToString($needle) + ) + ); + } } // }}} - // {{{ private static function failNotSame($expected, $actual, $message) + // {{{ private static function doAssertType($expected, $actual, $condition, $message) /** - * @param mixed $expected + * @param string $expected * @param mixed $actual + * @param boolean $condition * @param string $message - * @throws PHPUnit2_Framework_AssertionFailedError * @access private * @static + * @since 2.2.0 */ - private static function failNotSame($expected, $actual, $message) { - if (is_string($expected) && is_string($actual)) { - throw new PHPUnit2_Framework_ComparisonFailure($expected, $actual, $message); + private static function doAssertType($expected, $actual, $condition, $message) { + if (is_object($actual)) { + $actual = get_class($actual); + } else { + $actual = gettype($actual); } - self::fail( - sprintf( - '%s%sexpected same: <%s> was not: <%s>', + $result = ($expected == $actual); - $message, - ($message != '') ? ' ' : '', - self::objectToString($expected), - self::objectToString($actual) - ) - ); + if ($condition && !$result) { + self::failNotSame( + $expected, + $actual, + $message + ); + } + + else if (!$condition && $result) { + self::failSame( + $expected, + $actual, + $message + ); + } } // }}} - // {{{ private static function failNotEquals($expected, $actual, $message) + // {{{ private static function doAssertRegExp($pattern, $string, $condition, $message) /** - * @param mixed $expected - * @param mixed $actual + * @param mixed $pattern + * @param mixed $string + * @param boolean $condition * @param string $message - * @throws PHPUnit2_Framework_AssertionFailedError * @access private * @static + * @since 2.2.0 */ - private static function failNotEquals($expected, $actual, $message) { - self::fail(self::format($expected, $actual, $message)); + private static function doAssertRegExp($pattern, $string, $condition, $message) { + $result = preg_match($pattern, $string); + + if ($condition && !$result) { + self::fail( + sprintf( + '%s%s"%s" does not match pattern "%s"', + + $message, + ($message != '') ? ' ' : '', + $string, + $pattern + ) + ); + } + + else if (!$condition && $result) { + self::fail( + sprintf( + '%s%s"%s" matches pattern "%s"', + + $message, + ($message != '') ? ' ' : '', + $string, + $pattern + ) + ); + } } // }}} - // {{{ private static function failContains($needle, $haystack, $message) + // {{{ private static function failSame($message) /** - * @param mixed $needle - * @param mixed $haystack * @param string $message * @throws PHPUnit2_Framework_AssertionFailedError * @access private * @static - * @since 2.1.2 */ - private static function failContains($needle, $haystack, $message) { + private static function failSame($message) { self::fail( sprintf( - '%s%s"%s" contains "%s"', + '%s%sexpected not same', $message, - ($message != '') ? ' ' : '', - self::objectToString($haystack), - self::objectToString($needle) + ($message != '') ? ' ' : '' ) ); } // }}} - // {{{ private static function failNotContains($needle, $haystack, $message) + // {{{ private static function failNotSame($expected, $actual, $message) /** - * @param mixed $needle - * @param mixed $haystack + * @param mixed $expected + * @param mixed $actual * @param string $message * @throws PHPUnit2_Framework_AssertionFailedError * @access private * @static - * @since 2.1.2 */ - private static function failNotContains($needle, $haystack, $message) { + private static function failNotSame($expected, $actual, $message) { + if (is_string($expected) && is_string($actual)) { + throw new PHPUnit2_Framework_ComparisonFailure($expected, $actual, $message); + } + self::fail( sprintf( - '%s%s"%s" does not contain "%s"', + '%s%sexpected same: <%s> was not: <%s>', $message, ($message != '') ? ' ' : '', - self::objectToString($haystack), - self::objectToString($needle) + self::objectToString($expected), + self::objectToString($actual) ) ); } // }}} + // {{{ private static function failNotEquals($expected, $actual, $message) + + /** + * @param mixed $expected + * @param mixed $actual + * @param string $message + * @throws PHPUnit2_Framework_AssertionFailedError + * @access private + * @static + */ + private static function failNotEquals($expected, $actual, $message) { + self::fail(self::format($expected, $actual, $message)); + } + + // }}} // {{{ private static function objectToString($object) /** @@ -624,7 +622,7 @@ if (is_array($v)) { self::sortArrayRecursively($array[$k]); } - } + } } // }}}