Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Jan 5, 2024
1 parent 59805a3 commit e0350ae
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 12 deletions.
15 changes: 10 additions & 5 deletions src/Report/ViolationId.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,15 @@ public function toString(): string

public function match(self $violationId): bool
{
return $this->ruleShortName === $violationId->ruleShortName
&& (null === $this->identifier || $this->identifier === $violationId->identifier)
&& (null === $this->tokenName || $this->tokenName === $violationId->tokenName)
&& (null === $this->line || $this->line === $violationId->line)
&& (null === $this->linePosition || $this->linePosition === $violationId->linePosition);
return $this->matchValue($this->ruleShortName, $violationId->ruleShortName)
&& $this->matchValue($this->identifier, $violationId->identifier)
&& $this->matchValue($this->tokenName, $violationId->tokenName)
&& $this->matchValue($this->line, $violationId->line)
&& $this->matchValue($this->linePosition, $violationId->linePosition);
}

private function matchValue(string|int|null $self, string|int|null $other): bool
{
return null === $self || strtolower((string) $self) === strtolower((string) $other);
}
}
14 changes: 7 additions & 7 deletions src/Token/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function tokenize(Source $source): array

$this->pushToken(Token::EOF_TYPE);

return [$this->tokens, []];
return [$this->tokens, $this->ignoredViolations];
}

private function resetState(Source $source): void
Expand Down Expand Up @@ -689,12 +689,12 @@ private function getOperatorRegex(Environment $env): string
private function extractIgnoredViolations(string $comment): void
{
$comment = trim($comment);
if (1 === preg_match('/^twig-cs-fixer-disable(|-line|-next-line) ([\s\w,.:]+)/', $comment, $match)) {
if (1 === preg_match('/^twig-cs-fixer-disable(|-line|-next-line) ([\s\w,.:]+)/i', $comment, $match)) {
$this->setStateParam('ignoredViolations', preg_replace('/\s+/', ',', $match[2]) ?? '');
$this->setStateParam('ignoreType', trim('-', $match[1]));
$this->setStateParam('ignoredType', trim($match[1], '-'));
} else {
$this->setStateParam('ignoredViolations', '');
}

$this->setStateParam('ignoredViolations', '');
}

private function processIgnoredViolations(): void
Expand All @@ -704,8 +704,8 @@ private function processIgnoredViolations(): void
return;
}

