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

adding more owners tests #14

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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ $rule = $matcher->match($relative_path);
- [x] Should I support whitespace escaping in patterns?
- [x] Add ability to turn `Owners` into string.
- [x] Test owners entries.
- [ ] Test entries to string.
- [x] Test entries to string.
- [ ] Test pattern escaping.
- [ ] Test no-owners rules
- [x] Remove ext-json from requires.
- [x] Add ext-ctype to requires.
- [x] Convert json tests to PHP arrays.
- [ ] Add a command line tool for dot-renderer
- [ ] Add tests for all entries.

## Author(s)

Expand Down
16 changes: 15 additions & 1 deletion src/Owners.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class Owners
/**
* @param Entry[] $entries
*/
private function __construct(
public function __construct(
array $entries
) {
$this->entries = $entries;
Expand Down Expand Up @@ -163,4 +163,18 @@ public function getEntries(): iterable
{
return $this->entries;
}

/**
* @return string
*/
public function toString(): string
{
return implode(
PHP_EOL,
array_map(
fn(Entry $entry) => $entry->toString(),
$this->entries
)
) . "\n";
}
}
89 changes: 89 additions & 0 deletions tests/OwnersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,78 @@ private static function fromLines(array $lines): string
return implode(PHP_EOL, $lines);
}

/**
* @return iterable<string, array{Entry[], string}>
*/
public static function getToStringTests(): iterable
{
yield 'empty' => [
[],
"\n"
];

$expected = <<<CODEOWNERS
# comment 1
# comment 2
# comment 3

CODEOWNERS;

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

$expected = <<<CODEOWNERS


CODEOWNERS;
yield 'a blank' => [
[
new Blank(new SourceInfo(1, null)),
],
$expected
];

$expected = <<<CODEOWNERS
/a/ @a # first rule
/b/ @b
# comment 1

/z

CODEOWNERS;
yield 'mixed' => [
[
new Rule(
Pattern::parse('/a/'),
['@a'],
new SourceInfo(1, null),
'# first rule'
),
new Rule(
Pattern::parse('/b/'),
['@b'],
new SourceInfo(2, null),
null
),
new Comment('# comment 1', new SourceInfo(3, null)),
new Blank(new SourceInfo(4, null)),
new Rule(
Pattern::parse('/z'),
[],
new SourceInfo(5, null),
null
),
],
$expected
];
}

/**
* @param string $owners_file
* @param Entry[] $expected
Expand All @@ -108,4 +180,21 @@ public function testEntries(
$owners->getEntries()
);
}

/**
* @param Entry[] $entries
* @param string $expected
* @return void
* @dataProvider getToStringTests
*/
public function testToString(
array $entries,
string $expected
): void {
$owners = new Owners($entries);
self::assertSame(
$expected,
$owners->toString()
);
}
}
Loading