Skip to content

Util\Tokens: add tests #655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Util/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -777,12 +777,14 @@ public static function tokenName($token)
* For example T_CLASS tokens appear very infrequently in a file, and
* therefore have a high weighting.
*
* Returns false if there are no weightings for any of the specified tokens.
* If there are no weightings for any of the specified tokens, the first token
* seen in the passed array will be returned.
*
* @param array<int|string> $tokens The token types to get the highest weighted
* type for.
*
* @return int|false The highest weighted token.
* @return int The highest weighted token.
* On equal "weight", returns the first token of that particular weight.
*/
public static function getHighestWeightedToken(array $tokens)
{
Expand Down
162 changes: 162 additions & 0 deletions tests/Core/Util/Tokens/GetHighestWeightedTokenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php
/**
* Tests for the \PHP_CodeSniffer\Util\Tokens::getHighestWeightedToken() method.
*
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
* @copyright 2024 PHPCSStandards and contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Tests\Core\Util\Tokens;

use PHP_CodeSniffer\Util\Tokens;
use PHPUnit\Framework\TestCase;

/**
* Tests for the \PHP_CodeSniffer\Util\Tokens::getHighestWeightedToken() method.
*
* @covers \PHP_CodeSniffer\Util\Tokens::getHighestWeightedToken
*/
final class GetHighestWeightedTokenTest extends TestCase
{


/**
* Test the method.
*
* @param array<int|string> $tokens The tokens to find the heighest weighted one.
* @param int|false $expected The expected function return value.
*
* @dataProvider dataGetHighestWeightedToken
*
* @return void
*/
public function testGetHighestWeightedToken($tokens, $expected)
{
$this->assertSame($expected, Tokens::getHighestWeightedToken($tokens));

}//end testGetHighestWeightedToken()


/**
* Data provider.
*
* @return array<string, array<string, int|false|array<int|string>>
*/
public static function dataGetHighestWeightedToken()
{
$data = [
'Array of non-tokens passed, returns first' => [
'tokens' => [
PHP_SAPI,
PHP_MAJOR_VERSION,
PHP_OS,
],
'expected' => PHP_SAPI,
],
'No weightings available for any of the selected tokens, first one wins' => [
'tokens' => [
T_VARIABLE,
T_STRING,
T_EXTENDS,
T_IMPLEMENTS,
],
'expected' => T_VARIABLE,
],
'single token always returns that token' => [
'tokens' => [T_VARIABLE],
'expected' => T_VARIABLE,
],
'Unknown and known token, known token wins' => [
'tokens' => [
T_VARIABLE,
T_SELF,
],
'expected' => T_SELF,
],
'Known and unknown token, known token wins' => [
'tokens' => [
T_CLOSURE,
T_STRING,
],
'expected' => T_CLOSURE,
],
'Two tokens with equal weights passed, first one wins' => [
'tokens' => [
T_CLOSURE,
T_FUNCTION,
],
'expected' => T_CLOSURE,
],
'Five tokens with equal weights passed, first one wins' => [
'tokens' => [
T_NAMESPACE,
T_TRAIT,
T_ENUM,
T_CLASS,
T_INTERFACE,
],
'expected' => T_NAMESPACE,
],
'Tokens with different weights passed, heightest (25) wins' => [
'tokens' => [
T_BITWISE_OR,
T_SELF,
T_MUL_EQUAL,
],
'expected' => T_SELF,
],
'Tokens with different weights passed, heightest (50) wins' => [
'tokens' => [
T_BITWISE_XOR,
T_CATCH,
T_SPACESHIP,
T_PARENT,
],
'expected' => T_CATCH,
],
];

$high100 = [
T_MULTIPLY,
T_BITWISE_AND,
T_SELF,
T_FOREACH,
T_CLOSURE,
];
$data['Tokens with different weights passed, ordered low-high, heightest (100) wins'] = [
'tokens' => $high100,
'expected' => T_CLOSURE,
];

shuffle($high100);
$data['Tokens with different weights passed, order random, heightest (100) wins'] = [
'tokens' => $high100,
'expected' => T_CLOSURE,
];

$high1000 = [
T_ENUM,
T_FUNCTION,
T_ELSEIF,
T_PARENT,
T_BITWISE_OR,
T_MODULUS,
];
$data['Tokens with different weights passed, ordered low-high, heightest (1000) wins'] = [
'tokens' => $high1000,
'expected' => T_ENUM,
];

shuffle($high1000);
$data['Tokens with different weights passed, order random, heightest (1000) wins'] = [
'tokens' => $high1000,
'expected' => T_ENUM,
];

return $data;

}//end dataGetHighestWeightedToken()


}//end class
81 changes: 81 additions & 0 deletions tests/Core/Util/Tokens/TokenNameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* Tests for the \PHP_CodeSniffer\Util\Tokens::tokenName() method.
*
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
* @copyright 2024 PHPCSStandards and contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Tests\Core\Util\Tokens;

use PHP_CodeSniffer\Util\Tokens;
use PHPUnit\Framework\TestCase;

/**
* Tests for the \PHP_CodeSniffer\Util\Tokens::tokenName() method.
*
* @covers \PHP_CodeSniffer\Util\Tokens::tokenName
*/
final class TokenNameTest extends TestCase
{


/**
* Test the method.
*
* @param int|string $tokenCode The PHP/PHPCS token code to get the name for.
* @param string $expected The expected token name.
*
* @dataProvider dataTokenName
*
* @return void
*/
public function testTokenName($tokenCode, $expected)
{
$this->assertSame($expected, Tokens::tokenName($tokenCode));

}//end testTokenName()


/**
* Data provider.
*
* @return array<string, array<string, int|string>>
*/
public static function dataTokenName()
{
return [
'PHP native token: T_ECHO' => [
'tokenCode' => T_ECHO,
'expected' => 'T_ECHO',
],
'PHP native token: T_FUNCTION' => [
'tokenCode' => T_FUNCTION,
'expected' => 'T_FUNCTION',
],
'PHPCS native token: T_CLOSURE' => [
'tokenCode' => T_CLOSURE,
'expected' => 'T_CLOSURE',
],
'PHPCS native token: T_STRING_CONCAT' => [
'tokenCode' => T_STRING_CONCAT,
'expected' => 'T_STRING_CONCAT',
],

// Document the current behaviour for invalid input.
// This behaviour is subject to change.
'Non-token integer passed' => [
'tokenCode' => 100000,
'expected' => 'UNKNOWN',
],
'Non-token string passed' => [
'tokenCode' => 'something',
'expected' => 'ing',
],
];

}//end dataTokenName()


}//end class