Skip to content

Commit 0880146

Browse files
committed
Squiz/ForLoopDeclaration: add a lot more unit tests
Tests covering: * Missing first expression. * First/Last expression missing in combination with non-zero `requiredSpacesAfterOpen`/`requiredSpacesBeforeClose`. * Expressions interlaces with comments. * Multi-line expressions. * Multi-line whitespace which needs to be removed. * Expressions including code which itself contains a semi-colon. * The parse error warning.
1 parent b4ad583 commit 0880146

File tree

5 files changed

+319
-23
lines changed

5 files changed

+319
-23
lines changed

src/Standards/Squiz/Tests/ControlStructures/ForLoopDeclarationUnitTest.inc

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,81 @@ for ( $i = 0; $i < 10; $i++ ) {}
3939
for ( $i = 0; $i < 10; $i++ ) {}
4040
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesAfterOpen 0
4141
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesBeforeClose 0
42+
43+
for ( ; $i < 10; $i++) {}
44+
for (; $i < 10; $i++) {}
45+
46+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesAfterOpen 1
47+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesBeforeClose 1
48+
for ( ; $i < 10; $i++ ) {}
49+
for ( ; $i < 10; $i++ ) {}
50+
for (; $i < 10; $i++ ) {}
51+
52+
for ( $i = 0; $i < 10; ) {}
53+
for ( $i = 0; $i < 10;) {}
54+
for ( $i = 0; $i < 10; ) {}
55+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesAfterOpen 0
56+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesBeforeClose 0
57+
58+
// Test handling of comments and inline annotations.
59+
for ( /*phpcs:enable*/ $i = 0 /*start*/ ; /*end*/$i < 10/*comment*/; $i++ /*comment*/ ) {}
60+
61+
// Test multi-line FOR control structure.
62+
for (
63+
$i = 0;
64+
$i < 10;
65+
$i++
66+
) {}
67+
68+
// Test multi-line FOR control structure with comments and annotations.
69+
for (
70+
$i = 0; /* Start */
71+
$i < 10; /* phpcs:ignore Standard.Category.SniffName -- for reasons. */
72+
$i++ // comment
73+
74+
) {}
75+
76+
// Test fixing each error in one go. Note: lines 78 + 82 contain trailing whitespace on purpose.
77+
for (
78+
79+
80+
$i = 0
81+
82+
;
83+
84+
$i < 10
85+
86+
;
87+
88+
$i++
89+
90+
91+
) {}
92+
93+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesAfterOpen 1
94+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesBeforeClose 1
95+
for (
96+
97+
98+
99+
$i = 0
100+
101+
;
102+
103+
$i < 10
104+
105+
;
106+
107+
$i++
108+
109+
110+
) {}
111+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesAfterOpen 0
112+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesBeforeClose 0
113+
114+
// Test with semi-colon not belonging to for.
115+
for ($i = function() { return $this->i ; }; $i < function() { return $this->max; }; $i++) {}
116+
for ($i = function() { return $this->i; }; $i < function() { return $this->max; } ; $i++) {}
117+
118+
// This test has to be the last one in the file! Intentional parse error check.
119+
for

src/Standards/Squiz/Tests/ControlStructures/ForLoopDeclarationUnitTest.inc.fixed

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,47 @@ for ( $i = 0; $i < 10; $i++ ) {}
3939
for ( $i = 0; $i < 10; $i++ ) {}
4040
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesAfterOpen 0
4141
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesBeforeClose 0
42+
43+
for (; $i < 10; $i++) {}
44+
for (; $i < 10; $i++) {}
45+
46+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesAfterOpen 1
47+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesBeforeClose 1
48+
for ( ; $i < 10; $i++ ) {}
49+
for ( ; $i < 10; $i++ ) {}
50+
for ( ; $i < 10; $i++ ) {}
51+
52+
for ( $i = 0; $i < 10; ) {}
53+
for ( $i = 0; $i < 10; ) {}
54+
for ( $i = 0; $i < 10; ) {}
55+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesAfterOpen 0
56+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesBeforeClose 0
57+
58+
// Test handling of comments and inline annotations.
59+
for (/*phpcs:enable*/ $i = 0 /*start*/; /*end*/$i < 10/*comment*/; $i++ /*comment*/) {}
60+
61+
// Test multi-line FOR control structure.
62+
for ($i = 0; $i < 10; $i++) {}
63+
64+
// Test multi-line FOR control structure with comments and annotations.
65+
for ($i = 0; /* Start */
66+
$i < 10; /* phpcs:ignore Standard.Category.SniffName -- for reasons. */
67+
$i++ // comment
68+
69+
) {}
70+
71+
// Test fixing each error in one go. Note: lines 78 + 82 contain trailing whitespace on purpose.
72+
for ($i = 0; $i < 10; $i++) {}
73+
74+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesAfterOpen 1
75+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesBeforeClose 1
76+
for ( $i = 0; $i < 10; $i++ ) {}
77+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesAfterOpen 0
78+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesBeforeClose 0
79+
80+
// Test with semi-colon not belonging to for.
81+
for ($i = function() { return $this->i ; }; $i < function() { return $this->max; }; $i++) {}
82+
for ($i = function() { return $this->i; }; $i < function() { return $this->max; }; $i++) {}
83+
84+
// This test has to be the last one in the file! Intentional parse error check.
85+
for

