php-kilo/tests/FunctionTest.php

49 lines
1.2 KiB
PHP
Raw Normal View History

2019-11-08 16:27:08 -05:00
<?php declare(strict_types=1);
namespace Aviat\Kilo\Tests;
2019-11-14 11:12:32 -05:00
use function Aviat\Kilo\get_php_tokens;
2019-11-08 16:27:08 -05:00
use PHPUnit\Framework\TestCase;
class FunctionTest extends TestCase {
2019-11-14 11:12:32 -05:00
public function testGetPhpTokensLineAlignment(): void
{
$file = file_get_contents(realpath(__DIR__ . '/../test.php'));
$tokens = get_php_tokens($file);
2019-11-08 16:27:08 -05:00
2019-11-14 11:12:32 -05:00
$this->assertNotEmpty($file);
$lines = explode("\n", $file);
array_unshift($lines, '');
$misplacedTokens = [];
foreach ($tokens as $index => $lineTokens)
{
if (empty($lineTokens))
{
$this->assertNotEmpty(trim($lines[$index]), 'Token is empty for non-empty line');
}
foreach ($lineTokens as $token)
{
// don't compare whitespace-only tokens
if (empty(trim($token['char'])))
{
continue;
}
$this->assertIsArray($token, 'All outputted tokens should be arrays');
// Make sure the matched string for the token is on the correct line
if (strpos($lines[$index], trim($token['char'])) === FALSE)
{
$token['misplaced_line'] = $index;
$misplacedTokens[] = $token;
}
}
}
$this->assertEmpty($misplacedTokens, 'Not all tokens are on the correct lines: ' . print_r($misplacedTokens, TRUE));
}
2019-11-08 16:27:08 -05:00
}