Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 1 | <?php |
| 2 | |
Taufan Aditya | ac5373a | 2012-03-28 16:03:38 +0700 | [diff] [blame] | 3 | class Typography_test extends CI_TestCase { |
Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 4 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 5 | public function set_up() |
Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 6 | { |
| 7 | $obj = new StdClass; |
Taufan Aditya | ac5373a | 2012-03-28 16:03:38 +0700 | [diff] [blame] | 8 | $obj->type = new Mock_Libraries_Typography(); |
Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 9 | |
| 10 | $this->ci_instance($obj); |
| 11 | |
| 12 | $this->type = $obj->type; |
| 13 | } |
| 14 | |
| 15 | // -------------------------------------------------------------------- |
| 16 | |
| 17 | /** |
| 18 | * Tests the format_characters() function. |
| 19 | * |
| 20 | * this can and should grow. |
| 21 | */ |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 22 | public function test_format_characters() |
Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 23 | { |
| 24 | $strs = array( |
| 25 | '"double quotes"' => '“double quotes”', |
| 26 | '"testing" in "theory" that is' => '“testing” in “theory” that is', |
| 27 | "Here's what I'm" => 'Here’s what I’m', |
| 28 | '&' => '&', |
| 29 | '&' => '&', |
| 30 | ' ' => ' ', |
| 31 | '--' => '—', |
| 32 | 'foo...' => 'foo…', |
| 33 | 'foo..' => 'foo..', |
| 34 | 'foo...bar.' => 'foo…bar.', |
| 35 | 'test. new' => 'test. new', |
| 36 | ); |
| 37 | |
| 38 | foreach ($strs as $str => $expected) |
| 39 | { |
| 40 | $this->assertEquals($expected, $this->type->format_characters($str)); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // -------------------------------------------------------------------- |
| 45 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 46 | public function test_nl2br_except_pre() |
Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 47 | { |
| 48 | $str = <<<EOH |
| 49 | Hello, I'm a happy string with some new lines. |
| 50 | |
| 51 | I like to skip. |
| 52 | |
| 53 | Jump |
| 54 | |
| 55 | and sing. |
| 56 | |
| 57 | <pre> |
| 58 | I am inside a pre tag. Please don't mess with me. |
| 59 | |
| 60 | k? |
| 61 | </pre> |
| 62 | |
| 63 | That's my story and I'm sticking to it. |
| 64 | |
| 65 | The End. |
| 66 | EOH; |
| 67 | |
| 68 | $expected = <<<EOH |
| 69 | Hello, I'm a happy string with some new lines. <br /> |
| 70 | <br /> |
| 71 | I like to skip.<br /> |
| 72 | <br /> |
| 73 | Jump<br /> |
| 74 | <br /> |
| 75 | and sing.<br /> |
| 76 | <br /> |
| 77 | <pre> |
| 78 | I am inside a pre tag. Please don't mess with me. |
| 79 | |
| 80 | k? |
| 81 | </pre><br /> |
| 82 | <br /> |
| 83 | That's my story and I'm sticking to it.<br /> |
| 84 | <br /> |
| 85 | The End. |
| 86 | EOH; |
| 87 | |
| 88 | $this->assertEquals($expected, |
| 89 | $this->type->nl2br_except_pre($str)); |
| 90 | } |
| 91 | |
| 92 | // -------------------------------------------------------------------- |
| 93 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 94 | public function test_auto_typography() |
Greg Aker | 2cadade | 2011-04-21 15:00:49 -0500 | [diff] [blame] | 95 | { |
| 96 | $this->_blank_string(); |
| 97 | $this->_standardize_new_lines(); |
| 98 | $this->_reduce_linebreaks(); |
| 99 | $this->_remove_comments(); |
| 100 | $this->_protect_pre(); |
| 101 | $this->_no_opening_block(); |
| 102 | $this->_protect_braced_quotes(); |
| 103 | } |
| 104 | |
| 105 | // -------------------------------------------------------------------- |
| 106 | |
| 107 | private function _blank_string() |
| 108 | { |
| 109 | // Test blank string |
| 110 | $this->assertEquals('', $this->type->auto_typography('')); |
| 111 | } |
| 112 | |
| 113 | // -------------------------------------------------------------------- |
| 114 | |
| 115 | private function _standardize_new_lines() |
| 116 | { |
| 117 | $strs = array( |
| 118 | "My string\rhas return characters" => "<p>My string<br />\nhas return characters</p>", |
| 119 | 'This one does not!' => '<p>This one does not!</p>' |
| 120 | ); |
| 121 | |
| 122 | foreach ($strs as $str => $expect) |
| 123 | { |
| 124 | $this->assertEquals($expect, $this->type->auto_typography($str)); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // -------------------------------------------------------------------- |
| 129 | |
| 130 | private function _reduce_linebreaks() |
| 131 | { |
| 132 | $str = "This has way too many linebreaks.\n\n\n\nSee?"; |
| 133 | $expect = "<p>This has way too many linebreaks.</p>\n\n<p>See?</p>"; |
| 134 | |
| 135 | $this->assertEquals($expect, $this->type->auto_typography($str, TRUE)); |
| 136 | } |
| 137 | |
| 138 | // -------------------------------------------------------------------- |
| 139 | |
| 140 | private function _remove_comments() |
| 141 | { |
| 142 | $str = '<!-- I can haz comments? --> But no!'; |
| 143 | $expect = '<p><!-- I can haz comments? --> But no!</p>'; |
| 144 | |
| 145 | $this->assertEquals($expect, $this->type->auto_typography($str)); |
| 146 | } |
| 147 | |
| 148 | // -------------------------------------------------------------------- |
| 149 | |
| 150 | private function _protect_pre() |
| 151 | { |
| 152 | $str = '<p>My Sentence</p><pre>var_dump($this);</pre>'; |
| 153 | $expect = '<p>My Sentence</p><pre>var_dump($this);</pre>'; |
| 154 | |
| 155 | $this->assertEquals($expect, $this->type->auto_typography($str)); |
| 156 | } |
| 157 | |
| 158 | // -------------------------------------------------------------------- |
| 159 | |
| 160 | private function _no_opening_block() |
| 161 | { |
| 162 | $str = 'My Sentence<pre>var_dump($this);</pre>'; |
| 163 | $expect = '<p>My Sentence</p><pre>var_dump($this);</pre>'; |
| 164 | |
| 165 | $this->assertEquals($expect, $this->type->auto_typography($str)); |
| 166 | } |
| 167 | |
| 168 | // -------------------------------------------------------------------- |
| 169 | |
| 170 | public function _protect_braced_quotes() |
| 171 | { |
| 172 | $this->type->protect_braced_quotes = TRUE; |
| 173 | |
| 174 | $str = 'Test {parse="foobar"}'; |
| 175 | $expect = '<p>Test {parse="foobar"}</p>'; |
| 176 | |
| 177 | $this->assertEquals($expect, $this->type->auto_typography($str)); |
| 178 | |
| 179 | $this->type->protect_braced_quotes = FALSE; |
| 180 | |
| 181 | $str = 'Test {parse="foobar"}'; |
| 182 | $expect = '<p>Test {parse=“foobar”}</p>'; |
| 183 | |
| 184 | $this->assertEquals($expect, $this->type->auto_typography($str)); |
| 185 | |
| 186 | |
| 187 | } |
| 188 | } |