Skip to content

Commit

Permalink
File::find[Start|End]OfStatement(): add QA tests
Browse files Browse the repository at this point in the history
This commit adds two QA tests to find potential bugs in the `File::find[Start|End]OfStatement()` methods.

1. It adds a test to ensure that the return value of `File::findStartOfStatement()` is never _after_ the passed `$start` stack pointer.
2. It adds a test to ensure that the return value of `File::findEndOfStatement()` is never _before_ the passed `$start` stack pointer.

The tests use the existing test code, but tests all non-empty tokens within the file.

Note: this test doesn't test that the stack pointer returned is _correct_, only that it _could_ be correct.
  • Loading branch information
jrfnl committed May 21, 2024
1 parent 83373f9 commit a82f02e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
37 changes: 37 additions & 0 deletions tests/Core/File/FindEndOfStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace PHP_CodeSniffer\Tests\Core\File;

use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
use PHP_CodeSniffer\Util\Tokens;

/**
* Tests for the \PHP_CodeSniffer\Files\File::findEndOfStatement method.
Expand All @@ -20,6 +21,42 @@ final class FindEndOfStatementTest extends AbstractMethodUnitTest
{


/**
* 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 = self::$phpcsFile->findEndOfStatement($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);

}//end testEndIsNeverLessThanCurrentToken()


/**
* Test a simple assignment.
*
Expand Down
37 changes: 37 additions & 0 deletions tests/Core/File/FindStartOfStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace PHP_CodeSniffer\Tests\Core\File;

use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
use PHP_CodeSniffer\Util\Tokens;

/**
* Tests for the \PHP_CodeSniffer\Files\File:findStartOfStatement method.
Expand All @@ -22,6 +23,42 @@ final class FindStartOfStatementTest extends AbstractMethodUnitTest
{


/**
* 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 = self::$phpcsFile->findStartOfStatement($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);

}//end testStartIsNeverMoreThanCurrentToken()


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

0 comments on commit a82f02e

Please sign in to comment.