PHP Classes
elePHPant
Icontem

File: StringDiffTest.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Philipp Strazny  >  PHP String Diff Class  >  StringDiffTest.php  >  Download  
File: StringDiffTest.php
Role: Unit test script
Content type: text/plain
Description: testing all relevant functionality
Class: PHP String Diff Class
Compare and find differences between two strings
Author: By
Last change:
Date: 2 years ago
Size: 5,175 bytes
 

Contents

Class file image Download
<?php

require_once '/data/priv/prog/diff/StringDiff.php';

/**
 * Test class for StringDiff.
 * Generated by PHPUnit on 2013-02-10 at 22:02:43.
 */
class StringDiffTest extends PHPUnit_Framework_TestCase
{

   
    public function
testTripartite()
    {
       
$s = 'abc';
       
$middle = 'b';
       
$expected = array('a', 'b', 'c');
       
$actual = StringDiff::tripartite($s, $middle);
       
$this->assertEquals($expected, $actual, 'nice split in middle');
    }
    public function
testTripartiteZeroHead()
    {
       
$s = 'bc';
       
$middle = 'b';
       
$expected = array('', 'b', 'c');
       
$actual = StringDiff::tripartite($s, $middle);
       
$this->assertEquals($expected, $actual, 'split with empty head');
    }
    public function
testTripartiteZeroTail()
    {
       
$s = 'ab';
       
$middle = 'b';
       
$expected = array('a', 'b', '');
       
$actual = StringDiff::tripartite($s, $middle);
       
$this->assertEquals($expected, $actual, 'split with empty tail');
    }
    public function
testTripartiteZeroHeadAndTail()
    {
       
$s = 'b';
       
$middle = 'b';
       
$expected = array('', 'b', '');
       
$actual = StringDiff::tripartite($s, $middle);
       
$this->assertEquals($expected, $actual, 'split with empty head and tail');
    }

    public function
testDiffEmptyHead ()
    {
       
$a = 'ab';
       
$b = 'ac';
       
$expected = 'a<del>b</del><ins>c</ins>';
       
$actual = StringDiff::diff($a, $b);
       
$this->assertEquals($expected, $actual);
    }
    public function
testDiffEmptyTail ()
    {
       
$a = 'ab';
       
$b = 'cb';
       
$expected = '<del>a</del><ins>c</ins>b';
       
$actual = StringDiff::diff($a, $b);
       
$this->assertEquals($expected, $actual);
    }
    public function
testDiffEmptyHeadAndTail ()
    {
       
$a = 'a';
       
$b = 'a';
       
$expected = 'a';
       
$actual = StringDiff::diff($a, $b);
       
$this->assertEquals($expected, $actual);
    }
    public function
testDiffWithRecursion ()
    {
       
$a = 'abc aaabbcc dbc';
       
$b = 'adc aaabbcc dac';
       
$expected = 'a<del>b</del><ins>d</ins>c aaabbcc d<del>b</del><ins>a</ins>c';
       
$actual = StringDiff::diff($a, $b);
       
$this->assertEquals($expected, $actual);
    }
    public function
testDiffWithThai ()
    {
       
$a = 'อธิบายเกี่ยวกับนโยบายการขอเบิกเงินล่วงหน้าของพนักงาน';
       
$b = 'อธิบายเกี่ยวกับนโยบายการขอเบิกวงหน้าของพนักงาน';
       
$expected = 'อธิบายเกี่ยวกับนโยบายการขอเบิก<del>เงินล่</del>วงหน้าของพนักงาน';
       
$actual = StringDiff::diff($a, $b);
       
$this->assertEquals($expected, $actual);
    }
   
    public function
testForLackOfOverlap(){
       
$a = 'xxxxxxxxxxxxxxaaxxxxxxxxxxxxxx';
       
$b = 'yyyyyyyyyyyyyyaayyyyyyyyyyyyyy';
       
$expected = '<del>xxxxxxxxxxxxxxaaxxxxxxxxxxxxxx</del><ins>yyyyyyyyyyyyyyaayyyyyyyyyyyyyy</ins>';
       
$actual = StringDiff::diff($a, $b);
       
$this->assertEquals($expected, $actual);
       
$actual = StringDiff::diff($a, $b, 100.0); //pushing the overlap threshold down
       
$expected = '<del>xxxxxxxxxxxxxx</del><ins>yyyyyyyyyyyyyy</ins>aa<del>xxxxxxxxxxxxxx</del><ins>yyyyyyyyyyyyyy</ins>';
       
$this->assertEquals($expected, $actual);
    }
   
    public function
testOverlap(){
       
$a = 'xxxxxaaaaaaaaxxxxx';
       
$b = 'yyyyyaaaaaaaayyyyy';
       
$expected = 'xxxxx<overlap>aaaaaaaa</overlap>xxxxx';
       
$actual = StringDiff::overlap($a, $b);
       
$this->assertEquals($expected, $actual);
       
$expected = 'yyyyy<overlap>aaaaaaaa</overlap>yyyyy';
       
$actual = StringDiff::overlap($b, $a);
       
$this->assertEquals($expected, $actual);
       
$a = 'xxxxxaxxxxx';
       
$b = 'yyyyyayyyyy';
       
$expected = 'xxxxxaxxxxx';
       
$actual = StringDiff::overlap($a, $b);//threshold 3.0 too high
       
$this->assertEquals($expected, $actual);
       
$expected = 'xxxxx<overlap>a</overlap>xxxxx';
       
$actual = StringDiff::overlap($a, $b, 100.0);//pushing threshold down
       
$this->assertEquals($expected, $actual);
    }
   
    public function
testDiffEmptyArgs(){
       
$a = '';
       
$b = '';
       
$expected = '';
       
$actual = StringDiff::diff($a, $b);
       
$this->assertEquals($expected, $actual);
    }
    public function
testDiffEmptyA(){
       
$a = '';
       
$b = 'xxx';
       
$expected = '<ins>xxx</ins>';
       
$actual = StringDiff::diff($a, $b);
       
$this->assertEquals($expected, $actual);
    }
    public function
testDiffEmptyB(){
       
$a = 'xxx';
       
$b = '';
       
$expected = '<del>xxx</del>';
       
$actual = StringDiff::diff($a, $b);
       
$this->assertEquals($expected, $actual);
    }
    public function
testOverlapEmptyArgs(){
       
$a = '';
       
$b = '';
       
$expected = '';
       
$actual = StringDiff::overlap($a, $b);
       
$this->assertEquals($expected, $actual);
    }
    public function
testOverlapEmptyA(){
       
$a = '';
       
$b = 'xxx';
       
$expected = '';
       
$actual = StringDiff::overlap($a, $b);
       
$this->assertEquals($expected, $actual);
    }
    public function
testOverlapEmptyB(){
       
$a = 'xxx';
       
$b = '';
       
$expected = 'xxx';
       
$actual = StringDiff::overlap($a, $b);
       
$this->assertEquals($expected, $actual);
    }
   
}
?>