Skip to content

Commit

Permalink
OwnersTest
Browse files Browse the repository at this point in the history
  • Loading branch information
kellegous committed Oct 29, 2024
1 parent 5359884 commit 289adcc
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ $rule = $matcher->match($relative_path);

- [x] Add comments to the structure
- [x] Complete phpdocs
- [ ] Add error checking on `match` to normalize or reject impossible patterns (i.e. "" and "/...")
- [x] Add error checking on `match` to normalize or reject impossible patterns (i.e. "" and "/...")
- [ ] Explain how `AutomataMatcher` works in this file.
- [x] Should I support whitespace escaping in patterns?
- [x] Add ability to turn `Owners` into string.
- [ ] Test owners entries.
- [x] Test owners entries.
- [ ] Test entries to string.
- [ ] Test pattern escaping.
- [ ] Test no-owners rules
Expand Down
8 changes: 6 additions & 2 deletions src/Owners.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ private static function parseEntry(
$comment = '';
} else {
$content = trim(substr($line, 0, $comment_start));
$comment = trim(substr($line, $comment_start + 1));
$comment = trim(substr($line, $comment_start));
}

if ($content === '' && $comment === '') {
return new Blank($sourceInfo);
} elseif ($content === '' && $comment !== '') {
return new Comment($content, $sourceInfo);
return new Comment($comment, $sourceInfo);
}

return Rule::parse(
Expand Down Expand Up @@ -125,6 +125,10 @@ public static function fromString(
string $content,
?string $filename = null
): self {
if ($content === '') {
return new self([]);
}

$entries = iterator_to_array(
self::entriesFrom(
explode(PHP_EOL, $content),
Expand Down
111 changes: 111 additions & 0 deletions tests/OwnersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

declare(strict_types=1);

namespace Kellegous\CodeOwners;

use PHPUnit\Framework\TestCase;

/**
* @covers Owners
*/
class OwnersTest extends TestCase
{
/**
* @return iterable<string, array{string, Entry[]}>
* @throws ParseException
*/
public static function getEntriesTests(): iterable
{
yield 'empty file' => [
'',
[]
];

yield 'all blanks' => [
"\n\n",
[
new Blank(new SourceInfo(1, null)),
new Blank(new SourceInfo(2, null)),
new Blank(new SourceInfo(3, null)),
]
];

yield 'all comments' => [
self::fromLines([
' # comment 1',
' # comment 2',
'# comment 3',
]),
[
new Comment('# comment 1', new SourceInfo(1, null)),
new Comment('# comment 2', new SourceInfo(2, null)),
new Comment('# comment 3', new SourceInfo(3, null)),
]
];

yield 'mixed entries' => [
self::fromLines([
' # first rule',
'',
' /a/ # no owner',
' # second rule',
' /b/ @a',
' # third rule',
'/c/ @a @b # inline comment',
]),
[
new Comment('# first rule', new SourceInfo(1, null)),
new Blank(new SourceInfo(2, null)),
new Rule(
Pattern::parse('/a/'),
[],
new SourceInfo(3, null),
'# no owner'
),
new Comment('# second rule', new SourceInfo(4, null)),
new Rule(
Pattern::parse('/b/'),
['@a'],
new SourceInfo(5, null),
null
),
new Comment('# third rule', new SourceInfo(6, null)),
new Rule(
Pattern::parse('/c/'),
['@a', '@b'],
new SourceInfo(7, null),
'# inline comment'
)
]
];
}

/**
* @param string[] $lines
* @return string
*/
private static function fromLines(array $lines): string
{
return implode(PHP_EOL, $lines);
}

/**
* @param string $owners_file
* @param Entry[] $expected
* @return void
* @throws ParseException
*
* @dataProvider getEntriesTests
*/
public function testEntries(
string $owners_file,
array $expected
): void {
$owners = Owners::fromString($owners_file);
self::assertEquals(
$expected,
$owners->getEntries()
);
}
}

0 comments on commit 289adcc

Please sign in to comment.