src/Standards/Squiz/Tests/ControlStructures/ForLoopDeclarationUnitTest.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,81 @@ for ( var i = 0; i < 10; i++ ) {}
4545
for ( var i = 0; i < 10; i++ ) {}
4646
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesAfterOpen 0
4747
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesBeforeClose 0
48+
49+
for ( ; i < 10; i++) {}
50+
for (; i < 10; i++) {}
51+
52+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesAfterOpen 1
53+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesBeforeClose 1
54+
for ( ; i < 10; i++ ) {}
55+
for ( ; i < 10; i++ ) {}
56+
for (; i < 10; i++ ) {}
57+
58+
for ( i = 0; i < 10; ) {}
59+
for ( i = 0; i < 10;) {}
60+
for ( i = 0; i < 10; ) {}
61+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesAfterOpen 0
62+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesBeforeClose 0
63+
64+
// Test handling of comments and inline annotations.
65+
for ( /*phpcs:enable*/ i = 0 /*start*/ ; /*end*/i < 10/*comment*/; i++ /*comment*/ ) {}
66+
67+
// Test multi-line FOR control structure.
68+
for (
69+
i = 0;
70+
i < 10;
71+
i++
72+
) {}
73+
74+
// Test multi-line FOR control structure with comments and annotations.
75+
for (
76+
i = 0; /* Start */
77+
i < 10; /* phpcs:ignore Standard.Category.SniffName -- for reasons. */
78+
i++ // comment
79+
80+
) {}
81+
82+
// Test fixing each error in one go. Note: lines 84 + 88 contain trailing whitespace on purpose.
83+
for (
84+
85+
86+
i = 0
87+
88+
;
89+
90+
i < 10
91+
92+
;
93+
94+
i++
95+
96+
97+
) {}
98+
99+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesAfterOpen 1
100+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesBeforeClose 1
101+
for (
102+
103+
104+
105+
i = 0
106+
107+
;
108+
109+
i < 10
110+
111+
;
112+
113+
i++
114+
115+
116+
) {}
117+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesAfterOpen 0
118+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesBeforeClose 0
119+
120+
// Test with semi-colon not belonging to for.
121+
for (i = function() {self.widgetLoaded(widget.id) ; }; i < function() {self.widgetLoaded(widget.id);}; i++) {}
122+
for (i = function() {self.widgetLoaded(widget.id);}; i < function() {self.widgetLoaded(widget.id);} ; i++) {}
123+
124+
// This test has to be the last one in the file! Intentional parse error check.
125+
for

src/Standards/Squiz/Tests/ControlStructures/ForLoopDeclarationUnitTest.js.fixed

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,47 @@ for ( var i = 0; i < 10; i++ ) {}
4545
for ( var i = 0; i < 10; i++ ) {}
4646
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesAfterOpen 0
4747
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesBeforeClose 0
48+
49+
for (; i < 10; i++) {}
50+
for (; i < 10; i++) {}
51+
52+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesAfterOpen 1
53+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesBeforeClose 1
54+
for ( ; i < 10; i++ ) {}
55+
for ( ; i < 10; i++ ) {}
56+
for ( ; i < 10; i++ ) {}
57+
58+
for ( i = 0; i < 10; ) {}
59+
for ( i = 0; i < 10; ) {}
60+
for ( i = 0; i < 10; ) {}
61+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesAfterOpen 0
62+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesBeforeClose 0
63+
64+
// Test handling of comments and inline annotations.
65+
for (/*phpcs:enable*/ i = 0 /*start*/; /*end*/i < 10/*comment*/; i++ /*comment*/) {}
66+
67+
// Test multi-line FOR control structure.
68+
for (i = 0; i < 10; i++) {}
69+
70+
// Test multi-line FOR control structure with comments and annotations.
71+
for (i = 0; /* Start */
72+
i < 10; /* phpcs:ignore Standard.Category.SniffName -- for reasons. */
73+
i++ // comment
74+
75+
) {}
76+
77+
// Test fixing each error in one go. Note: lines 84 + 88 contain trailing whitespace on purpose.
78+
for (i = 0; i < 10; i++) {}
79+
80+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesAfterOpen 1
81+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesBeforeClose 1
82+
for ( i = 0; i < 10; i++ ) {}
83+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesAfterOpen 0
84+
// phpcs:set Squiz.ControlStructures.ForLoopDeclaration requiredSpacesBeforeClose 0
85+
86+
// Test with semi-colon not belonging to for.
87+
for (i = function() {self.widgetLoaded(widget.id) ; }; i < function() {self.widgetLoaded(widget.id);}; i++) {}
88+
for (i = function() {self.widgetLoaded(widget.id);}; i < function() {self.widgetLoaded(widget.id);}; i++) {}
89+
90+
// This test has to be the last one in the file! Intentional parse error check.
91+
for

