Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | require_once(BASEPATH.'helpers/string_helper.php'); |
| 4 | |
Greg Aker | b4d93db | 2011-04-21 14:42:33 -0500 | [diff] [blame] | 5 | class String_helper_test extends CI_TestCase |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 6 | { |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 7 | public function test_trim_slashes() |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 8 | { |
| 9 | $strs = array( |
| 10 | '//Slashes//\/' => 'Slashes//\\', |
| 11 | '/var/www/html/' => 'var/www/html' |
| 12 | ); |
| 13 | |
| 14 | foreach ($strs as $str => $expect) |
| 15 | { |
| 16 | $this->assertEquals($expect, trim_slashes($str)); |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | // -------------------------------------------------------------------- |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 21 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 22 | public function test_strip_quotes() |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 23 | { |
| 24 | $strs = array( |
| 25 | '"me oh my!"' => 'me oh my!', |
| 26 | "it's a winner!" => 'its a winner!', |
| 27 | ); |
| 28 | |
| 29 | foreach ($strs as $str => $expect) |
| 30 | { |
| 31 | $this->assertEquals($expect, strip_quotes($str)); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // -------------------------------------------------------------------- |
| 36 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 37 | public function test_quotes_to_entities() |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 38 | { |
| 39 | $strs = array( |
| 40 | '"me oh my!"' => '"me oh my!"', |
| 41 | "it's a winner!" => 'it's a winner!', |
| 42 | ); |
| 43 | |
| 44 | foreach ($strs as $str => $expect) |
| 45 | { |
| 46 | $this->assertEquals($expect, quotes_to_entities($str)); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // -------------------------------------------------------------------- |
| 51 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 52 | public function test_reduce_double_slashes() |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 53 | { |
| 54 | $strs = array( |
| 55 | 'http://codeigniter.com' => 'http://codeigniter.com', |
| 56 | '//var/www/html/example.com/' => '/var/www/html/example.com/', |
| 57 | '/var/www/html//index.php' => '/var/www/html/index.php' |
| 58 | ); |
| 59 | |
| 60 | foreach ($strs as $str => $expect) |
| 61 | { |
| 62 | $this->assertEquals($expect, reduce_double_slashes($str)); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // -------------------------------------------------------------------- |
| 67 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 68 | public function test_reduce_multiples() |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 69 | { |
| 70 | $strs = array( |
| 71 | 'Fred, Bill,, Joe, Jimmy' => 'Fred, Bill, Joe, Jimmy', |
| 72 | 'Ringo, John, Paul,,' => 'Ringo, John, Paul,' |
| 73 | ); |
| 74 | |
| 75 | foreach ($strs as $str => $expect) |
| 76 | { |
| 77 | $this->assertEquals($expect, reduce_multiples($str)); |
| 78 | } |
| 79 | |
| 80 | $strs = array( |
| 81 | 'Fred, Bill,, Joe, Jimmy' => 'Fred, Bill, Joe, Jimmy', |
| 82 | 'Ringo, John, Paul,,' => 'Ringo, John, Paul' |
| 83 | ); |
| 84 | |
| 85 | foreach ($strs as $str => $expect) |
| 86 | { |
| 87 | $this->assertEquals($expect, reduce_multiples($str, ',', TRUE)); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // -------------------------------------------------------------------- |
| 92 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 93 | public function test_repeater() |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 94 | { |
| 95 | $strs = array( |
| 96 | 'a' => 'aaaaaaaaaa', |
| 97 | ' ' => ' ', |
| 98 | '<br>' => '<br><br><br><br><br><br><br><br><br><br>' |
| 99 | |
| 100 | ); |
| 101 | |
| 102 | foreach ($strs as $str => $expect) |
| 103 | { |
| 104 | $this->assertEquals($expect, repeater($str, 10)); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // -------------------------------------------------------------------- |
| 109 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 110 | |
| 111 | public function test_random_string() |
| 112 | { |
| 113 | $this->assertEquals(16, strlen(random_string('alnum', 16))); |
| 114 | $this->assertEquals(32, strlen(random_string('unique', 16))); |
| 115 | $this->assertInternalType('string', random_string('numeric', 16)); |
| 116 | } |
| 117 | |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 118 | } |