Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 1 | <?php |
| 2 | |
Taufan Aditya | e1dc9ea | 2012-03-28 16:49:49 +0700 | [diff] [blame] | 3 | class Text_helper_test extends CI_TestCase { |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 4 | |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 5 | private $_long_string; |
| 6 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 7 | public function set_up() |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 8 | { |
Taufan Aditya | e1dc9ea | 2012-03-28 16:49:49 +0700 | [diff] [blame] | 9 | $this->helper('text'); |
| 10 | |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 11 | $this->_long_string = 'Once upon a time, a framework had no tests. It sad. So some nice people began to write tests. The more time that went on, the happier it became. Everyone was happy.'; |
| 12 | } |
| 13 | |
| 14 | // ------------------------------------------------------------------------ |
| 15 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 16 | public function test_word_limiter() |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 17 | { |
| 18 | $this->assertEquals('Once upon a time,…', word_limiter($this->_long_string, 4)); |
| 19 | $this->assertEquals('Once upon a time,…', word_limiter($this->_long_string, 4, '…')); |
| 20 | $this->assertEquals('', word_limiter('', 4)); |
| 21 | } |
| 22 | |
| 23 | // ------------------------------------------------------------------------ |
| 24 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 25 | public function test_character_limiter() |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 26 | { |
| 27 | $this->assertEquals('Once upon a time, a…', character_limiter($this->_long_string, 20)); |
| 28 | $this->assertEquals('Once upon a time, a…', character_limiter($this->_long_string, 20, '…')); |
| 29 | $this->assertEquals('Short', character_limiter('Short', 20)); |
| 30 | $this->assertEquals('Short', character_limiter('Short', 5)); |
| 31 | } |
| 32 | |
| 33 | // ------------------------------------------------------------------------ |
| 34 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 35 | public function test_ascii_to_entities() |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 36 | { |
| 37 | $strs = array( |
| 38 | '“‘ “test”' => '“‘ “test”', |
| 39 | '†¥¨ˆøåß∂ƒ©˙∆˚¬' => '†¥¨ˆøåß∂ƒ©˙∆˚¬' |
| 40 | ); |
| 41 | |
| 42 | foreach ($strs as $str => $expect) |
| 43 | { |
| 44 | $this->assertEquals($expect, ascii_to_entities($str)); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // ------------------------------------------------------------------------ |
| 49 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 50 | public function test_entities_to_ascii() |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 51 | { |
| 52 | $strs = array( |
| 53 | '“‘ “test”' => '“‘ “test”', |
| 54 | '†¥¨ˆøåß∂ƒ©˙∆˚¬' => '†¥¨ˆøåß∂ƒ©˙∆˚¬' |
| 55 | ); |
| 56 | |
| 57 | foreach ($strs as $str => $expect) |
| 58 | { |
| 59 | $this->assertEquals($expect, entities_to_ascii($str)); |
| 60 | } |
| 61 | } |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 62 | |
| 63 | // ------------------------------------------------------------------------ |
| 64 | |
| 65 | function test_convert_accented_characters() |
| 66 | { |
| 67 | $this->assertEquals('AAAeEEEIIOOEUUUeY', convert_accented_characters('ÀÂÄÈÊËÎÏÔŒÙÛÜŸ')); |
| 68 | $this->assertEquals('a e i o u n ue', convert_accented_characters('á é í ó ú ñ ü')); |
| 69 | } |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 70 | |
| 71 | // ------------------------------------------------------------------------ |
| 72 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 73 | public function test_censored_words() |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 74 | { |
| 75 | $censored = array('boob', 'nerd', 'ass', 'fart'); |
| 76 | |
| 77 | $strs = array( |
| 78 | 'Ted bobbled the ball' => 'Ted bobbled the ball', |
| 79 | 'Jake is a nerdo' => 'Jake is a nerdo', |
| 80 | 'The borg will assimilate you' => 'The borg will assimilate you', |
| 81 | 'Did Mary Fart?' => 'Did Mary $*#?', |
| 82 | 'Jake is really a boob' => 'Jake is really a $*#' |
| 83 | ); |
| 84 | |
| 85 | |
| 86 | foreach ($strs as $str => $expect) |
| 87 | { |
| 88 | $this->assertEquals($expect, word_censor($str, $censored, '$*#')); |
| 89 | } |
| 90 | |
| 91 | // test censored words being sent as a string |
| 92 | $this->assertEquals('test', word_censor('test', 'test')); |
| 93 | } |
| 94 | |
| 95 | // ------------------------------------------------------------------------ |
| 96 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 97 | public function test_highlight_code() |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 98 | { |
| 99 | $code = '<?php var_dump($this); ?>'; |
| 100 | $expect = "<code><span style=\"color: #000000\">\n<span style=\"color: #0000BB\"><?php var_dump</span><span style=\"color: #007700\">(</span><span style=\"color: #0000BB\">\$this</span><span style=\"color: #007700\">); </span><span style=\"color: #0000BB\">?> </span>\n</span>\n</code>"; |
| 101 | |
| 102 | $this->assertEquals($expect, highlight_code($code)); |
| 103 | } |
| 104 | |
| 105 | // ------------------------------------------------------------------------ |
| 106 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 107 | public function test_highlight_phrase() |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 108 | { |
| 109 | $strs = array( |
| 110 | 'this is a phrase' => '<strong>this is</strong> a phrase', |
| 111 | 'this is another' => '<strong>this is</strong> another', |
| 112 | 'Gimme a test, Sally' => 'Gimme a test, Sally', |
| 113 | 'Or tell me what this is' => 'Or tell me what <strong>this is</strong>', |
| 114 | '' => '' |
| 115 | ); |
| 116 | |
| 117 | foreach ($strs as $str => $expect) |
| 118 | { |
| 119 | $this->assertEquals($expect, highlight_phrase($str, 'this is')); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // ------------------------------------------------------------------------ |
| 124 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 125 | public function test_ellipsizing() |
Greg Aker | 9071573 | 2011-04-21 14:36:26 -0500 | [diff] [blame] | 126 | { |
| 127 | $strs = array( |
| 128 | '0' => array( |
| 129 | 'this is my string' => '… my string', |
| 130 | "here's another one" => '…nother one', |
| 131 | 'this one is just a bit longer' => '…bit longer', |
| 132 | 'short' => 'short' |
| 133 | ), |
| 134 | '.5' => array( |
| 135 | 'this is my string' => 'this …tring', |
| 136 | "here's another one" => "here'…r one", |
| 137 | 'this one is just a bit longer' => 'this …onger', |
| 138 | 'short' => 'short' |
| 139 | ), |
| 140 | '1' => array( |
| 141 | 'this is my string' => 'this is my…', |
| 142 | "here's another one" => "here's ano…", |
| 143 | 'this one is just a bit longer' => 'this one i…', |
| 144 | 'short' => 'short' |
| 145 | ), |
| 146 | ); |
| 147 | |
| 148 | foreach ($strs as $pos => $s) |
| 149 | { |
| 150 | foreach ($s as $str => $expect) |
| 151 | { |
| 152 | $this->assertEquals($expect, ellipsize($str, 10, $pos)); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // ------------------------------------------------------------------------ |
| 158 | |
| 159 | } |