1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-08-12 08:14:06 +02:00

Add trimLeft, trimRight, support unicode whitespace

This commit is contained in:
Daniel St. Jules
2015-07-22 13:55:21 -07:00
parent 7763df3c3b
commit fe3368bd8b
6 changed files with 194 additions and 74 deletions

View File

@@ -630,7 +630,7 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
);
}
public function trimProviderWithoutParams()
public function trimProvider()
{
return array(
array('foo bar', ' foo bar '),
@@ -640,24 +640,49 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
array('fòô bàř', ' fòô bàř '),
array('fòô bàř', ' fòô bàř'),
array('fòô bàř', 'fòô bàř '),
array('fòô bàř', "\n\t fòô bàř \n\t")
array(' foo bar ', "\n\t foo bar \n\t", "\n\t"),
array('fòô bàř', "\n\t fòô bàř \n\t", null, 'UTF-8'),
array('fòô', 'fòô', null, 'UTF-8'), // narrow no-break space (U+202F)
array('fòô', 'fòô', null, 'UTF-8'), // medium mathematical space (U+205F)
array('fòô', ' fòô', null, 'UTF-8') // spaces U+2000 to U+200A
);
}
public function trimProviderWithParams()
public function trimLeftProvider()
{
return array(
array('foo bar', ' foo bar ', " \t\n\r\0\x0B", 'trim'),
array('foo bar', ' foo bar', " \t\n\r\0\x0B", 'trim'),
array('foo bar', 'foo bar ', " \t\n\r\0\x0B", 'trim'),
array('foo bar', "\n\t foo bar \n\t", " \t\n\r\0\x0B", 'trim'),
array('fòô bàř', ' fòô bàř ', " \t\n\r\0\x0B", 'trim'),
array('fòô bàř', ' fòô bàř', " \t\n\r\0\x0B", 'trim'),
array('fòô bàř', 'fòô bàř ', " \t\n\r\0\x0B", 'trim'),
array('fòô bàř', "\n\t fòô bàř \n\t", " \t\n\r\0\x0B", 'trim'),
array(' foo bar', ' foo bar ', " \t\n\r\0\x0B", 'rtrim'),
array('foo bar ', ' foo bar ', " \t\n\r\0\x0B", 'ltrim'),
array(' foo bar ', ' foo bar ', "\t\n\r\0\x0B", 'ltrim')
array('foo bar ', ' foo bar '),
array('foo bar', ' foo bar'),
array('foo bar ', 'foo bar '),
array("foo bar \n\t", "\n\t foo bar \n\t"),
array('fòô bàř ', ' fòô bàř '),
array('fòô bàř', ' fòô bàř'),
array('fòô bàř ', 'fòô bàř '),
array('foo bar', '--foo bar', '-'),
array('fòô bàř', 'òòfòô bàř', 'ò', 'UTF-8'),
array("fòô bàř \n\t", "\n\t fòô bàř \n\t", null, 'UTF-8'),
array('fòô', 'fòô', null, 'UTF-8'), // narrow no-break space (U+202F)
array('fòô', 'fòô', null, 'UTF-8'), // medium mathematical space (U+205F)
array('fòô', ' fòô', null, 'UTF-8') // spaces U+2000 to U+200A
);
}
public function trimRightProvider()
{
return array(
array(' foo bar', ' foo bar '),
array('foo bar', 'foo bar '),
array(' foo bar', ' foo bar'),
array("\n\t foo bar", "\n\t foo bar \n\t"),
array(' fòô bàř', ' fòô bàř '),
array('fòô bàř', 'fòô bàř '),
array(' fòô bàř', ' fòô bàř'),
array('foo bar', 'foo bar--', '-'),
array('fòô bàř', 'fòô bàřòò', 'ò', 'UTF-8'),
array("\n\t fòô bàř", "\n\t fòô bàř \n\t", null, 'UTF-8'),
array('fòô', 'fòô', null, 'UTF-8'), // narrow no-break space (U+202F)
array('fòô', 'fòô', null, 'UTF-8'), // medium mathematical space (U+205F)
array('fòô', 'fòô ', null, 'UTF-8') // spaces U+2000 to U+200A
);
}

View File

@@ -400,11 +400,33 @@ class StaticStringyTestCase extends CommonTest
}
/**
* @dataProvider trimProviderWithoutParams()
* @dataProvider trimProvider()
*/
public function testTrim($expected, $str)
public function testTrim($expected, $str, $chars = null, $encoding = null)
{
$result = S::trim($str);
$result = S::trim($str, $chars, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider trimLeftProvider()
*/
public function testTrimLeft($expected, $str, $chars = null,
$encoding = null)
{
$result = S::trimLeft($str, $chars, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider trimRightProvider()
*/
public function testTrimRight($expected, $str, $chars = null,
$encoding = null)
{
$result = S::trimRight($str, $chars, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}

View File

@@ -624,49 +624,41 @@ class StringyTestCase extends CommonTest
}
/**
* @dataProvider trimProviderWithoutParams()
* @dataProvider trimProvider()
*/
public function testTrimWithoutParams($expected, $str)
public function testTrim($expected, $str, $chars = null, $encoding = null)
{
$stringy = S::create($str);
$result = $stringy->trim();
$stringy = S::create($str, $encoding);
$result = $stringy->trim($chars);
$this->assertStringy($result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider trimProviderWithParams()
* @dataProvider trimLeftProvider()
*/
public function testTrimWithParams($expected, $str, $charList, $type)
public function testTrimLeft($expected, $str, $chars = null,
$encoding = null)
{
$stringy = S::create($str);
$result = $stringy->trim($charList, $type);
$stringy = S::create($str, $encoding);
$result = $stringy->trimLeft($chars);
$this->assertStringy($result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @expectedException InvalidArgumentException
* @dataProvider trimRightProvider()
*/
public function testTrimWithInvalidCharset()
public function testTrimRight($expected, $str, $chars = null,
$encoding = null)
{
$stringy = S::create('test');
$stringy->trim(array('test1', 'test2'), 'trim');
$this->fail('Expecting exception when the first argument passed is not a string');
}
/**
* @expectedException InvalidArgumentException
*/
public function testTrimWithInvalidType()
{
$stringy = S::create('test');
$stringy->trim(' test ', 'aa');
$stringy->trim('btest ', 'Trim');
$stringy->trim(' btest', 'RTrim');
$this->fail('Expecting exception when the first argument passed is not a string');
$stringy = S::create($str, $encoding);
$result = $stringy->trimRight($chars);
$this->assertStringy($result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**