1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-08-18 02:51:19 +02:00

Add longestCommonPrefix and longestCommonSuffix

This commit is contained in:
Daniel St. Jules
2013-07-26 23:55:52 -04:00
parent 3e78d2b75d
commit aeef5493c0
6 changed files with 183 additions and 5 deletions

View File

@@ -463,6 +463,42 @@ class CommonTest extends PHPUnit_Framework_TestCase
return $testData;
}
public function stringsForLongestCommonPrefix()
{
$testData = array(
array('foo', 'foobar', 'foo bar'),
array('foo bar', 'foo bar', 'foo bar'),
array('f', 'foo bar', 'far boo'),
array('', 'toy car', 'foo bar'),
array('', 'foo bar', ''),
array('fòô', 'fòôbar', 'fòô bar', 'UTF-8'),
array('fòô bar', 'fòô bar', 'fòô bar', 'UTF-8'),
array('fò', 'fòô bar', 'fòr bar', 'UTF-8'),
array('', 'toy car', 'fòô bar', 'UTF-8'),
array('', 'fòô bar', '', 'UTF-8'),
);
return $testData;
}
public function stringsForLongestCommonSuffix()
{
$testData = array(
array('bar', 'foobar', 'foo bar'),
array('foo bar', 'foo bar', 'foo bar'),
array('ar', 'foo bar', 'boo far'),
array('', 'foo bad', 'foo bar'),
array('', 'foo bar', ''),
array('bàř', 'fòôbàř', 'fòô bàř', 'UTF-8'),
array('fòô bàř', 'fòô bàř', 'fòô bàř', 'UTF-8'),
array(' bàř', 'fòô bàř', 'fòr bàř', 'UTF-8'),
array('', 'toy car', 'fòô bàř', 'UTF-8'),
array('', 'fòô bàř', '', 'UTF-8'),
);
return $testData;
}
// A test is required so as not to throw an error
// This is a lot cleaner than using PHPUnit's mocks to spy
public function test() {

View File

@@ -235,7 +235,7 @@ class StaticStringyTestCase extends CommonTest
* @dataProvider stringsForSafeTruncate
*/
public function testSafeTruncate($expected, $str, $length, $substring = '',
$encoding = null)
$encoding = null)
{
$result = S::safeTruncate($str, $length, $substring, $encoding);
$this->assertEquals($expected, $result);
@@ -268,4 +268,24 @@ class StaticStringyTestCase extends CommonTest
$result = S::trim($str);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForLongestCommonPrefix
*/
public function testLongestCommonPrefix($expected, $str, $otherString,
$encoding = null)
{
$result = S::longestCommonPrefix($str, $otherString, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForLongestCommonSuffix
*/
public function testLongestCommonSuffix($expected, $str, $otherString,
$encoding = null)
{
$result = S::longestCommonSuffix($str, $otherString, $encoding);
$this->assertEquals($expected, $result);
}
}

View File

@@ -235,7 +235,7 @@ class StringyTestCase extends CommonTest
* @dataProvider stringsForSafeTruncate
*/
public function testSafeTruncate($expected, $str, $length, $substring = '',
$encoding = null)
$encoding = null)
{
$result = S::create($str, $encoding)->safeTruncate($length, $substring);
$this->assertEquals($expected, $result);
@@ -268,4 +268,26 @@ class StringyTestCase extends CommonTest
$result = S::create($str)->trim();
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForLongestCommonPrefix
*/
public function testLongestCommonPrefix($expected, $str, $otherString,
$encoding = null)
{
$result = S::create($str, $encoding)
->longestCommonPrefix($otherString);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForLongestCommonSuffix
*/
public function testLongestCommonSuffix($expected, $str, $otherString,
$encoding = null)
{
$result = S::create($str, $encoding)
->longestCommonSuffix($otherString);
$this->assertEquals($expected, $result);
}
}