comparison cake/tests/cases/libs/string.test.php @ 0:261e66bd5a0c

hg init
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Sun, 24 Jul 2011 21:08:31 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:261e66bd5a0c
1 <?php
2 /**
3 * StringTest file
4 *
5 * PHP versions 4 and 5
6 *
7 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
9 *
10 * Licensed under The MIT License
11 * Redistributions of files must retain the above copyright notice.
12 *
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14 * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
15 * @package cake
16 * @subpackage cake.tests.cases.libs
17 * @since CakePHP(tm) v 1.2.0.5432
18 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
19 */
20 App::import('Core', 'String');
21
22 /**
23 * StringTest class
24 *
25 * @package cake
26 * @subpackage cake.tests.cases.libs
27 */
28 class StringTest extends CakeTestCase {
29
30 /**
31 * testUuidGeneration method
32 *
33 * @access public
34 * @return void
35 */
36 function testUuidGeneration() {
37 $result = String::uuid();
38 $pattern = "/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/";
39 $match = preg_match($pattern, $result);
40 $this->assertTrue($match);
41 }
42
43 /**
44 * testMultipleUuidGeneration method
45 *
46 * @access public
47 * @return void
48 */
49 function testMultipleUuidGeneration() {
50 $check = array();
51 $count = mt_rand(10, 1000);
52 $pattern = "/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/";
53
54 for($i = 0; $i < $count; $i++) {
55 $result = String::uuid();
56 $match = preg_match($pattern, $result);
57 $this->assertTrue($match);
58 $this->assertFalse(in_array($result, $check));
59 $check[] = $result;
60 }
61 }
62
63 /**
64 * testInsert method
65 *
66 * @access public
67 * @return void
68 */
69 function testInsert() {
70 $string = 'some string';
71 $expected = 'some string';
72 $result = String::insert($string, array());
73 $this->assertEqual($result, $expected);
74
75 $string = '2 + 2 = :sum. Cake is :adjective.';
76 $expected = '2 + 2 = 4. Cake is yummy.';
77 $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'));
78 $this->assertEqual($result, $expected);
79
80 $string = '2 + 2 = %sum. Cake is %adjective.';
81 $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('before' => '%'));
82 $this->assertEqual($result, $expected);
83
84 $string = '2 + 2 = 2sum2. Cake is 9adjective9.';
85 $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('format' => '/([\d])%s\\1/'));
86 $this->assertEqual($result, $expected);
87
88 $string = '2 + 2 = 12sum21. Cake is 23adjective45.';
89 $expected = '2 + 2 = 4. Cake is 23adjective45.';
90 $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('format' => '/([\d])([\d])%s\\2\\1/'));
91 $this->assertEqual($result, $expected);
92
93 $string = ':web :web_site';
94 $expected = 'www http';
95 $result = String::insert($string, array('web' => 'www', 'web_site' => 'http'));
96 $this->assertEqual($result, $expected);
97
98 $string = '2 + 2 = <sum. Cake is <adjective>.';
99 $expected = '2 + 2 = <sum. Cake is yummy.';
100 $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('before' => '<', 'after' => '>'));
101 $this->assertEqual($result, $expected);
102
103 $string = '2 + 2 = \:sum. Cake is :adjective.';
104 $expected = '2 + 2 = :sum. Cake is yummy.';
105 $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'));
106 $this->assertEqual($result, $expected);
107
108 $string = '2 + 2 = !:sum. Cake is :adjective.';
109 $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('escape' => '!'));
110 $this->assertEqual($result, $expected);
111
112 $string = '2 + 2 = \%sum. Cake is %adjective.';
113 $expected = '2 + 2 = %sum. Cake is yummy.';
114 $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array('before' => '%'));
115 $this->assertEqual($result, $expected);
116
117 $string = ':a :b \:a :a';
118 $expected = '1 2 :a 1';
119 $result = String::insert($string, array('a' => 1, 'b' => 2));
120 $this->assertEqual($result, $expected);
121
122 $string = ':a :b :c';
123 $expected = '2 3';
124 $result = String::insert($string, array('b' => 2, 'c' => 3), array('clean' => true));
125 $this->assertEqual($result, $expected);
126
127 $string = ':a :b :c';
128 $expected = '1 3';
129 $result = String::insert($string, array('a' => 1, 'c' => 3), array('clean' => true));
130 $this->assertEqual($result, $expected);
131
132 $string = ':a :b :c';
133 $expected = '2 3';
134 $result = String::insert($string, array('b' => 2, 'c' => 3), array('clean' => true));
135 $this->assertEqual($result, $expected);
136
137 $string = ':a, :b and :c';
138 $expected = '2 and 3';
139 $result = String::insert($string, array('b' => 2, 'c' => 3), array('clean' => true));
140 $this->assertEqual($result, $expected);
141
142 $string = '":a, :b and :c"';
143 $expected = '"1, 2"';
144 $result = String::insert($string, array('a' => 1, 'b' => 2), array('clean' => true));
145 $this->assertEqual($result, $expected);
146
147 $string = '"${a}, ${b} and ${c}"';
148 $expected = '"1, 2"';
149 $result = String::insert($string, array('a' => 1, 'b' => 2), array('before' => '${', 'after' => '}', 'clean' => true));
150 $this->assertEqual($result, $expected);
151
152 $string = '<img src=":src" alt=":alt" class="foo :extra bar"/>';
153 $expected = '<img src="foo" class="foo bar"/>';
154 $result = String::insert($string, array('src' => 'foo'), array('clean' => 'html'));
155
156 $this->assertEqual($result, $expected);
157
158 $string = '<img src=":src" class=":no :extra"/>';
159 $expected = '<img src="foo"/>';
160 $result = String::insert($string, array('src' => 'foo'), array('clean' => 'html'));
161 $this->assertEqual($result, $expected);
162
163 $string = '<img src=":src" class=":no :extra"/>';
164 $expected = '<img src="foo" class="bar"/>';
165 $result = String::insert($string, array('src' => 'foo', 'extra' => 'bar'), array('clean' => 'html'));
166 $this->assertEqual($result, $expected);
167
168 $result = String::insert("this is a ? string", "test");
169 $expected = "this is a test string";
170 $this->assertEqual($result, $expected);
171
172 $result = String::insert("this is a ? string with a ? ? ?", array('long', 'few?', 'params', 'you know'));
173 $expected = "this is a long string with a few? params you know";
174 $this->assertEqual($result, $expected);
175
176 $result = String::insert('update saved_urls set url = :url where id = :id', array('url' => 'http://www.testurl.com/param1:url/param2:id','id' => 1));
177 $expected = "update saved_urls set url = http://www.testurl.com/param1:url/param2:id where id = 1";
178 $this->assertEqual($result, $expected);
179
180 $result = String::insert('update saved_urls set url = :url where id = :id', array('id' => 1, 'url' => 'http://www.testurl.com/param1:url/param2:id'));
181 $expected = "update saved_urls set url = http://www.testurl.com/param1:url/param2:id where id = 1";
182 $this->assertEqual($result, $expected);
183
184 $result = String::insert(':me cake. :subject :verb fantastic.', array('me' => 'I :verb', 'subject' => 'cake', 'verb' => 'is'));
185 $expected = "I :verb cake. cake is fantastic.";
186 $this->assertEqual($result, $expected);
187
188 $result = String::insert(':I.am: :not.yet: passing.', array('I.am' => 'We are'), array('before' => ':', 'after' => ':', 'clean' => array('replacement' => ' of course', 'method' => 'text')));
189 $expected = "We are of course passing.";
190 $this->assertEqual($result, $expected);
191
192 $result = String::insert(
193 ':I.am: :not.yet: passing.',
194 array('I.am' => 'We are'),
195 array('before' => ':', 'after' => ':', 'clean' => true)
196 );
197 $expected = "We are passing.";
198 $this->assertEqual($result, $expected);
199
200 $result = String::insert('?-pended result', array('Pre'));
201 $expected = "Pre-pended result";
202 $this->assertEqual($result, $expected);
203
204 $string = 'switching :timeout / :timeout_count';
205 $expected = 'switching 5 / 10';
206 $result = String::insert($string, array('timeout' => 5, 'timeout_count' => 10));
207 $this->assertEqual($result, $expected);
208
209 $string = 'switching :timeout / :timeout_count';
210 $expected = 'switching 5 / 10';
211 $result = String::insert($string, array('timeout_count' => 10, 'timeout' => 5));
212 $this->assertEqual($result, $expected);
213
214 $string = 'switching :timeout_count by :timeout';
215 $expected = 'switching 10 by 5';
216 $result = String::insert($string, array('timeout' => 5, 'timeout_count' => 10));
217 $this->assertEqual($result, $expected);
218
219 $string = 'switching :timeout_count by :timeout';
220 $expected = 'switching 10 by 5';
221 $result = String::insert($string, array('timeout_count' => 10, 'timeout' => 5));
222 $this->assertEqual($result, $expected);
223 }
224
225 /**
226 * test Clean Insert
227 *
228 * @return void
229 */
230 function testCleanInsert() {
231 $result = String::cleanInsert(':incomplete', array(
232 'clean' => true, 'before' => ':', 'after' => ''
233 ));
234 $this->assertEqual($result, '');
235
236 $result = String::cleanInsert(':incomplete', array(
237 'clean' => array('method' => 'text', 'replacement' => 'complete'),
238 'before' => ':', 'after' => '')
239 );
240 $this->assertEqual($result, 'complete');
241
242 $result = String::cleanInsert(':in.complete', array(
243 'clean' => true, 'before' => ':', 'after' => ''
244 ));
245 $this->assertEqual($result, '');
246
247 $result = String::cleanInsert(':in.complete and', array(
248 'clean' => true, 'before' => ':', 'after' => '')
249 );
250 $this->assertEqual($result, '');
251
252 $result = String::cleanInsert(':in.complete or stuff', array(
253 'clean' => true, 'before' => ':', 'after' => ''
254 ));
255 $this->assertEqual($result, 'stuff');
256
257 $result = String::cleanInsert(
258 '<p class=":missing" id=":missing">Text here</p>',
259 array('clean' => 'html', 'before' => ':', 'after' => '')
260 );
261 $this->assertEqual($result, '<p>Text here</p>');
262 }
263
264 /**
265 * Tests that non-insertable variables (i.e. arrays) are skipped when used as values in
266 * String::insert().
267 *
268 * @return void
269 */
270 function testAutoIgnoreBadInsertData() {
271 $data = array('foo' => 'alpha', 'bar' => 'beta', 'fale' => array());
272 $result = String::insert('(:foo > :bar || :fale!)', $data, array('clean' => 'text'));
273 $this->assertEqual($result, '(alpha > beta || !)');
274 }
275
276 /**
277 * testTokenize method
278 *
279 * @access public
280 * @return void
281 */
282 function testTokenize() {
283 $result = String::tokenize('A,(short,boring test)');
284 $expected = array('A', '(short,boring test)');
285 $this->assertEqual($result, $expected);
286
287 $result = String::tokenize('A,(short,more interesting( test)');
288 $expected = array('A', '(short,more interesting( test)');
289 $this->assertEqual($result, $expected);
290
291 $result = String::tokenize('A,(short,very interesting( test))');
292 $expected = array('A', '(short,very interesting( test))');
293 $this->assertEqual($result, $expected);
294
295 $result = String::tokenize('"single tag"', ' ', '"', '"');
296 $expected = array('"single tag"');
297 $this->assertEqual($expected, $result);
298
299 $result = String::tokenize('tagA "single tag" tagB', ' ', '"', '"');
300 $expected = array('tagA', '"single tag"', 'tagB');
301 $this->assertEqual($expected, $result);
302 }
303
304 function testReplaceWithQuestionMarkInString() {
305 $string = ':a, :b and :c?';
306 $expected = '2 and 3?';
307 $result = String::insert($string, array('b' => 2, 'c' => 3), array('clean' => true));
308 $this->assertEqual($expected, $result);
309 }
310 }