Skip to content

Commit a82f02e

Browse files
committed
File::find[Start|End]OfStatement(): add QA tests
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.
1 parent 83373f9 commit a82f02e

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

tests/Core/File/FindEndOfStatementTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace PHP_CodeSniffer\Tests\Core\File;
1111

1212
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
13+
use PHP_CodeSniffer\Util\Tokens;
1314

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

2223

24+
/**
25+
* Test that end of statement is NEVER before the "current" token.
26+
*
27+
* @return void
28+
*/
29+
public function testEndIsNeverLessThanCurrentToken()
30+
{
31+
$tokens = self::$phpcsFile->getTokens();
32+
$errors = [];
33+
34+
for ($i = 0; $i < self::$phpcsFile->numTokens; $i++) {
35+
if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true) {
36+
continue;
37+
}
38+
39+
$end = self::$phpcsFile->findEndOfStatement($i);
40+
41+
// Collect all the errors.
42+
if ($end < $i) {
43+
$errors[] = sprintf(
44+
'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',
45+
$i,
46+
$tokens[$i]['type'],
47+
$tokens[$i]['content'],
48+
$tokens[$i]['line'],
49+
$end,
50+
$tokens[$end]['type']
51+
);
52+
}
53+
}
54+
55+
$this->assertSame([], $errors);
56+
57+
}//end testEndIsNeverLessThanCurrentToken()
58+
59+
2360
/**
2461
* Test a simple assignment.
2562
*

tests/Core/File/FindStartOfStatementTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace PHP_CodeSniffer\Tests\Core\File;
1313

1414
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
15+
use PHP_CodeSniffer\Util\Tokens;
1516

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

2425

26+
/**
27+
* Test that start of statement is NEVER beyond the "current" token.
28+
*
29+
* @return void
30+
*/
31+
public function testStartIsNeverMoreThanCurrentToken()
32+
{
33+
$tokens = self::$phpcsFile->getTokens();
34+
$errors = [];
35+
36+
for ($i = 0; $i < self::$phpcsFile->numTokens; $i++) {
37+
if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true) {
38+
continue;
39+
}
40+
41+
$start = self::$phpcsFile->findStartOfStatement($i);
42+
43+
// Collect all the errors.
44+
if ($start > $i) {
45+
$errors[] = sprintf(
46+
'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',
47+
$i,
48+
$tokens[$i]['type'],
49+
$tokens[$i]['content'],
50+
$tokens[$i]['line'],
51+
$start,
52+
$tokens[$start]['type']
53+
);
54+
}
55+
}
56+
57+
$this->assertSame([], $errors);
58+
59+
}//end testStartIsNeverMoreThanCurrentToken()
60+
61+
2562
/**
2663
* Test a simple assignment.
2764
*

0 commit comments

Comments
 (0)