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

Added substr()

This commit is contained in:
Daniel St. Jules
2013-07-27 13:09:11 -04:00
parent 95e706fe31
commit d757ce38de
6 changed files with 93 additions and 1 deletions

View File

@@ -529,6 +529,23 @@ class CommonTest extends PHPUnit_Framework_TestCase
return $testData;
}
public function stringsForSubstr()
{
$testData = array(
array('foo bar', 'foo bar', 0),
array('bar', 'foo bar', 4),
array('bar', 'foo bar', 4, null),
array('o b', 'foo bar', 2, 3),
array('', 'foo bar', 4, 0),
array('fòô bàř', 'fòô bàř', 0, null, 'UTF-8'),
array('bàř', 'fòô bàř', 4, null, 'UTF-8'),
array('ô b', 'fòô bàř', 2, 3, 'UTF-8'),
array('', 'fòô bàř', 4, 0, '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

@@ -307,4 +307,14 @@ class StaticStringyTestCase extends CommonTest
$result = S::length($str, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForSubstr
*/
public function testSubstr($expected, $str, $start, $length = null,
$encoding = null)
{
$result = S::substr($str, $start, $length, $encoding);
$this->assertEquals($expected, $result);
}
}

View File

@@ -299,4 +299,14 @@ class StringyTestCase extends CommonTest
$result = S::create($str, $encoding)->length();
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForSubstr
*/
public function testSubstr($expected, $str, $start, $length = null,
$encoding = null)
{
$result = S::create($str, $encoding)->substr($start, $length);
$this->assertEquals($expected, $result);
}
}