|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tempest\Highlight\Tests\Languages\TypeScript; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use Tempest\Highlight\Highlighter; |
| 10 | + |
| 11 | +class TypeScriptLanguageTest extends TestCase |
| 12 | +{ |
| 13 | + #[DataProvider('provide_highlight_cases')] |
| 14 | + public function test_highlight(string $content, string $expected): void |
| 15 | + { |
| 16 | + $highlighter = new Highlighter(); |
| 17 | + |
| 18 | + $this->assertSame( |
| 19 | + $expected, |
| 20 | + $highlighter->parse($content, 'ts'), |
| 21 | + ); |
| 22 | + |
| 23 | + $this->assertSame( |
| 24 | + $expected, |
| 25 | + $highlighter->parse($content, 'typescript'), |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + public static function provide_highlight_cases(): iterable |
| 30 | + { |
| 31 | + return [ |
| 32 | + [ |
| 33 | + 'type Alias = string;', |
| 34 | + '<span class="hl-keyword">type</span> Alias = <span class="hl-type">string</span>;', |
| 35 | + ], |
| 36 | + [ |
| 37 | + <<<'TXT' |
| 38 | + interface User { |
| 39 | + id: number; |
| 40 | + name: string; |
| 41 | + } |
| 42 | + TXT, |
| 43 | + <<<'TXT' |
| 44 | + <span class="hl-keyword">interface</span> User { |
| 45 | + <span class="hl-property">id</span>: <span class="hl-type">number</span>; |
| 46 | + <span class="hl-property">name</span>: <span class="hl-type">string</span>; |
| 47 | + } |
| 48 | + TXT, |
| 49 | + ], |
| 50 | + [ |
| 51 | + 'const x: boolean = true;', |
| 52 | + '<span class="hl-keyword">const</span> <span class="hl-property">x</span>: <span class="hl-type">boolean</span> = <span class="hl-keyword">true</span>;', |
| 53 | + ], |
| 54 | + [ |
| 55 | + 'readonly name: string;', |
| 56 | + '<span class="hl-keyword">readonly</span> <span class="hl-property">name</span>: <span class="hl-type">string</span>;', |
| 57 | + ], |
| 58 | + [ |
| 59 | + 'function greet(name: string): void {}', |
| 60 | + '<span class="hl-keyword">function</span> <span class="hl-property">greet</span>(<span class="hl-property">name</span>: <span class="hl-type">string</span>): <span class="hl-keyword">void</span> {}', |
| 61 | + ], |
| 62 | + [ |
| 63 | + 'let u: User = getUser();', |
| 64 | + '<span class="hl-keyword">let</span> <span class="hl-property">u</span>: <span class="hl-type">User</span> = <span class="hl-property">getUser</span>();', |
| 65 | + ], |
| 66 | + [ |
| 67 | + 'function identity<T>(v: T): T {}', |
| 68 | + '<span class="hl-keyword">function</span> identity<span class="hl-generic"><T></span>(<span class="hl-property">v</span>: <span class="hl-type">T</span>): <span class="hl-type">T</span> {}', |
| 69 | + ], |
| 70 | + [ |
| 71 | + 'class Service<K, V extends Base> {}', |
| 72 | + '<span class="hl-keyword">class</span> <span class="hl-type">Service</span><span class="hl-generic"><K, V <span class="hl-keyword">extends</span> Base></span> {}', |
| 73 | + ], |
| 74 | + [ |
| 75 | + <<<'TXT' |
| 76 | + @Component({ selector: 'x' }) |
| 77 | + class Foo {} |
| 78 | + TXT, |
| 79 | + <<<'TXT' |
| 80 | + <span class="hl-attribute">@<span class="hl-property">Component</span></span>({ <span class="hl-property">selector</span>: <span class="hl-value">'x'</span> }) |
| 81 | + <span class="hl-keyword">class</span> <span class="hl-type">Foo</span> {} |
| 82 | + TXT, |
| 83 | + ], |
| 84 | + [ |
| 85 | + <<<'TXT' |
| 86 | + @Injectable |
| 87 | + export class Bar {} |
| 88 | + TXT, |
| 89 | + <<<'TXT' |
| 90 | + <span class="hl-attribute">@Injectable</span> |
| 91 | + <span class="hl-keyword">export</span> <span class="hl-keyword">class</span> <span class="hl-type">Bar</span> {} |
| 92 | + TXT, |
| 93 | + ], |
| 94 | + [ |
| 95 | + <<<'TXT' |
| 96 | + /** |
| 97 | + * Greet a user. |
| 98 | + * @param {string} name |
| 99 | + */ |
| 100 | + function greet(name: string): void {} |
| 101 | + TXT, |
| 102 | + <<<'TXT' |
| 103 | + <span class="hl-comment">/** |
| 104 | + * Greet a user. |
| 105 | + * <span class="hl-value">@param</span> <span class="hl-type">{string}</span> <span class="hl-value">name</span> |
| 106 | + */</span> |
| 107 | + <span class="hl-keyword">function</span> <span class="hl-property">greet</span>(<span class="hl-property">name</span>: <span class="hl-type">string</span>): <span class="hl-keyword">void</span> {} |
| 108 | + TXT, |
| 109 | + ], |
| 110 | + ]; |
| 111 | + } |
| 112 | +} |
0 commit comments