forked from TheLartians/TypeScript2Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimports.test.ts
More file actions
85 lines (74 loc) 路 2.24 KB
/
Copy pathimports.test.ts
File metadata and controls
85 lines (74 loc) 路 2.24 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
import { typeScriptToPython } from "../typeScriptToPython";
import { createProject, ts } from "@ts-morph/bootstrap";
describe("transpiling referenced types", () => {
it("can refer to types defined in other files", async () => {
const project = await createProject();
project.createSourceFile("foo.ts", "export type Foo = { foo: number }");
const barSource = project.createSourceFile(
"bar.ts",
"import {Foo} from './foo';\nexport type Bar = {foo: Foo}",
);
const program = project.createProgram();
const diagnostics = ts.getPreEmitDiagnostics(program);
if (diagnostics.length > 0) {
throw new Error(
`code compiled with errors: ${project.formatDiagnosticsWithColorAndContext(
diagnostics,
)}`,
);
}
const transpiled = typeScriptToPython(
program.getTypeChecker(),
[barSource],
{},
);
expect(transpiled).toEqual(
`from typing_extensions import TypedDict
class Ts2Py_vxK3pg8Yk2(TypedDict):
foo: float
class Bar(TypedDict):
foo: Ts2Py_vxK3pg8Yk2`,
);
});
it("doesn't get confused if imported types have the same name", async () => {
const project = await createProject();
project.createSourceFile(
"foo.ts",
`
type Content = { publicFoo: "foo" };
export type Foo = { foo: Content }
`,
);
project.createSourceFile(
"bar.ts",
`
type Content = { publicBar: "bar" };
export type Bar = { bar: Content }
`,
);
const commonSource = project.createSourceFile(
"common.ts",
`
import {Foo} from './foo';
import {Bar} from './bar';
export type FooBar = { foo: Foo, bar: Bar }
`,
);
const program = project.createProgram();
const diagnostics = ts.getPreEmitDiagnostics(program);
if (diagnostics.length > 0) {
throw new Error(
`code compiled with errors: ${project.formatDiagnosticsWithColorAndContext(
diagnostics,
)}`,
);
}
const transpiled = typeScriptToPython(
program.getTypeChecker(),
[commonSource],
{},
);
expect(transpiled).toContain('publicFoo: Literal["foo"]');
expect(transpiled).toContain('publicBar: Literal["bar"]');
});
});