Skip to content

Commit 82fae8e

Browse files
rodrigoprimojrfnl
authored andcommitted
Tests/Tokenizer: expand recurseScopeMap() tests for the case keyword
This commit expands the existing `Tokenizer::recurseScopeMap()` tests for the `case` keyword when used in a switch statement to check the value of the `scope_condition`, `scope_opener` and `scope_closer` indexes besides checking if they are set. This is needed for a new test case that will be added in a subsequent commit.
1 parent 9546775 commit 82fae8e

File tree

2 files changed

+110
-13
lines changed

2 files changed

+110
-13
lines changed

tests/Core/Tokenizers/Tokenizer/RecurseScopeMapCaseKeywordConditionsTest.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,21 @@ switch (true) {
6262
case CONSTANT = 1:
6363
/* testIsNotEnumCaseIsCaseInsensitive */
6464
cAsE CONSTANT:
65+
/* testCaseConstantCloserMarker */
6566
}
6667

6768
switch ($x) {
6869
/* testCaseInSwitchWhenCreatingEnumInSwitch1 */
6970
case 'a': {
7071
enum Foo {}
7172
break;
73+
/* testCaseInSwitchWhenCreatingEnumInSwitch1CloserMarker */
7274
}
7375

7476
/* testCaseInSwitchWhenCreatingEnumInSwitch2 */
7577
case 'b';
7678
enum Bar {}
79+
/* testCaseInSwitchWhenCreatingEnumInSwitch2CloserMarker */
7780
break;
7881
}
7982

tests/Core/Tokenizers/Tokenizer/RecurseScopeMapCaseKeywordConditionsTest.php

