Skip to content

Commit e2052af

Browse files
benno5020jrfnl
authored andcommitted
Util/Common: add tests to IsCamelCapsTest
This commit aims to further document existing behaviour and tries to anticipate potential mistakes if the regular expressions are ever changed. See #846
1 parent d2d4b47 commit e2052af

File tree

2 files changed

+161
-16
lines changed

2 files changed

+161
-16
lines changed

.cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"mins",
4444
"nekudotayim",
4545
"nowdoc",
46+
"numeronym",
4647
"paamayim",
4748
"pcre",
4849
"php's",

tests/Core/Util/Common/IsCamelCapsTest.php

Lines changed: 160 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public static function dataValidNotClassFormatPublic()
5656
'name' => 'thisISCamelCaps',
5757
'strict' => false,
5858
],
59+
'lower camelCase string with initial acronym' => [
60+
'name' => 'ISThisCamelCaps',
61+
'strict' => false,
62+
],
5963
];
6064

6165
}//end dataValidNotClassFormatPublic()
@@ -87,36 +91,49 @@ public function testInvalidNotClassFormatPublic($name)
8791
public static function dataInvalidNotClassFormatPublic()
8892
{
8993
return [
90-
'string with initial underscore (invalid when $public is true)' => [
94+
'string with initial underscore (invalid when $public is true)' => [
9195
'name' => '_thisIsCamelCaps',
9296
],
93-
'lower camelCase string with acronym (invalid when $strict is true)' => [
97+
'lower camelCase string with acronym (invalid when $strict is true)' => [
9498
'name' => 'thisISCamelCaps',
9599
],
96-
'PascalCase string' => [
100+
'lower camelCase string with initial acronym (invalid when $strict is true)' => [
101+
'name' => 'ISThisCamelCaps',
102+
],
103+
'PascalCase string' => [
97104
'name' => 'ThisIsCamelCaps',
98105
],
99-
'lower camelCase string with initial digit' => [
106+
'lower camelCase string with initial digit' => [
100107
'name' => '3thisIsCamelCaps',
101108
],
102-
'lower camelCase string with initial [^a-zA-z_] character: *' => [
109+
'lower camelCase string with initial illegal character: *' => [
103110
'name' => '*thisIsCamelCaps',
104111
],
105-
'lower camelCase string with initial [^a-zA-z_] character: -' => [
112+
'lower camelCase string with initial illegal character: -' => [
106113
'name' => '-thisIsCamelCaps',
107114
],
108-
'lower camelCase string with medial [^a-zA-z_] character: *' => [
115+
'lower camelCase string with initial illegal character: é' => [
116+
'name' => 'éCamelCaps',
117+
],
118+
'lower camelCase string with medial illegal character: *' => [
109119
'name' => 'this*IsCamelCaps',
110120
],
111-
'lower camelCase string with medial [^a-zA-z_] character: -' => [
121+
'lower camelCase string with medial illegal character: -' => [
112122
'name' => 'this-IsCamelCaps',
113123
],
114-
'lower camelCase string with single medial underscore' => [
124+
'lower camelCase string with medial illegal character: é' => [
125+
// No camels were harmed in the cspell:disable-next-line.
126+
'name' => 'thisIsCamélCaps',
127+
],
128+
'lower camelCase string with single medial underscore' => [
115129
'name' => 'this_IsCamelCaps',
116130
],
117-
'snake_case string' => [
131+
'snake_case string' => [
118132
'name' => 'this_is_camel_caps',
119133
],
134+
'empty string' => [
135+
'name' => '',
136+
],
120137
];
121138

122139
}//end dataInvalidNotClassFormatPublic()
@@ -149,19 +166,23 @@ public function testValidNotClassFormatPrivate($name, $strict)
149166
public static function dataValidNotClassFormatPrivate()
150167
{
151168
return [
152-
'lower camelCase string with initial underscore' => [
169+
'lower camelCase string with initial underscore' => [
153170
'name' => '_thisIsCamelCaps',
154171
'strict' => true,
155172
],
156-
'lower camelCase string with acronym and initial underscore' => [
173+
'lower camelCase string with acronym and initial underscore' => [
157174
'name' => '_thisISCamelCaps',
158175
'strict' => false,
159176
],
160-
'_i18N' => [
177+
'lower camelCase string with acronym after initial underscore' => [
178+
'name' => '_ISThisCamelCaps',
179+
'strict' => false,
180+
],
181+
'numeronym with initial underscore and capital after digit' => [
161182
'name' => '_i18N',
162183
'strict' => true,
163184
],
164-
'_i18n' => [
185+
'numeronym with initial underscore and lowercase character after digit' => [
165186
'name' => '_i18n',
166187
'strict' => true,
167188
],
@@ -221,18 +242,30 @@ public static function dataInvalidNotClassFormatPrivate()
221242
'name' => '3thisIsCamelCaps',
222243
'strict' => true,
223244
],
224-
'lower camelCase string with initial [^a-zA-Z_] character: *' => [
245+
'lower camelCase string with initial illegal character: *' => [
225246
'name' => '*thisIsCamelCaps',
226247
'strict' => true,
227248
],
228-
'lower camelCase string with initial [^a-zA-Z_] character: -' => [
249+
'lower camelCase string with initial illegal character: -' => [
229250
'name' => '-thisIsCamelCaps',
230251
'strict' => true,
231252
],
253+
'lower camelCase string with initial illegal character: é' => [
254+
'name' => 'éCamelCaps',
255+
'strict' => true,
256+
],
232257
'snake_case string with initial underscore' => [
233258
'name' => '_this_is_camel_caps',
234259
'strict' => true,
235260
],
261+
'single underscore' => [
262+
'name' => '_',
263+
'strict' => true,
264+
],
265+
'empty string' => [
266+
'name' => '',
267+
'strict' => true,
268+
],
236269
];
237270

238271
}//end dataInvalidNotClassFormatPrivate()
@@ -277,6 +310,26 @@ public static function dataValidClassFormatPublic()
277310
'name' => 'This3IsCamelCaps',
278311
'strict' => false,
279312
],
313+
'PascalCase string with digit inside word' => [
314+
'name' => 'Th1sIsCamelCaps',
315+
'strict' => false,
316+
],
317+
'Single capital (strict)' => [
318+
'name' => 'A',
319+
'strict' => true,
320+
],
321+
'Single capital with digit (strict)' => [
322+
'name' => 'A1',
323+
'strict' => true,
324+
],
325+
'Single capital (relaxed)' => [
326+
'name' => 'A',
327+
'strict' => false,
328+
],
329+
'Single capital with digit (relaxed)' => [
330+
'name' => 'A1',
331+
'strict' => false,
332+
],
280333
];
281334

282335
}//end dataValidClassFormatPublic()
@@ -317,6 +370,9 @@ public static function dataInvalidClassFormat()
317370
'capitalised snake case' => [
318371
'name' => 'This_Is_Camel_Caps',
319372
],
373+
'empty string' => [
374+
'name' => '',
375+
],
320376
];
321377

322378
}//end dataInvalidClassFormat()
@@ -360,9 +416,97 @@ public static function dataInvalidClassFormatPrivate()
360416
'name' => '_ThisIsCamelCaps',
361417
'public' => false,
362418
],
419+
'empty string (public)' => [
420+
'name' => '',
421+
'public' => true,
422+
],
423+
'empty string (private)' => [
424+
'name' => '',
425+
'public' => false,
426+
],
363427
];
364428

365429
}//end dataInvalidClassFormatPrivate()
366430

367431

432+
/**
433+
* Test valid strings with default arguments.
434+
*
435+
* @param string $name The tested name.
436+
*
437+
* @dataProvider dataValidDefaultArguments
438+
*
439+
* @return void
440+
*/
441+
public function testValidDefaultArguments($name)
442+
{
443+
$this->assertTrue(Common::isCamelCaps($name));
444+
445+
}//end testValidDefaultArguments()
446+
447+
448+
/**
449+
* Data provider.
450+
*
451+
* @see testValidDefaultArguments()
452+
*
453+
* @return array<string, array<string, string>>
454+
*/
455+
public static function dataValidDefaultArguments()
456+
{
457+
return [
458+
'lower camelCase string' => [
459+
'name' => 'thisIsCamelCaps',
460+
],
461+
'lower camelCase string with medial digit' => [
462+
'name' => 'this3IsCamelCaps',
463+
],
464+
];
465+
466+
}//end dataValidDefaultArguments()
467+
468+
469+
/**
470+
* Test invalid strings with default arguments.
471+
*
472+
* @param string $name The tested name.
473+
*
474+
* @dataProvider dataInvalidDefaultArguments
475+
*
476+
* @return void
477+
*/
478+
public function testInvalidDefaultArguments($name)
479+
{
480+
$this->assertFalse(Common::isCamelCaps($name));
481+
482+
}//end testInvalidDefaultArguments()
483+
484+
485+
/**
486+
* Data provider.
487+
*
488+
* @see testInvalidDefaultArguments()
489+
*
490+
* @return array<string, array<string, string>>
491+
*/
492+
public static function dataInvalidDefaultArguments()
493+
{
494+
return [
495+
'PascalCase string' => [
496+
'name' => 'ThisIsCamelCaps',
497+
],
498+
'PascalCase string with acronym' => [
499+
'name' => 'ThisISCamelCaps',
500+
],
501+
'lower camelCase string with initial underscore' => [
502+
'name' => '_thisIsCamelCaps',
503+
],
504+
'lower camelCase string with acronym' => [
505+
'name' => 'thisISCamelCaps',
506+
],
507+
];
508+
509+
}//end dataInvalidDefaultArguments()
510+
511+
368512
}//end class

0 commit comments

Comments
 (0)