home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / PHPUnit2 / Framework / ComparisonFailure.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  4.9 KB  |  154 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * PHP Version 5
  6.  *
  7.  * Copyright (c) 2002-2006, Sebastian Bergmann <sb@sebastian-bergmann.de>.
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  *
  14.  *   * Redistributions of source code must retain the above copyright
  15.  *     notice, this list of conditions and the following disclaimer.
  16.  * 
  17.  *   * Redistributions in binary form must reproduce the above copyright
  18.  *     notice, this list of conditions and the following disclaimer in
  19.  *     the documentation and/or other materials provided with the
  20.  *     distribution.
  21.  *
  22.  *   * Neither the name of Sebastian Bergmann nor the names of his
  23.  *     contributors may be used to endorse or promote products derived
  24.  *     from this software without specific prior written permission.
  25.  *
  26.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29.  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30.  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  32.  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
  35.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37.  * POSSIBILITY OF SUCH DAMAGE.
  38.  *
  39.  * @category   Testing
  40.  * @package    PHPUnit2
  41.  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
  42.  * @copyright  2002-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>
  43.  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
  44.  * @version    CVS: $Id: ComparisonFailure.php,v 1.13.2.3 2005/12/17 16:04:56 sebastian Exp $
  45.  * @link       http://pear.php.net/package/PHPUnit2
  46.  * @since      File available since Release 2.0.0
  47.  */
  48.  
  49. require_once 'PHPUnit2/Framework/Assert.php';
  50. require_once 'PHPUnit2/Framework/AssertionFailedError.php';
  51.  
  52. /**
  53.  * Thrown when an assertion for string equality failed.
  54.  *
  55.  * @category   Testing
  56.  * @package    PHPUnit2
  57.  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
  58.  * @copyright  2002-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>
  59.  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
  60.  * @version    Release: 2.3.6
  61.  * @link       http://pear.php.net/package/PHPUnit2
  62.  * @since      Class available since Release 2.0.0
  63.  */
  64. class PHPUnit2_Framework_ComparisonFailure extends PHPUnit2_Framework_AssertionFailedError {
  65.     /**
  66.      * @var    string
  67.      * @access private
  68.      */
  69.     private $expected = '';
  70.  
  71.     /**
  72.      * @var    string
  73.      * @access private
  74.      */
  75.     private $actual = '';
  76.  
  77.     /**
  78.      * Constructs a comparison failure.
  79.      *
  80.      * @param  string $expected
  81.      * @param  string $actual
  82.      * @param  string $message
  83.      * @access public
  84.      */
  85.     public function __construct($expected, $actual, $message = '') {
  86.         parent::__construct($message);
  87.  
  88.         $this->expected = ($expected === NULL) ? 'NULL' : $expected;
  89.         $this->actual   = ($actual   === NULL) ? 'NULL' : $actual;
  90.     }
  91.  
  92.     /**
  93.      * Returns "..." in place of common prefix and "..." in
  94.      * place of common suffix between expected and actual.
  95.      *
  96.      * @return string
  97.      * @access public
  98.      */
  99.     public function toString() {
  100.         $end = min(strlen($this->expected), strlen($this->actual));
  101.         $i   = 0;
  102.         $j   = strlen($this->expected) - 1;
  103.         $k   = strlen($this->actual)   - 1;
  104.  
  105.         for (; $i < $end; $i++) {
  106.             if ($this->expected[$i] != $this->actual[$i]) {
  107.                 break;
  108.             }
  109.         }
  110.  
  111.         for (; $k >= $i && $j >= $i; $k--,$j--) {
  112.             if ($this->expected[$j] != $this->actual[$k]) {
  113.                 break;
  114.             }
  115.         }
  116.  
  117.         if ($j < $i && $k < $i) {
  118.             $expected = $this->expected;
  119.             $actual   = $this->actual;
  120.         } else {
  121.             $expected = substr($this->expected, $i, ($j + 1 - $i));
  122.             $actual   = substr($this->actual,   $i, ($k + 1 - $i));;
  123.  
  124.             if ($i <= $end && $i > 0) {
  125.                 $expected = '...' . $expected;
  126.                 $actual   = '...' . $actual;
  127.             }
  128.       
  129.             if ($j < strlen($this->expected) - 1) {
  130.                 $expected .= '...';
  131.             }
  132.  
  133.             if ($k < strlen($this->actual) - 1) {
  134.                 $actual .= '...';
  135.             }
  136.         }
  137.  
  138.         return PHPUnit2_Framework_Assert::format(
  139.             $expected,
  140.             $actual,
  141.             parent::getMessage()
  142.         );
  143.     }
  144. }
  145.  
  146. /*
  147.  * Local variables:
  148.  * tab-width: 4
  149.  * c-basic-offset: 4
  150.  * c-hanging-comment-ender-p: nil
  151.  * End:
  152.  */
  153. ?>
  154.