Skip to content

Commit

Permalink
FindEndOfStatementTest: simplify the tests
Browse files Browse the repository at this point in the history
* Use the `AbstractMethodUnitTest::getTargetToken()` method instead of doing a `findNext()` with a calculation.
    This makes the `$start` token retrieval more descriptive.
* Assert based on the token pointers, not the sub-array value.
    As for both the "expected" as well as "found" values, the token sub-array is retrieved from the same `$tokens` variable, if the token pointers are the same, the sub-arrays will be too, so doing the comparison based on the sub-array doesn't add any extra value to the test.
    Also: doing an array assertion is more expensive than doing a simple integer based assertion.
* Remove redundant calls to the `getTokens()` method.
  • Loading branch information
jrfnl committed Mar 17, 2020
1 parent caddfde commit 82b8c5d
Showing 1 changed file with 31 additions and 48 deletions.
79 changes: 31 additions & 48 deletions tests/Core/File/FindEndOfStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ class FindEndOfStatementTest extends AbstractMethodUnitTest
*/
public function testSimpleAssignment()
{
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testSimpleAssignment */') + 2);
$start = $this->getTargetToken('/* testSimpleAssignment */', T_VARIABLE);
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[($start + 5)], $tokens[$found]);
$this->assertSame(($start + 5), $found);

}//end testSimpleAssignment()

Expand All @@ -38,11 +37,10 @@ public function testSimpleAssignment()
*/
public function testControlStructure()
{
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testControlStructure */') + 2);
$start = $this->getTargetToken('/* testControlStructure */', T_WHILE);
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[($start + 6)], $tokens[$found]);
$this->assertSame(($start + 6), $found);

}//end testControlStructure()

Expand All @@ -54,11 +52,10 @@ public function testControlStructure()
*/
public function testClosureAssignment()
{
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testClosureAssignment */') + 2);
$start = $this->getTargetToken('/* testClosureAssignment */', T_VARIABLE, '$a');
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[($start + 13)], $tokens[$found]);
$this->assertSame(($start + 13), $found);

}//end testClosureAssignment()

Expand All @@ -71,25 +68,22 @@ public function testClosureAssignment()
public function testHeredocFunctionArg()
{
// Find the end of the function.
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testHeredocFunctionArg */') + 2);
$start = $this->getTargetToken('/* testHeredocFunctionArg */', T_STRING, 'myFunction');
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[($start + 10)], $tokens[$found]);
$this->assertSame(($start + 10), $found);

// Find the end of the heredoc.
$start += 2;
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[($start + 4)], $tokens[$found]);
$this->assertSame(($start + 4), $found);

// Find the end of the last arg.
$start = ($found + 2);
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[$start], $tokens[$found]);
$this->assertSame($start, $found);

}//end testHeredocFunctionArg()

Expand All @@ -102,25 +96,22 @@ public function testHeredocFunctionArg()
public function testSwitch()
{
// Find the end of the switch.
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testSwitch */') + 2);
$start = $this->getTargetToken('/* testSwitch */', T_SWITCH);
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[($start + 28)], $tokens[$found]);
$this->assertSame(($start + 28), $found);

// Find the end of the case.
$start += 9;
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[($start + 8)], $tokens[$found]);
$this->assertSame(($start + 8), $found);

// Find the end of default case.
$start += 11;
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[($start + 6)], $tokens[$found]);
$this->assertSame(($start + 6), $found);

}//end testSwitch()

Expand All @@ -133,25 +124,22 @@ public function testSwitch()
public function testStatementAsArrayValue()
{
// Test short array syntax.
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testStatementAsArrayValue */') + 7);
$start = $this->getTargetToken('/* testStatementAsArrayValue */', T_NEW);
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[($start + 2)], $tokens[$found]);
$this->assertSame(($start + 2), $found);

// Test long array syntax.
$start += 12;
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[($start + 2)], $tokens[$found]);
$this->assertSame(($start + 2), $found);

// Test same statement outside of array.
$start += 10;
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[($start + 3)], $tokens[$found]);
$this->assertSame(($start + 3), $found);

}//end testStatementAsArrayValue()

Expand All @@ -163,27 +151,25 @@ public function testStatementAsArrayValue()
*/
public function testUseGroup()
{
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testUseGroup */') + 2);
$start = $this->getTargetToken('/* testUseGroup */', T_USE);
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[($start + 23)], $tokens[$found]);
$this->assertSame(($start + 23), $found);

}//end testUseGroup()


/**
* Test a use group.
* Test arrow function as array value.
*
* @return void
*/
public function testArrowFunctionArrayValue()
{
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testArrowFunctionArrayValue */') + 7);
$start = $this->getTargetToken('/* testArrowFunctionArrayValue */', T_FN);
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[($start + 9)], $tokens[$found]);
$this->assertSame(($start + 9), $found);

}//end testArrowFunctionArrayValue()

Expand All @@ -195,8 +181,8 @@ public function testArrowFunctionArrayValue()
*/
public function testStaticArrowFunction()
{
$static = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testStaticArrowFunction */') + 2);
$fn = self::$phpcsFile->findNext(T_FN, ($static + 1));
$static = $this->getTargetToken('/* testStaticArrowFunction */', T_STATIC);
$fn = $this->getTargetToken('/* testStaticArrowFunction */', T_FN);

$endOfStatementStatic = self::$phpcsFile->findEndOfStatement($static);
$endOfStatementFn = self::$phpcsFile->findEndOfStatement($fn);
Expand All @@ -213,11 +199,10 @@ public function testStaticArrowFunction()
*/
public function testArrowFunctionReturnValue()
{
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testArrowFunctionReturnValue */') + 2);
$start = $this->getTargetToken('/* testArrowFunctionReturnValue */', T_FN);
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[($start + 18)], $tokens[$found]);
$this->assertSame(($start + 18), $found);

}//end testArrowFunctionReturnValue()

Expand All @@ -229,11 +214,10 @@ public function testArrowFunctionReturnValue()
*/
public function testArrowFunctionAsArgument()
{
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testArrowFunctionAsArgument */') + 10);
$start = $this->getTargetToken('/* testArrowFunctionAsArgument */', T_FN);
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[($start + 8)], $tokens[$found]);
$this->assertSame(($start + 8), $found);

}//end testArrowFunctionAsArgument()

Expand All @@ -245,11 +229,10 @@ public function testArrowFunctionAsArgument()
*/
public function testArrowFunctionWithArrayAsArgument()
{
$start = (self::$phpcsFile->findNext(T_COMMENT, 0, null, false, '/* testArrowFunctionWithArrayAsArgument */') + 10);
$start = $this->getTargetToken('/* testArrowFunctionWithArrayAsArgument */', T_FN);
$found = self::$phpcsFile->findEndOfStatement($start);

$tokens = self::$phpcsFile->getTokens();
$this->assertSame($tokens[($start + 17)], $tokens[$found]);
$this->assertSame(($start + 17), $found);

}//end testArrowFunctionWithArrayAsArgument()

Expand Down

0 comments on commit 82b8c5d

Please sign in to comment.