From fd8244286ae5526230f5dc41409d8599112304c9 Mon Sep 17 00:00:00 2001 From: Kelly Norton Date: Tue, 29 Oct 2024 13:19:46 -0400 Subject: [PATCH] adding more owners tests --- README.md | 3 +- src/Owners.php | 16 +++++++- tests/OwnersTest.php | 89 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 64b4f03..b83c269 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/Owners.php b/src/Owners.php index de5d827..5f13e36 100644 --- a/src/Owners.php +++ b/src/Owners.php @@ -19,7 +19,7 @@ final class Owners /** * @param Entry[] $entries */ - private function __construct( + public function __construct( array $entries ) { $this->entries = $entries; @@ -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"; + } } \ No newline at end of file diff --git a/tests/OwnersTest.php b/tests/OwnersTest.php index fd2b5d8..2f51130 100644 --- a/tests/OwnersTest.php +++ b/tests/OwnersTest.php @@ -90,6 +90,78 @@ private static function fromLines(array $lines): string return implode(PHP_EOL, $lines); } + /** + * @return iterable + */ + public static function getToStringTests(): iterable + { + yield 'empty' => [ + [], + "\n" + ]; + + $expected = << [ + [ + 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 = << [ + [ + new Blank(new SourceInfo(1, null)), + ], + $expected + ]; + + $expected = << [ + [ + 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 @@ -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() + ); + } }