Skip to content

Commit

Permalink
Tokenizer arrow functions backfill tests: fix it ;-)
Browse files Browse the repository at this point in the history
Follow up on 2860. This fixes the tests.

#### Test case file:
* Adds tests with mixed case `fn` keyword.
* Removes a few redundant tests (weren't testing anything which wasn't covered already).

#### Test file:
* Adds a couple of tests which were missing from the data provider.
* Updates the test itself to use a `$testContent` parameter to allow for the `fn` in the tests being non-lowercase.
  • Loading branch information
jrfnl committed Feb 9, 2020
1 parent ea810a2 commit 2adcb64
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
16 changes: 5 additions & 11 deletions tests/Core/Tokenizer/BackfillFnTokenTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn(array $a) : array => $a;
$fn = fn($a) => $a ? /* testTernaryThen */ fn() : string => 'a' : /* testTernaryElse */ fn() : string => 'b';

/* testConstantDeclaration */
const fn = 'a';
const FN = 'a';

class Foo {
/* testStaticMethodName */
Expand All @@ -91,27 +91,21 @@ class Foo {

$anon = new class() {
/* testAnonClassMethodName */
protected function fn($param) {
protected function fN($param) {
}
}

/* testNonArrowStaticMethodCall */
$a = Foo::fn($param);

/* testNonArrowStaticMethodCallWithChaining */
$a = Foo::fn($param)->another();

/* testNonArrowStaticConstant */
$a = MyClass::fn;

/* testNonArrowStaticConstantDeref */
$a = MyClass::fn[$a];
/* testNonArrowConstantAccess */
$a = MyClass::FN;

/* testNonArrowObjectMethodCall */
$a = $obj->fn($param);

/* testNonArrowNamespacedFunctionCall */
$a = MyNS\Sub\fn($param);
$a = MyNS\Sub\Fn($param);

/* testNonArrowNamespaceOperatorFunctionCall */
$a = namespace\fn($param);
Expand Down
30 changes: 21 additions & 9 deletions tests/Core/Tokenizer/BackfillFnTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -562,18 +562,19 @@ public function testNestedInMethod()
* Verify that "fn" keywords which are not arrow functions get tokenized as T_STRING and don't
* have the extra token array indexes.
*
* @param string $testMarker The comment prefacing the target token.
* @param string $testMarker The comment prefacing the target token.
* @param string $testContent The token content to look for.
*
* @dataProvider dataNotAnArrowFunction
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testNotAnArrowFunction($testMarker)
public function testNotAnArrowFunction($testMarker, $testContent='fn')
{
$tokens = self::$phpcsFile->getTokens();

$token = $this->getTargetToken($testMarker, [T_STRING, T_FN], 'fn');
$token = $this->getTargetToken($testMarker, [T_STRING, T_FN], $testContent);
$tokenArray = $tokens[$token];

$this->assertSame('T_STRING', $tokenArray['type'], 'Token tokenized as '.$tokenArray['type'].', not T_STRING');
Expand All @@ -598,16 +599,27 @@ public function testNotAnArrowFunction($testMarker)
public function dataNotAnArrowFunction()
{
return [
['/* testConstantDeclaration */'],
['/* testFunctionName */'],
[
'/* testConstantDeclaration */',
'FN',
],
['/* testStaticMethodName */'],
['/* testAnonClassMethodName */'],
['/* testPropertyAssignment */'],
[
'/* testAnonClassMethodName */',
'fN',
],
['/* testNonArrowStaticMethodCall */'],
['/* testNonArrowStaticMethodCallWithChaining */'],
['/* testNonArrowStaticConstant */'],
['/* testNonArrowStaticConstantDeref */'],
[
'/* testNonArrowConstantAccess */',
'FN',
],
['/* testNonArrowObjectMethodCall */'],
['/* testNonArrowNamespacedFunctionCall */'],
[
'/* testNonArrowNamespacedFunctionCall */',
'Fn',
],
['/* testNonArrowNamespaceOperatorFunctionCall */'],
['/* testLiveCoding */'],
];
Expand Down

0 comments on commit 2adcb64

Please sign in to comment.