blob: 2d7f96aa5edff7d3cc5612665e06b1dfb765302a [file] [log] [blame]
Greg Aker052b01d2011-04-21 14:38:03 -05001<?php
2
3require_once(BASEPATH.'helpers/string_helper.php');
4
Greg Akerb4d93db2011-04-21 14:42:33 -05005class String_helper_test extends CI_TestCase
Greg Aker052b01d2011-04-21 14:38:03 -05006{
Eric Barnes68286a42011-04-21 22:00:33 -04007 public function test_trim_slashes()
Greg Aker052b01d2011-04-21 14:38:03 -05008 {
9 $strs = array(
10 '//Slashes//\/' => 'Slashes//\\',
11 '/var/www/html/' => 'var/www/html'
12 );
Eric Barnes78ec0602011-11-27 00:48:56 -050013
Greg Aker052b01d2011-04-21 14:38:03 -050014 foreach ($strs as $str => $expect)
15 {
16 $this->assertEquals($expect, trim_slashes($str));
17 }
18 }
Eric Barnes78ec0602011-11-27 00:48:56 -050019
20 // --------------------------------------------------------------------
Greg Aker052b01d2011-04-21 14:38:03 -050021
Eric Barnes68286a42011-04-21 22:00:33 -040022 public function test_strip_quotes()
Greg Aker052b01d2011-04-21 14:38:03 -050023 {
24 $strs = array(
25 '"me oh my!"' => 'me oh my!',
26 "it's a winner!" => 'its a winner!',
27 );
Eric Barnes78ec0602011-11-27 00:48:56 -050028
Greg Aker052b01d2011-04-21 14:38:03 -050029 foreach ($strs as $str => $expect)
30 {
31 $this->assertEquals($expect, strip_quotes($str));
32 }
33 }
34
Eric Barnes78ec0602011-11-27 00:48:56 -050035 // --------------------------------------------------------------------
36
Eric Barnes68286a42011-04-21 22:00:33 -040037 public function test_quotes_to_entities()
Greg Aker052b01d2011-04-21 14:38:03 -050038 {
39 $strs = array(
40 '"me oh my!"' => '&quot;me oh my!&quot;',
41 "it's a winner!" => 'it&#39;s a winner!',
42 );
Eric Barnes78ec0602011-11-27 00:48:56 -050043
Greg Aker052b01d2011-04-21 14:38:03 -050044 foreach ($strs as $str => $expect)
45 {
46 $this->assertEquals($expect, quotes_to_entities($str));
Eric Barnes78ec0602011-11-27 00:48:56 -050047 }
Greg Aker052b01d2011-04-21 14:38:03 -050048 }
49
Eric Barnes78ec0602011-11-27 00:48:56 -050050 // --------------------------------------------------------------------
51
Eric Barnes68286a42011-04-21 22:00:33 -040052 public function test_reduce_double_slashes()
Greg Aker052b01d2011-04-21 14:38:03 -050053 {
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 );
Eric Barnes78ec0602011-11-27 00:48:56 -050059
Greg Aker052b01d2011-04-21 14:38:03 -050060 foreach ($strs as $str => $expect)
61 {
62 $this->assertEquals($expect, reduce_double_slashes($str));
Eric Barnes78ec0602011-11-27 00:48:56 -050063 }
Greg Aker052b01d2011-04-21 14:38:03 -050064 }
65
Eric Barnes78ec0602011-11-27 00:48:56 -050066 // --------------------------------------------------------------------
67
Eric Barnes68286a42011-04-21 22:00:33 -040068 public function test_reduce_multiples()
Greg Aker052b01d2011-04-21 14:38:03 -050069 {
70 $strs = array(
71 'Fred, Bill,, Joe, Jimmy' => 'Fred, Bill, Joe, Jimmy',
72 'Ringo, John, Paul,,' => 'Ringo, John, Paul,'
73 );
Eric Barnes78ec0602011-11-27 00:48:56 -050074
Greg Aker052b01d2011-04-21 14:38:03 -050075 foreach ($strs as $str => $expect)
76 {
77 $this->assertEquals($expect, reduce_multiples($str));
78 }
Eric Barnes78ec0602011-11-27 00:48:56 -050079
Greg Aker052b01d2011-04-21 14:38:03 -050080 $strs = array(
81 'Fred, Bill,, Joe, Jimmy' => 'Fred, Bill, Joe, Jimmy',
82 'Ringo, John, Paul,,' => 'Ringo, John, Paul'
Eric Barnes78ec0602011-11-27 00:48:56 -050083 );
Greg Aker052b01d2011-04-21 14:38:03 -050084
85 foreach ($strs as $str => $expect)
86 {
87 $this->assertEquals($expect, reduce_multiples($str, ',', TRUE));
Eric Barnes78ec0602011-11-27 00:48:56 -050088 }
Greg Aker052b01d2011-04-21 14:38:03 -050089 }
Eric Barnes78ec0602011-11-27 00:48:56 -050090
91 // --------------------------------------------------------------------
92
Eric Barnes68286a42011-04-21 22:00:33 -040093 public function test_repeater()
Greg Aker052b01d2011-04-21 14:38:03 -050094 {
95 $strs = array(
96 'a' => 'aaaaaaaaaa',
97 '&nbsp;' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
98 '<br>' => '<br><br><br><br><br><br><br><br><br><br>'
Eric Barnes78ec0602011-11-27 00:48:56 -050099
Greg Aker052b01d2011-04-21 14:38:03 -0500100 );
Eric Barnes78ec0602011-11-27 00:48:56 -0500101
Greg Aker052b01d2011-04-21 14:38:03 -0500102 foreach ($strs as $str => $expect)
103 {
104 $this->assertEquals($expect, repeater($str, 10));
105 }
Eric Barnes78ec0602011-11-27 00:48:56 -0500106 }
Greg Aker052b01d2011-04-21 14:38:03 -0500107
Eric Barnes78ec0602011-11-27 00:48:56 -0500108 // --------------------------------------------------------------------
Eric Barnes68286a42011-04-21 22:00:33 -0400109
110 public function test_random_string()
111 {
112 $this->assertEquals(16, strlen(random_string('alnum', 16)));
113 $this->assertEquals(32, strlen(random_string('unique', 16)));
114 $this->assertInternalType('string', random_string('numeric', 16));
115 }
Eric Barnes78ec0602011-11-27 00:48:56 -0500116
117 // --------------------------------------------------------------------
118
119 public function test_increment_string()
120 {
121 $this->assertEquals('my-test_1', increment_string('my-test'));
122 $this->assertEquals('my-test-1', increment_string('my-test', '-'));
123 $this->assertEquals('file_5', increment_string('file_4'));
124 $this->assertEquals('file-5', increment_string('file-4', '-'));
125 $this->assertEquals('file-5', increment_string('file-4', '-'));
126 $this->assertEquals('file-1', increment_string('file', '-', '1'));
127 $this->assertEquals(124, increment_string('123', ''));
128 }
Greg Aker052b01d2011-04-21 14:38:03 -0500129}