|
167 | 167 | "[END 2x REPEATED LINE]", |
168 | 168 | ]); |
169 | 169 | }); |
| 170 | + |
| 171 | +test('truncate string shorter than limit', function () { |
| 172 | + $str = 'Short string'; |
| 173 | + expect(cywise_truncate_string($str, 50))->toEqual($str); |
| 174 | +}); |
| 175 | + |
| 176 | +test('truncate string equal to limit', function () { |
| 177 | + $str = str_repeat('a', 100); |
| 178 | + expect(cywise_truncate_string($str, 100))->toEqual($str); |
| 179 | +}); |
| 180 | + |
| 181 | +test('truncate string longer than limit', function () { |
| 182 | + |
| 183 | + $start = str_repeat('a', 50); |
| 184 | + $middle = str_repeat('b', 10); |
| 185 | + $end = str_repeat('c', 50); |
| 186 | + $str = $start . $middle . $end; |
| 187 | + |
| 188 | + // Total length is 110, limit is 100 |
| 189 | + $expected = $start . '[...]' . $end; |
| 190 | + expect(cywise_truncate_string($str, 100))->toEqual($expected); |
| 191 | +}); |
| 192 | + |
| 193 | +test('truncate string with default limit (500)', function () { |
| 194 | + $str = str_repeat('a', 501); |
| 195 | + $expected = str_repeat('a', 50) . '[...]' . str_repeat('a', 50); |
| 196 | + expect(cywise_truncate_string($str))->toEqual($expected); |
| 197 | +}); |
| 198 | + |
| 199 | +test('truncate string with multibyte characters', function () { |
| 200 | + |
| 201 | + $start = str_repeat('é', 50); |
| 202 | + $middle = '🚀'; |
| 203 | + $end = str_repeat('à', 50); |
| 204 | + $str = $start . $middle . $end; |
| 205 | + |
| 206 | + // Total mb_length is 101, limit is 100 |
| 207 | + $expected = $start . '[...]' . $end; |
| 208 | + expect(cywise_truncate_string($str, 100))->toEqual($expected); |
| 209 | +}); |
| 210 | + |
| 211 | +test('truncate string with very short string but limit smaller than 100', function () { |
| 212 | + |
| 213 | + $str = "This is a string that is longer than ten characters."; // 52 chars |
| 214 | + $limit = 10; |
| 215 | + |
| 216 | + // Since length (52) > limit (10), it will take first 50 and last 50. |
| 217 | + $start = mb_substr($str, 0, 50); |
| 218 | + $end = mb_substr($str, -50); |
| 219 | + $expected = $start . '[...]' . $end; |
| 220 | + |
| 221 | + expect(cywise_truncate_string($str, $limit))->toEqual($expected); |
| 222 | +}); |
0 commit comments