Skip to content

Commit a463796

Browse files
committed
Tests/Tokenizer: expand default keyword tests
This commit adds two new tests to the tokenizer tests for the `default` keyword: - Ensure that the `default` scope is set correctly when the scope closer is shared with a `switch`. - Ensure that the `default` scope is set correctly when there is an inner if condition with and without braces. Both tests were copied from similar tests for the `case` keyword that were added to protect against regressions (see b9809c7 and 61b9e9b).
1 parent 5e5e6dc commit a463796

File tree

2 files changed

+62
-18
lines changed

2 files changed

+62
-18
lines changed

tests/Core/Tokenizers/Tokenizer/RecurseScopeMapDefaultKeywordConditionsTest.inc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,26 @@ class Foo {
201201
/* testMethodDeclaration */
202202
public function default() {}
203203
}
204+
205+
function switchAndDefaultSharingScopeCloser($i) {
206+
switch ($i):
207+
/* testSwitchAndDefaultSharingScopeCloser */
208+
default:
209+
echo 'one';
210+
endswitch;
211+
}
212+
213+
function switchDefaultNestedIfWithAndWithoutBraces($i, $foo, $baz) {
214+
switch ($i) {
215+
/* testSwitchDefaultNestedIfWithAndWithoutBraces */
216+
default:
217+
if ($foo) {
218+
return true;
219+
} elseif ($baz) {
220+
return true;
221+
} else {
222+
echo 'else';
223+
}
224+
break;
225+
}
226+
}

