Skip to content
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

cleanup matcher tests #12

Merged
merged 1 commit into from
Oct 29, 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: 6 additions & 0 deletions src/AutomataMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ private static function parseToken(
*/
public function match(string $path): ?Rule
{
if (str_starts_with($path, '/') || str_ends_with($path, '/')) {
throw new \InvalidArgumentException(
"path should be a relative path to a file, thus it cannot start or end with a /"
);
}

$path = explode('/', $path);
$index = $this->start->match($path);
return $index >= 0
Expand Down
6 changes: 6 additions & 0 deletions src/SimpleMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public function __construct(iterable $rules)
*/
public function match(string $path): ?Rule
{
if (str_starts_with($path, '/') || str_ends_with($path, '/')) {
throw new \InvalidArgumentException(
"path should be a relative path to a file, thus it cannot start or end with a /"
);
}

foreach ($this->rules as [$matcher, $rule]) {
if ($matcher($path)) {
return $rule;
Expand Down
63 changes: 32 additions & 31 deletions tests/AutomataMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace Kellegous\CodeOwners;

use Exception;
use PHPUnit\Framework\TestCase;

require_once __DIR__ . '/TestProvider.php';

/**
* @covers AutomataMatcher
*/
Expand All @@ -15,49 +18,30 @@ final class AutomataMatcherTest extends TestCase
*/
public static function getMatchTests(): iterable
{
$tests = include __DIR__ . '/matcher_tests.php';
foreach ($tests as $desc => ['lines' => $lines, 'expected' => $expected]) {
$matcher = self::matcherWith($lines);
foreach ($expected as $path => $line) {
yield "{$desc} w/ {$path}" => [
$matcher,
$path,
$line
];
}
}
return TestProvider::forMatchTests(
fn(iterable $rules) => AutomataMatcher::build($rules)
);
}

/**
* @param string[] $lines
* @return AutomataMatcher
* @return iterable<array{AutomataMatcher, string, ?int}>
* @throws ParseException
*/
private static function matcherWith(array $lines): AutomataMatcher
public static function getExampleTests(): iterable
{
return AutomataMatcher::build(
Owners::fromString(
implode(PHP_EOL, $lines)
)->getRules()
return TestProvider::forExampleTests(
fn(iterable $rules) => AutomataMatcher::build($rules)
);
}

/**
* @return iterable<array{AutomataMatcher, string, ?int}>
* @throws ParseException
* @return iterable<array{AutomataMatcher, string, Exception}>
*/
public static function getExampleTests(): iterable
public static function getBadInputTests(): iterable
{
$owners = Owners::fromFile(__DIR__ . '/CODEOWNERS.example');
$matcher = AutomataMatcher::build($owners->getRules());
$tests = include __DIR__ . '/example_tests.php';
foreach ($tests as $path => $line) {
yield $path => [
$matcher,
$path,
$line
];
}
return TestProvider::forBadInputTests(
fn(iterable $rules) => AutomataMatcher::build($rules)
);
}

/**
Expand Down Expand Up @@ -99,4 +83,21 @@ public function testExampleMatch(
: null;
self::assertSame($expected_line, $line);
}

/**
* @param AutomataMatcher $matcher
* @param string $path
* @param Exception $exception
* @return void
*
* @dataProvider getBadInputTests
*/
public function testBadInput(
RuleMatcher $matcher,
string $path,
Exception $exception
): void {
self::expectExceptionObject($exception);
$matcher->match($path);
}
}
2 changes: 1 addition & 1 deletion tests/CODEOWNERS.example
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ a/**/b @doctocat

# In this example nobody owns the files
# This line should be ignored by the parser.
/src
/src
2 changes: 1 addition & 1 deletion tests/PatternTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ public function testPatternMatch(
$matcher($path)
);
}
}
}
63 changes: 32 additions & 31 deletions tests/SimpleMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace Kellegous\CodeOwners;

use Exception;
use PHPUnit\Framework\TestCase;

require_once __DIR__ . '/TestProvider.php';

/**
* @covers SimpleMatcher
*/
Expand All @@ -15,49 +18,30 @@ class SimpleMatcherTest extends TestCase
*/
public static function getMatchTests(): iterable
{
$tests = include __DIR__ . '/matcher_tests.php';
foreach ($tests as $desc => ['lines' => $lines, 'expected' => $expected]) {
$matcher = self::matcherWith($lines);
foreach ($expected as $path => $line) {
yield "{$desc} w/ {$path}" => [
$matcher,
$path,
$line
];
}
}
return TestProvider::forMatchTests(
fn(iterable $rules) => new SimpleMatcher($rules)
);
}

/**
* @param string[] $lines
* @return SimpleMatcher
* @return iterable<array{SimpleMatcher, string, ?int}>
* @throws ParseException
*/
private static function matcherWith(array $lines): SimpleMatcher
public static function getExampleTests(): iterable
{
return new SimpleMatcher(
Owners::fromString(
implode(PHP_EOL, $lines)
)->getRules()
return TestProvider::forExampleTests(
fn(iterable $rules) => new SimpleMatcher($rules)
);
}

/**
* @return iterable<array{SimpleMatcher, string, ?int}>
* @throws ParseException
* @return iterable<array{SimpleMatcher, string, Exception}>
*/
public static function getExampleTests(): iterable
public static function getBadInputTests(): iterable
{
$owners = Owners::fromFile(__DIR__ . '/CODEOWNERS.example');
$matcher = new SimpleMatcher($owners->getRules());
$tests = include __DIR__ . '/example_tests.php';
foreach ($tests as $path => $line) {
yield $path => [
$matcher,
$path,
$line
];
}
return TestProvider::forBadInputTests(
fn(iterable $rules) => new SimpleMatcher($rules)
);
}

/**
Expand Down Expand Up @@ -99,4 +83,21 @@ public function testExampleMatch(
: null;
self::assertSame($expected_line, $line);
}

/**
* @param RuleMatcher $matcher
* @param string $path
* @param Exception $exception
* @return void
*
* @dataProvider getBadInputTests
*/
public function testBadInput(
RuleMatcher $matcher,
string $path,
Exception $exception
): void {
self::expectExceptionObject($exception);
$matcher->match($path);
}
}
91 changes: 91 additions & 0 deletions tests/TestProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

namespace Kellegous\CodeOwners;

use Closure;
use Exception;
use InvalidArgumentException;

/**
* Provides a common set of tests for all RuleMatchers.
*/
final class TestProvider
{

private function __construct()
{
}

/**
* @template T extends RulMatcher
* @param Closure(iterable<Rule>):T $create_matcher
* @return iterable<array{T, string, ?int}>
* @throws ParseException
*/
public static function forMatchTests(
Closure $create_matcher
): iterable {
$tests = include __DIR__ . '/matcher_tests.php';
foreach ($tests as $desc => ['lines' => $lines, 'expected' => $expected]) {
$rules = Owners::fromString(
implode(PHP_EOL, $lines)
)->getRules();
$matcher = $create_matcher($rules);
foreach ($expected as $path => $line) {
yield "{$desc} w/ {$path}" => [
$matcher,
$path,
$line
];
}
}
}

/**
* @template T extends RuleMatcher
* @param Closure(iterable<Rule>):T $create_matcher
* @return iterable<array{T, string, ?int}>
* @throws ParseException
*/
public static function forExampleTests(
Closure $create_matcher
): iterable {
$owners = Owners::fromFile(__DIR__ . '/CODEOWNERS.example');
$matcher = $create_matcher($owners->getRules());
$tests = include __DIR__ . '/example_tests.php';
foreach ($tests as $path => $line) {
yield $path => [
$matcher,
$path,
$line
];
}
}

/**
* @template T extends RuleMatcher
* @param Closure(iterable<Rule>):T $create_matcher
* @return iterable<array{T, string, Exception}>
*/
public static function forBadInputTests(
Closure $create_matcher
): iterable {
$matcher = $create_matcher([]);
yield 'leading /' => [
$matcher,
'/foo/bar',
new InvalidArgumentException(
"path should be a relative path to a file, thus it cannot start or end with a /"
)
];
yield 'trailing /' => [
$matcher,
'foo/bar/',
new InvalidArgumentException(
"path should be a relative path to a file, thus it cannot start or end with a /"
)
];
}
}
7 changes: 3 additions & 4 deletions tests/matcher_tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@
'lines' => ['/*a/'],
'expected' => [
'1a' => null,
'2a/b/' => 1,
'2a/c/d/' => 1,
'2a/b' => 1,
'2a/c/d' => 1,
]
],
'/a*/' => [
Expand Down Expand Up @@ -154,5 +154,4 @@
'b/d/e' => null,
]
],

];
];
Loading