Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Jan 10, 2024
1 parent 0043e2c commit f96490d
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 95 deletions.
10 changes: 5 additions & 5 deletions tests/Rules/AbstractRuleTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
abstract class AbstractRuleTestCase extends TestCase
{
/**
* @param array<array<int, int>> $expects
* @param array<string> $expects
*/
protected function checkRule(
RuleInterface $rule,
Expand Down Expand Up @@ -51,8 +51,8 @@ protected function checkRule(

$messages = $report->getFileViolations($filePath);

/** @var array<array<int, int>> $messagePositions */
$messagePositions = [];
/** @var array<string> $messageIds */
$messageIds = [];
foreach ($messages as $message) {
if (Violation::LEVEL_FATAL === $message->getLevel()) {
$errorMessage = $message->getMessage();
Expand All @@ -64,10 +64,10 @@ protected function checkRule(
static::fail($errorMessage);
}

$messagePositions[] = [$message->getLine() ?? 0 => $message->getLinePosition()];
$messageIds[] = $message->getIdentifier()->toString();

Check failure on line 67 in tests/Rules/AbstractRuleTestCase.php

View workflow job for this annotation

GitHub Actions / PHPStan

Cannot call method toString() on TwigCsFixer\Report\ViolationId|null.

Check failure on line 67 in tests/Rules/AbstractRuleTestCase.php

View workflow job for this annotation

GitHub Actions / Psalm

PossiblyNullReference

tests/Rules/AbstractRuleTestCase.php:67:56: PossiblyNullReference: Cannot call method toString on possibly null value (see https://psalm.dev/083)
}

static::assertSame($expects, $messagePositions);
static::assertSame($expects, $messageIds);
}

private function generateFilePath(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ final class BlockNameSpacingTest extends AbstractRuleTestCase
public function testRule(): void
{
$this->checkRule(new BlockNameSpacingRule(), [
[1 => 5],
[1 => 5],
[3 => 3],
[3 => 3],
'BlockNameSpacing.After.BlockName:1:5',
'BlockNameSpacing.Before.BlockName:1:5',
'BlockNameSpacing.After.BlockName:3:3',
'BlockNameSpacing.Before.BlockName:3:3',
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ final class DelimiterSpacingTest extends AbstractRuleTestCase
public function testRule(): void
{
$this->checkRule(new DelimiterSpacingRule(), [
[15 => 1],
[15 => 12],
[15 => 15],
[15 => 25],
'DelimiterSpacing.After.BlockStart:15:1',
'DelimiterSpacing.Before.BlockEnd:15:12',
'DelimiterSpacing.After.BlockStart:15:15',
'DelimiterSpacing.Before.BlockEnd:15:25',
]);
}
}
3 changes: 2 additions & 1 deletion tests/Rules/Fixtures/disable2.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
{# 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 Foo.Error, 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-line #} This comment disable for the line for every rule
{# this comment use twig-cs-fixer-disable but not at the start of the comment #} => ERROR ON THIS LINE
{# this comment use twig-cs-fixer-disable Fake.Error #} But not at the start of the comment => 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
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ final class OperatorNameSpacingTest extends AbstractRuleTestCase
public function testRule(): void
{
$this->checkRule(new OperatorNameSpacingRule(), [
[2 => 13],
[3 => 13],
[4 => 10],
'OperatorNameSpacing.Error.Operator:2:13',
'OperatorNameSpacing.Error.Operator:3:13',
'OperatorNameSpacing.Error.Operator:4:10',
]);
}
}
88 changes: 44 additions & 44 deletions tests/Rules/Operator/OperatorSpacing/OperatorSpacingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,50 @@ final class OperatorSpacingTest extends AbstractRuleTestCase
public function testRule(): void
{
$this->checkRule(new OperatorSpacingRule(), [
[1 => 5],
[1 => 5],
[2 => 5],
[2 => 5],
[3 => 5],
[3 => 5],
[4 => 5],
[4 => 5],
[5 => 5],
[5 => 5],
[6 => 5],
[6 => 5],
[7 => 5],
[7 => 5],
[8 => 7],
[8 => 7],
[9 => 10],
[9 => 10],
[9 => 19],
[9 => 19],
[10 => 5],
[10 => 5],
[11 => 4],
[12 => 11],
[12 => 11],
[13 => 11],
[13 => 11],
[14 => 7],
[14 => 7],
[15 => 7],
[15 => 7],
[19 => 5],
[19 => 5],
[20 => 5],
[20 => 5],
[22 => 6],
[33 => 10],
[33 => 10],
[35 => 13],
[35 => 13],
[36 => 13],
[36 => 13],
[37 => 13],
[37 => 13],
'OperatorSpacing.After.Operator:1:5',
'OperatorSpacing.Before.Operator:1:5',
'OperatorSpacing.After.Operator:2:5',
'OperatorSpacing.Before.Operator:2:5',
'OperatorSpacing.After.Operator:3:5',
'OperatorSpacing.Before.Operator:3:5',
'OperatorSpacing.After.Operator:4:5',
'OperatorSpacing.Before.Operator:4:5',
'OperatorSpacing.After.Operator:5:5',
'OperatorSpacing.Before.Operator:5:5',
'OperatorSpacing.After.Operator:6:5',
'OperatorSpacing.Before.Operator:6:5',
'OperatorSpacing.After.Operator:7:5',
'OperatorSpacing.Before.Operator:7:5',
'OperatorSpacing.After.Operator:8:7',
'OperatorSpacing.Before.Operator:8:7',
'OperatorSpacing.After.Operator:9:10',
'OperatorSpacing.Before.Operator:9:10',
'OperatorSpacing.After.Operator:9:19',
'OperatorSpacing.Before.Operator:9:19',
'OperatorSpacing.After.Operator:10:5',
'OperatorSpacing.Before.Operator:10:5',
'OperatorSpacing.After.Operator:11:4',
'OperatorSpacing.After.Operator:12:11',
'OperatorSpacing.Before.Operator:12:11',
'OperatorSpacing.After.Operator:13:11',
'OperatorSpacing.Before.Operator:13:11',
'OperatorSpacing.After.Operator:14:7',
'OperatorSpacing.Before.Operator:14:7',
'OperatorSpacing.After.Operator:15:7',
'OperatorSpacing.Before.Operator:15:7',
'OperatorSpacing.After.Operator:19:5',
'OperatorSpacing.Before.Operator:19:5',
'OperatorSpacing.After.Operator:20:5',
'OperatorSpacing.Before.Operator:20:5',
'OperatorSpacing.After.Operator:22:6',
'OperatorSpacing.After.Operator:33:10',
'OperatorSpacing.Before.Operator:33:10',
'OperatorSpacing.After.Operator:35:13',
'OperatorSpacing.Before.Operator:35:13',
'OperatorSpacing.After.Operator:36:13',
'OperatorSpacing.Before.Operator:36:13',
'OperatorSpacing.After.Operator:37:13',
'OperatorSpacing.Before.Operator:37:13',
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ final class PunctuationSpacingTest extends AbstractRuleTestCase
public function testRule(): void
{
$this->checkRule(new PunctuationSpacingRule(), [
[3 => 4],
[3 => 10],
[4 => 4],
[4 => 10],
[4 => 16],
[4 => 22],
[4 => 28],
[5 => 12],
[5 => 16],
[5 => 20],
[5 => 24],
[6 => 6],
[6 => 6],
[7 => 12],
[7 => 15],
'PunctuationSpacing.After.Punctuation:3:4',
'PunctuationSpacing.Before.Punctuation:3:10',
'PunctuationSpacing.After.Punctuation:4:4',
'PunctuationSpacing.Before.Punctuation:4:10',
'PunctuationSpacing.Before.Punctuation:4:16',
'PunctuationSpacing.Before.Punctuation:4:22',
'PunctuationSpacing.Before.Punctuation:4:28',
'PunctuationSpacing.After.Punctuation:5:12',
'PunctuationSpacing.Before.Punctuation:5:16',
'PunctuationSpacing.Before.Punctuation:5:20',
'PunctuationSpacing.Before.Punctuation:5:24',
'PunctuationSpacing.After.Punctuation:6:6',
'PunctuationSpacing.Before.Punctuation:6:6',
'PunctuationSpacing.Before.Punctuation:7:12',
'PunctuationSpacing.Before.Punctuation:7:15',
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ final class TrailingCommaSingleLineTest extends AbstractRuleTestCase
public function testRule(): void
{
$this->checkRule(new TrailingCommaSingleLineRule(), [
[2 => 9],
[4 => 13],
[6 => 12],
'TrailingCommaSingleLine.Error.Punctuation:2:9',
'TrailingCommaSingleLine.Error.Punctuation:4:13',
'TrailingCommaSingleLine.Error.Punctuation:6:12',
]);
}
}
2 changes: 1 addition & 1 deletion tests/Rules/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public static function ignoredViolationsDataProvider(): iterable
];
yield [
__DIR__.'/Fixtures/disable2.twig',
[3, 6, 8, 10, 11, 13],
[3, 7, 9, 11, 12, 14],
];
}
}
6 changes: 3 additions & 3 deletions tests/Rules/Whitespace/BlankEOF/BlankEOFTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ final class BlankEOFTest extends AbstractRuleTestCase
public function testRule(): void
{
$this->checkRule(new BlankEOFRule(), [
[4 => 1],
'BlankEOF.Error.Eof:4:1',
]);

$this->checkRule(new BlankEOFRule(), [
[2 => 7],
'BlankEOF.Error.Eof:2:7',
], __DIR__.'/BlankEOFTest2.twig');
}

Expand All @@ -25,7 +25,7 @@ public function testRuleForEmptyFile(): void
$this->checkRule(new BlankEOFRule(), [], __DIR__.'/BlankEOFTest.empty.twig');

$this->checkRule(new BlankEOFRule(), [
[3 => 1],
'BlankEOF.Error.Eof:3:1',
], __DIR__.'/BlankEOFTest.empty2.twig');
}
}
6 changes: 3 additions & 3 deletions tests/Rules/Whitespace/EmptyLines/EmptyLinesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ final class EmptyLinesTest extends AbstractRuleTestCase
public function testRule(): void
{
$this->checkRule(new EmptyLinesRule(), [
[2 => 1],
[5 => 1],
[10 => 1],
'EmptyLines.Error.Eol:2:1',
'EmptyLines.Error.Eol:5:1',
'EmptyLines.Error.Eol:10:1',
]);
}
}
8 changes: 4 additions & 4 deletions tests/Rules/Whitespace/Indent/IndentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public function testConfiguration(): void
public function testRule(): void
{
$this->checkRule(new IndentRule(), [
[2 => 1],
[4 => 1],
'Indent.Error.CommentTab:2:1',
'Indent.Error.Tab:4:1',
]);
}

Expand All @@ -28,8 +28,8 @@ public function testRuleWithSpaceRatio(): void
$this->checkRule(
new IndentRule(2),
[
[2 => 1],
[4 => 1],
'Indent.Error.CommentTab:2:1',
'Indent.Error.Tab:4:1',
],
__DIR__.'/IndentTest.twig',
__DIR__.'/IndentTest.fixed2.twig',
Expand Down
10 changes: 5 additions & 5 deletions tests/Rules/Whitespace/TrailingSpace/TrailingSpaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ final class TrailingSpaceTest extends AbstractRuleTestCase
public function testRule(): void
{
$this->checkRule(new TrailingSpaceRule(), [
[2 => 33],
[4 => 23],
'TrailingSpace.Error.CommentEol:2:33',
'TrailingSpace.Error.Eol:4:23',
]);
}

public function testRuleWithTab(): void
{
$this->checkRule(new TrailingSpaceRule(), [
[2 => 32],
[4 => 21],
'TrailingSpace.Error.CommentEol:2:32',
'TrailingSpace.Error.Eol:4:21',
], __DIR__.'/TrailingSpaceTest.tab.twig');
}

Expand All @@ -34,7 +34,7 @@ public function testRuleWithEmptyFile(): void
);

$this->checkRule(new TrailingSpaceRule(), [
[1 => 2],
'TrailingSpace.Error.Eol:1:2',
], __DIR__.'/TrailingSpaceTest.empty2.twig');
}
}

0 comments on commit f96490d

Please sign in to comment.