Mercurial > hg > Members > shoshi > webvirt
comparison cake/tests/cases/libs/inflector.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 * InflectorTest | |
4 * | |
5 * InflectorTest is used to test cases on the Inflector class | |
6 * | |
7 * PHP versions 4 and 5 | |
8 * | |
9 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | |
10 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
11 * | |
12 * Licensed under The Open Group Test Suite License | |
13 * Redistributions of files must retain the above copyright notice. | |
14 * | |
15 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
16 * @link http://book.cakephp.org/view/1196/Testing | |
17 * @package cake.tests | |
18 * @subpackage cake.tests.cases.libs | |
19 * @since CakePHP(tm) v 1.2.0.4206 | |
20 * @license Open Group Test Suite License (http://www.opensource.org/licenses/opengroup.php) | |
21 */ | |
22 | |
23 /** | |
24 * Included libraries. | |
25 * | |
26 */ | |
27 App::import('Core', 'Inflector'); | |
28 | |
29 /** | |
30 * Short description for class. | |
31 * | |
32 * @package cake.tests | |
33 * @subpackage cake.tests.cases.libs | |
34 */ | |
35 class InflectorTest extends CakeTestCase { | |
36 | |
37 /** | |
38 * Inflector property | |
39 * | |
40 * @var mixed null | |
41 * @access public | |
42 */ | |
43 var $Inflector = null; | |
44 | |
45 /** | |
46 * testInstantiation method | |
47 * | |
48 * @access public | |
49 * @return void | |
50 */ | |
51 function testInstantiation() { | |
52 $Inflector =& Inflector::getInstance(); | |
53 $this->assertEqual(Inflector::getInstance(), $Inflector); | |
54 } | |
55 | |
56 /** | |
57 * testInflectingSingulars method | |
58 * | |
59 * @access public | |
60 * @return void | |
61 */ | |
62 function testInflectingSingulars() { | |
63 $this->assertEqual(Inflector::singularize('categorias'), 'categoria'); | |
64 $this->assertEqual(Inflector::singularize('menus'), 'menu'); | |
65 $this->assertEqual(Inflector::singularize('news'), 'news'); | |
66 $this->assertEqual(Inflector::singularize('food_menus'), 'food_menu'); | |
67 $this->assertEqual(Inflector::singularize('Menus'), 'Menu'); | |
68 $this->assertEqual(Inflector::singularize('FoodMenus'), 'FoodMenu'); | |
69 $this->assertEqual(Inflector::singularize('houses'), 'house'); | |
70 $this->assertEqual(Inflector::singularize('powerhouses'), 'powerhouse'); | |
71 $this->assertEqual(Inflector::singularize('quizzes'), 'quiz'); | |
72 $this->assertEqual(Inflector::singularize('Buses'), 'Bus'); | |
73 $this->assertEqual(Inflector::singularize('buses'), 'bus'); | |
74 $this->assertEqual(Inflector::singularize('matrix_rows'), 'matrix_row'); | |
75 $this->assertEqual(Inflector::singularize('matrices'), 'matrix'); | |
76 $this->assertEqual(Inflector::singularize('vertices'), 'vertex'); | |
77 $this->assertEqual(Inflector::singularize('indices'), 'index'); | |
78 $this->assertEqual(Inflector::singularize('Aliases'), 'Alias'); | |
79 $this->assertEqual(Inflector::singularize('Alias'), 'Alias'); | |
80 $this->assertEqual(Inflector::singularize('Media'), 'Media'); | |
81 $this->assertEqual(Inflector::singularize('alumni'), 'alumnus'); | |
82 $this->assertEqual(Inflector::singularize('bacilli'), 'bacillus'); | |
83 $this->assertEqual(Inflector::singularize('cacti'), 'cactus'); | |
84 $this->assertEqual(Inflector::singularize('foci'), 'focus'); | |
85 $this->assertEqual(Inflector::singularize('fungi'), 'fungus'); | |
86 $this->assertEqual(Inflector::singularize('nuclei'), 'nucleus'); | |
87 $this->assertEqual(Inflector::singularize('octopuses'), 'octopus'); | |
88 $this->assertEqual(Inflector::singularize('radii'), 'radius'); | |
89 $this->assertEqual(Inflector::singularize('stimuli'), 'stimulus'); | |
90 $this->assertEqual(Inflector::singularize('syllabi'), 'syllabus'); | |
91 $this->assertEqual(Inflector::singularize('termini'), 'terminus'); | |
92 $this->assertEqual(Inflector::singularize('viri'), 'virus'); | |
93 $this->assertEqual(Inflector::singularize('people'), 'person'); | |
94 $this->assertEqual(Inflector::singularize('gloves'), 'glove'); | |
95 $this->assertEqual(Inflector::singularize('doves'), 'dove'); | |
96 $this->assertEqual(Inflector::singularize('lives'), 'life'); | |
97 $this->assertEqual(Inflector::singularize('knives'), 'knife'); | |
98 $this->assertEqual(Inflector::singularize('wolves'), 'wolf'); | |
99 $this->assertEqual(Inflector::singularize('slaves'), 'slave'); | |
100 $this->assertEqual(Inflector::singularize('shelves'), 'shelf'); | |
101 $this->assertEqual(Inflector::singularize('taxis'), 'taxi'); | |
102 $this->assertEqual(Inflector::singularize('taxes'), 'tax'); | |
103 $this->assertEqual(Inflector::singularize('Taxes'), 'Tax'); | |
104 $this->assertEqual(Inflector::singularize('AwesomeTaxes'), 'AwesomeTax'); | |
105 $this->assertEqual(Inflector::singularize('faxes'), 'fax'); | |
106 $this->assertEqual(Inflector::singularize('waxes'), 'wax'); | |
107 $this->assertEqual(Inflector::singularize('niches'), 'niche'); | |
108 $this->assertEqual(Inflector::singularize('waves'), 'wave'); | |
109 $this->assertEqual(Inflector::singularize('bureaus'), 'bureau'); | |
110 $this->assertEqual(Inflector::singularize('genetic_analyses'), 'genetic_analysis'); | |
111 $this->assertEqual(Inflector::singularize('doctor_diagnoses'), 'doctor_diagnosis'); | |
112 $this->assertEqual(Inflector::singularize('parantheses'), 'paranthesis'); | |
113 $this->assertEqual(Inflector::singularize('Causes'), 'Cause'); | |
114 $this->assertEqual(Inflector::singularize('colossuses'), 'colossus'); | |
115 $this->assertEqual(Inflector::singularize('diagnoses'), 'diagnosis'); | |
116 $this->assertEqual(Inflector::singularize('bases'), 'basis'); | |
117 $this->assertEqual(Inflector::singularize('analyses'), 'analysis'); | |
118 | |
119 $this->assertEqual(Inflector::singularize(''), ''); | |
120 } | |
121 | |
122 /** | |
123 * testInflectingPlurals method | |
124 * | |
125 * @access public | |
126 * @return void | |
127 */ | |
128 function testInflectingPlurals() { | |
129 $this->assertEqual(Inflector::pluralize('categoria'), 'categorias'); | |
130 $this->assertEqual(Inflector::pluralize('house'), 'houses'); | |
131 $this->assertEqual(Inflector::pluralize('powerhouse'), 'powerhouses'); | |
132 $this->assertEqual(Inflector::pluralize('Bus'), 'Buses'); | |
133 $this->assertEqual(Inflector::pluralize('bus'), 'buses'); | |
134 $this->assertEqual(Inflector::pluralize('menu'), 'menus'); | |
135 $this->assertEqual(Inflector::pluralize('news'), 'news'); | |
136 $this->assertEqual(Inflector::pluralize('food_menu'), 'food_menus'); | |
137 $this->assertEqual(Inflector::pluralize('Menu'), 'Menus'); | |
138 $this->assertEqual(Inflector::pluralize('FoodMenu'), 'FoodMenus'); | |
139 $this->assertEqual(Inflector::pluralize('quiz'), 'quizzes'); | |
140 $this->assertEqual(Inflector::pluralize('matrix_row'), 'matrix_rows'); | |
141 $this->assertEqual(Inflector::pluralize('matrix'), 'matrices'); | |
142 $this->assertEqual(Inflector::pluralize('vertex'), 'vertices'); | |
143 $this->assertEqual(Inflector::pluralize('index'), 'indices'); | |
144 $this->assertEqual(Inflector::pluralize('Alias'), 'Aliases'); | |
145 $this->assertEqual(Inflector::pluralize('Aliases'), 'Aliases'); | |
146 $this->assertEqual(Inflector::pluralize('Media'), 'Media'); | |
147 $this->assertEqual(Inflector::pluralize('alumnus'), 'alumni'); | |
148 $this->assertEqual(Inflector::pluralize('bacillus'), 'bacilli'); | |
149 $this->assertEqual(Inflector::pluralize('cactus'), 'cacti'); | |
150 $this->assertEqual(Inflector::pluralize('focus'), 'foci'); | |
151 $this->assertEqual(Inflector::pluralize('fungus'), 'fungi'); | |
152 $this->assertEqual(Inflector::pluralize('nucleus'), 'nuclei'); | |
153 $this->assertEqual(Inflector::pluralize('octopus'), 'octopuses'); | |
154 $this->assertEqual(Inflector::pluralize('radius'), 'radii'); | |
155 $this->assertEqual(Inflector::pluralize('stimulus'), 'stimuli'); | |
156 $this->assertEqual(Inflector::pluralize('syllabus'), 'syllabi'); | |
157 $this->assertEqual(Inflector::pluralize('terminus'), 'termini'); | |
158 $this->assertEqual(Inflector::pluralize('virus'), 'viri'); | |
159 $this->assertEqual(Inflector::pluralize('person'), 'people'); | |
160 $this->assertEqual(Inflector::pluralize('people'), 'people'); | |
161 $this->assertEqual(Inflector::pluralize('glove'), 'gloves'); | |
162 $this->assertEqual(Inflector::pluralize('crisis'), 'crises'); | |
163 $this->assertEqual(Inflector::pluralize('tax'), 'taxes'); | |
164 $this->assertEqual(Inflector::pluralize('wave'), 'waves'); | |
165 $this->assertEqual(Inflector::pluralize('bureau'), 'bureaus'); | |
166 $this->assertEqual(Inflector::pluralize(''), ''); | |
167 } | |
168 | |
169 /** | |
170 * testInflectorSlug method | |
171 * | |
172 * @access public | |
173 * @return void | |
174 */ | |
175 function testInflectorSlug() { | |
176 $result = Inflector::slug('Foo Bar: Not just for breakfast any-more'); | |
177 $expected = 'Foo_Bar_Not_just_for_breakfast_any_more'; | |
178 $this->assertEqual($result, $expected); | |
179 | |
180 $result = Inflector::slug('this/is/a/path'); | |
181 $expected = 'this_is_a_path'; | |
182 $this->assertEqual($result, $expected); | |
183 | |
184 $result = Inflector::slug('Foo Bar: Not just for breakfast any-more', "-"); | |
185 $expected = 'Foo-Bar-Not-just-for-breakfast-any-more'; | |
186 $this->assertEqual($result, $expected); | |
187 | |
188 $result = Inflector::slug('Foo Bar: Not just for breakfast any-more', "+"); | |
189 $expected = 'Foo+Bar+Not+just+for+breakfast+any+more'; | |
190 $this->assertEqual($result, $expected); | |
191 | |
192 $result = Inflector::slug('Äpfel Über Öl grün ärgert groß öko', '-'); | |
193 $expected = 'Aepfel-Ueber-Oel-gruen-aergert-gross-oeko'; | |
194 $this->assertEqual($result, $expected); | |
195 | |
196 $result = Inflector::slug('The truth - and- more- news', '-'); | |
197 $expected = 'The-truth-and-more-news'; | |
198 $this->assertEqual($result, $expected); | |
199 | |
200 $result = Inflector::slug('The truth: and more news', '-'); | |
201 $expected = 'The-truth-and-more-news'; | |
202 $this->assertEqual($result, $expected); | |
203 | |
204 $result = Inflector::slug('La langue française est un attribut de souveraineté en France', '-'); | |
205 $expected = 'La-langue-francaise-est-un-attribut-de-souverainete-en-France'; | |
206 $this->assertEqual($result, $expected); | |
207 | |
208 $result = Inflector::slug('!@$#exciting stuff! - what !@-# was that?', '-'); | |
209 $expected = 'exciting-stuff-what-was-that'; | |
210 $this->assertEqual($result, $expected); | |
211 | |
212 $result = Inflector::slug('20% of profits went to me!', '-'); | |
213 $expected = '20-of-profits-went-to-me'; | |
214 $this->assertEqual($result, $expected); | |
215 | |
216 $result = Inflector::slug('#this melts your face1#2#3', '-'); | |
217 $expected = 'this-melts-your-face1-2-3'; | |
218 $this->assertEqual($result, $expected); | |
219 | |
220 $result = Inflector::slug('controller/action/りんご/1'); | |
221 $expected = 'controller_action_りんご_1'; | |
222 $this->assertEqual($result, $expected); | |
223 | |
224 $result = Inflector::slug('の話が出たので大丈夫かなあと'); | |
225 $expected = 'の話が出たので大丈夫かなあと'; | |
226 $this->assertEqual($result, $expected); | |
227 | |
228 $result = Inflector::slug('posts/view/한국어/page:1/sort:asc'); | |
229 $expected = 'posts_view_한국어_page_1_sort_asc'; | |
230 $this->assertEqual($result, $expected); | |
231 } | |
232 | |
233 /** | |
234 * testInflectorSlugWithMap method | |
235 * | |
236 * @access public | |
237 * @return void | |
238 */ | |
239 function testInflectorSlugWithMap() { | |
240 $result = Inflector::slug('replace every r', array('/r/' => '1')); | |
241 $expected = '1eplace_eve1y_1'; | |
242 $this->assertEqual($result, $expected); | |
243 | |
244 $result = Inflector::slug('replace every r', '_', array('/r/' => '1')); | |
245 $expected = '1eplace_eve1y_1'; | |
246 $this->assertEqual($result, $expected); | |
247 } | |
248 | |
249 /** | |
250 * testInflectorSlugWithMapOverridingDefault method | |
251 * | |
252 * @access public | |
253 * @return void | |
254 */ | |
255 function testInflectorSlugWithMapOverridingDefault() { | |
256 $result = Inflector::slug('Testing æ ø å', '-', array('/å/' => 'aa', '/ø/' => 'oe')); | |
257 $expected = 'Testing-ae-oe-aa'; | |
258 $this->assertEqual($result, $expected); | |
259 } | |
260 | |
261 /** | |
262 * testInflectorUnderscore method | |
263 * | |
264 * @return void | |
265 * @access public | |
266 */ | |
267 function testInflectorUnderscore() { | |
268 $this->assertIdentical(Inflector::underscore('TestThing'), 'test_thing'); | |
269 $this->assertIdentical(Inflector::underscore('testThing'), 'test_thing'); | |
270 $this->assertIdentical(Inflector::underscore('TestThingExtra'), 'test_thing_extra'); | |
271 $this->assertIdentical(Inflector::underscore('testThingExtra'), 'test_thing_extra'); | |
272 | |
273 // Identical checks test the cache code path. | |
274 $this->assertIdentical(Inflector::underscore('TestThing'), 'test_thing'); | |
275 $this->assertIdentical(Inflector::underscore('testThing'), 'test_thing'); | |
276 $this->assertIdentical(Inflector::underscore('TestThingExtra'), 'test_thing_extra'); | |
277 $this->assertIdentical(Inflector::underscore('testThingExtra'), 'test_thing_extra'); | |
278 | |
279 // Test stupid values | |
280 $this->assertIdentical(Inflector::underscore(''), ''); | |
281 $this->assertIdentical(Inflector::underscore(0), '0'); | |
282 $this->assertIdentical(Inflector::underscore(false), ''); | |
283 } | |
284 | |
285 /** | |
286 * testVariableNaming method | |
287 * | |
288 * @access public | |
289 * @return void | |
290 */ | |
291 function testVariableNaming() { | |
292 $this->assertEqual(Inflector::variable('test_field'), 'testField'); | |
293 $this->assertEqual(Inflector::variable('test_fieLd'), 'testFieLd'); | |
294 $this->assertEqual(Inflector::variable('test field'), 'testField'); | |
295 $this->assertEqual(Inflector::variable('Test_field'), 'testField'); | |
296 } | |
297 | |
298 /** | |
299 * testClassNaming method | |
300 * | |
301 * @access public | |
302 * @return void | |
303 */ | |
304 function testClassNaming() { | |
305 $this->assertEqual(Inflector::classify('artists_genres'), 'ArtistsGenre'); | |
306 $this->assertEqual(Inflector::classify('file_systems'), 'FileSystem'); | |
307 $this->assertEqual(Inflector::classify('news'), 'News'); | |
308 $this->assertEqual(Inflector::classify('bureaus'), 'Bureau'); | |
309 } | |
310 | |
311 /** | |
312 * testTableNaming method | |
313 * | |
314 * @access public | |
315 * @return void | |
316 */ | |
317 function testTableNaming() { | |
318 $this->assertEqual(Inflector::tableize('ArtistsGenre'), 'artists_genres'); | |
319 $this->assertEqual(Inflector::tableize('FileSystem'), 'file_systems'); | |
320 $this->assertEqual(Inflector::tableize('News'), 'news'); | |
321 $this->assertEqual(Inflector::tableize('Bureau'), 'bureaus'); | |
322 } | |
323 | |
324 /** | |
325 * testHumanization method | |
326 * | |
327 * @access public | |
328 * @return void | |
329 */ | |
330 function testHumanization() { | |
331 $this->assertEqual(Inflector::humanize('posts'), 'Posts'); | |
332 $this->assertEqual(Inflector::humanize('posts_tags'), 'Posts Tags'); | |
333 $this->assertEqual(Inflector::humanize('file_systems'), 'File Systems'); | |
334 } | |
335 | |
336 /** | |
337 * This test if run in isolation should not cause errors in PHP4. | |
338 * | |
339 * @return void | |
340 */ | |
341 function testRulesNoErrorPHP4() { | |
342 Inflector::rules('plural', array( | |
343 'rules' => array(), | |
344 'irregular' => array(), | |
345 'uninflected' => array('pays') | |
346 )); | |
347 } | |
348 | |
349 /** | |
350 * testCustomPluralRule method | |
351 * | |
352 * @access public | |
353 * @return void | |
354 */ | |
355 function testCustomPluralRule() { | |
356 Inflector::rules('plural', array('/^(custom)$/i' => '\1izables')); | |
357 $this->assertEqual(Inflector::pluralize('custom'), 'customizables'); | |
358 | |
359 Inflector::rules('plural', array('uninflected' => array('uninflectable'))); | |
360 $this->assertEqual(Inflector::pluralize('uninflectable'), 'uninflectable'); | |
361 | |
362 Inflector::rules('plural', array( | |
363 'rules' => array('/^(alert)$/i' => '\1ables'), | |
364 'uninflected' => array('noflect', 'abtuse'), | |
365 'irregular' => array('amaze' => 'amazable', 'phone' => 'phonezes') | |
366 )); | |
367 $this->assertEqual(Inflector::pluralize('noflect'), 'noflect'); | |
368 $this->assertEqual(Inflector::pluralize('abtuse'), 'abtuse'); | |
369 $this->assertEqual(Inflector::pluralize('alert'), 'alertables'); | |
370 $this->assertEqual(Inflector::pluralize('amaze'), 'amazable'); | |
371 $this->assertEqual(Inflector::pluralize('phone'), 'phonezes'); | |
372 } | |
373 | |
374 /** | |
375 * testCustomSingularRule method | |
376 * | |
377 * @access public | |
378 * @return void | |
379 */ | |
380 function testCustomSingularRule() { | |
381 Inflector::rules('singular', array('/(eple)r$/i' => '\1', '/(jente)r$/i' => '\1')); | |
382 | |
383 $this->assertEqual(Inflector::singularize('epler'), 'eple'); | |
384 $this->assertEqual(Inflector::singularize('jenter'), 'jente'); | |
385 | |
386 Inflector::rules('singular', array( | |
387 'rules' => array('/^(bil)er$/i' => '\1', '/^(inflec|contribu)tors$/i' => '\1ta'), | |
388 'uninflected' => array('singulars'), | |
389 'irregular' => array('spins' => 'spinor') | |
390 )); | |
391 | |
392 $this->assertEqual(Inflector::singularize('inflectors'), 'inflecta'); | |
393 $this->assertEqual(Inflector::singularize('contributors'), 'contributa'); | |
394 $this->assertEqual(Inflector::singularize('spins'), 'spinor'); | |
395 $this->assertEqual(Inflector::singularize('singulars'), 'singulars'); | |
396 } | |
397 | |
398 /** | |
399 * testCustomTransliterationRule method | |
400 * | |
401 * @access public | |
402 * @return void | |
403 */ | |
404 function testCustomTransliterationRule() { | |
405 $this->assertEqual(Inflector::slug('Testing æ ø å'), 'Testing_ae_o_a'); | |
406 | |
407 Inflector::rules('transliteration', array('/å/' => 'aa', '/ø/' => 'oe')); | |
408 $this->assertEqual(Inflector::slug('Testing æ ø å'), 'Testing_ae_oe_aa'); | |
409 | |
410 Inflector::rules('transliteration', array('/ä|æ/' => 'ae', '/å/' => 'aa'), true); | |
411 $this->assertEqual(Inflector::slug('Testing æ ø å'), 'Testing_ae_ø_aa'); | |
412 | |
413 $this->assertEqual(Inflector::slug('Testing æ ø å', '-', array('/ø/' => 'oe')), 'Testing-ae-oe-aa'); | |
414 } | |
415 | |
416 /** | |
417 * test that setting new rules clears the inflector caches. | |
418 * | |
419 * @return void | |
420 */ | |
421 function testRulesClearsCaches() { | |
422 $this->assertEqual(Inflector::singularize('Bananas'), 'Banana'); | |
423 $this->assertEqual(Inflector::tableize('Banana'), 'bananas'); | |
424 $this->assertEqual(Inflector::pluralize('Banana'), 'Bananas'); | |
425 | |
426 Inflector::rules('singular', array( | |
427 'rules' => array('/(.*)nas$/i' => '\1zzz') | |
428 )); | |
429 $this->assertEqual(Inflector::singularize('Bananas'), 'Banazzz', 'Was inflected with old rules. %s'); | |
430 | |
431 Inflector::rules('plural', array( | |
432 'rules' => array('/(.*)na$/i' => '\1zzz') | |
433 )); | |
434 $this->assertEqual(Inflector::pluralize('Banana'), 'Banazzz', 'Was inflected with old rules. %s'); | |
435 } | |
436 | |
437 /** | |
438 * Test resetting inflection rules. | |
439 * | |
440 * @return void | |
441 */ | |
442 function testCustomRuleWithReset() { | |
443 $uninflected = array('atlas', 'lapis', 'onibus', 'pires', 'virus', '.*x'); | |
444 $pluralIrregular = array('as' => 'ases'); | |
445 | |
446 Inflector::rules('singular', array( | |
447 'rules' => array('/^(.*)(a|e|o|u)is$/i' => '\1\2l'), | |
448 'uninflected' => $uninflected, | |
449 ), true); | |
450 | |
451 Inflector::rules('plural', array( | |
452 'rules' => array( | |
453 '/^(.*)(a|e|o|u)l$/i' => '\1\2is', | |
454 ), | |
455 'uninflected' => $uninflected, | |
456 'irregular' => $pluralIrregular | |
457 ), true); | |
458 | |
459 $this->assertEqual(Inflector::pluralize('Alcool'), 'Alcoois'); | |
460 $this->assertEqual(Inflector::pluralize('Atlas'), 'Atlas'); | |
461 $this->assertEqual(Inflector::singularize('Alcoois'), 'Alcool'); | |
462 $this->assertEqual(Inflector::singularize('Atlas'), 'Atlas'); | |
463 } | |
464 | |
465 } |