Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Nov 29, 2024
1 parent c552527 commit bc38e06
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Rules/Function/NamedArgumentNameRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected function process(int $tokenIndex, Tokens $tokens): void
$nameTokenIndex = $tokens->findPrevious(Token::NAME_TYPE, $tokenIndex);
Assert::notFalse($nameTokenIndex, 'A NAMED_ARGUMENT_SEPARATOR_TYPE always follow a name');

$name = $token->getValue();
$name = $tokens->get($nameTokenIndex)->getValue();
$expected = match ($this->case) {
self::SNAKE_CASE => StringUtil::toSnakeCase($name),
self::CAMEL_CASE => StringUtil::toCamelCase($name),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace TwigCsFixer\Tests\Rules\Function\NamedArgumentName;

use TwigCsFixer\Rules\Function\NamedArgumentNameRule;
use TwigCsFixer\Test\AbstractRuleTestCase;

final class NamedArgumentNameRuleTest extends AbstractRuleTestCase
{
public function testConfiguration(): void
{
static::assertSame(
[
'case' => NamedArgumentNameRule::SNAKE_CASE,
],
(new NamedArgumentNameRule())->getConfiguration()
);
static::assertSame(
[
'case' => NamedArgumentNameRule::CAMEL_CASE,
],
(new NamedArgumentNameRule(NamedArgumentNameRule::CAMEL_CASE))->getConfiguration()
);
}

public function testRule(): void
{
$this->checkRule(new NamedArgumentNameRule(), [
'NamedArgumentName.Error:1:28' => 'The named argument must use snake_case; expected baz_baz.',
]);
}

public function testRulePascalCase(): void
{
$this->checkRule(new NamedArgumentNameRule(NamedArgumentNameRule::PASCAL_CASE), [
'NamedArgumentName.Error:1:15' => 'The named argument must use PascalCase; expected BarBar.',
'NamedArgumentName.Error:1:28' => 'The named argument must use PascalCase; expected BazBaz.',
]);
}

public function testRuleCamelCase(): void
{
$this->checkRule(new NamedArgumentNameRule(NamedArgumentNameRule::CAMEL_CASE), [
'NamedArgumentName.Error:1:15' => 'The named argument must use camelCase; expected barBar.',
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{ foo(bar_bar=true, bazBaz: 42) }}
{% macro foo(bar = true) %}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace TwigCsFixer\Tests\Rules\Function;
namespace TwigCsFixer\Tests\Rules\Function\NamedArgumentSeparator;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
Expand Down

0 comments on commit bc38e06

Please sign in to comment.