|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace TypeLang\PHPDoc\Tests\Unit; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 8 | +use PHPUnit\Framework\Attributes\Group; |
| 9 | +use TypeLang\PHPDoc\Parser\Comment\CommentParserInterface; |
| 10 | +use TypeLang\PHPDoc\Parser\Comment\LexerAwareCommentParser; |
| 11 | + |
| 12 | +#[Group('unit'), Group('type-lang/phpdoc')] |
| 13 | +final class CommentParserTest extends TestCase |
| 14 | +{ |
| 15 | + public static function parserDataProvider(): iterable |
| 16 | + { |
| 17 | + yield LexerAwareCommentParser::class => [ |
| 18 | + new LexerAwareCommentParser(), |
| 19 | + ]; |
| 20 | + } |
| 21 | + |
| 22 | + public static function delimiterDataProvider(): array |
| 23 | + { |
| 24 | + return [ |
| 25 | + 'LF' => ["\n"], |
| 26 | + 'CRLF' => ["\r\n"], |
| 27 | + ]; |
| 28 | + } |
| 29 | + |
| 30 | + public static function parserWithVariantDelimitersDataProvider(): iterable |
| 31 | + { |
| 32 | + foreach (self::parserDataProvider() as $parserName => [$provider]) { |
| 33 | + foreach (self::delimiterDataProvider() as $delimiterName => [$delimiter]) { |
| 34 | + yield $parserName . ' + ' . $delimiterName => [$provider, $delimiter]; |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + private function convertLineDelimiter(string $docblock, string $delimiter = "\r\n"): string |
| 40 | + { |
| 41 | + return \implode($delimiter, \explode("\n", $docblock)); |
| 42 | + } |
| 43 | + |
| 44 | + private function parseAsArray(CommentParserInterface $parser, string $docblock, string $delimiter = "\n"): array |
| 45 | + { |
| 46 | + if ($delimiter !== "\n") { |
| 47 | + $docblock = $this->convertLineDelimiter($docblock, $delimiter); |
| 48 | + } |
| 49 | + |
| 50 | + $result = []; |
| 51 | + |
| 52 | + foreach ($parser->parse($docblock) as $segment) { |
| 53 | + $result[$segment->offset] = $segment->text; |
| 54 | + } |
| 55 | + |
| 56 | + return $result; |
| 57 | + } |
| 58 | + |
| 59 | + #[DataProvider('parserWithVariantDelimitersDataProvider')] |
| 60 | + public function testWrappedComment(CommentParserInterface $parser, string $delimiter): void |
| 61 | + { |
| 62 | + $docBlock = <<<'PHP' |
| 63 | + /** |
| 64 | + * Line 1 |
| 65 | + * Line 2 |
| 66 | + */ |
| 67 | + PHP; |
| 68 | + |
| 69 | + self::assertSame(match($delimiter) { |
| 70 | + "\n" => [7 => "Line 1$delimiter", 17 => "Line 2$delimiter"], |
| 71 | + "\r\n" => [8 => "Line 1$delimiter", 19 => "Line 2$delimiter"], |
| 72 | + }, $this->parseAsArray($parser, $docBlock, $delimiter)); |
| 73 | + } |
| 74 | + |
| 75 | + #[DataProvider('parserWithVariantDelimitersDataProvider')] |
| 76 | + public function testIncompleteEndingWrappedComment(CommentParserInterface $parser, string $delimiter): void |
| 77 | + { |
| 78 | + $docBlock = <<<'PHP' |
| 79 | + /** |
| 80 | + * Line 1 |
| 81 | + * |
| 82 | + * Line 3 |
| 83 | + PHP; |
| 84 | + |
| 85 | + self::assertSame(match($delimiter) { |
| 86 | + "\n" => [7 => "Line 1$delimiter", 20 => "Line 3"], |
| 87 | + "\r\n" => [8 => "Line 1$delimiter", 23 => "Line 3"], |
| 88 | + }, $this->parseAsArray($parser, $docBlock, $delimiter)); |
| 89 | + } |
| 90 | + |
| 91 | + #[DataProvider('parserWithVariantDelimitersDataProvider')] |
| 92 | + public function testIncompleteStartingWrappedComment(CommentParserInterface $parser, string $delimiter): void |
| 93 | + { |
| 94 | + $docBlock = <<<'PHP' |
| 95 | + * Line 1 |
| 96 | + * |
| 97 | + * Line 3 |
| 98 | + */ |
| 99 | + PHP; |
| 100 | + |
| 101 | + self::assertSame( |
| 102 | + expected: [0 => $this->convertLineDelimiter($docBlock, $delimiter)], |
| 103 | + actual: $this->parseAsArray($parser, $docBlock, $delimiter), |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + #[DataProvider('parserWithVariantDelimitersDataProvider')] |
| 108 | + public function testExtraNonWhitespaceCharsBeforeWrappedComment(CommentParserInterface $parser, string $delimiter): void |
| 109 | + { |
| 110 | + $docBlock = <<<'PHP' |
| 111 | + Some unrecognized chars |
| 112 | + /** |
| 113 | + * Line 1 |
| 114 | + * |
| 115 | + * Line 3 |
| 116 | + */ |
| 117 | + PHP; |
| 118 | + |
| 119 | + self::assertSame( |
| 120 | + expected: [0 => $this->convertLineDelimiter($docBlock, $delimiter)], |
| 121 | + actual: $this->parseAsArray($parser, $docBlock, $delimiter), |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + #[DataProvider('parserWithVariantDelimitersDataProvider')] |
| 126 | + public function testExtraWhitespaceCharsBeforeWrappedComment(CommentParserInterface $parser, string $delimiter): void |
| 127 | + { |
| 128 | + $docBlock = <<<PHP |
| 129 | + \u{0020}\u{000A} |
| 130 | + /** |
| 131 | + * Line 1 |
| 132 | + * Line 2 |
| 133 | + */ |
| 134 | + PHP; |
| 135 | + |
| 136 | + self::assertSame(match($delimiter) { |
| 137 | + "\n" => [10 => "Line 1$delimiter", 20 => "Line 2$delimiter"], |
| 138 | + "\r\n" => [13 => "Line 1$delimiter", 24 => "Line 2$delimiter"], |
| 139 | + }, $this->parseAsArray($parser, $docBlock, $delimiter)); |
| 140 | + } |
| 141 | + |
| 142 | + #[DataProvider('parserWithVariantDelimitersDataProvider')] |
| 143 | + public function testNonWrappedComment(CommentParserInterface $parser, string $delimiter): void |
| 144 | + { |
| 145 | + $docBlock = <<<'PHP' |
| 146 | + Example some |
| 147 | + @phpdoc test |
| 148 | + any test |
| 149 | + @return void Description |
| 150 | + PHP; |
| 151 | + |
| 152 | + self::assertSame( |
| 153 | + expected: [0 => $this->convertLineDelimiter($docBlock, $delimiter)], |
| 154 | + actual: $this->parseAsArray($parser, $docBlock, $delimiter), |
| 155 | + ); |
| 156 | + } |
| 157 | +} |
0 commit comments