-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_typescript_parser.py
More file actions
195 lines (143 loc) · 5.76 KB
/
test_typescript_parser.py
File metadata and controls
195 lines (143 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from pathlib import Path
import pytest
from mimir.models import SymbolKind, Visibility
from mimir.parsers.typescript import TypeScriptParser
SIMPLE_CLASS = b"""
class PaymentService {
private name: string;
public process(): void {}
constructor(name: string) {}
}
"""
INTERFACE = b"""
interface IProcessor {
process(): void;
getCount(): number;
}
"""
ABSTRACT_CLASS = b"""
abstract class Base {
protected abstract run(): void;
public execute(): void {}
}
"""
EXPORTED_FUNCTION = b"""
export function helper(x: number): string {
return String(x);
}
export function calculate(a: number, b: number): number {
return a + b;
}
"""
NESTED_CLASS = b"""
class Outer {
class Inner {
doWork(): void {}
}
}
"""
TSX_COMPONENT = b"""
interface ButtonProps {
label: string;
}
class Button {
private props: ButtonProps;
render(): string { return ''; }
}
"""
TS_IMPORTS = b"""
import React from 'react';
import { useState } from 'react';
import type { FC } from 'react';
class Component {}
"""
@pytest.fixture()
def parser() -> TypeScriptParser:
return TypeScriptParser(tsx=False)
@pytest.fixture()
def tsx_parser() -> TypeScriptParser:
return TypeScriptParser(tsx=True)
def _extract(parser: TypeScriptParser, source: bytes, tmp_path: Path, filename: str = "module.ts"): # noqa: ANN202
f = tmp_path / filename
f.write_bytes(source)
result = parser.extract(f, filename, "abc123")
assert result is not None
return result
def _parse(parser: TypeScriptParser, source: bytes, tmp_path: Path, filename: str = "module.ts") -> list:
f = tmp_path / filename
f.write_bytes(source)
result = parser.extract(f, filename, "abc123")
assert result is not None
return result.symbols
def test_extracts_class(parser: TypeScriptParser, tmp_path: Path) -> None:
symbols = _parse(parser, SIMPLE_CLASS, tmp_path)
classes = [s for s in symbols if s.kind == SymbolKind.CLASS]
assert len(classes) == 1
assert classes[0].name == "PaymentService"
def test_extracts_methods(parser: TypeScriptParser, tmp_path: Path) -> None:
symbols = _parse(parser, SIMPLE_CLASS, tmp_path)
methods = {s.name for s in symbols if s.kind == SymbolKind.METHOD}
assert "process" in methods
def test_extracts_constructor(parser: TypeScriptParser, tmp_path: Path) -> None:
symbols = _parse(parser, SIMPLE_CLASS, tmp_path)
ctors = [s for s in symbols if s.kind == SymbolKind.CONSTRUCTOR]
assert len(ctors) == 1
def test_extracts_field(parser: TypeScriptParser, tmp_path: Path) -> None:
symbols = _parse(parser, SIMPLE_CLASS, tmp_path)
fields = [s for s in symbols if s.kind == SymbolKind.FIELD]
assert any(s.name == "name" for s in fields)
def test_field_visibility(parser: TypeScriptParser, tmp_path: Path) -> None:
symbols = _parse(parser, SIMPLE_CLASS, tmp_path)
name_field = next(s for s in symbols if s.name == "name")
assert name_field.visibility == Visibility.PRIVATE
def test_method_visibility(parser: TypeScriptParser, tmp_path: Path) -> None:
symbols = _parse(parser, SIMPLE_CLASS, tmp_path)
process = next(s for s in symbols if s.name == "process")
assert process.visibility == Visibility.PUBLIC
def test_method_qualified_name(parser: TypeScriptParser, tmp_path: Path) -> None:
symbols = _parse(parser, SIMPLE_CLASS, tmp_path)
process = next(s for s in symbols if s.name == "process")
assert process.qualified_name == "PaymentService.process"
assert process.parent_name == "PaymentService"
def test_extracts_interface(parser: TypeScriptParser, tmp_path: Path) -> None:
symbols = _parse(parser, INTERFACE, tmp_path)
ifaces = [s for s in symbols if s.kind == SymbolKind.INTERFACE]
assert len(ifaces) == 1
assert ifaces[0].name == "IProcessor"
def test_extracts_exported_functions(parser: TypeScriptParser, tmp_path: Path) -> None:
symbols = _parse(parser, EXPORTED_FUNCTION, tmp_path)
fns = {s.name for s in symbols if s.kind == SymbolKind.FUNCTION}
assert "helper" in fns
assert "calculate" in fns
def test_abstract_class_extracted(parser: TypeScriptParser, tmp_path: Path) -> None:
symbols = _parse(parser, ABSTRACT_CLASS, tmp_path)
classes = [s for s in symbols if s.kind == SymbolKind.CLASS]
assert any(s.name == "Base" for s in classes)
def test_test_file_marks_symbols(parser: TypeScriptParser, tmp_path: Path) -> None:
f = tmp_path / "service.test.ts"
f.write_bytes(b"class Foo { run(): void {} }")
result = parser.extract(f, "service.test.ts", "abc123")
assert result is not None
assert all(s.is_test for s in result.symbols)
def test_spec_file_marks_symbols(parser: TypeScriptParser, tmp_path: Path) -> None:
f = tmp_path / "service.spec.ts"
f.write_bytes(b"class Foo { run(): void {} }")
result = parser.extract(f, "service.spec.ts", "abc123")
assert result is not None
assert all(s.is_test for s in result.symbols)
def test_tsx_parser_language(tsx_parser: TypeScriptParser, tmp_path: Path) -> None:
symbols = _parse(tsx_parser, TSX_COMPONENT, tmp_path, filename="Button.tsx")
assert any(s.name == "Button" for s in symbols)
assert any(s.name == "ButtonProps" for s in symbols)
def test_tsx_file_language_field(tsx_parser: TypeScriptParser, tmp_path: Path) -> None:
f = tmp_path / "Button.tsx"
f.write_bytes(b"class X {}")
result = tsx_parser.extract(f, "Button.tsx", "abc")
assert result is not None
assert result.language == "tsx"
def test_imports_extracted(parser: TypeScriptParser, tmp_path: Path) -> None:
indexed = _extract(parser, TS_IMPORTS, tmp_path)
assert "react" in indexed.imports
def test_no_imports_when_absent(parser: TypeScriptParser, tmp_path: Path) -> None:
indexed = _extract(parser, SIMPLE_CLASS, tmp_path)
assert indexed.imports == []