Lines changed: 107 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,65 @@ public static function dataEnumCases()
6666
/**
6767
* Test that "case" that is not enum case is still tokenized as `T_CASE`.
6868
*
69-
* @param string $testMarker The comment which prefaces the target token in the test file.
69+
* @param string $testMarker The comment which prefaces the target token in the test file.
70+
* @param array<string, int|string> $expectedTokens The expected token codes for the scope opener/closer.
71+
* @param string|null $testCloserMarker Optional. The comment which prefaces the scope closer if different
72+
* from the test marker.
7073
*
7174
* @dataProvider dataNotEnumCases
7275
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap
7376
*
7477
* @return void
7578
*/
76-
public function testNotEnumCases($testMarker)
79+
public function testNotEnumCases($testMarker, $expectedTokens, $testCloserMarker=null)
7780
{
7881
$tokens = $this->phpcsFile->getTokens();
79-
$case = $this->getTargetToken($testMarker, [T_ENUM_CASE, T_CASE]);
80-
$tokenArray = $tokens[$case];
82+
$caseIndex = $this->getTargetToken($testMarker, [T_ENUM_CASE, T_CASE]);
83+
$tokenArray = $tokens[$caseIndex];
84+
85+
$scopeCloserMarker = $testMarker;
86+
if (isset($testCloserMarker) === true) {
87+
$scopeCloserMarker = $testCloserMarker;
88+
}
89+
90+
$expectedScopeCondition = $caseIndex;
91+
$expectedScopeOpener = $this->getTargetToken($testMarker, $expectedTokens['scope_opener']);
92+
$expectedScopeCloser = $this->getTargetToken($scopeCloserMarker, $expectedTokens['scope_closer']);
8193

8294
// Make sure we're looking at the right token.
8395
$this->assertSame(T_CASE, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_CASE (code)');
8496

8597
$this->assertArrayHasKey('scope_condition', $tokenArray, 'Scope condition is not set');
98+
$this->assertSame(
99+
$expectedScopeCondition,
100+
$tokenArray['scope_condition'],
101+
sprintf(
102+
'Scope condition not set correctly; expected T_CASE, found %s.',
103+
$tokens[$tokenArray['scope_condition']]['type']
104+
)
105+
);
106+
86107
$this->assertArrayHasKey('scope_opener', $tokenArray, 'Scope opener is not set');
108+
$this->assertSame(
109+
$expectedScopeOpener,
110+
$tokenArray['scope_opener'],
111+
sprintf(
112+
'Scope opener not set correctly; expected %s, found %s.',
113+
$tokens[$expectedScopeOpener]['type'],
114+
$tokens[$tokenArray['scope_opener']]['type']
115+
)
116+
);
117+
87118
$this->assertArrayHasKey('scope_closer', $tokenArray, 'Scope closer is not set');
119+
$this->assertSame(
120+
$expectedScopeCloser,
121+
$tokenArray['scope_closer'],
122+
sprintf(
123+
'Scope closer not set correctly; expected %s, found %s.',
124+
$tokens[$expectedScopeCloser]['type'],
125+
$tokens[$tokenArray['scope_closer']]['type']
126+
)
127+
);
88128

89129
}//end testNotEnumCases()
90130

@@ -94,19 +134,73 @@ public function testNotEnumCases($testMarker)
94134
*
95135
* @see testNotEnumCases()
96136
*
97-
* @return array<string, array<string>>
137+
* @return array<string, array<string, string|array<string, int|string>>>
98138
*/
99139
public static function dataNotEnumCases()
100140
{
101141
return [
102-
'switch case with constant, semicolon condition end' => ['/* testCaseWithSemicolonIsNotEnumCase */'],
103-
'switch case with constant, colon condition end' => ['/* testCaseWithConstantIsNotEnumCase */'],
104-
'switch case with constant, comparison' => ['/* testCaseWithConstantAndIdenticalIsNotEnumCase */'],
105-
'switch case with constant, assignment' => ['/* testCaseWithAssigmentToConstantIsNotEnumCase */'],
106-
'switch case with constant, keyword in mixed case' => ['/* testIsNotEnumCaseIsCaseInsensitive */'],
107-
'switch case, body in curlies declares enum' => ['/* testCaseInSwitchWhenCreatingEnumInSwitch1 */'],
108-
'switch case, body after semicolon declares enum' => ['/* testCaseInSwitchWhenCreatingEnumInSwitch2 */'],
109-
'switch case, shared closer with switch' => ['/* testSwitchCaseScopeCloserSharedWithSwitch */'],
142+
'switch case with constant, semicolon condition end' => [
143+
'testMarker' => '/* testCaseWithSemicolonIsNotEnumCase */',
144+
'expectedTokens' => [
145+
'scope_opener' => T_SEMICOLON,
146+
'scope_closer' => T_CLOSE_CURLY_BRACKET,
147+
],
148+
],
149+
'switch case with constant, colon condition end' => [
150+
'testMarker' => '/* testCaseWithConstantIsNotEnumCase */',
151+
'expectedTokens' => [
152+
'scope_opener' => T_COLON,
153+
'scope_closer' => T_CLOSE_CURLY_BRACKET,
154+
],
155+
'testCloserMarker' => '/* testCaseConstantCloserMarker */',
156+
],
157+
'switch case with constant, comparison' => [
158+
'testMarker' => '/* testCaseWithConstantAndIdenticalIsNotEnumCase */',
159+
'expectedTokens' => [
160+
'scope_opener' => T_COLON,
161+
'scope_closer' => T_CLOSE_CURLY_BRACKET,
162+
],
163+
'testCloserMarker' => '/* testCaseConstantCloserMarker */',
164+
],
165+
'switch case with constant, assignment' => [
166+
'testMarker' => '/* testCaseWithAssigmentToConstantIsNotEnumCase */',
167+
'expectedTokens' => [
168+
'scope_opener' => T_COLON,
169+
'scope_closer' => T_CLOSE_CURLY_BRACKET,
170+
],
171+
'testCloserMarker' => '/* testCaseConstantCloserMarker */',
172+
],
173+
'switch case with constant, keyword in mixed case' => [
174+
'testMarker' => '/* testIsNotEnumCaseIsCaseInsensitive */',
175+
'expectedTokens' => [
176+
'scope_opener' => T_COLON,
177+
'scope_closer' => T_CLOSE_CURLY_BRACKET,
178+
],
179+
'testCloserMarker' => '/* testCaseConstantCloserMarker */',
180+
],
181+
'switch case, body in curlies declares enum' => [
182+
'testMarker' => '/* testCaseInSwitchWhenCreatingEnumInSwitch1 */',
183+
'expectedTokens' => [
184+
'scope_opener' => T_OPEN_CURLY_BRACKET,
185+
'scope_closer' => T_CLOSE_CURLY_BRACKET,
186+
],
187+
'testCloserMarker' => '/* testCaseInSwitchWhenCreatingEnumInSwitch1CloserMarker */',
188+
],
189+
'switch case, body after semicolon declares enum' => [
190+
'testMarker' => '/* testCaseInSwitchWhenCreatingEnumInSwitch2 */',
191+
'expectedTokens' => [
192+
'scope_opener' => T_SEMICOLON,
193+
'scope_closer' => T_BREAK,
194+
],
195+
'testCloserMarker' => '/* testCaseInSwitchWhenCreatingEnumInSwitch2CloserMarker */',
196+
],
197+
'switch case, shared closer with switch' => [
198+
'testMarker' => '/* testSwitchCaseScopeCloserSharedWithSwitch */',
199+
'expectedTokens' => [
200+
'scope_opener' => T_COLON,
201+
'scope_closer' => T_ENDSWITCH,
202+
],
203+
],
110204
];
111205

112206
}//end dataNotEnumCases()

0 commit comments

Comments
 (0)