Skip to content

Tests/Tokenizer: improve switch keyword tests #812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ switch ($value) {
default :
echo 'other';
break;
/* testSwitchNormalSyntaxScopeCloser */
}

// Test for https://github.com/squizlabs/PHP_CodeSniffer/issues/497
/* testSwitchAlternativeSyntax */
switch ($value):
switch ($value) :
/* testSwitchAlternativeSyntaxEnsureTestWillNotPickUpWrongColon */
case 1:
echo 'one';
break;
default:
echo 'other';
break;
/* testSwitchAlternativeSyntaxEnd */
/* testSwitchAlternativeSyntaxScopeCloser */
endswitch;

// Test for https://github.com/squizlabs/PHP_CodeSniffer/issues/543
Expand All @@ -30,4 +31,30 @@ switch((function () {
})()) /* testSwitchClosureWithinConditionScopeOpener */ {
case 1:
return 'test';
/* testSwitchClosureWithinConditionScopeCloser */
}

/* testSwitchClosureWithReturnTypeWithinCondition */
switch((function (): string {
return 'bar';
})()) /* testSwitchClosureWithReturnTypeWithinConditionScopeOpener */ :
/* testSwitchClosureWithReturnTypeWithinConditionEnsureTestWillNotPickUpWrongColon */
case 1:
return 'test';
/* testSwitchClosureWithReturnTypeWithinConditionScopeCloser */
endswitch;

/* testSwitchArrowFunctionWithinCondition */
switch((fn() => $obj->{$foo . $bar})()) /* testSwitchArrowFunctionWithinConditionScopeOpener */ {
case 1:
return 'test';
/* testSwitchArrowFunctionWithinConditionScopeCloser */
}

/* testSwitchArrowFunctionWithReturnTypeWithinCondition */
switch((fn(): string => $condition ? 'foo' : 'bar')()) /* testSwitchArrowFunctionWithReturnTypeWithinConditionScopeOpener */ :
/* testSwitchArrowFunctionWithReturnTypeWithinConditionEnsureTestWillNotPickUpWrongColon */
case 1:
return 'test';
/* testSwitchArrowFunctionWithReturnTypeWithinConditionScopeCloser */
endswitch;
Original file line number Diff line number Diff line change
Expand Up @@ -97,31 +97,61 @@ public function testSwitchScope($testMarker, $expectedTokens, $testOpenerMarker=
public static function dataSwitchScope()
{
return [
'switch normal syntax' => [
'testMarker' => '/* testSwitchNormalSyntax */',
'expectedTokens' => [
'switch normal syntax' => [
'testMarker' => '/* testSwitchNormalSyntax */',
'expectedTokens' => [
'scope_opener' => T_OPEN_CURLY_BRACKET,
'scope_closer' => T_CLOSE_CURLY_BRACKET,
],
'testOpenerMarker' => null,
'testCloserMarker' => '/* testSwitchNormalSyntaxScopeCloser */',
],
'switch alternative syntax' => [
'switch alternative syntax' => [
'testMarker' => '/* testSwitchAlternativeSyntax */',
'expectedTokens' => [
'scope_opener' => T_COLON,
'scope_closer' => T_ENDSWITCH,
],
'testOpenerMarker' => null,
'testCloserMarker' => '/* testSwitchAlternativeSyntaxEnd */',
'testCloserMarker' => '/* testSwitchAlternativeSyntaxScopeCloser */',
],
'switch with closure in the condition' => [
'switch with closure in the condition' => [
'testMarker' => '/* testSwitchClosureWithinCondition */',
'expectedTokens' => [
'scope_opener' => T_OPEN_CURLY_BRACKET,
'scope_closer' => T_CLOSE_CURLY_BRACKET,
],
'testOpenerMarker' => '/* testSwitchClosureWithinConditionScopeOpener */',
'testCloserMarker' => '/* testSwitchClosureWithinConditionScopeOpener */',
'testCloserMarker' => '/* testSwitchClosureWithinConditionScopeCloser */',
],
'switch alternative syntax with closure containing return type in the condition' => [
'testMarker' => '/* testSwitchClosureWithReturnTypeWithinCondition */',
'expectedTokens' => [
'scope_opener' => T_COLON,
'scope_closer' => T_ENDSWITCH,
],
'testOpenerMarker' => '/* testSwitchClosureWithReturnTypeWithinConditionScopeOpener */',
'testCloserMarker' => '/* testSwitchClosureWithReturnTypeWithinConditionScopeCloser */',
],
'switch with arrow function in the condition' => [
'testMarker' => '/* testSwitchArrowFunctionWithinCondition */',
'expectedTokens' => [
'scope_opener' => T_OPEN_CURLY_BRACKET,
'scope_closer' => T_CLOSE_CURLY_BRACKET,
],
'testOpenerMarker' => '/* testSwitchArrowFunctionWithinConditionScopeOpener */',
'testCloserMarker' => '/* testSwitchArrowFunctionWithinConditionScopeCloser */',
],
'switch alternative syntax with arrow function containing return type in the condition' => [
'testMarker' => '/* testSwitchArrowFunctionWithReturnTypeWithinCondition */',
'expectedTokens' => [
'scope_opener' => T_COLON,
'scope_closer' => T_ENDSWITCH,
],
'testOpenerMarker' => '/* testSwitchArrowFunctionWithReturnTypeWithinConditionScopeOpener */',
'testCloserMarker' => '/* testSwitchArrowFunctionWithReturnTypeWithinConditionScopeCloser */',
],

];

}//end dataSwitchScope()
Expand Down