Skip to content

Commit 0644a1d

Browse files
committed
Tests/BCFile::find[Start|End]OfStatement(): sync in new tests from upstream [5]
See PR PHPCSStandards/PHP_CodeSniffer 509 / PHPCSStandards/PHP_CodeSniffer@a82f02e
1 parent b72362d commit 0644a1d

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

Tests/BackCompat/BCFile/FindEndOfStatementTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
namespace PHPCSUtils\Tests\BackCompat\BCFile;
2424

25+
use PHP_CodeSniffer\Util\Tokens;
2526
use PHPCSUtils\BackCompat\BCFile;
2627
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
2728

@@ -35,6 +36,40 @@
3536
final class FindEndOfStatementTest extends UtilityMethodTestCase
3637
{
3738

39+
/**
40+
* Test that end of statement is NEVER before the "current" token.
41+
*
42+
* @return void
43+
*/
44+
public function testEndIsNeverLessThanCurrentToken()
45+
{
46+
$tokens = self::$phpcsFile->getTokens();
47+
$errors = [];
48+
49+
for ($i = 0; $i < self::$phpcsFile->numTokens; $i++) {
50+
if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true) {
51+
continue;
52+
}
53+
54+
$end = BCFile::findEndOfStatement(self::$phpcsFile, $i);
55+
56+
// Collect all the errors.
57+
if ($end < $i) {
58+
$errors[] = sprintf(
59+
'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',
60+
$i,
61+
$tokens[$i]['type'],
62+
$tokens[$i]['content'],
63+
$tokens[$i]['line'],
64+
$end,
65+
$tokens[$end]['type']
66+
);
67+
}
68+
}
69+
70+
$this->assertSame([], $errors);
71+
}
72+
3873
/**
3974
* Test a simple assignment.
4075
*

Tests/BackCompat/BCFile/FindStartOfStatementTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace PHPCSUtils\Tests\BackCompat\BCFile;
1212

13+
use PHP_CodeSniffer\Util\Tokens;
1314
use PHPCSUtils\BackCompat\BCFile;
1415
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
1516

@@ -23,6 +24,40 @@
2324
final class FindStartOfStatementTest extends UtilityMethodTestCase
2425
{
2526

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

0 commit comments

Comments
 (0)