tests/Core/Tokenizers/Tokenizer/RecurseScopeMapDefaultKeywordConditionsTest.php

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,22 @@ public static function dataMatchDefault()
109109
* Note: Cases and default structures within a switch control structure *do* get case/default scope
110110
* conditions.
111111
*
112-
* @param string $testMarker The comment prefacing the target token.
113-
* @param int $openerOffset The expected offset of the scope opener in relation to the testMarker.
114-
* @param int $closerOffset The expected offset of the scope closer in relation to the testMarker.
115-
* @param int|null $conditionStop The expected offset at which tokens stop having T_DEFAULT as a scope condition.
116-
* @param string $testContent The token content to look for.
112+
* @param string $testMarker The comment prefacing the target token.
113+
* @param int $openerOffset The expected offset of the scope opener in relation to the testMarker.
114+
* @param int $closerOffset The expected offset of the scope closer in relation to the testMarker.
115+
* @param int|null $conditionStop The expected offset at which tokens stop having T_DEFAULT as a scope condition.
116+
* @param string $testContent The token content to look for.
117+
* @param bool $sharedScopeCloser Whether to skip checking for the `scope_condition` of the
118+
* scope closer. Needed when the default and switch
119+
* structures share a scope closer. See
120+
* https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/810.
117121
*
118122
* @dataProvider dataSwitchDefault
119123
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap
120124
*
121125
* @return void
122126
*/
123-
public function testSwitchDefault($testMarker, $openerOffset, $closerOffset, $conditionStop=null, $testContent='default')
127+
public function testSwitchDefault($testMarker, $openerOffset, $closerOffset, $conditionStop=null, $testContent='default', $sharedScopeCloser=false)
124128
{
125129
$tokens = $this->phpcsFile->getTokens();
126130

@@ -147,13 +151,17 @@ public function testSwitchDefault($testMarker, $openerOffset, $closerOffset, $co
147151
$this->assertSame($expectedScopeOpener, $tokens[$opener]['scope_opener'], 'T_DEFAULT opener scope opener token incorrect');
148152
$this->assertSame($expectedScopeCloser, $tokens[$opener]['scope_closer'], 'T_DEFAULT opener scope closer token incorrect');
149153

150-
$closer = $tokenArray['scope_closer'];
151-
$this->assertArrayHasKey('scope_condition', $tokens[$closer], 'Closer scope condition is not set');
152-
$this->assertArrayHasKey('scope_opener', $tokens[$closer], 'Closer scope opener is not set');
153-
$this->assertArrayHasKey('scope_closer', $tokens[$closer], 'Closer scope closer is not set');
154-
$this->assertSame($token, $tokens[$closer]['scope_condition'], 'Closer scope condition is not the T_DEFAULT token');
155-
$this->assertSame($expectedScopeOpener, $tokens[$closer]['scope_opener'], 'T_DEFAULT closer scope opener token incorrect');
156-
$this->assertSame($expectedScopeCloser, $tokens[$closer]['scope_closer'], 'T_DEFAULT closer scope closer token incorrect');
154+
if ($sharedScopeCloser === false) {
155+
$closer = $tokenArray['scope_closer'];
156+
$this->assertArrayHasKey('scope_condition', $tokens[$closer], 'Closer scope condition is not set');
157+
$this->assertArrayHasKey('scope_opener', $tokens[$closer], 'Closer scope opener is not set');
158+
$this->assertArrayHasKey('scope_closer', $tokens[$closer], 'Closer scope closer is not set');
159+
$this->assertSame($token, $tokens[$closer]['scope_condition'], 'Closer scope condition is not the T_DEFAULT token');
160+
$this->assertSame($expectedScopeOpener, $tokens[$closer]['scope_opener'], 'T_DEFAULT closer scope opener token incorrect');
161+
$this->assertSame($expectedScopeCloser, $tokens[$closer]['scope_closer'], 'T_DEFAULT closer scope closer token incorrect');
162+
} else {
163+
$closer = $expectedScopeCloser;
164+
}
157165

158166
if (($opener + 1) !== $closer) {
159167
$end = $closer;
@@ -183,12 +191,12 @@ public function testSwitchDefault($testMarker, $openerOffset, $closerOffset, $co
183191
public static function dataSwitchDefault()
184192
{
185193
return [
186-
'simple_switch_default' => [
194+
'simple_switch_default' => [
187195
'testMarker' => '/* testSimpleSwitchDefault */',
188196
'openerOffset' => 1,
189197
'closerOffset' => 4,
190198
],
191-
'simple_switch_default_with_curlies' => [
199+
'simple_switch_default_with_curlies' => [
192200
// For a default structure with curly braces, the scope opener
193201
// will be the open curly and the closer the close curly.
194202
// However, scope conditions will not be set for open to close,
@@ -198,21 +206,34 @@ public static function dataSwitchDefault()
198206
'closerOffset' => 12,
199207
'conditionStop' => 6,
200208
],
201-
'switch_default_toplevel' => [
209+
'switch_default_toplevel' => [
202210
'testMarker' => '/* testSwitchDefault */',
203211
'openerOffset' => 1,
204212
'closerOffset' => 43,
205213
],
206-
'switch_default_nested_in_match_case' => [
214+
'switch_default_nested_in_match_case' => [
207215
'testMarker' => '/* testSwitchDefaultNestedInMatchCase */',
208216
'openerOffset' => 1,
209217
'closerOffset' => 20,
210218
],
211-
'switch_default_nested_in_match_default' => [
219+
'switch_default_nested_in_match_default' => [
212220
'testMarker' => '/* testSwitchDefaultNestedInMatchDefault */',
213221
'openerOffset' => 1,
214222
'closerOffset' => 18,
215223
],
224+
'switch_and_default_sharing_scope_closer' => [
225+
'testMarker' => '/* testSwitchAndDefaultSharingScopeCloser */',
226+
'openerOffset' => 1,
227+
'closerOffset' => 10,
228+
'conditionStop' => null,
229+
'testContent' => 'default',
230+
'sharedScopeCloser' => true,
231+
],
232+
'switch_and_default_with_nested_if_with_and_without_braces' => [
233+
'testMarker' => '/* testSwitchDefaultNestedIfWithAndWithoutBraces */',
234+
'openerOffset' => 1,
235+
'closerOffset' => 52,
236+
],
216237
];
217238

218239
}//end dataSwitchDefault()

0 commit comments

Comments
 (0)