@alloy-js/python 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +3 -0
- package/LICENSE +7 -0
- package/api-extractor.json +4 -0
- package/dist/src/builtins/python.d.ts +9 -0
- package/dist/src/builtins/python.d.ts.map +1 -0
- package/dist/src/builtins/python.js +17 -0
- package/dist/src/components/Atom.d.ts +19 -0
- package/dist/src/components/Atom.d.ts.map +1 -0
- package/dist/src/components/Atom.js +82 -0
- package/dist/src/components/CallSignature.d.ts +79 -0
- package/dist/src/components/CallSignature.d.ts.map +1 -0
- package/dist/src/components/CallSignature.js +201 -0
- package/dist/src/components/ClassDeclaration.d.ts +37 -0
- package/dist/src/components/ClassDeclaration.d.ts.map +1 -0
- package/dist/src/components/ClassDeclaration.js +83 -0
- package/dist/src/components/ClassInstantiation.d.ts +24 -0
- package/dist/src/components/ClassInstantiation.d.ts.map +1 -0
- package/dist/src/components/ClassInstantiation.js +35 -0
- package/dist/src/components/Declaration.d.ts +48 -0
- package/dist/src/components/Declaration.d.ts.map +1 -0
- package/dist/src/components/Declaration.js +37 -0
- package/dist/src/components/EnumDeclaration.d.ts +164 -0
- package/dist/src/components/EnumDeclaration.d.ts.map +1 -0
- package/dist/src/components/EnumDeclaration.js +278 -0
- package/dist/src/components/EnumMember.d.ts +46 -0
- package/dist/src/components/EnumMember.d.ts.map +1 -0
- package/dist/src/components/EnumMember.js +67 -0
- package/dist/src/components/FunctionCallExpression.d.ts +19 -0
- package/dist/src/components/FunctionCallExpression.d.ts.map +1 -0
- package/dist/src/components/FunctionCallExpression.js +40 -0
- package/dist/src/components/FunctionDeclaration.d.ts +47 -0
- package/dist/src/components/FunctionDeclaration.d.ts.map +1 -0
- package/dist/src/components/FunctionDeclaration.js +107 -0
- package/dist/src/components/ImportStatement.d.ts +39 -0
- package/dist/src/components/ImportStatement.d.ts.map +1 -0
- package/dist/src/components/ImportStatement.js +104 -0
- package/dist/src/components/MemberExpression.d.ts +97 -0
- package/dist/src/components/MemberExpression.d.ts.map +1 -0
- package/dist/src/components/MemberExpression.js +308 -0
- package/dist/src/components/NoNamePolicy.d.ts +23 -0
- package/dist/src/components/NoNamePolicy.d.ts.map +1 -0
- package/dist/src/components/NoNamePolicy.js +27 -0
- package/dist/src/components/PyDoc.d.ts +90 -0
- package/dist/src/components/PyDoc.d.ts.map +1 -0
- package/dist/src/components/PyDoc.js +280 -0
- package/dist/src/components/PythonBlock.d.ts +23 -0
- package/dist/src/components/PythonBlock.d.ts.map +1 -0
- package/dist/src/components/PythonBlock.js +31 -0
- package/dist/src/components/Reference.d.ts +13 -0
- package/dist/src/components/Reference.d.ts.map +1 -0
- package/dist/src/components/Reference.js +18 -0
- package/dist/src/components/SourceFile.d.ts +46 -0
- package/dist/src/components/SourceFile.d.ts.map +1 -0
- package/dist/src/components/SourceFile.js +75 -0
- package/dist/src/components/StatementList.d.ts +25 -0
- package/dist/src/components/StatementList.d.ts.map +1 -0
- package/dist/src/components/StatementList.js +29 -0
- package/dist/src/components/VariableDeclaration.d.ts +62 -0
- package/dist/src/components/VariableDeclaration.d.ts.map +1 -0
- package/dist/src/components/VariableDeclaration.js +131 -0
- package/dist/src/components/index.d.ts +19 -0
- package/dist/src/components/index.d.ts.map +1 -0
- package/dist/src/components/index.js +18 -0
- package/dist/src/create-module.d.ts +16 -0
- package/dist/src/create-module.d.ts.map +1 -0
- package/dist/src/create-module.js +64 -0
- package/dist/src/index.d.ts +8 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +7 -0
- package/dist/src/name-policy.d.ts +5 -0
- package/dist/src/name-policy.d.ts.map +1 -0
- package/dist/src/name-policy.js +47 -0
- package/dist/src/parameter-descriptor.d.ts +31 -0
- package/dist/src/parameter-descriptor.d.ts.map +1 -0
- package/dist/src/parameter-descriptor.js +1 -0
- package/dist/src/symbol-creation.d.ts +4 -0
- package/dist/src/symbol-creation.d.ts.map +1 -0
- package/dist/src/symbol-creation.js +24 -0
- package/dist/src/symbols/custom-output-scope.d.ts +10 -0
- package/dist/src/symbols/custom-output-scope.d.ts.map +1 -0
- package/dist/src/symbols/custom-output-scope.js +25 -0
- package/dist/src/symbols/index.d.ts +7 -0
- package/dist/src/symbols/index.d.ts.map +1 -0
- package/dist/src/symbols/index.js +6 -0
- package/dist/src/symbols/python-member-scope.d.ts +7 -0
- package/dist/src/symbols/python-member-scope.d.ts.map +1 -0
- package/dist/src/symbols/python-member-scope.js +9 -0
- package/dist/src/symbols/python-module-scope.d.ts +25 -0
- package/dist/src/symbols/python-module-scope.d.ts.map +1 -0
- package/dist/src/symbols/python-module-scope.js +52 -0
- package/dist/src/symbols/python-output-symbol.d.ts +19 -0
- package/dist/src/symbols/python-output-symbol.d.ts.map +1 -0
- package/dist/src/symbols/python-output-symbol.js +22 -0
- package/dist/src/symbols/reference.d.ts +4 -0
- package/dist/src/symbols/reference.d.ts.map +1 -0
- package/dist/src/symbols/reference.js +60 -0
- package/dist/src/symbols/scopes.d.ts +5 -0
- package/dist/src/symbols/scopes.d.ts.map +1 -0
- package/dist/src/symbols/scopes.js +4 -0
- package/dist/src/utils.d.ts +7 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +12 -0
- package/dist/test/callsignatures.test.d.ts +2 -0
- package/dist/test/callsignatures.test.d.ts.map +1 -0
- package/dist/test/callsignatures.test.js +276 -0
- package/dist/test/classdeclarations.test.d.ts +2 -0
- package/dist/test/classdeclarations.test.d.ts.map +1 -0
- package/dist/test/classdeclarations.test.js +397 -0
- package/dist/test/classinstantiations.test.d.ts +2 -0
- package/dist/test/classinstantiations.test.d.ts.map +1 -0
- package/dist/test/classinstantiations.test.js +168 -0
- package/dist/test/enums.test.d.ts +2 -0
- package/dist/test/enums.test.d.ts.map +1 -0
- package/dist/test/enums.test.js +211 -0
- package/dist/test/externals.test.d.ts +2 -0
- package/dist/test/externals.test.d.ts.map +1 -0
- package/dist/test/externals.test.js +219 -0
- package/dist/test/functioncallexpressions.test.d.ts +2 -0
- package/dist/test/functioncallexpressions.test.d.ts.map +1 -0
- package/dist/test/functioncallexpressions.test.js +156 -0
- package/dist/test/functiondeclaration.test.d.ts +2 -0
- package/dist/test/functiondeclaration.test.d.ts.map +1 -0
- package/dist/test/functiondeclaration.test.js +363 -0
- package/dist/test/imports.test.d.ts +2 -0
- package/dist/test/imports.test.d.ts.map +1 -0
- package/dist/test/imports.test.js +262 -0
- package/dist/test/memberexpressions.test.d.ts +2 -0
- package/dist/test/memberexpressions.test.d.ts.map +1 -0
- package/dist/test/memberexpressions.test.js +879 -0
- package/dist/test/namepolicies.test.d.ts +2 -0
- package/dist/test/namepolicies.test.d.ts.map +1 -0
- package/dist/test/namepolicies.test.js +109 -0
- package/dist/test/pydocs.test.d.ts +2 -0
- package/dist/test/pydocs.test.d.ts.map +1 -0
- package/dist/test/pydocs.test.js +500 -0
- package/dist/test/references.test.d.ts +2 -0
- package/dist/test/references.test.d.ts.map +1 -0
- package/dist/test/references.test.js +49 -0
- package/dist/test/sourcefiles.test.d.ts +2 -0
- package/dist/test/sourcefiles.test.d.ts.map +1 -0
- package/dist/test/sourcefiles.test.js +198 -0
- package/dist/test/utils.d.ts +23 -0
- package/dist/test/utils.d.ts.map +1 -0
- package/dist/test/utils.js +88 -0
- package/dist/test/values.test.d.ts +2 -0
- package/dist/test/values.test.d.ts.map +1 -0
- package/dist/test/values.test.js +78 -0
- package/dist/test/variables.test.d.ts +2 -0
- package/dist/test/variables.test.d.ts.map +1 -0
- package/dist/test/variables.test.js +173 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +39 -0
- package/src/builtins/python.ts +20 -0
- package/src/components/Atom.tsx +76 -0
- package/src/components/CallSignature.tsx +251 -0
- package/src/components/ClassDeclaration.tsx +98 -0
- package/src/components/ClassInstantiation.tsx +54 -0
- package/src/components/Declaration.tsx +91 -0
- package/src/components/EnumDeclaration.tsx +291 -0
- package/src/components/EnumMember.tsx +92 -0
- package/src/components/FunctionCallExpression.tsx +36 -0
- package/src/components/FunctionDeclaration.tsx +121 -0
- package/src/components/ImportStatement.tsx +134 -0
- package/src/components/MemberExpression.tsx +456 -0
- package/src/components/NoNamePolicy.tsx +31 -0
- package/src/components/PyDoc.tsx +331 -0
- package/src/components/PythonBlock.tsx +26 -0
- package/src/components/Reference.tsx +21 -0
- package/src/components/SourceFile.tsx +93 -0
- package/src/components/StatementList.tsx +28 -0
- package/src/components/VariableDeclaration.tsx +180 -0
- package/src/components/index.ts +18 -0
- package/src/create-module.ts +102 -0
- package/src/index.ts +7 -0
- package/src/name-policy.ts +101 -0
- package/src/parameter-descriptor.ts +36 -0
- package/src/symbol-creation.ts +36 -0
- package/src/symbols/custom-output-scope.ts +35 -0
- package/src/symbols/index.ts +6 -0
- package/src/symbols/python-member-scope.ts +12 -0
- package/src/symbols/python-module-scope.ts +89 -0
- package/src/symbols/python-output-symbol.ts +36 -0
- package/src/symbols/reference.ts +99 -0
- package/src/symbols/scopes.ts +9 -0
- package/src/utils.ts +27 -0
- package/temp/api.json +7207 -0
- package/test/callsignatures.test.tsx +256 -0
- package/test/classdeclarations.test.tsx +320 -0
- package/test/classinstantiations.test.tsx +159 -0
- package/test/enums.test.tsx +203 -0
- package/test/externals.test.tsx +190 -0
- package/test/functioncallexpressions.test.tsx +145 -0
- package/test/functiondeclaration.test.tsx +327 -0
- package/test/imports.test.tsx +214 -0
- package/test/memberexpressions.test.tsx +725 -0
- package/test/namepolicies.test.tsx +109 -0
- package/test/pydocs.test.tsx +528 -0
- package/test/references.test.tsx +36 -0
- package/test/sourcefiles.test.tsx +131 -0
- package/test/utils.tsx +131 -0
- package/test/values.test.tsx +61 -0
- package/test/variables.test.tsx +153 -0
- package/tsconfig.json +12 -0
- package/tsdoc-metadata.json +11 -0
- package/vitest.config.ts +10 -0
- package/vitest.setup.ts +1 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { refkey } from "@alloy-js/core";
|
|
2
|
+
import { d } from "@alloy-js/core/testing";
|
|
3
|
+
import { expect, it } from "vitest";
|
|
4
|
+
import { createModule } from "../src/create-module.js";
|
|
5
|
+
import * as py from "../src/index.js";
|
|
6
|
+
import { toSourceText } from "./utils.jsx";
|
|
7
|
+
|
|
8
|
+
it("declaration of class instance with variables", () => {
|
|
9
|
+
// Creating the reference separately so the naming policy doesn't interfere
|
|
10
|
+
const classRef = refkey();
|
|
11
|
+
const result = toSourceText([
|
|
12
|
+
<py.StatementList>
|
|
13
|
+
<py.ClassDeclaration name="one-class" refkey={classRef} />
|
|
14
|
+
<py.ClassInstantiation
|
|
15
|
+
target={classRef}
|
|
16
|
+
args={[
|
|
17
|
+
<py.Atom jsValue={"A name"} />,
|
|
18
|
+
<py.Atom jsValue={42} />,
|
|
19
|
+
<py.Atom jsValue={true} />,
|
|
20
|
+
]}
|
|
21
|
+
/>
|
|
22
|
+
</py.StatementList>,
|
|
23
|
+
]);
|
|
24
|
+
const expected = d`
|
|
25
|
+
class OneClass:
|
|
26
|
+
pass
|
|
27
|
+
|
|
28
|
+
OneClass("A name", 42, True)
|
|
29
|
+
`;
|
|
30
|
+
expect(result).toRenderTo(expected);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("correct resolving of external module", () => {
|
|
34
|
+
const requestsLib = createModule({
|
|
35
|
+
name: "requests",
|
|
36
|
+
descriptor: {
|
|
37
|
+
models: ["Request"],
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
const result = toSourceText(
|
|
41
|
+
[
|
|
42
|
+
<py.StatementList>
|
|
43
|
+
<py.ClassInstantiation target={requestsLib["models"].Request} />
|
|
44
|
+
</py.StatementList>,
|
|
45
|
+
],
|
|
46
|
+
{ externals: [requestsLib] },
|
|
47
|
+
);
|
|
48
|
+
const expected = d`
|
|
49
|
+
from requests.models import Request
|
|
50
|
+
|
|
51
|
+
Request()
|
|
52
|
+
`;
|
|
53
|
+
expect(result).toRenderTo(expected);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("Class instantiation without a reference", () => {
|
|
57
|
+
const result = toSourceText([
|
|
58
|
+
<py.StatementList>
|
|
59
|
+
<py.ClassInstantiation
|
|
60
|
+
target={"ExampleClass"}
|
|
61
|
+
args={[
|
|
62
|
+
<py.Atom jsValue={"A name"} />,
|
|
63
|
+
<py.Atom jsValue={42} />,
|
|
64
|
+
<py.Atom jsValue={true} />,
|
|
65
|
+
]}
|
|
66
|
+
/>
|
|
67
|
+
</py.StatementList>,
|
|
68
|
+
]);
|
|
69
|
+
const expected = d`
|
|
70
|
+
ExampleClass("A name", 42, True)
|
|
71
|
+
`;
|
|
72
|
+
expect(result).toRenderTo(expected);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("Class instantiation without a reference and with call statement vars", () => {
|
|
76
|
+
const result = toSourceText([
|
|
77
|
+
<py.StatementList>
|
|
78
|
+
<py.ClassInstantiation
|
|
79
|
+
target={"ExampleClass"}
|
|
80
|
+
args={[
|
|
81
|
+
<py.VariableDeclaration
|
|
82
|
+
name="name"
|
|
83
|
+
initializer={"A name"}
|
|
84
|
+
callStatementVar
|
|
85
|
+
/>,
|
|
86
|
+
<py.VariableDeclaration
|
|
87
|
+
name="number"
|
|
88
|
+
initializer={42}
|
|
89
|
+
callStatementVar
|
|
90
|
+
/>,
|
|
91
|
+
<py.VariableDeclaration
|
|
92
|
+
name="flag"
|
|
93
|
+
initializer={true}
|
|
94
|
+
callStatementVar
|
|
95
|
+
/>,
|
|
96
|
+
]}
|
|
97
|
+
/>
|
|
98
|
+
</py.StatementList>,
|
|
99
|
+
]);
|
|
100
|
+
const expected = d`
|
|
101
|
+
ExampleClass(name="A name", number=42, flag=True)
|
|
102
|
+
`;
|
|
103
|
+
expect(result).toRenderTo(expected);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("Class instantiation without a reference mixing unnamed and named vars", () => {
|
|
107
|
+
const result = toSourceText([
|
|
108
|
+
<py.StatementList>
|
|
109
|
+
<py.ClassInstantiation
|
|
110
|
+
target={"ExampleClass"}
|
|
111
|
+
args={[
|
|
112
|
+
<py.Atom jsValue={"A name"} />,
|
|
113
|
+
<py.VariableDeclaration
|
|
114
|
+
name="number"
|
|
115
|
+
initializer={42}
|
|
116
|
+
callStatementVar
|
|
117
|
+
/>,
|
|
118
|
+
<py.VariableDeclaration
|
|
119
|
+
name="flag"
|
|
120
|
+
initializer={true}
|
|
121
|
+
callStatementVar
|
|
122
|
+
/>,
|
|
123
|
+
]}
|
|
124
|
+
/>
|
|
125
|
+
</py.StatementList>,
|
|
126
|
+
]);
|
|
127
|
+
const expected = d`
|
|
128
|
+
ExampleClass("A name", number=42, flag=True)
|
|
129
|
+
`;
|
|
130
|
+
expect(result).toRenderTo(expected);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("incorrect Class instantiation works", () => {
|
|
134
|
+
const result = toSourceText([
|
|
135
|
+
<py.ClassInstantiation
|
|
136
|
+
target="MyClass"
|
|
137
|
+
args={[
|
|
138
|
+
<py.ClassDeclaration name="NestedClass" />,
|
|
139
|
+
<py.FunctionDeclaration name="myFunc" />,
|
|
140
|
+
<py.StatementList>
|
|
141
|
+
<py.VariableDeclaration name="x" />
|
|
142
|
+
</py.StatementList>,
|
|
143
|
+
]}
|
|
144
|
+
/>,
|
|
145
|
+
]);
|
|
146
|
+
|
|
147
|
+
const expected = d`
|
|
148
|
+
MyClass(
|
|
149
|
+
class NestedClass:
|
|
150
|
+
pass
|
|
151
|
+
,
|
|
152
|
+
def my_func():
|
|
153
|
+
pass
|
|
154
|
+
,
|
|
155
|
+
x = None
|
|
156
|
+
)
|
|
157
|
+
`;
|
|
158
|
+
expect(result).toRenderTo(expected);
|
|
159
|
+
});
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { refkey } from "@alloy-js/core";
|
|
2
|
+
import { d } from "@alloy-js/core/testing";
|
|
3
|
+
import { describe, expect, it } from "vitest";
|
|
4
|
+
import { enumModule } from "../src/builtins/python.js";
|
|
5
|
+
import * as py from "../src/index.js";
|
|
6
|
+
import { toSourceText } from "./utils.jsx";
|
|
7
|
+
|
|
8
|
+
describe("Python Enum", () => {
|
|
9
|
+
it("classic enum with explicit values", () => {
|
|
10
|
+
const result = toSourceText(
|
|
11
|
+
[
|
|
12
|
+
<py.EnumDeclaration
|
|
13
|
+
name="Color"
|
|
14
|
+
baseType="IntEnum"
|
|
15
|
+
members={[
|
|
16
|
+
{ name: "RED", value: "1" },
|
|
17
|
+
{ name: "GREEN", value: "2" },
|
|
18
|
+
{ name: "BLUE", value: "3" },
|
|
19
|
+
]}
|
|
20
|
+
/>,
|
|
21
|
+
],
|
|
22
|
+
{ externals: [enumModule] },
|
|
23
|
+
);
|
|
24
|
+
const expected = d`
|
|
25
|
+
from enum import IntEnum
|
|
26
|
+
|
|
27
|
+
class Color(IntEnum):
|
|
28
|
+
RED = 1
|
|
29
|
+
GREEN = 2
|
|
30
|
+
BLUE = 3
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
`;
|
|
34
|
+
expect(result).toRenderTo(expected);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("classic enum with jsValues", () => {
|
|
38
|
+
const result = toSourceText(
|
|
39
|
+
[
|
|
40
|
+
<py.EnumDeclaration
|
|
41
|
+
name="Color"
|
|
42
|
+
baseType="IntEnum"
|
|
43
|
+
members={[
|
|
44
|
+
{ name: "RED", jsValue: "1" },
|
|
45
|
+
{ name: "GREEN", jsValue: 2 },
|
|
46
|
+
{ name: "BLUE", jsValue: "3" },
|
|
47
|
+
]}
|
|
48
|
+
/>,
|
|
49
|
+
],
|
|
50
|
+
{ externals: [enumModule] },
|
|
51
|
+
);
|
|
52
|
+
const expected = d`
|
|
53
|
+
from enum import IntEnum
|
|
54
|
+
|
|
55
|
+
class Color(IntEnum):
|
|
56
|
+
RED = "1"
|
|
57
|
+
GREEN = 2
|
|
58
|
+
BLUE = "3"
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
`;
|
|
62
|
+
expect(result).toRenderTo(expected);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("classic enum with a refkey as jsValue", () => {
|
|
66
|
+
const result = toSourceText(
|
|
67
|
+
[
|
|
68
|
+
<py.StatementList>
|
|
69
|
+
<py.ClassDeclaration name="Dog" />
|
|
70
|
+
<py.ClassDeclaration name="Cat" />
|
|
71
|
+
<py.EnumDeclaration
|
|
72
|
+
name="Animal"
|
|
73
|
+
baseType="Enum"
|
|
74
|
+
members={[
|
|
75
|
+
{ name: "DOG", value: refkey("Dog") },
|
|
76
|
+
{ name: "CAT", value: refkey("Cat") },
|
|
77
|
+
]}
|
|
78
|
+
/>
|
|
79
|
+
</py.StatementList>,
|
|
80
|
+
],
|
|
81
|
+
{ externals: [enumModule] },
|
|
82
|
+
);
|
|
83
|
+
const expected = d`
|
|
84
|
+
from enum import Enum
|
|
85
|
+
|
|
86
|
+
class Dog:
|
|
87
|
+
pass
|
|
88
|
+
|
|
89
|
+
class Cat:
|
|
90
|
+
pass
|
|
91
|
+
|
|
92
|
+
class Animal(Enum):
|
|
93
|
+
DOG = Dog
|
|
94
|
+
CAT = Cat
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
`;
|
|
98
|
+
expect(result).toRenderTo(expected);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("enum with auto() values", () => {
|
|
102
|
+
const result = toSourceText(
|
|
103
|
+
[
|
|
104
|
+
<py.EnumDeclaration
|
|
105
|
+
name="Animal"
|
|
106
|
+
style="auto"
|
|
107
|
+
members={[{ name: "DOG" }, { name: "CAT" }, { name: "RABBIT" }]}
|
|
108
|
+
/>,
|
|
109
|
+
],
|
|
110
|
+
{ externals: [enumModule] },
|
|
111
|
+
);
|
|
112
|
+
const expected = d`
|
|
113
|
+
from enum import auto
|
|
114
|
+
from enum import Enum
|
|
115
|
+
|
|
116
|
+
class Animal(Enum):
|
|
117
|
+
DOG = auto()
|
|
118
|
+
CAT = auto()
|
|
119
|
+
RABBIT = auto()
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
`;
|
|
123
|
+
expect(result).toRenderTo(expected);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("enum with mixed manual and auto() values", () => {
|
|
127
|
+
const result = toSourceText(
|
|
128
|
+
[
|
|
129
|
+
<py.EnumDeclaration
|
|
130
|
+
name="Permission"
|
|
131
|
+
baseType="Flag"
|
|
132
|
+
style="auto"
|
|
133
|
+
members={[
|
|
134
|
+
{ name: "READ", value: 1 },
|
|
135
|
+
{ name: "WRITE" },
|
|
136
|
+
{ name: "EXECUTE" },
|
|
137
|
+
]}
|
|
138
|
+
/>,
|
|
139
|
+
],
|
|
140
|
+
{ externals: [enumModule] },
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
const expected = d`
|
|
144
|
+
from enum import auto
|
|
145
|
+
from enum import Flag
|
|
146
|
+
|
|
147
|
+
class Permission(Flag):
|
|
148
|
+
READ = 1
|
|
149
|
+
WRITE = auto()
|
|
150
|
+
EXECUTE = auto()
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
`;
|
|
154
|
+
expect(result).toRenderTo(expected);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("functional enum with list", () => {
|
|
158
|
+
const result = toSourceText(
|
|
159
|
+
[
|
|
160
|
+
<py.EnumDeclaration
|
|
161
|
+
name="Direction"
|
|
162
|
+
style="functional"
|
|
163
|
+
members={[
|
|
164
|
+
{ name: "NORTH" },
|
|
165
|
+
{ name: "SOUTH" },
|
|
166
|
+
{ name: "EAST" },
|
|
167
|
+
{ name: "WEST" },
|
|
168
|
+
]}
|
|
169
|
+
/>,
|
|
170
|
+
],
|
|
171
|
+
{ externals: [enumModule] },
|
|
172
|
+
);
|
|
173
|
+
const expected = d`
|
|
174
|
+
from enum import Enum
|
|
175
|
+
|
|
176
|
+
Direction = Enum('Direction', ['NORTH', 'SOUTH', 'EAST', 'WEST'])
|
|
177
|
+
`;
|
|
178
|
+
expect(result).toRenderTo(expected);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("functional enum with mapping", () => {
|
|
182
|
+
const result = toSourceText(
|
|
183
|
+
[
|
|
184
|
+
<py.EnumDeclaration
|
|
185
|
+
name="Priority"
|
|
186
|
+
style="functional"
|
|
187
|
+
members={[
|
|
188
|
+
{ name: "HIGH", value: 1 },
|
|
189
|
+
{ name: "MEDIUM", value: 2 },
|
|
190
|
+
{ name: "LOW", value: 3 },
|
|
191
|
+
]}
|
|
192
|
+
/>,
|
|
193
|
+
],
|
|
194
|
+
{ externals: [enumModule] },
|
|
195
|
+
);
|
|
196
|
+
const expected = d`
|
|
197
|
+
from enum import Enum
|
|
198
|
+
|
|
199
|
+
Priority = Enum('Priority', {'HIGH' : 1, 'MEDIUM' : 2, 'LOW' : 3})
|
|
200
|
+
`;
|
|
201
|
+
expect(result).toRenderTo(expected);
|
|
202
|
+
});
|
|
203
|
+
});
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { code, refkey } from "@alloy-js/core";
|
|
2
|
+
import { d } from "@alloy-js/core/testing";
|
|
3
|
+
import { expect, it } from "vitest";
|
|
4
|
+
import * as py from "../src/index.js";
|
|
5
|
+
import { createModule } from "../src/index.js";
|
|
6
|
+
import { toSourceText } from "./utils.js";
|
|
7
|
+
|
|
8
|
+
it("uses import from external library", () => {
|
|
9
|
+
const requestsLib = createModule({
|
|
10
|
+
name: "requests",
|
|
11
|
+
descriptor: {
|
|
12
|
+
".": ["get", "post"],
|
|
13
|
+
models: ["Response", "Request"],
|
|
14
|
+
"models.anothermodule": ["something"],
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
const result = toSourceText(
|
|
18
|
+
[
|
|
19
|
+
<py.StatementList>
|
|
20
|
+
{requestsLib["."].get}
|
|
21
|
+
{requestsLib["."].post}
|
|
22
|
+
{requestsLib["models"].Request}
|
|
23
|
+
{requestsLib["models"].Response}
|
|
24
|
+
{requestsLib["models.anothermodule"].something}
|
|
25
|
+
</py.StatementList>,
|
|
26
|
+
],
|
|
27
|
+
{ externals: [requestsLib] },
|
|
28
|
+
);
|
|
29
|
+
const expected = d`
|
|
30
|
+
from requests import get
|
|
31
|
+
from requests import post
|
|
32
|
+
from requests.models import Request
|
|
33
|
+
from requests.models import Response
|
|
34
|
+
from requests.models.anothermodule import something
|
|
35
|
+
|
|
36
|
+
get
|
|
37
|
+
post
|
|
38
|
+
Request
|
|
39
|
+
Response
|
|
40
|
+
something
|
|
41
|
+
`;
|
|
42
|
+
expect(result).toRenderTo(expected);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("uses import from external library in multiple functions", () => {
|
|
46
|
+
const functionDeclarations = [
|
|
47
|
+
<py.FunctionDeclaration
|
|
48
|
+
name={"getUser"}
|
|
49
|
+
parameters={[{ name: "userId", type: "int" }]}
|
|
50
|
+
returnType={py.requestsModule["models"]["Response"]}
|
|
51
|
+
instanceFunction={true}
|
|
52
|
+
>
|
|
53
|
+
<py.StatementList>
|
|
54
|
+
<py.VariableDeclaration
|
|
55
|
+
name="response"
|
|
56
|
+
initializer={
|
|
57
|
+
<py.FunctionCallExpression
|
|
58
|
+
target={py.requestsModule["."]["get"]}
|
|
59
|
+
args={[1]}
|
|
60
|
+
/>
|
|
61
|
+
}
|
|
62
|
+
/>
|
|
63
|
+
{code`
|
|
64
|
+
return response.json()
|
|
65
|
+
`}
|
|
66
|
+
</py.StatementList>
|
|
67
|
+
</py.FunctionDeclaration>,
|
|
68
|
+
<py.FunctionDeclaration
|
|
69
|
+
name={"createUser"}
|
|
70
|
+
parameters={[{ name: "userName", type: "string" }]}
|
|
71
|
+
returnType={py.requestsModule["models"]["Response"]}
|
|
72
|
+
instanceFunction={true}
|
|
73
|
+
>
|
|
74
|
+
<py.StatementList>
|
|
75
|
+
<py.VariableDeclaration
|
|
76
|
+
name="response"
|
|
77
|
+
initializer={
|
|
78
|
+
<py.FunctionCallExpression
|
|
79
|
+
target={py.requestsModule["."]["post"]}
|
|
80
|
+
args={[1]}
|
|
81
|
+
/>
|
|
82
|
+
}
|
|
83
|
+
/>
|
|
84
|
+
{code`
|
|
85
|
+
return response.json()
|
|
86
|
+
`}
|
|
87
|
+
</py.StatementList>
|
|
88
|
+
</py.FunctionDeclaration>,
|
|
89
|
+
];
|
|
90
|
+
|
|
91
|
+
const result = toSourceText(functionDeclarations, {
|
|
92
|
+
externals: [py.requestsModule],
|
|
93
|
+
});
|
|
94
|
+
const expected = d`
|
|
95
|
+
from requests import get
|
|
96
|
+
from requests import post
|
|
97
|
+
from requests.models import Response
|
|
98
|
+
|
|
99
|
+
def get_user(self, user_id: int) -> Response:
|
|
100
|
+
response = get(1)
|
|
101
|
+
return response.json()
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def create_user(self, user_name: string) -> Response:
|
|
105
|
+
response = post(1)
|
|
106
|
+
return response.json()
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
`;
|
|
110
|
+
expect(result).toRenderTo(expected);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("uses import from external library in multiple class methods", () => {
|
|
114
|
+
const functionDeclarations = [
|
|
115
|
+
<py.ClassDeclaration name="UserClient">
|
|
116
|
+
<py.StatementList>
|
|
117
|
+
<py.VariableDeclaration
|
|
118
|
+
name="some_var"
|
|
119
|
+
initializer={12}
|
|
120
|
+
instanceVariable
|
|
121
|
+
/>
|
|
122
|
+
<py.FunctionDeclaration
|
|
123
|
+
name={"getUser"}
|
|
124
|
+
parameters={[{ name: "userId", type: "int" }]}
|
|
125
|
+
returnType={py.requestsModule["models"]["Response"]}
|
|
126
|
+
instanceFunction={true}
|
|
127
|
+
>
|
|
128
|
+
<py.StatementList>
|
|
129
|
+
<py.VariableDeclaration
|
|
130
|
+
name="response"
|
|
131
|
+
initializer={
|
|
132
|
+
<py.FunctionCallExpression
|
|
133
|
+
target={py.requestsModule["."]["get"]}
|
|
134
|
+
args={[refkey("some_var")]}
|
|
135
|
+
/>
|
|
136
|
+
}
|
|
137
|
+
/>
|
|
138
|
+
{code`
|
|
139
|
+
return response.json()
|
|
140
|
+
`}
|
|
141
|
+
</py.StatementList>
|
|
142
|
+
</py.FunctionDeclaration>
|
|
143
|
+
<py.FunctionDeclaration
|
|
144
|
+
name={"createUser"}
|
|
145
|
+
parameters={[{ name: "userName", type: "string" }]}
|
|
146
|
+
returnType={py.requestsModule["models"]["Response"]}
|
|
147
|
+
instanceFunction={true}
|
|
148
|
+
>
|
|
149
|
+
<py.StatementList>
|
|
150
|
+
<py.VariableDeclaration
|
|
151
|
+
name="response"
|
|
152
|
+
initializer={
|
|
153
|
+
<py.FunctionCallExpression
|
|
154
|
+
target={py.requestsModule["."]["post"]}
|
|
155
|
+
args={[refkey("some_var")]}
|
|
156
|
+
/>
|
|
157
|
+
}
|
|
158
|
+
/>
|
|
159
|
+
{code`
|
|
160
|
+
return response.json()
|
|
161
|
+
`}
|
|
162
|
+
</py.StatementList>
|
|
163
|
+
</py.FunctionDeclaration>
|
|
164
|
+
</py.StatementList>
|
|
165
|
+
</py.ClassDeclaration>,
|
|
166
|
+
];
|
|
167
|
+
|
|
168
|
+
const result = toSourceText(functionDeclarations, {
|
|
169
|
+
externals: [py.requestsModule],
|
|
170
|
+
});
|
|
171
|
+
const expected = d`
|
|
172
|
+
from requests import get
|
|
173
|
+
from requests import post
|
|
174
|
+
from requests.models import Response
|
|
175
|
+
|
|
176
|
+
class UserClient:
|
|
177
|
+
some_var = 12
|
|
178
|
+
def get_user(self, user_id: int) -> Response:
|
|
179
|
+
response = get(self.some_var)
|
|
180
|
+
return response.json()
|
|
181
|
+
|
|
182
|
+
def create_user(self, user_name: string) -> Response:
|
|
183
|
+
response = post(self.some_var)
|
|
184
|
+
return response.json()
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
`;
|
|
189
|
+
expect(result).toRenderTo(expected);
|
|
190
|
+
});
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { refkey } from "@alloy-js/core";
|
|
2
|
+
import { d } from "@alloy-js/core/testing";
|
|
3
|
+
import { describe, expect, it } from "vitest";
|
|
4
|
+
import * as py from "../src/index.js";
|
|
5
|
+
import { toSourceText } from "./utils.jsx";
|
|
6
|
+
|
|
7
|
+
describe("FunctionCallExpression", () => {
|
|
8
|
+
it("renders", () => {
|
|
9
|
+
const result = toSourceText([<py.FunctionCallExpression target="foo" />]);
|
|
10
|
+
expect(result).toRenderTo(d`
|
|
11
|
+
foo()
|
|
12
|
+
`);
|
|
13
|
+
});
|
|
14
|
+
it("renders with args", () => {
|
|
15
|
+
const result = toSourceText([
|
|
16
|
+
<py.FunctionCallExpression target="foo" args={["a", "b"]} />,
|
|
17
|
+
]);
|
|
18
|
+
expect(result).toRenderTo(d`
|
|
19
|
+
foo(a, b)
|
|
20
|
+
`);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("function call with variables", () => {
|
|
24
|
+
// Creating the reference separately so the naming policy doesn't interfere
|
|
25
|
+
const methodRef = refkey();
|
|
26
|
+
const result = toSourceText([
|
|
27
|
+
<py.StatementList>
|
|
28
|
+
<py.FunctionDeclaration name="runFunc" refkey={methodRef} />
|
|
29
|
+
<py.FunctionCallExpression
|
|
30
|
+
target={methodRef}
|
|
31
|
+
args={[
|
|
32
|
+
<py.Atom jsValue={"A name"} />,
|
|
33
|
+
<py.Atom jsValue={42} />,
|
|
34
|
+
<py.Atom jsValue={true} />,
|
|
35
|
+
]}
|
|
36
|
+
/>
|
|
37
|
+
</py.StatementList>,
|
|
38
|
+
]);
|
|
39
|
+
const expected = d`
|
|
40
|
+
def run_func():
|
|
41
|
+
pass
|
|
42
|
+
|
|
43
|
+
run_func("A name", 42, True)
|
|
44
|
+
`;
|
|
45
|
+
expect(result).toRenderTo(expected);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("function call with variables and assignment", () => {
|
|
49
|
+
// Creating the reference separately so the naming policy doesn't interfere
|
|
50
|
+
const methodRef = refkey();
|
|
51
|
+
const result = toSourceText([
|
|
52
|
+
<py.StatementList>
|
|
53
|
+
<py.FunctionDeclaration
|
|
54
|
+
name="runFunc"
|
|
55
|
+
returnType="str"
|
|
56
|
+
refkey={methodRef}
|
|
57
|
+
parameters={[
|
|
58
|
+
{ name: "name", type: "str" },
|
|
59
|
+
{ name: "number", type: "int" },
|
|
60
|
+
{ name: "flag", type: "bool" },
|
|
61
|
+
]}
|
|
62
|
+
/>
|
|
63
|
+
<py.VariableDeclaration
|
|
64
|
+
name="result"
|
|
65
|
+
type="str"
|
|
66
|
+
initializer={
|
|
67
|
+
<py.FunctionCallExpression
|
|
68
|
+
target={methodRef}
|
|
69
|
+
args={[
|
|
70
|
+
<py.Atom jsValue={"A name"} />,
|
|
71
|
+
<py.Atom jsValue={42} />,
|
|
72
|
+
<py.Atom jsValue={true} />,
|
|
73
|
+
]}
|
|
74
|
+
/>
|
|
75
|
+
}
|
|
76
|
+
/>
|
|
77
|
+
</py.StatementList>,
|
|
78
|
+
]);
|
|
79
|
+
const expected = d`
|
|
80
|
+
def run_func(name: str, number: int, flag: bool) -> str:
|
|
81
|
+
pass
|
|
82
|
+
|
|
83
|
+
result: str = run_func("A name", 42, True)
|
|
84
|
+
`;
|
|
85
|
+
expect(result).toRenderTo(expected);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("Method call without a reference and with call statement vars", () => {
|
|
89
|
+
const result = toSourceText([
|
|
90
|
+
<py.StatementList>
|
|
91
|
+
<py.FunctionCallExpression
|
|
92
|
+
target={"example_method"}
|
|
93
|
+
args={[
|
|
94
|
+
<py.VariableDeclaration
|
|
95
|
+
name="name"
|
|
96
|
+
initializer={"A name"}
|
|
97
|
+
callStatementVar
|
|
98
|
+
/>,
|
|
99
|
+
<py.VariableDeclaration
|
|
100
|
+
name="number"
|
|
101
|
+
initializer={42}
|
|
102
|
+
callStatementVar
|
|
103
|
+
/>,
|
|
104
|
+
<py.VariableDeclaration
|
|
105
|
+
name="flag"
|
|
106
|
+
initializer={true}
|
|
107
|
+
callStatementVar
|
|
108
|
+
/>,
|
|
109
|
+
]}
|
|
110
|
+
/>
|
|
111
|
+
</py.StatementList>,
|
|
112
|
+
]);
|
|
113
|
+
const expected = d`
|
|
114
|
+
example_method(name="A name", number=42, flag=True)
|
|
115
|
+
`;
|
|
116
|
+
expect(result).toRenderTo(expected);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("Method call without a reference mixing unnamed and named vars", () => {
|
|
120
|
+
const result = toSourceText([
|
|
121
|
+
<py.StatementList>
|
|
122
|
+
<py.FunctionCallExpression
|
|
123
|
+
target={"example_method"}
|
|
124
|
+
args={[
|
|
125
|
+
<py.Atom jsValue={"A name"} />,
|
|
126
|
+
<py.VariableDeclaration
|
|
127
|
+
name="number"
|
|
128
|
+
initializer={42}
|
|
129
|
+
callStatementVar
|
|
130
|
+
/>,
|
|
131
|
+
<py.VariableDeclaration
|
|
132
|
+
name="flag"
|
|
133
|
+
initializer={true}
|
|
134
|
+
callStatementVar
|
|
135
|
+
/>,
|
|
136
|
+
]}
|
|
137
|
+
/>
|
|
138
|
+
</py.StatementList>,
|
|
139
|
+
]);
|
|
140
|
+
const expected = d`
|
|
141
|
+
example_method("A name", number=42, flag=True)
|
|
142
|
+
`;
|
|
143
|
+
expect(result).toRenderTo(expected);
|
|
144
|
+
});
|
|
145
|
+
});
|