$line = match ($this->getStateParam('ignoreType')) {
'line' => $this->getStateParam('startLine'),
$line = match ($this->getStateParam('ignoredType')) {
'line' => (int) $this->getStateParam('startLine'),
'next-line' => $this->line + 1,
default => null,
};
Expand Down
7 changes: 7 additions & 0 deletions tests/Rules/Fixtures/FakeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@

use TwigCsFixer\Rules\AbstractRule;

/**
* This rule reports an error for the first token of every line.
*/
class FakeRule extends AbstractRule
{
public function process(int $tokenPosition, array $tokens): void
{
$token = $tokens[$tokenPosition];
if (1 === $token->getPosition()) {
$this->addError('First token of the line', $token);
}
}
}
Empty file.
2 changes: 2 additions & 0 deletions tests/Rules/Fixtures/disable1.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{# twig-cs-fixer-disable Fake.Error #}
This comment disable for the whole file.
11 changes: 11 additions & 0 deletions tests/Rules/Fixtures/disable2.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{# twig-cs-fixer-disable Fake.Error:1 #} This comment disable for the first line.
{# twig-cs-fixer-disable Fake.Error:2:1 #} This comment disable for the first token of the line.
{# twig-cs-fixer-disable Fake.Error:3:2 #} This comment disable for the second token of the line. => ERROR ON THIS LINE
{# twig-cs-fixer-disable-line Fake.Error #} This comment disable for the line.
{#
twig-cs-fixer-disable-line Fake.Error #} This comment disable for the line with "{ #" => ERROR ON THIS LINE
{# twig-cs-fixer-disable-line Fake.Error::1 #} This comment disable for the first token of the line.
{# twig-cs-fixer-disable-line Fake.Error::2 #} This comment disable for the second token of the line. => ERROR ON THIS LINE
{# twig-cs-fixer-disable-next-line Fake.Error #} This comment disable for the first token of the next-line. => ERROR ON THIS LINE
{# twig-cs-fixer-disable-next-line Fake.Error
#} This comment disable for the next line. => ERROR ON THIS LINE
51 changes: 51 additions & 0 deletions tests/Rules/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@

namespace TwigCsFixer\Tests\Rules;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use SplFileInfo;
use TwigCsFixer\Environment\StubbedEnvironment;
use TwigCsFixer\Report\Report;
use TwigCsFixer\Report\Violation;
use TwigCsFixer\Rules\AbstractRule;
use TwigCsFixer\Ruleset\Ruleset;
use TwigCsFixer\Runner\Fixer;
use TwigCsFixer\Runner\Linter;
use TwigCsFixer\Tests\Rules\Fixtures\FakeRule;
use TwigCsFixer\Tests\TestHelper;
use TwigCsFixer\Token\Token;
use TwigCsFixer\Token\Tokenizer;

final class RuleTest extends TestCase
{
Expand Down Expand Up @@ -106,4 +114,47 @@ protected function process(int $tokenPosition, array $tokens): void
static::assertSame(0, $report->getTotalWarnings());
static::assertSame(2, $report->getTotalErrors());
}

/**
* @param array<int> $expectedLines
*/
#[DataProvider('ignoredViolationsDataProvider')]
public function testIgnoredViolations(string $filePath, array $expectedLines): void
{
$env = new StubbedEnvironment();
$tokenizer = new Tokenizer($env);
$linter = new Linter($env, $tokenizer);
$ruleset = new Ruleset();

$ruleset->addRule(new FakeRule());
$report = $linter->run([new SplFileInfo($filePath)], $ruleset);
$messages = $report->getFileViolations($filePath);

static::assertSame(
$expectedLines,
array_map(
static fn (Violation $violation) => $violation->getLine(),
$messages,
),
);
}

/**
* @return iterable<array{string, array<int>}>
*/
public static function ignoredViolationsDataProvider(): iterable
{
yield [
__DIR__.'/Fixtures/disable0.twig',
[1],
];
yield [
__DIR__.'/Fixtures/disable1.twig',
[],
];
yield [
__DIR__.'/Fixtures/disable2.twig',
[3, 6, 8, 9, 11],
];
}
}
4 changes: 4 additions & 0 deletions tests/Token/Tokenizer/Fixtures/ignored_violations.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{# twig-cs-fixer-disable Foo.Bar.Baz #}
{# Twig-CS-Fixer-disable Foo.Bar.BazInsensitive #}
{# twig-cs-fixer-disable-line Foo.Bar #}
{# twig-cs-fixer-disable-next-line Foo.Bar Bar.Foo #}
26 changes: 26 additions & 0 deletions tests/Token/Tokenizer/TokenizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Twig\Source;
use TwigCsFixer\Environment\StubbedEnvironment;
use TwigCsFixer\Exception\CannotTokenizeException;
use TwigCsFixer\Report\ViolationId;
use TwigCsFixer\Tests\TestHelper;
use TwigCsFixer\Tests\Token\Tokenizer\Fixtures\CustomTwigExtension;
use TwigCsFixer\Token\Token;
Expand Down Expand Up @@ -71,6 +72,31 @@ public function testTokenizeWithCustomOperators(): void
);
}

public function testTokenizeIgnoredViolations(): void
{
$filePath = __DIR__.'/Fixtures/ignored_violations.twig';
$content = file_get_contents($filePath);
static::assertNotFalse($content);

$env = new StubbedEnvironment([new CustomTwigExtension()]);
$tokenizer = new Tokenizer($env);
$source = new Source($content, $filePath);

static::assertEquals(
[
'Foo.Bar.Baz',
'Foo.Bar.BazInsensitive',
'Foo.Bar:3',
'Foo.Bar:5',
'Bar.Foo:5',
],
array_map(
static fn (ViolationId $validationId) => $validationId->toString(),
$tokenizer->tokenize($source)[1]
)
);
}

/**
* @param array<int, int|string> $expectedTokenTypes
*
Expand Down

0 comments on commit e0350ae

Please sign in to comment.