Skip to content

Commit 5c19bd3

Browse files
committed
Add TypeScript language tests, fixtures, and benchmarks
1 parent 78fe7e4 commit 5c19bd3

3 files changed

Lines changed: 170 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* User service with basic CRUD operations.
3+
*/
4+
import { Injectable } from '@angular/core';
5+
6+
interface User {
7+
id: number;
8+
name: string;
9+
email: string;
10+
roles: readonly string[];
11+
}
12+
13+
type UserId = number;
14+
15+
enum Role {
16+
Admin = 'admin',
17+
Member = 'member',
18+
Guest = 'guest',
19+
}
20+
21+
@Injectable({ providedIn: 'root' })
22+
export class UserService<T extends User> {
23+
private readonly users: Map<UserId, T> = new Map();
24+
25+
constructor(private readonly apiUrl: string) {}
26+
27+
public async findById(id: UserId): Promise<T | undefined> {
28+
const user = this.users.get(id);
29+
if (user === undefined) {
30+
return undefined;
31+
}
32+
return user;
33+
}
34+
35+
public async create(data: Omit<T, 'id'>): Promise<T> {
36+
const id: number = Math.floor(Math.random() * 1_000_000);
37+
const user = { id, ...data } as T;
38+
this.users.set(id, user);
39+
return user;
40+
}
41+
42+
public delete(id: UserId): boolean {
43+
return this.users.delete(id);
44+
}
45+
46+
public list(): readonly T[] {
47+
return Array.from(this.users.values());
48+
}
49+
}
50+
51+
const service = new UserService<User>('https://api.example.com');
52+
const admin: User = {
53+
id: 1,
54+
name: 'Alice',
55+
email: 'alice@example.com',
56+
roles: ['admin'],
57+
};

tests/Bench/HighlighterBench.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ final class HighlighterBench
3939
'sql' => 'sql.txt',
4040
'terminal' => 'terminal.txt',
4141
'terraform' => 'terraform.txt',
42+
'typescript' => 'typescript.txt',
4243
'twig' => 'twig.txt',
4344
'xml' => 'xml.txt',
4445
'yaml' => 'yaml.txt',
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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">&lt;T&gt;</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">&lt;K, V <span class="hl-keyword">extends</span> Base&gt;</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

Comments
 (0)