cvs: pear(PHPUNIT_2_1) /PHPUnit2/Framework Assert.php

From: Date: Tue, 05 Oct 2004 14:33:54 +0000
Subject: cvs: pear(PHPUNIT_2_1) /PHPUnit2/Framework Assert.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-24620@lists.php.net to get a copy of this message
sebastian Tue Oct 5 10:33:54 2004 EDT Modified files: (Branch: PHPUNIT_2_1) /pear/PHPUnit2/Framework Assert.php Log: MFH: Improve assertContains(), assertNotContains(). http://cvs.php.net/diff.php/pear/PHPUnit2/Framework/Assert.php?r1=1.14.2.3&r2=1.14.2.4&ty=u Index: pear/PHPUnit2/Framework/Assert.php diff -u pear/PHPUnit2/Framework/Assert.php:1.14.2.3 pear/PHPUnit2/Framework/Assert.php:1.14.2.4 --- pear/PHPUnit2/Framework/Assert.php:1.14.2.3 Tue Oct 5 10:10:21 2004 +++ pear/PHPUnit2/Framework/Assert.php Tue Oct 5 10:33:53 2004 @@ -12,7 +12,7 @@ // | license@php.net so we can mail you a copy immediately. | // +------------------------------------------------------------------------+ // -// $Id: Assert.php,v 1.14.2.3 2004/10/05 14:10:21 sebastian Exp $ +// $Id: Assert.php,v 1.14.2.4 2004/10/05 14:33:53 sebastian Exp $ // require_once 'PHPUnit2/Framework/AssertionFailedError.php'; @@ -64,7 +64,9 @@ */ public static function assertContains($needle, $haystack, $message = '') { if (is_string($needle) && is_string($haystack)) { - self::assertTrue(strpos($haystack, $needle) !== FALSE ? TRUE : FALSE, $message); + if (strpos($haystack, $needle) === FALSE) { + self::failNotContains($needle, $haystack, $message); + } } else if (is_array($haystack)) { @@ -78,9 +80,13 @@ } } - self::assertTrue($found, $message); + if (!$found) { + self::failNotContains($needle, $haystack, $message); + } } else { - self::assertTrue(in_array($needle, $haystack), $message); + if (!in_array($needle, $haystack)) { + self::failNotContains($needle, $haystack, $message); + } } } @@ -104,7 +110,9 @@ */ public static function assertNotContains($needle, $haystack, $message = '') { if (is_string($needle) && is_string($haystack)) { - self::assertFalse(strpos($haystack, $needle) !== FALSE ? TRUE : FALSE, $message); + if (strpos($haystack, $needle) !== FALSE) { + self::failContains($needle, $haystack, $message); + } } else if (is_array($haystack)) { @@ -118,14 +126,18 @@ } } - self::assertFalse($found, $message); + if ($found) { + self::failContains($needle, $haystack, $message); + } } else { - self::assertFalse(in_array($needle, $haystack), $message); + if (in_array($needle, $haystack)) { + self::failContains($needle, $haystack, $message); + } } } else { - throw new Exception('Unsupported parameter passed to assertNotContains().'); + throw new Exception('Unsupported parameter passed to assertContains().'); } } @@ -509,6 +521,56 @@ } // }}} + // {{{ private static function failContains($needle, $haystack, $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) { + self::fail( + sprintf( + '%s%s"%s" contains "%s"', + + $message, + ($message != '') ? ' ' : '', + self::objectToString($haystack), + self::objectToString($needle) + ) + ); + } + + // }}} + // {{{ private static function failNotContains($needle, $haystack, $message) + + /** + * @param mixed $needle + * @param mixed $haystack + * @param string $message + * @throws PHPUnit2_Framework_AssertionFailedError + * @access private + * @static + * @since 2.1.2 + */ + private static function failNotContains($needle, $haystack, $message) { + self::fail( + sprintf( + '%s%s"%s" does not contain "%s"', + + $message, + ($message != '') ? ' ' : '', + self::objectToString($haystack), + self::objectToString($needle) + ) + ); + } + + // }}} // {{{ private static function objectToString($object) /** @@ -543,7 +605,7 @@ if (is_array($v)) { self::sortArrayRecursively($array[$k]); } - } + } } // }}}

« previous php.pear.cvs (#24620) next »