@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,131 @@
|
|
|
1
|
+
import { d } from "@alloy-js/core/testing";
|
|
2
|
+
import { expect, it } from "vitest";
|
|
3
|
+
import * as py from "../src/index.js";
|
|
4
|
+
import { toSourceText } from "./utils.jsx";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* toSourceText wraps the children in a SourceFile component
|
|
8
|
+
* and renders it to a string.
|
|
9
|
+
*/
|
|
10
|
+
it("renders an empty source file", () => {
|
|
11
|
+
const result = toSourceText([]);
|
|
12
|
+
const expected = d`
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
`;
|
|
16
|
+
expect(result).toRenderTo(expected);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("correct formatting of source file", () => {
|
|
20
|
+
const result = toSourceText([
|
|
21
|
+
<py.ClassDeclaration name="someClass">
|
|
22
|
+
<py.StatementList>
|
|
23
|
+
<py.FunctionDeclaration name="someMethod" returnType="str">
|
|
24
|
+
<py.StatementList>
|
|
25
|
+
<py.VariableDeclaration
|
|
26
|
+
name="x"
|
|
27
|
+
type="int"
|
|
28
|
+
initializer={<py.Atom jsValue={42} />}
|
|
29
|
+
/>
|
|
30
|
+
<py.VariableDeclaration
|
|
31
|
+
name="y"
|
|
32
|
+
type="int"
|
|
33
|
+
initializer={<py.Atom jsValue={42} />}
|
|
34
|
+
/>
|
|
35
|
+
<py.FunctionCallExpression target="foo" args={["a", "b"]} />
|
|
36
|
+
<py.MemberExpression>
|
|
37
|
+
<py.MemberExpression.Part id="a" />
|
|
38
|
+
<py.MemberExpression.Part id="b" />
|
|
39
|
+
<py.MemberExpression.Part key={"special-prop"} />
|
|
40
|
+
</py.MemberExpression>
|
|
41
|
+
<py.VariableDeclaration
|
|
42
|
+
name="z"
|
|
43
|
+
type="int"
|
|
44
|
+
initializer={<py.Atom jsValue={42} />}
|
|
45
|
+
/>
|
|
46
|
+
</py.StatementList>
|
|
47
|
+
</py.FunctionDeclaration>
|
|
48
|
+
<py.VariableDeclaration
|
|
49
|
+
name="someVar"
|
|
50
|
+
type="int"
|
|
51
|
+
initializer={<py.Atom jsValue={42} />}
|
|
52
|
+
/>
|
|
53
|
+
<py.FunctionDeclaration name="someOtherMethod" returnType="str" />
|
|
54
|
+
<py.VariableDeclaration
|
|
55
|
+
name="someOtherVar"
|
|
56
|
+
type="int"
|
|
57
|
+
initializer={<py.Atom jsValue={42} />}
|
|
58
|
+
/>
|
|
59
|
+
</py.StatementList>
|
|
60
|
+
</py.ClassDeclaration>,
|
|
61
|
+
<py.FunctionDeclaration name="someFunction">
|
|
62
|
+
<py.StatementList>
|
|
63
|
+
<py.VariableDeclaration
|
|
64
|
+
name="x"
|
|
65
|
+
type="int"
|
|
66
|
+
initializer={<py.Atom jsValue={42} />}
|
|
67
|
+
/>
|
|
68
|
+
<py.VariableDeclaration
|
|
69
|
+
name="y"
|
|
70
|
+
type="int"
|
|
71
|
+
initializer={<py.Atom jsValue={42} />}
|
|
72
|
+
/>
|
|
73
|
+
<py.FunctionCallExpression target="foo" args={["a", "b"]} />
|
|
74
|
+
<py.MemberExpression>
|
|
75
|
+
<py.MemberExpression.Part id="a" />
|
|
76
|
+
<py.MemberExpression.Part id="b" />
|
|
77
|
+
<py.MemberExpression.Part key={"special-prop"} />
|
|
78
|
+
</py.MemberExpression>
|
|
79
|
+
<py.VariableDeclaration
|
|
80
|
+
name="z"
|
|
81
|
+
type="int"
|
|
82
|
+
initializer={<py.Atom jsValue={42} />}
|
|
83
|
+
/>
|
|
84
|
+
</py.StatementList>
|
|
85
|
+
</py.FunctionDeclaration>,
|
|
86
|
+
<py.ClassDeclaration name="someOtherClass">
|
|
87
|
+
<py.StatementList>
|
|
88
|
+
<py.FunctionDeclaration name="someMethod" returnType="str" />
|
|
89
|
+
</py.StatementList>
|
|
90
|
+
</py.ClassDeclaration>,
|
|
91
|
+
<py.MemberExpression>
|
|
92
|
+
<py.MemberExpression.Part id="a" />
|
|
93
|
+
<py.MemberExpression.Part id="b" />
|
|
94
|
+
<py.MemberExpression.Part key={"special-prop"} />
|
|
95
|
+
</py.MemberExpression>,
|
|
96
|
+
]);
|
|
97
|
+
const expected = d`
|
|
98
|
+
class SomeClass:
|
|
99
|
+
def some_method() -> str:
|
|
100
|
+
x: int = 42
|
|
101
|
+
y: int = 42
|
|
102
|
+
foo(a, b)
|
|
103
|
+
a.b["special-prop"]
|
|
104
|
+
z: int = 42
|
|
105
|
+
|
|
106
|
+
some_var: int = 42
|
|
107
|
+
def some_other_method() -> str:
|
|
108
|
+
pass
|
|
109
|
+
|
|
110
|
+
some_other_var: int = 42
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def some_function():
|
|
114
|
+
x: int = 42
|
|
115
|
+
y: int = 42
|
|
116
|
+
foo(a, b)
|
|
117
|
+
a.b["special-prop"]
|
|
118
|
+
z: int = 42
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
class SomeOtherClass:
|
|
122
|
+
def some_method() -> str:
|
|
123
|
+
pass
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
a.b["special-prop"]
|
|
128
|
+
|
|
129
|
+
`;
|
|
130
|
+
expect(result).toRenderTo(expected);
|
|
131
|
+
});
|
package/test/utils.tsx
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Binder,
|
|
3
|
+
Children,
|
|
4
|
+
ContentOutputFile,
|
|
5
|
+
NamePolicy,
|
|
6
|
+
Output,
|
|
7
|
+
OutputDirectory,
|
|
8
|
+
OutputFile,
|
|
9
|
+
PrintTreeOptions,
|
|
10
|
+
SymbolCreator,
|
|
11
|
+
render,
|
|
12
|
+
} from "@alloy-js/core";
|
|
13
|
+
import { dedent } from "@alloy-js/core/testing";
|
|
14
|
+
import { expect } from "vitest";
|
|
15
|
+
import * as py from "../src/components/index.js";
|
|
16
|
+
import { createPythonNamePolicy } from "../src/name-policy.js";
|
|
17
|
+
import { CustomOutputScope } from "../src/symbols/custom-output-scope.js";
|
|
18
|
+
import { PythonModuleScope } from "../src/symbols/index.js";
|
|
19
|
+
|
|
20
|
+
export function findFile(
|
|
21
|
+
res: OutputDirectory,
|
|
22
|
+
path: string,
|
|
23
|
+
): ContentOutputFile {
|
|
24
|
+
const result = findFileWorker(res, path);
|
|
25
|
+
|
|
26
|
+
if (!result) {
|
|
27
|
+
throw new Error("Expected to find file " + path);
|
|
28
|
+
}
|
|
29
|
+
return result as ContentOutputFile;
|
|
30
|
+
|
|
31
|
+
function findFileWorker(
|
|
32
|
+
res: OutputDirectory,
|
|
33
|
+
path: string,
|
|
34
|
+
): OutputFile | null {
|
|
35
|
+
for (const item of res.contents) {
|
|
36
|
+
if (item.kind === "file") {
|
|
37
|
+
if (item.path === path) {
|
|
38
|
+
return item;
|
|
39
|
+
}
|
|
40
|
+
continue;
|
|
41
|
+
} else {
|
|
42
|
+
const found = findFileWorker(item, path);
|
|
43
|
+
if (found) {
|
|
44
|
+
return found;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function assertFileContents(
|
|
53
|
+
res: OutputDirectory,
|
|
54
|
+
expectedFiles: Record<string, string>,
|
|
55
|
+
) {
|
|
56
|
+
for (const [path, contents] of Object.entries(expectedFiles)) {
|
|
57
|
+
const file = findFile(res, path);
|
|
58
|
+
expect(file.contents).toBe(dedent(contents));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function toSourceTextMultiple(
|
|
63
|
+
sourceFiles: Children[],
|
|
64
|
+
{
|
|
65
|
+
policy,
|
|
66
|
+
externals,
|
|
67
|
+
options,
|
|
68
|
+
printOptions,
|
|
69
|
+
}: {
|
|
70
|
+
policy?: NamePolicy<string>;
|
|
71
|
+
externals?: SymbolCreator[];
|
|
72
|
+
options?: { externals?: SymbolCreator[] };
|
|
73
|
+
printOptions?: PrintTreeOptions;
|
|
74
|
+
} = {},
|
|
75
|
+
): OutputDirectory {
|
|
76
|
+
if (!policy) {
|
|
77
|
+
policy = createPythonNamePolicy();
|
|
78
|
+
}
|
|
79
|
+
const mergedExternals = options?.externals ?? externals;
|
|
80
|
+
if (printOptions === undefined) {
|
|
81
|
+
printOptions = {
|
|
82
|
+
printWidth: 80,
|
|
83
|
+
tabWidth: 4,
|
|
84
|
+
};
|
|
85
|
+
} else {
|
|
86
|
+
printOptions.tabWidth = 4;
|
|
87
|
+
}
|
|
88
|
+
const content = (
|
|
89
|
+
<Output externals={mergedExternals} namePolicy={policy}>
|
|
90
|
+
{sourceFiles}
|
|
91
|
+
</Output>
|
|
92
|
+
);
|
|
93
|
+
return render(content, printOptions);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function toSourceText(
|
|
97
|
+
c: Children[],
|
|
98
|
+
{
|
|
99
|
+
policy,
|
|
100
|
+
externals,
|
|
101
|
+
options,
|
|
102
|
+
printOptions,
|
|
103
|
+
}: {
|
|
104
|
+
policy?: NamePolicy<string>;
|
|
105
|
+
externals?: SymbolCreator[];
|
|
106
|
+
options?: { externals?: SymbolCreator[] };
|
|
107
|
+
printOptions?: PrintTreeOptions;
|
|
108
|
+
} = {},
|
|
109
|
+
): string {
|
|
110
|
+
const content = <py.SourceFile path="test.py">{c}</py.SourceFile>;
|
|
111
|
+
const res = toSourceTextMultiple([content], {
|
|
112
|
+
policy,
|
|
113
|
+
externals,
|
|
114
|
+
options,
|
|
115
|
+
printOptions,
|
|
116
|
+
});
|
|
117
|
+
const file = findFile(res, "test.py");
|
|
118
|
+
return file.contents;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Helper function to create a PythonModuleScope to be used in tests
|
|
122
|
+
export function createPythonModuleScope(
|
|
123
|
+
name: string,
|
|
124
|
+
parent: CustomOutputScope | undefined,
|
|
125
|
+
binder: Binder | undefined = undefined,
|
|
126
|
+
): PythonModuleScope {
|
|
127
|
+
return new PythonModuleScope(name, {
|
|
128
|
+
parent: parent,
|
|
129
|
+
binder: binder,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import * as py from "../src/index.js";
|
|
3
|
+
import { toSourceText } from "./utils.jsx";
|
|
4
|
+
|
|
5
|
+
describe("Atom", () => {
|
|
6
|
+
it("renders string value", () => {
|
|
7
|
+
expect(toSourceText([<py.Atom jsValue={"Test"} />])).toRenderTo('"Test"');
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("renders null/undefined object", () => {
|
|
11
|
+
expect(toSourceText([<py.Atom jsValue={undefined} />])).toRenderTo("None");
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("renders number", () => {
|
|
15
|
+
expect(toSourceText([<py.Atom jsValue={123} />])).toRenderTo("123");
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("renders boolean - True", () => {
|
|
19
|
+
expect(toSourceText([<py.Atom jsValue={true} />])).toRenderTo("True");
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("renders boolean - False", () => {
|
|
23
|
+
expect(toSourceText([<py.Atom jsValue={false} />])).toRenderTo("False");
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("renders array", () => {
|
|
27
|
+
expect(toSourceText([<py.Atom jsValue={[1, 2, 3]} />])).toRenderTo(
|
|
28
|
+
"[1, 2, 3]",
|
|
29
|
+
);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("renders object", () => {
|
|
33
|
+
expect(toSourceText([<py.Atom jsValue={{ a: 1, b: 2 }} />])).toRenderTo(
|
|
34
|
+
'{"a": 1, "b": 2}',
|
|
35
|
+
);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("renders more complex object", () => {
|
|
39
|
+
expect(
|
|
40
|
+
toSourceText([<py.Atom jsValue={{ a: "1", b: 2, c: true }} />]),
|
|
41
|
+
).toRenderTo('{"a": "1", "b": 2, "c": True}');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("renders empty object", () => {
|
|
45
|
+
expect(toSourceText([<py.Atom jsValue={{}} />])).toRenderTo("{}");
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("renders function", () => {
|
|
49
|
+
function Test() {
|
|
50
|
+
return <>Test</>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
expect(toSourceText([<py.Atom jsValue={Test} />])).toRenderTo("Test");
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("renders nested object", () => {
|
|
57
|
+
expect(
|
|
58
|
+
toSourceText([<py.Atom jsValue={{ a: { b: { c: 1 } }, d: 2 }} />]),
|
|
59
|
+
).toRenderTo('{"a": {"b": {"c": 1}}, "d": 2}');
|
|
60
|
+
});
|
|
61
|
+
});
|
|
@@ -0,0 +1,153 @@
|
|
|
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 {
|
|
6
|
+
assertFileContents,
|
|
7
|
+
toSourceText,
|
|
8
|
+
toSourceTextMultiple,
|
|
9
|
+
} from "./utils.jsx";
|
|
10
|
+
|
|
11
|
+
describe("Python Variable", () => {
|
|
12
|
+
it("declares a python variable", () => {
|
|
13
|
+
const res = toSourceText([
|
|
14
|
+
<py.VariableDeclaration name="myVar" type="int" initializer={42} />,
|
|
15
|
+
]);
|
|
16
|
+
expect(res).toBe(`my_var: int = 42`);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("declares a python variable without value", () => {
|
|
20
|
+
const res = toSourceText([
|
|
21
|
+
<py.VariableDeclaration name="myVar" type="int" omitNone />,
|
|
22
|
+
]);
|
|
23
|
+
expect(res).toBe(`my_var: int`);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("declares a python variable without typeAnnotations", () => {
|
|
27
|
+
const res = toSourceText([
|
|
28
|
+
<py.VariableDeclaration name="myVar" initializer={42} />,
|
|
29
|
+
]);
|
|
30
|
+
expect(res).toBe(`my_var = 42`);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("declares a python variable as None when undefined", () => {
|
|
34
|
+
const res = toSourceText([<py.VariableDeclaration name="myVar" />]);
|
|
35
|
+
expect(res).toBe(`my_var = None`);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("declares a python variable as None when null", () => {
|
|
39
|
+
const res = toSourceText([
|
|
40
|
+
<py.VariableDeclaration
|
|
41
|
+
name="myVar"
|
|
42
|
+
initializer={<py.Atom jsValue={null} />}
|
|
43
|
+
/>,
|
|
44
|
+
]);
|
|
45
|
+
expect(res).toBe(`my_var = None`);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("declares a python variable that's an array", () => {
|
|
49
|
+
const res = toSourceText([
|
|
50
|
+
<py.VariableDeclaration
|
|
51
|
+
name="numbers"
|
|
52
|
+
type="list[int]"
|
|
53
|
+
initializer={<py.Atom jsValue={[1, 2, 3]} />}
|
|
54
|
+
/>,
|
|
55
|
+
]);
|
|
56
|
+
expect(res).toBe(`numbers: list[int] = [1, 2, 3]`);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("declares a python variable with a python value", () => {
|
|
60
|
+
const res = toSourceText([
|
|
61
|
+
<py.VariableDeclaration
|
|
62
|
+
name="nameIdPairs"
|
|
63
|
+
initializer={<py.Atom jsValue={{ John: 123, Doe: 234 }} />}
|
|
64
|
+
/>,
|
|
65
|
+
]);
|
|
66
|
+
expect(res).toBe(`name_id_pairs = {"John": 123, "Doe": 234}`);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("declares a python variable with omitNone", () => {
|
|
70
|
+
const res = toSourceText([
|
|
71
|
+
<py.VariableDeclaration name="omitNoneVar" type="int" omitNone={true} />,
|
|
72
|
+
]);
|
|
73
|
+
expect(res).toBe(`omit_none_var: int`);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("declares a call statement python variable", () => {
|
|
77
|
+
const res = toSourceText([
|
|
78
|
+
<py.VariableDeclaration
|
|
79
|
+
name="callStmtVar"
|
|
80
|
+
initializer={12}
|
|
81
|
+
callStatementVar={true}
|
|
82
|
+
/>,
|
|
83
|
+
]);
|
|
84
|
+
expect(res).toBe(`call_stmt_var=12`);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("declares a call statement python variable without name", () => {
|
|
88
|
+
const res = toSourceText([
|
|
89
|
+
<py.VariableDeclaration
|
|
90
|
+
name=""
|
|
91
|
+
initializer={12}
|
|
92
|
+
callStatementVar={true}
|
|
93
|
+
/>,
|
|
94
|
+
]);
|
|
95
|
+
expect(res).toBe(`12`);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("declares a python variable with a class type", () => {
|
|
99
|
+
const res = toSourceText([
|
|
100
|
+
<py.StatementList>
|
|
101
|
+
<py.ClassDeclaration name="MyClass" />
|
|
102
|
+
<py.VariableDeclaration
|
|
103
|
+
name="my_var"
|
|
104
|
+
type={<py.Reference refkey={refkey("MyClass")} />}
|
|
105
|
+
/>
|
|
106
|
+
</py.StatementList>,
|
|
107
|
+
]);
|
|
108
|
+
expect(res).toBe(d`
|
|
109
|
+
class MyClass:
|
|
110
|
+
pass
|
|
111
|
+
|
|
112
|
+
my_var: MyClass = None`);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("declares a python variable with a class type from a different module", () => {
|
|
116
|
+
const res = toSourceTextMultiple([
|
|
117
|
+
<py.SourceFile path="classes.py">
|
|
118
|
+
<py.ClassDeclaration name="MyClass" />
|
|
119
|
+
</py.SourceFile>,
|
|
120
|
+
<py.SourceFile path="usage.py">
|
|
121
|
+
<py.VariableDeclaration
|
|
122
|
+
name="my_var"
|
|
123
|
+
type={<py.Reference refkey={refkey("MyClass")} />}
|
|
124
|
+
/>
|
|
125
|
+
</py.SourceFile>,
|
|
126
|
+
]);
|
|
127
|
+
assertFileContents(res, {
|
|
128
|
+
"classes.py": `
|
|
129
|
+
class MyClass:
|
|
130
|
+
pass
|
|
131
|
+
|
|
132
|
+
`,
|
|
133
|
+
"usage.py": `
|
|
134
|
+
from classes import MyClass
|
|
135
|
+
|
|
136
|
+
my_var: MyClass = None
|
|
137
|
+
`,
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("declares a python variable receiving other variable as value", () => {
|
|
142
|
+
const res = toSourceText([
|
|
143
|
+
<py.StatementList>
|
|
144
|
+
<py.VariableDeclaration name="my_var" initializer={42} />
|
|
145
|
+
<py.VariableDeclaration
|
|
146
|
+
name="my_other_var"
|
|
147
|
+
initializer={refkey("my_var")}
|
|
148
|
+
/>
|
|
149
|
+
</py.StatementList>,
|
|
150
|
+
]);
|
|
151
|
+
expect(res).toBe(`my_var = 42\nmy_other_var = my_var`);
|
|
152
|
+
});
|
|
153
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"emitDeclarationOnly": true,
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"outDir": "dist",
|
|
7
|
+
"types": ["@alloy-js/core/testing/matchers"]
|
|
8
|
+
},
|
|
9
|
+
"references": [{ "path": "../core" }],
|
|
10
|
+
"include": ["src/**/*.ts", "src/**/*.tsx", "test/**/*.ts", "test/**/*.tsx"],
|
|
11
|
+
"exclude": ["node_modules", "dist"]
|
|
12
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// This file is read by tools that parse documentation comments conforming to the TSDoc standard.
|
|
2
|
+
// It should be published with your NPM package. It should not be tracked by Git.
|
|
3
|
+
{
|
|
4
|
+
"tsdocVersion": "0.12",
|
|
5
|
+
"toolPackages": [
|
|
6
|
+
{
|
|
7
|
+
"packageName": "@microsoft/api-extractor",
|
|
8
|
+
"packageVersion": "7.52.8"
|
|
9
|
+
}
|
|
10
|
+
]
|
|
11
|
+
}
|
package/vitest.config.ts
ADDED
package/vitest.setup.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "@alloy-js/core/testing";
|