Skip to content

Commit

Permalink
Tests/BCFile::find[Start|End]OfStatement(): sync in new tests from up…
Browse files Browse the repository at this point in the history
…stream [5]

See PR PHPCSStandards/PHP_CodeSniffer 509 / PHPCSStandards/PHP_CodeSniffer@a82f02e
  • Loading branch information
jrfnl committed Oct 7, 2024
1 parent b72362d commit 0644a1d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Tests/BackCompat/BCFile/FindEndOfStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

namespace PHPCSUtils\Tests\BackCompat\BCFile;

use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\BackCompat\BCFile;
use PHPCSUtils\TestUtils\UtilityMethodTestCase;

Expand All @@ -35,6 +36,40 @@
final class FindEndOfStatementTest extends UtilityMethodTestCase
{

/**
* Test that end of statement is NEVER before the "current" token.
*
* @return void
*/
public function testEndIsNeverLessThanCurrentToken()
{
$tokens = self::$phpcsFile->getTokens();
$errors = [];

for ($i = 0; $i < self::$phpcsFile->numTokens; $i++) {
if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true) {
continue;
}

$end = BCFile::findEndOfStatement(self::$phpcsFile, $i);

// Collect all the errors.
if ($end < $i) {
$errors[] = sprintf(
'End of statement for token %1$d (%2$s: %3$s) on line %4$d is %5$d (%6$s), which is less than %1$d',
$i,
$tokens[$i]['type'],
$tokens[$i]['content'],
$tokens[$i]['line'],
$end,
$tokens[$end]['type']
);
}
}

$this->assertSame([], $errors);
}

/**
* Test a simple assignment.
*
Expand Down
35 changes: 35 additions & 0 deletions Tests/BackCompat/BCFile/FindStartOfStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace PHPCSUtils\Tests\BackCompat\BCFile;

use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\BackCompat\BCFile;
use PHPCSUtils\TestUtils\UtilityMethodTestCase;

Expand All @@ -23,6 +24,40 @@
final class FindStartOfStatementTest extends UtilityMethodTestCase
{

/**
* Test that start of statement is NEVER beyond the "current" token.
*
* @return void
*/
public function testStartIsNeverMoreThanCurrentToken()
{
$tokens = self::$phpcsFile->getTokens();
$errors = [];

for ($i = 0; $i < self::$phpcsFile->numTokens; $i++) {
if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true) {
continue;
}

$start = BCFile::findStartOfStatement(self::$phpcsFile, $i);

// Collect all the errors.
if ($start > $i) {
$errors[] = sprintf(
'Start of statement for token %1$d (%2$s: %3$s) on line %4$d is %5$d (%6$s), which is more than %1$d',
$i,
$tokens[$i]['type'],
$tokens[$i]['content'],
$tokens[$i]['line'],
$start,
$tokens[$start]['type']
);
}
}

$this->assertSame([], $errors);
}

/**
* Test a simple assignment.
*
Expand Down

0 comments on commit 0644a1d

Please sign in to comment.