Copyright (c) 2009 Chris Boulton All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither the name of the Chris Boulton nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Пример #1
0
 /**
  * Get the difference between the supplied strings.
  *
  * @param string $from The from value.
  * @param string $to   The to value.
  *
  * @return string The difference.
  */
 public function difference($from, $to)
 {
     $flags = PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY;
     $from = preg_split('/(\\W+)/u', $from, -1, $flags);
     $to = preg_split('/(\\W+)/u', $to, -1, $flags);
     $matcher = new DifferenceSequenceMatcher($from, $to);
     $diff = '';
     foreach ($matcher->getOpcodes() as $opcode) {
         list($tag, $i1, $i2, $j1, $j2) = $opcode;
         if ($tag === 'equal') {
             $diff .= implode(array_slice($from, $i1, $i2 - $i1));
         } else {
             if ($tag === 'replace' || $tag === 'delete') {
                 $diff .= $this->removeStart . implode(array_slice($from, $i1, $i2 - $i1)) . $this->removeEnd;
             }
             if ($tag === 'replace' || $tag === 'insert') {
                 $diff .= $this->addStart . implode(array_slice($to, $j1, $j2 - $j1)) . $this->addEnd;
             }
         }
     }
     return $diff;
 }
Пример #2
0
 /**
  * Render a failed inOrder() verification.
  *
  * @param array<Event> $expected The expected events.
  * @param array<Event> $actual   The actual events.
  *
  * @return string The rendered failure message.
  */
 public function renderInOrder(array $expected, array $actual)
 {
     if (empty($expected)) {
         return $this->reset . 'Expected events.' . PHP_EOL . $this->failStart . 'No events recorded.' . $this->reset;
     }
     $from = $this->renderEvents($expected);
     $to = $this->renderEvents($actual);
     $matcher = new DifferenceSequenceMatcher($from, $to);
     $diff = array();
     foreach ($matcher->getOpcodes() as $opcode) {
         list($tag, $i1, $i2, $j1, $j2) = $opcode;
         if ($tag === 'equal') {
             foreach (array_slice($from, $i1, $i2 - $i1) as $event) {
                 $diff[] = '    ' . $this->pass . '   ' . $event;
             }
         } else {
             if ($tag === 'replace' || $tag === 'delete') {
                 foreach (array_slice($from, $i1, $i2 - $i1) as $event) {
                     $diff[] = '    ' . $this->fail . ' ' . $this->removeStart . $event . $this->removeEnd;
                 }
             }
             if ($tag === 'replace' || $tag === 'insert') {
                 foreach (array_slice($to, $j1, $j2 - $j1) as $event) {
                     $diff[] = '    - ' . $this->addStart . $event . $this->addEnd;
                 }
             }
         }
     }
     $renderedExpected = array();
     foreach ($from as $event) {
         $renderedExpected[] = '    - ' . $event;
     }
     $renderedActual = array();
     foreach ($to as $event) {
         $renderedActual[] = '    - ' . $event;
     }
     return $this->reset . 'Expected events in order:' . PHP_EOL . implode(PHP_EOL, $renderedExpected) . PHP_EOL . 'Actual order:' . PHP_EOL . implode(PHP_EOL, $renderedActual) . PHP_EOL . 'Difference:' . PHP_EOL . implode(PHP_EOL, $diff);
 }