Added unit tests for Str::limit and limit_words. Also fixed bug in limit with multibyte characters.

This commit is contained in:
Eric Barnes 2011-10-11 16:35:54 -04:00
parent d9512221dc
commit aa607c9962
2 changed files with 14 additions and 1 deletions

View File

@ -118,7 +118,7 @@ public static function limit($value, $length = 100, $end = '...')
if (function_exists('mb_substr')) if (function_exists('mb_substr'))
{ {
return mb_substr($value, 0, $length).$end; return mb_substr($value, 0, $length, Config::get('application.encoding')).$end;
} }
return substr($value, 0, $length).$end; return substr($value, 0, $length).$end;

View File

@ -35,4 +35,17 @@ public function test_random()
{ {
$this->assertEquals(5, strlen(Laravel\Str::random(5))); $this->assertEquals(5, strlen(Laravel\Str::random(5)));
} }
public function test_limit()
{
$this->assertEquals('Thi...', Laravel\Str::limit('This is a string of text', 3, '...'));
$this->assertEquals('This is ', Laravel\Str::limit('This is a string of text', 7, ' '));
$this->assertEquals('τάχ', Laravel\Str::limit('τάχιστη', 3, ''));
}
public function test_limit_words()
{
$this->assertEquals('This is a...', Laravel\Str::limit_words('This is a string of text', 3, '...'));
$this->assertEquals('This is a string ', Laravel\Str::limit_words('This is a string of text', 4, ' '));
}
} }