|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace TypeLang\Printer\Tests\Unit; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 8 | +use PHPUnit\Framework\Attributes\Group; |
| 9 | +use TypeLang\Parser\Node\Stmt\TypeStatement; |
| 10 | +use TypeLang\Parser\Parser; |
| 11 | +use TypeLang\Printer\NativeTypePrinter; |
| 12 | +use TypeLang\Printer\PrettyPrinter; |
| 13 | + |
| 14 | +#[Group('unit'), Group('type-lang/printer')] |
| 15 | +final class SimplePrintTypesTest extends TestCase |
| 16 | +{ |
| 17 | + private static function typesList(): iterable |
| 18 | + { |
| 19 | + yield 'example-type'; |
| 20 | + yield 'Non\Qualified\Name'; |
| 21 | + yield '\Full\Qualified\Name'; |
| 22 | + |
| 23 | + yield 'Generic<T>' => 'Generic'; |
| 24 | + yield 'Generic<hint T>' => 'Generic'; |
| 25 | + |
| 26 | + yield '$this' => 'self'; |
| 27 | + yield '$this|null|string' => 'self|null|string'; |
| 28 | + |
| 29 | + yield 'Example|Union|Type'; |
| 30 | + yield 'Example & Intersection & Type' => 'Example&Intersection&Type'; |
| 31 | + |
| 32 | + yield '?NullableType' => '?NullableType'; |
| 33 | + yield 'ListType[]' => 'iterable'; |
| 34 | + |
| 35 | + yield 'GLOBAL_CONST'; |
| 36 | + yield 'Namespaced\GLOBAL_CONST'; |
| 37 | + yield '\Full\Qualified\CONST'; |
| 38 | + |
| 39 | + yield 'GLOBAL_CONST_MASK_*' => 'mixed'; |
| 40 | + yield 'Namespaced\GLOBAL_CONST_MASK_*' => 'mixed'; |
| 41 | + yield '\Full\Qualified\CONST_MASK_*' => 'mixed'; |
| 42 | + |
| 43 | + yield 'ClassName::CONST' => 'mixed'; |
| 44 | + yield 'Namespaced\ClassName::CONST' => 'mixed'; |
| 45 | + yield '\Full\Qualified\ClassName::CONST' => 'mixed'; |
| 46 | + |
| 47 | + yield 'ClassName::CONST_MASK_*' => 'mixed'; |
| 48 | + yield 'Namespaced\ClassName::CONST_MASK_*' => 'mixed'; |
| 49 | + yield '\Full\Qualified\ClassName::CONST_MASK_*' => 'mixed'; |
| 50 | + |
| 51 | + yield 'callable()' => 'callable'; |
| 52 | + yield 'callable(): With\Type' => 'callable'; |
| 53 | + yield 'callable(Param): With\Type' => 'callable'; |
| 54 | + yield 'callable(ParamNamed $named): With\Type' => 'callable'; |
| 55 | + yield 'callable(Optional=): With\Type' => 'callable'; |
| 56 | + yield 'callable(OptionalNamed $name=): With\Type' => 'callable'; |
| 57 | + yield 'callable(Out&): With\Type' => 'callable'; |
| 58 | + yield 'callable(OutNamed &$name): With\Type' => 'callable'; |
| 59 | + yield 'callable(OutOptional&=): With\Type' => 'callable'; |
| 60 | + yield 'callable(OutOptionalNamed &$name=): With\Type' => 'callable'; |
| 61 | + yield 'callable(Variadic...): With\Type' => 'callable'; |
| 62 | + yield 'callable(VariadicNamed ...$name): With\Type' => 'callable'; |
| 63 | + yield 'callable(OutVariadic &...$name): With\Type' => 'callable'; |
| 64 | + |
| 65 | + yield '(T is U ? V : W)' => 'V|W'; |
| 66 | + yield '(T is not U ? V : W)' => 'V|W'; |
| 67 | + yield '(T > U ? V : W)' => 'V|W'; |
| 68 | + yield '(T < U ? V : W)' => 'V|W'; |
| 69 | + yield '(T >= U ? V : W)' => 'V|W'; |
| 70 | + yield '(T <= U ? V : W)' => 'V|W'; |
| 71 | + |
| 72 | + yield '($var is foo() ? T : class-string<T>)' => 'T|string'; |
| 73 | + |
| 74 | + yield 'Shape{}' => 'Shape'; |
| 75 | + yield <<<'PHP' |
| 76 | + Shape{ |
| 77 | + k: T, |
| 78 | + v: U |
| 79 | + } |
| 80 | + PHP => 'Shape'; |
| 81 | + yield <<<'PHP' |
| 82 | + Shape{ |
| 83 | + k: T, |
| 84 | + v: U, |
| 85 | + ... |
| 86 | + } |
| 87 | + PHP => 'Shape'; |
| 88 | + yield <<<'PHP' |
| 89 | + Shape{ |
| 90 | + k: T, |
| 91 | + v: U, |
| 92 | + ...<T1, T2> |
| 93 | + } |
| 94 | + PHP => 'Shape'; |
| 95 | + yield <<<'PHP' |
| 96 | + Shape{ |
| 97 | + k?: T, |
| 98 | + v?: U |
| 99 | + } |
| 100 | + PHP => 'Shape'; |
| 101 | + yield <<<'PHP' |
| 102 | + Shape{ |
| 103 | + k?: T, |
| 104 | + v?: U, |
| 105 | + ... |
| 106 | + } |
| 107 | + PHP => 'Shape'; |
| 108 | + yield <<<'PHP' |
| 109 | + Shape{ |
| 110 | + k?: T, |
| 111 | + v?: U, |
| 112 | + ...<T1, T2> |
| 113 | + } |
| 114 | + PHP => 'Shape'; |
| 115 | + yield <<<'PHP' |
| 116 | + Shape{ |
| 117 | + T, |
| 118 | + U |
| 119 | + } |
| 120 | + PHP => 'Shape'; |
| 121 | + yield <<<'PHP' |
| 122 | + Shape{ |
| 123 | + T, |
| 124 | + U, |
| 125 | + ... |
| 126 | + } |
| 127 | + PHP => 'Shape'; |
| 128 | + yield <<<'PHP' |
| 129 | + Shape{ |
| 130 | + T, |
| 131 | + U, |
| 132 | + ...<T1> |
| 133 | + } |
| 134 | + PHP => 'Shape'; |
| 135 | + yield <<<'PHP' |
| 136 | + Shape{ |
| 137 | + 0: T, |
| 138 | + 1: U |
| 139 | + } |
| 140 | + PHP => 'Shape'; |
| 141 | + yield <<<'PHP' |
| 142 | + Shape{ |
| 143 | + 0: T, |
| 144 | + 1: U, |
| 145 | + ... |
| 146 | + } |
| 147 | + PHP => 'Shape'; |
| 148 | + yield <<<'PHP' |
| 149 | + Shape{ |
| 150 | + 0: T, |
| 151 | + 1: U, |
| 152 | + ...<T1> |
| 153 | + } |
| 154 | + PHP => 'Shape'; |
| 155 | + } |
| 156 | +
|
| 157 | + public static function prettyPrintableTypesDataProvider(): iterable |
| 158 | + { |
| 159 | + $parser = new Parser(); |
| 160 | +
|
| 161 | + foreach (self::typesList() as $type => $native) { |
| 162 | + if (\is_int($type)) { |
| 163 | + $type = $native; |
| 164 | + } |
| 165 | +
|
| 166 | + yield $type => [$parser->parse($type), $type]; |
| 167 | + } |
| 168 | + } |
| 169 | +
|
| 170 | + #[DataProvider('prettyPrintableTypesDataProvider')] |
| 171 | + public function testPrettyPrinting(TypeStatement $stmt, string $expected): void |
| 172 | + { |
| 173 | + $printer = new PrettyPrinter(); |
| 174 | +
|
| 175 | + self::assertSame($expected, $printer->print($stmt)); |
| 176 | + } |
| 177 | +
|
| 178 | + public static function nativePrintableTypesDataProvider(): iterable |
| 179 | + { |
| 180 | + $parser = new Parser(); |
| 181 | +
|
| 182 | + foreach (self::typesList() as $type => $native) { |
| 183 | + if (\is_int($type)) { |
| 184 | + $type = $native; |
| 185 | + } |
| 186 | +
|
| 187 | + yield $type => [$parser->parse($type), $native]; |
| 188 | + } |
| 189 | + } |
| 190 | +
|
| 191 | + #[DataProvider('nativePrintableTypesDataProvider')] |
| 192 | + public function testNativePrettyPrinting(TypeStatement $stmt, string $expected): void |
| 193 | + { |
| 194 | + $printer = new NativeTypePrinter(); |
| 195 | +
|
| 196 | + self::assertSame($expected, $printer->print($stmt)); |
| 197 | + } |
| 198 | +} |
0 commit comments