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