Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 1 | <?php |
| 2 | |
Taufan Aditya | e1dc9ea | 2012-03-28 16:49:49 +0700 | [diff] [blame] | 3 | class String_helper_test extends CI_TestCase { |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 4 | |
Taufan Aditya | e1dc9ea | 2012-03-28 16:49:49 +0700 | [diff] [blame] | 5 | public function set_up() |
| 6 | { |
| 7 | $this->helper('string'); |
| 8 | } |
| 9 | |
tiyowan | 3fb5aa5 | 2012-03-16 19:08:34 +0400 | [diff] [blame] | 10 | public function test_strip_slashes() |
| 11 | { |
| 12 | $expected = array( |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 13 | "Is your name O'reilly?", |
tiyowan | 3fb5aa5 | 2012-03-16 19:08:34 +0400 | [diff] [blame] | 14 | "No, my name is O'connor." |
| 15 | ); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 16 | |
tiyowan | 3fb5aa5 | 2012-03-16 19:08:34 +0400 | [diff] [blame] | 17 | $str = array( |
| 18 | "Is your name O\'reilly?", |
| 19 | "No, my name is O\'connor." |
| 20 | ); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 21 | |
tiyowan | 3fb5aa5 | 2012-03-16 19:08:34 +0400 | [diff] [blame] | 22 | $this->assertEquals($expected, strip_slashes($str)); |
| 23 | } |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 24 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 25 | public function test_trim_slashes() |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 26 | { |
| 27 | $strs = array( |
| 28 | '//Slashes//\/' => 'Slashes//\\', |
| 29 | '/var/www/html/' => 'var/www/html' |
| 30 | ); |
Eric Barnes | 78ec060 | 2011-11-27 00:48:56 -0500 | [diff] [blame] | 31 | |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 32 | foreach ($strs as $str => $expect) |
| 33 | { |
| 34 | $this->assertEquals($expect, trim_slashes($str)); |
| 35 | } |
| 36 | } |
Eric Barnes | 78ec060 | 2011-11-27 00:48:56 -0500 | [diff] [blame] | 37 | |
| 38 | // -------------------------------------------------------------------- |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 39 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 40 | public function test_strip_quotes() |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 41 | { |
| 42 | $strs = array( |
| 43 | '"me oh my!"' => 'me oh my!', |
| 44 | "it's a winner!" => 'its a winner!', |
| 45 | ); |
Eric Barnes | 78ec060 | 2011-11-27 00:48:56 -0500 | [diff] [blame] | 46 | |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 47 | foreach ($strs as $str => $expect) |
| 48 | { |
| 49 | $this->assertEquals($expect, strip_quotes($str)); |
| 50 | } |
| 51 | } |
| 52 | |
Eric Barnes | 78ec060 | 2011-11-27 00:48:56 -0500 | [diff] [blame] | 53 | // -------------------------------------------------------------------- |
| 54 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 55 | public function test_quotes_to_entities() |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 56 | { |
| 57 | $strs = array( |
| 58 | '"me oh my!"' => '"me oh my!"', |
| 59 | "it's a winner!" => 'it's a winner!', |
| 60 | ); |
Eric Barnes | 78ec060 | 2011-11-27 00:48:56 -0500 | [diff] [blame] | 61 | |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 62 | foreach ($strs as $str => $expect) |
| 63 | { |
| 64 | $this->assertEquals($expect, quotes_to_entities($str)); |
Eric Barnes | 78ec060 | 2011-11-27 00:48:56 -0500 | [diff] [blame] | 65 | } |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 66 | } |
| 67 | |
Eric Barnes | 78ec060 | 2011-11-27 00:48:56 -0500 | [diff] [blame] | 68 | // -------------------------------------------------------------------- |
| 69 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 70 | public function test_reduce_double_slashes() |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 71 | { |
| 72 | $strs = array( |
| 73 | 'http://codeigniter.com' => 'http://codeigniter.com', |
| 74 | '//var/www/html/example.com/' => '/var/www/html/example.com/', |
| 75 | '/var/www/html//index.php' => '/var/www/html/index.php' |
| 76 | ); |
Eric Barnes | 78ec060 | 2011-11-27 00:48:56 -0500 | [diff] [blame] | 77 | |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 78 | foreach ($strs as $str => $expect) |
| 79 | { |
| 80 | $this->assertEquals($expect, reduce_double_slashes($str)); |
Eric Barnes | 78ec060 | 2011-11-27 00:48:56 -0500 | [diff] [blame] | 81 | } |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 82 | } |
| 83 | |
Eric Barnes | 78ec060 | 2011-11-27 00:48:56 -0500 | [diff] [blame] | 84 | // -------------------------------------------------------------------- |
| 85 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 86 | public function test_reduce_multiples() |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 87 | { |
| 88 | $strs = array( |
| 89 | 'Fred, Bill,, Joe, Jimmy' => 'Fred, Bill, Joe, Jimmy', |
| 90 | 'Ringo, John, Paul,,' => 'Ringo, John, Paul,' |
| 91 | ); |
Eric Barnes | 78ec060 | 2011-11-27 00:48:56 -0500 | [diff] [blame] | 92 | |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 93 | foreach ($strs as $str => $expect) |
| 94 | { |
| 95 | $this->assertEquals($expect, reduce_multiples($str)); |
| 96 | } |
Eric Barnes | 78ec060 | 2011-11-27 00:48:56 -0500 | [diff] [blame] | 97 | |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 98 | $strs = array( |
| 99 | 'Fred, Bill,, Joe, Jimmy' => 'Fred, Bill, Joe, Jimmy', |
| 100 | 'Ringo, John, Paul,,' => 'Ringo, John, Paul' |
Eric Barnes | 78ec060 | 2011-11-27 00:48:56 -0500 | [diff] [blame] | 101 | ); |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 102 | |
| 103 | foreach ($strs as $str => $expect) |
| 104 | { |
| 105 | $this->assertEquals($expect, reduce_multiples($str, ',', TRUE)); |
Eric Barnes | 78ec060 | 2011-11-27 00:48:56 -0500 | [diff] [blame] | 106 | } |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 107 | } |
Eric Barnes | 78ec060 | 2011-11-27 00:48:56 -0500 | [diff] [blame] | 108 | |
| 109 | // -------------------------------------------------------------------- |
| 110 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 111 | public function test_repeater() |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 112 | { |
| 113 | $strs = array( |
| 114 | 'a' => 'aaaaaaaaaa', |
| 115 | ' ' => ' ', |
| 116 | '<br>' => '<br><br><br><br><br><br><br><br><br><br>' |
Eric Barnes | 78ec060 | 2011-11-27 00:48:56 -0500 | [diff] [blame] | 117 | |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 118 | ); |
Eric Barnes | 78ec060 | 2011-11-27 00:48:56 -0500 | [diff] [blame] | 119 | |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 120 | foreach ($strs as $str => $expect) |
| 121 | { |
| 122 | $this->assertEquals($expect, repeater($str, 10)); |
| 123 | } |
Eric Barnes | 78ec060 | 2011-11-27 00:48:56 -0500 | [diff] [blame] | 124 | } |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 125 | |
Eric Barnes | 78ec060 | 2011-11-27 00:48:56 -0500 | [diff] [blame] | 126 | // -------------------------------------------------------------------- |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 127 | |
| 128 | public function test_random_string() |
| 129 | { |
| 130 | $this->assertEquals(16, strlen(random_string('alnum', 16))); |
| 131 | $this->assertEquals(32, strlen(random_string('unique', 16))); |
| 132 | $this->assertInternalType('string', random_string('numeric', 16)); |
| 133 | } |
Eric Barnes | 78ec060 | 2011-11-27 00:48:56 -0500 | [diff] [blame] | 134 | |
| 135 | // -------------------------------------------------------------------- |
| 136 | |
| 137 | public function test_increment_string() |
| 138 | { |
| 139 | $this->assertEquals('my-test_1', increment_string('my-test')); |
| 140 | $this->assertEquals('my-test-1', increment_string('my-test', '-')); |
| 141 | $this->assertEquals('file_5', increment_string('file_4')); |
| 142 | $this->assertEquals('file-5', increment_string('file-4', '-')); |
| 143 | $this->assertEquals('file-5', increment_string('file-4', '-')); |
| 144 | $this->assertEquals('file-1', increment_string('file', '-', '1')); |
| 145 | $this->assertEquals(124, increment_string('123', '')); |
| 146 | } |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 147 | |
Greg Aker | 052b01d | 2011-04-21 14:38:03 -0500 | [diff] [blame] | 148 | } |