src/Standards/Squiz/Tests/ControlStructures/ForLoopDeclarationUnitTest.php

Lines changed: 75 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,33 +30,74 @@ public function getErrorList($testFile='ForLoopDeclarationUnitTest.inc')
3030
switch ($testFile) {
3131
case 'ForLoopDeclarationUnitTest.inc':
3232
return [
33-
8 => 2,
34-
11 => 2,
35-
14 => 2,
36-
17 => 2,
37-
21 => 6,
38-
27 => 1,
39-
30 => 1,
40-
37 => 2,
41-
39 => 2,
33+
8 => 2,
34+
11 => 2,
35+
14 => 2,
36+
17 => 2,
37+
21 => 6,
38+
27 => 1,
39+
30 => 1,
40+
37 => 2,
41+
39 => 2,
42+
43 => 1,
43+
49 => 1,
44+
50 => 1,
45+
53 => 1,
46+
54 => 1,
47+
59 => 4,
48+
62 => 1,
49+
63 => 1,
50+
64 => 1,
51+
66 => 1,
52+
69 => 1,
53+
74 => 1,
54+
77 => 1,
55+
82 => 2,
56+
86 => 2,
57+
91 => 1,
58+
95 => 1,
59+
101 => 2,
60+
105 => 2,
61+
110 => 1,
62+
116 => 2,
4263
];
43-
break;
64+
4465
case 'ForLoopDeclarationUnitTest.js':
4566
return [
46-
6 => 2,
47-
9 => 2,
48-
12 => 2,
49-
15 => 2,
50-
19 => 6,
51-
33 => 1,
52-
36 => 1,
53-
43 => 2,
54-
45 => 2,
67+
6 => 2,
68+
9 => 2,
69+
12 => 2,
70+
15 => 2,
71+
19 => 6,
72+
33 => 1,
73+
36 => 1,
74+
43 => 2,
75+
45 => 2,
76+
49 => 1,
77+
55 => 1,
78+
56 => 1,
79+
59 => 1,
80+
60 => 1,
81+
65 => 4,
82+
68 => 1,
83+
69 => 1,
84+
70 => 1,
85+
72 => 1,
86+
75 => 1,
87+
80 => 1,
88+
83 => 1,
89+
88 => 2,
90+
92 => 2,
91+
97 => 1,
92+
101 => 1,
93+
107 => 2,
94+
111 => 2,
95+
116 => 1,
96+
122 => 2,
5597
];
56-
break;
98+
5799
default:
58100
return [];
59-
break;
60101
}//end switch
61102

62103
}//end getErrorList()
@@ -68,11 +109,22 @@ public function getErrorList($testFile='ForLoopDeclarationUnitTest.inc')
68109
* The key of the array should represent the line number and the value
69110
* should represent the number of warnings that should occur on that line.
70111
*
112+
* @param string $testFile The name of the file being tested.
113+
*
71114
* @return array<int, int>
72115
*/
73-
public function getWarningList()
116+
public function getWarningList($testFile='ForLoopDeclarationUnitTest.inc')
74117
{
75-
return [];
118+
switch ($testFile) {
119+
case 'ForLoopDeclarationUnitTest.inc':
120+
return [119 => 1];
121+
122+
case 'ForLoopDeclarationUnitTest.js':
123+
return [125 => 1];
124+
125+
default:
126+
return [];
127+
}//end switch
76128

77129
}//end getWarningList()
78130

0 commit comments

Comments
 (0)