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

Update tests for pad, add padLeft, padRight and padBoth

This commit is contained in:
Daniel St. Jules
2013-07-17 00:43:44 -04:00
parent c1bfe35f57
commit e85687fd5b
3 changed files with 161 additions and 17 deletions

View File

@@ -17,6 +17,9 @@ A PHP library with a variety of string manipulation functions with multibyte sup
* [clean](#clean) * [clean](#clean)
* [standardize](#standardize) * [standardize](#standardize)
* [pad](#pad) * [pad](#pad)
* [padLeft](#padleft)
* [padRight](#padright)
* [padBoth](#padboth)
* [Tests](#tests) * [Tests](#tests)
* [License](#license) * [License](#license)
@@ -203,9 +206,40 @@ default string used for padding is a space, and the default type (one of
S::pad('fòô bàř', 10, '¬ø', 'left', 'UTF-8'); // '¬ø¬fòô bàř' S::pad('fòô bàř', 10, '¬ø', 'left', 'UTF-8'); // '¬ø¬fòô bàř'
``` ```
## TODO ##### padLeft
**center** S::padLeft(string $str , int $length [, string $padStr [, string $encoding]])
Returns a new string of a given length such that the beginning of the
string is padded. Alias for pad($str, $length, $padStr, 'left', $encoding)
```php
S::padLeft('foo bar', 9, ' '); // ' foo bar'
```
##### padRight
S::padRight(string $str , int $length [, string $padStr [, string $encoding]])
Returns a new string of a given length such that the end of the string is
padded. Alias for pad($str, $length, $padStr, 'right', $encoding)
```php
S::padRight('foo bar', 10, '_*'); // 'foo bar_*_'
```
##### padBoth
S::padBoth(string $str , int $length [, string $padStr [, string $encoding]])
Returns a new string of a given length such that both sides of the string
string are padded. Alias for pad($str, $length, $padStr, 'both', $encoding)
```php
S::padBoth('foo bar', 9, ' '); // ' foo bar '
```
## TODO
**startsWith** **startsWith**

View File

@@ -323,6 +323,48 @@ class Stringy {
return $paddedStr; return $paddedStr;
} }
/**
* Returns a new string of a given length such that the beginning of the
* string is padded. Alias for pad($str, $length, $padStr, 'left', $encoding)
*
* @param string $str String to pad
* @param int $length Desired string length after padding
* @param string $padStr String used to pad, defaults to space
* @param string $encoding The character encoding
* @return string The padded string
*/
public static function padLeft($str, $length, $padStr = ' ', $encoding = null) {
return self::pad($str, $length, $padStr, 'left', $encoding);
}
/**
* Returns a new string of a given length such that the end of the string is
* padded. Alias for pad($str, $length, $padStr, 'right', $encoding)
*
* @param string $str String to pad
* @param int $length Desired string length after padding
* @param string $padStr String used to pad, defaults to space
* @param string $encoding The character encoding
* @return string The padded string
*/
public static function padRight($str, $length, $padStr = ' ', $encoding = null) {
return self::pad($str, $length, $padStr, 'right', $encoding);
}
/**
* Returns a new string of a given length such that both sides of the string
* string are padded. Alias for pad($str, $length, $padStr, 'both', $encoding)
*
* @param string $str String to pad
* @param int $length Desired string length after padding
* @param string $padStr String used to pad, defaults to space
* @param string $encoding The character encoding
* @return string The padded string
*/
public static function padBoth ($str, $length, $padStr = ' ', $encoding = null) {
return self::pad($str, $length, $padStr, 'both', $encoding);
}
} }
?> ?>

View File

@@ -185,7 +185,8 @@ class StringyTestCase extends PHPUnit_Framework_TestCase {
/** /**
* @dataProvider stringsForTitleize * @dataProvider stringsForTitleize
*/ */
public function testTitleize($string, $expected, $ignore = null, $encoding = null) { public function testTitleize($string, $expected, $ignore = null,
$encoding = null) {
$result = S::titleize($string, $ignore, $encoding); $result = S::titleize($string, $ignore, $encoding);
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
@@ -291,32 +292,99 @@ class StringyTestCase extends PHPUnit_Framework_TestCase {
public function stringsForPad() { public function stringsForPad() {
$testData = array( $testData = array(
// $length <= $str
array('foo bar', 'foo bar', -1), array('foo bar', 'foo bar', -1),
array('foo bar', 'foo bar', 7), array('foo bar', 'foo bar', 7),
array('fòô bàř', 'fòô bàř', 7, ' ', 'right', 'UTF-8'),
// right
array('foo bar', 'foo bar ', 9), array('foo bar', 'foo bar ', 9),
array('foo bar', ' foo bar', 9, ' ', 'left'),
array('foo bar', 'foo bar ', 8, ' ', 'both'),
array('foo bar', ' foo bar ', 9, ' ', 'both'),
array('foo bar', '_*foo bar', 9, '_*', 'left'),
array('foo bar', '_*_foo bar', 10, '_*', 'left'),
array('foo bar', 'foo bar_*', 9, '_*', 'right'), array('foo bar', 'foo bar_*', 9, '_*', 'right'),
array('foo bar', 'foo bar_*_', 10, '_*', 'right'), array('foo bar', 'foo bar_*_', 10, '_*', 'right'),
array('fòô bàř', 'fòô bàř', -1, 'UTF-8'),
array('fòô bàř', 'fòô bàř', 7, 'UTF-8'),
array('fòô bàř', 'fòô bàř ', 9, ' ', 'right', 'UTF-8'), array('fòô bàř', 'fòô bàř ', 9, ' ', 'right', 'UTF-8'),
array('fòô bàř', ' fòô bàř', 9, ' ', 'left', 'UTF-8'),
array('fòô bàř', 'fòô bàř ', 8, ' ', 'both', 'UTF-8'),
array('fòô bàř', ' fòô bàř ', 9, ' ', 'both', 'UTF-8'),
array('fòô bàř', '¬øfòô bàř', 9, '¬ø', 'left', 'UTF-8'),
array('fòô bàř', '¬ø¬fòô bàř', 10, '¬ø', 'left', 'UTF-8'),
array('fòô bàř', '¬ø¬øfòô bàř', 11, '¬ø', 'left', 'UTF-8'),
array('fòô bàř', 'fòô bàř¬ø', 9, '¬ø', 'right', 'UTF-8'), array('fòô bàř', 'fòô bàř¬ø', 9, '¬ø', 'right', 'UTF-8'),
array('fòô bàř', 'fòô bàř¬ø¬', 10, '¬ø', 'right', 'UTF-8'), array('fòô bàř', 'fòô bàř¬ø¬', 10, '¬ø', 'right', 'UTF-8'),
array('fòô bàř', 'fòô bàř¬ø¬ø', 11, '¬ø', 'right', 'UTF-8'), array('fòô bàř', 'fòô bàř¬ø¬ø', 11, '¬ø', 'right', 'UTF-8'),
// left
array('foo bar', ' foo bar', 9, ' ', 'left'),
array('foo bar', '_*foo bar', 9, '_*', 'left'),
array('foo bar', '_*_foo bar', 10, '_*', 'left'),
array('fòô bàř', ' fòô bàř', 9, ' ', 'left', 'UTF-8'),
array('fòô bàř', '¬øfòô bàř', 9, '¬ø', 'left', 'UTF-8'),
array('fòô bàř', '¬ø¬fòô bàř', 10, '¬ø', 'left', 'UTF-8'),
array('fòô bàř', '¬ø¬øfòô bàř', 11, '¬ø', 'left', 'UTF-8'),
// both
array('foo bar', 'foo bar ', 8, ' ', 'both'),
array('foo bar', ' foo bar ', 9, ' ', 'both'),
array('fòô bàř', 'fòô bàř ', 8, ' ', 'both', 'UTF-8'),
array('fòô bàř', ' fòô bàř ', 9, ' ', 'both', 'UTF-8'),
array('fòô bàř', 'fòô bàř¬', 8, '¬ø', 'both', 'UTF-8'), array('fòô bàř', 'fòô bàř¬', 8, '¬ø', 'both', 'UTF-8'),
array('fòô bàř', '¬fòô bàř¬', 9, '¬ø', 'both', 'UTF-8'), array('fòô bàř', '¬fòô bàř¬', 9, '¬ø', 'both', 'UTF-8'),
array('fòô bàř', '¬fòô bàř¬ø', 10, '¬ø', 'both', 'UTF-8'), array('fòô bàř', '¬fòô bàř¬ø', 10, '¬ø', 'both', 'UTF-8'),
array('fòô bàř', '¬øfòô bàř¬ø', 11, '¬ø', 'both', 'UTF-8'), array('fòô bàř', '¬øfòô bàř¬ø', 11, '¬ø', 'both', 'UTF-8'),
array('fòô bàř', '¬fòô bàř¬ø', 10, '¬øÿ', 'both', 'UTF-8'),
array('fòô bàř', '¬øfòô bàř¬ø', 11, '¬øÿ', 'both', 'UTF-8'),
array('fòô bàř', '¬øfòô bàř¬øÿ', 12, '¬øÿ', 'both', 'UTF-8')
);
return $testData;
}
/**
* @dataProvider stringsForPadLeft
*/
public function testPadLeft($string, $expected, $length, $padStr = ' ',
$encoding = null) {
$result = S::padLeft($string, $length, $padStr, $encoding);
$this->assertEquals($expected, $result);
}
public function stringsForPadLeft() {
$testData = array(
array('foo bar', ' foo bar', 9),
array('foo bar', '_*_foo bar', 10, '_*'),
array('fòô bàř', '¬ø¬øfòô bàř', 11, '¬ø', 'UTF-8'),
);
return $testData;
}
/**
* @dataProvider stringsForPadRight
*/
public function testPadRight($string, $expected, $length, $padStr = ' ',
$encoding = null) {
$result = S::padRight($string, $length, $padStr, $encoding);
$this->assertEquals($expected, $result);
}
public function stringsForPadRight() {
$testData = array(
array('foo bar', 'foo bar ', 9),
array('foo bar', 'foo bar_*_', 10, '_*'),
array('fòô bàř', 'fòô bàř¬ø¬ø', 11, '¬ø', 'UTF-8'),
);
return $testData;
}
/**
* @dataProvider stringsForPadBoth
*/
public function testPadBoth($string, $expected, $length, $padStr = ' ',
$encoding = null) {
$result = S::padBoth($string, $length, $padStr, $encoding);
$this->assertEquals($expected, $result);
}
public function stringsForPadBoth() {
$testData = array(
array('foo bar', 'foo bar ', 8),
array('foo bar', ' foo bar ', 9, ' '),
array('fòô bàř', '¬fòô bàř¬ø', 10, '¬øÿ', 'UTF-8'),
array('fòô bàř', '¬øfòô bàř¬øÿ', 12, '¬øÿ', 'UTF-8')
); );
return $testData; return $testData;