Skip to content

Commit 9c70e44

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 9feb4e5 commit 9c70e44

File tree

2 files changed

+63
-19
lines changed

2 files changed

+63
-19
lines changed

tests/Core/Tokenizers/Tokenizer/RecurseScopeMapDefaultKeywordConditionsTest.inc

+23
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,29 @@ function switchWithDefaultInMatch() {
9292
};
9393
}
9494

95+
function switchAndDefaultSharingScopeCloser($i) {
96+
switch ($i):
97+
/* testSwitchAndDefaultSharingScopeCloser */
98+
default:
99+
echo 'one';
100+
endswitch;
101+
}
102+
103+
function switchDefaultNestedIfWithAndWithoutBraces($i, $foo, $baz) {
104+
switch ($i) {
105+
/* testSwitchDefaultNestedIfWithAndWithoutBraces */
106+
default:
107+
if ($foo) {
108+
return true;
109+
} elseif ($baz)
110+
return true;
111+
else {
112+
echo 'else';
113+
}
114+
break;
115+
}
116+
}
117+
95118
function shortArrayWithConstantKey() {
96119
$arr = [
97120
/* testClassConstantAsShortArrayKey */

tests/Core/Tokenizers/Tokenizer/RecurseScopeMapDefaultKeywordConditionsTest.php

+40-19
Original file line numberDiff line numberDiff line change
@@ -109,19 +109,23 @@ 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 in relation to the testMarker, at which tokens stop
116-
* having T_DEFAULT as a scope condition.
117-
* @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 in relation to the testMarker, at which tokens stop
116+
* having T_DEFAULT as a scope condition.
117+
* @param string $testContent The token content to look for.
118+
* @param bool $sharedScopeCloser Whether to skip checking for the `scope_condition` of the
119+
* scope closer. Needed when the default and switch
120+
* structures share a scope closer. See
121+
* https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/810.
118122
*
119123
* @dataProvider dataSwitchDefault
120124
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap
121125
*
122126
* @return void
123127
*/
124-
public function testSwitchDefault($testMarker, $openerOffset, $closerOffset, $conditionStop=null, $testContent='default')
128+
public function testSwitchDefault($testMarker, $openerOffset, $closerOffset, $conditionStop=null, $testContent='default', $sharedScopeCloser=false)
125129
{
126130
$tokens = $this->phpcsFile->getTokens();
127131

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

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

159167
if (($opener + 1) !== $closer) {
160168
$end = $closer;
@@ -184,12 +192,12 @@ public function testSwitchDefault($testMarker, $openerOffset, $closerOffset, $co
184192
public static function dataSwitchDefault()
185193
{
186194
return [
187-
'simple_switch_default' => [
195+
'simple_switch_default' => [
188196
'testMarker' => '/* testSimpleSwitchDefault */',
189197
'openerOffset' => 1,
190198
'closerOffset' => 4,
191199
],
192-
'simple_switch_default_with_curlies' => [
200+
'simple_switch_default_with_curlies' => [
193201
// For a default structure with curly braces, the scope opener
194202
// will be the open curly and the closer the close curly.
195203
// However, scope conditions will not be set for open to close,
@@ -199,21 +207,34 @@ public static function dataSwitchDefault()
199207
'closerOffset' => 12,
200208
'conditionStop' => 6,
201209
],
202-
'switch_default_toplevel' => [
210+
'switch_default_toplevel' => [
203211
'testMarker' => '/* testSwitchDefault */',
204212
'openerOffset' => 1,
205213
'closerOffset' => 43,
206214
],
207-
'switch_default_nested_in_match_case' => [
215+
'switch_default_nested_in_match_case' => [
208216
'testMarker' => '/* testSwitchDefaultNestedInMatchCase */',
209217
'openerOffset' => 1,
210218
'closerOffset' => 20,
211219
],
212-
'switch_default_nested_in_match_default' => [
220+
'switch_default_nested_in_match_default' => [
213221
'testMarker' => '/* testSwitchDefaultNestedInMatchDefault */',
214222
'openerOffset' => 1,
215223
'closerOffset' => 18,
216224
],
225+
'switch_and_default_sharing_scope_closer' => [
226+
'testMarker' => '/* testSwitchAndDefaultSharingScopeCloser */',
227+
'openerOffset' => 1,
228+
'closerOffset' => 10,
229+
'conditionStop' => null,
230+
'testContent' => 'default',
231+
'sharedScopeCloser' => true,
232+
],
233+
'switch_and_default_with_nested_if_with_and_without_braces' => [
234+
'testMarker' => '/* testSwitchDefaultNestedIfWithAndWithoutBraces */',
235+
'openerOffset' => 1,
236+
'closerOffset' => 48,
237+
],
217238
];
218239

219240
}//end dataSwitchDefault()

0 commit comments

Comments
 (0)