cvs: pear /PHPUnit2/Framework Assert.php

From: Date: Tue, 05 Oct 2004 14:32:44 +0000
Subject: cvs: pear /PHPUnit2/Framework Assert.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-24619@lists.php.net to get a copy of this message
sebastian Tue Oct 5 10:32:44 2004 EDT Modified files: /pear/PHPUnit2/Framework Assert.php Log: Improve assertContains(), assertNotContains(). http://cvs.php.net/diff.php/pear/PHPUnit2/Framework/Assert.php?r1=1.17&r2=1.18&ty=u Index: pear/PHPUnit2/Framework/Assert.php diff -u pear/PHPUnit2/Framework/Assert.php:1.17 pear/PHPUnit2/Framework/Assert.php:1.18 --- pear/PHPUnit2/Framework/Assert.php:1.17 Tue Oct 5 10:10:00 2004 +++ pear/PHPUnit2/Framework/Assert.php Tue Oct 5 10:32:44 2004 @@ -12,7 +12,7 @@ // | license@php.net so we can mail you a copy immediately. | // +------------------------------------------------------------------------+ // -// $Id: Assert.php,v 1.17 2004/10/05 14:10:00 sebastian Exp $ +// $Id: Assert.php,v 1.18 2004/10/05 14:32:44 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().'); } } @@ -506,6 +518,56 @@ */ private static function failNotEquals($expected, $actual, $message) { self::fail(self::format($expected, $actual, $message)); + } + + // }}} + // {{{ 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) + ) + ); } // }}}

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