-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbasic.test.ts
More file actions
116 lines (112 loc) · 3.82 KB
/
Copy pathbasic.test.ts
File metadata and controls
116 lines (112 loc) · 3.82 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
import { transpileString } from "./utils";
describe("transpiling basic types", () => {
it.each([
["export type T = boolean;", "T = bool"],
["export type T = number;", "T = float"],
["export type T = string;", "T = str"],
["export type T = undefined;", "T = None"],
["export type T = void;", "T = None"],
["export type T = null;", "T = None"],
[
"export type T = true;",
"from typing_extensions import Literal\n\nT = Literal[True]",
],
[
"export type T = false;",
"from typing_extensions import Literal\n\nT = Literal[False]",
],
[
"export type T = 42;",
"from typing_extensions import Literal\n\nT = Literal[42]",
],
[
"export type T = 'foo';",
'from typing_extensions import Literal\n\nT = Literal["foo"]',
],
[
"export type T = {[key: string]: boolean};",
"from typing_extensions import Dict\n\nT = Dict[str,bool]",
],
[
"export type T = {[key: string]: number};",
"from typing_extensions import Dict\n\nT = Dict[str,float]",
],
[
"export type T = [string, number]",
"from typing_extensions import Tuple\n\nT = Tuple[str,float]",
],
[
"export type T = number[]",
"from typing_extensions import List\n\nT = List[float]",
],
["export type T = any;", "from typing_extensions import Any\n\nT = Any"],
[
"export type T = unknown;",
"from typing_extensions import Any\n\nT = Any",
],
[
"export type T = {[key: string]: {[key: string]: number}};",
"from typing_extensions import Dict\n\nT = Dict[str,Dict[str,float]]",
],
[
"export type T = Record<string, number>;",
"from typing_extensions import Dict\n\nT = Dict[str,float]",
],
[
"export type T = number | string | Record<string, boolean>",
"from typing_extensions import Dict, Union\n\nT = Union[str,float,Dict[str,bool]]",
],
[
"export type T = number | undefined",
"from typing_extensions import Union\n\nT = Union[None,float]",
],
["export type T = `a.b.${string}`", "T = str"],
[
"export type T = symbol",
'from typing_extensions import NewType\n\nTs2Py_symbol_0 = NewType("Ts2Py_symbol_0", object)\n\nT = Ts2Py_symbol_0',
],
])("transpiles %p to %p", async (input, expected) => {
const result = await transpileString(input);
expect(result).toEqual(expected);
});
it.each([
[
"export type T = number | undefined",
"from typing_extensions import Union\n\nT = Union[None,float]",
],
[
"export type T = number | null",
"from typing_extensions import Union\n\nT = Union[None,float]",
],
])("transpiles %p to %p when strict", async (input, expected) => {
const result = await transpileString(input, {}, { strict: true });
expect(result).toEqual(expected);
});
it("only transpiles exported types", async () => {
const result = await transpileString(`
type NotExported = number;
const notExported: NotExported = 42;
export type Exported = number;
export const exported: Exported = 42;
`);
expect(result).not.toContain("NotExported");
expect(result).not.toContain("exported");
expect(result).toContain("Exported = float");
});
it("re-uses existing symbol definitions", async () => {
const result = await transpileString(`
const a = Symbol("a")
const b = Symbol("b")
export type T1 = symbol;
export type T2 = symbol;
export type T3 = typeof a;
export type T4 = symbol;
export type T5 = typeof b;
`);
expect(result).toContain(`T1 = Ts2Py_symbol_0`);
expect(result).toContain(`T2 = Ts2Py_symbol_0`);
expect(result).toContain(`T3 = Ts2Py_symbol_1`);
expect(result).toContain(`T4 = Ts2Py_symbol_0`);
expect(result).toContain(`T5 = Ts2Py_symbol_2`);
});
});