Skip to content

Commit c93eb90

Browse files
authored
Merge pull request #813 from rodrigoprimo/improve-default-keyword-tokenizer-tests
Tests/Tokenizer: expand `default` keyword tests
2 parents a0a4980 + 9c70e44 commit c93eb90

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
@@ -107,19 +107,23 @@ public static function dataMatchDefault()
107107
* Note: Cases and default structures within a switch control structure *do* get case/default scope
108108
* conditions.
109109
*
110-
* @param string $testMarker The comment prefacing the target token.
111-
* @param int $openerOffset The expected offset of the scope opener in relation to the testMarker.
112-
* @param int $closerOffset The expected offset of the scope closer in relation to the testMarker.
113-
* @param int|null $conditionStop The expected offset in relation to the testMarker, at which tokens stop
114-
* having T_DEFAULT as a scope condition.
115-
* @param string $testContent The token content to look for.
110+
* @param string $testMarker The comment prefacing the target token.
111+
* @param int $openerOffset The expected offset of the scope opener in relation to the testMarker.
112+
* @param int $closerOffset The expected offset of the scope closer in relation to the testMarker.
113+
* @param int|null $conditionStop The expected offset in relation to the testMarker, at which tokens stop
114+
* having T_DEFAULT as a scope condition.
115+
* @param string $testContent The token content to look for.
116+
* @param bool $sharedScopeCloser Whether to skip checking for the `scope_condition` of the
117+
* scope closer. Needed when the default and switch
118+
* structures share a scope closer. See
119+
* https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/810.
116120
*
117121
* @dataProvider dataSwitchDefault
118122
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap
119123
*
120124
* @return void
121125
*/
122-
public function testSwitchDefault($testMarker, $openerOffset, $closerOffset, $conditionStop=null, $testContent='default')
126+
public function testSwitchDefault($testMarker, $openerOffset, $closerOffset, $conditionStop=null, $testContent='default', $sharedScopeCloser=false)
123127
{
124128
$tokens = $this->phpcsFile->getTokens();
125129

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

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

157165
if (($opener + 1) !== $closer) {
158166
$end = $closer;
@@ -182,12 +190,12 @@ public function testSwitchDefault($testMarker, $openerOffset, $closerOffset, $co
182190
public static function dataSwitchDefault()
183191
{
184192
return [
185-
'simple_switch_default' => [
193+
'simple_switch_default' => [
186194
'testMarker' => '/* testSimpleSwitchDefault */',
187195
'openerOffset' => 1,
188196
'closerOffset' => 4,
189197
],
190-
'simple_switch_default_with_curlies' => [
198+
'simple_switch_default_with_curlies' => [
191199
// For a default structure with curly braces, the scope opener
192200
// will be the open curly and the closer the close curly.
193201
// However, scope conditions will not be set for open to close,
@@ -197,21 +205,34 @@ public static function dataSwitchDefault()
197205
'closerOffset' => 12,
198206
'conditionStop' => 6,
199207
],
200-
'switch_default_toplevel' => [
208+
'switch_default_toplevel' => [
201209
'testMarker' => '/* testSwitchDefault */',
202210
'openerOffset' => 1,
203211
'closerOffset' => 43,
204212
],
205-
'switch_default_nested_in_match_case' => [
213+
'switch_default_nested_in_match_case' => [
206214
'testMarker' => '/* testSwitchDefaultNestedInMatchCase */',
207215
'openerOffset' => 1,
208216
'closerOffset' => 20,
209217
],
210-
'switch_default_nested_in_match_default' => [
218+
'switch_default_nested_in_match_default' => [
211219
'testMarker' => '/* testSwitchDefaultNestedInMatchDefault */',
212220
'openerOffset' => 1,
213221
'closerOffset' => 18,
214222
],
223+
'switch_and_default_sharing_scope_closer' => [
224+
'testMarker' => '/* testSwitchAndDefaultSharingScopeCloser */',
225+
'openerOffset' => 1,
226+
'closerOffset' => 10,
227+
'conditionStop' => null,
228+
'testContent' => 'default',
229+
'sharedScopeCloser' => true,
230+
],
231+
'switch_and_default_with_nested_if_with_and_without_braces' => [
232+
'testMarker' => '/* testSwitchDefaultNestedIfWithAndWithoutBraces */',
233+
'openerOffset' => 1,
234+
'closerOffset' => 48,
235+
],
215236
];
216237

217238
}//end dataSwitchDefault()

0 commit comments

Comments
 (0)