@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.
Files changed (206) hide show
  1. package/CHANGELOG.md +3 -0
  2. package/LICENSE +7 -0
  3. package/api-extractor.json +4 -0
  4. package/dist/src/builtins/python.d.ts +9 -0
  5. package/dist/src/builtins/python.d.ts.map +1 -0
  6. package/dist/src/builtins/python.js +17 -0
  7. package/dist/src/components/Atom.d.ts +19 -0
  8. package/dist/src/components/Atom.d.ts.map +1 -0
  9. package/dist/src/components/Atom.js +82 -0
  10. package/dist/src/components/CallSignature.d.ts +79 -0
  11. package/dist/src/components/CallSignature.d.ts.map +1 -0
  12. package/dist/src/components/CallSignature.js +201 -0
  13. package/dist/src/components/ClassDeclaration.d.ts +37 -0
  14. package/dist/src/components/ClassDeclaration.d.ts.map +1 -0
  15. package/dist/src/components/ClassDeclaration.js +83 -0
  16. package/dist/src/components/ClassInstantiation.d.ts +24 -0
  17. package/dist/src/components/ClassInstantiation.d.ts.map +1 -0
  18. package/dist/src/components/ClassInstantiation.js +35 -0
  19. package/dist/src/components/Declaration.d.ts +48 -0
  20. package/dist/src/components/Declaration.d.ts.map +1 -0
  21. package/dist/src/components/Declaration.js +37 -0
  22. package/dist/src/components/EnumDeclaration.d.ts +164 -0
  23. package/dist/src/components/EnumDeclaration.d.ts.map +1 -0
  24. package/dist/src/components/EnumDeclaration.js +278 -0
  25. package/dist/src/components/EnumMember.d.ts +46 -0
  26. package/dist/src/components/EnumMember.d.ts.map +1 -0
  27. package/dist/src/components/EnumMember.js +67 -0
  28. package/dist/src/components/FunctionCallExpression.d.ts +19 -0
  29. package/dist/src/components/FunctionCallExpression.d.ts.map +1 -0
  30. package/dist/src/components/FunctionCallExpression.js +40 -0
  31. package/dist/src/components/FunctionDeclaration.d.ts +47 -0
  32. package/dist/src/components/FunctionDeclaration.d.ts.map +1 -0
  33. package/dist/src/components/FunctionDeclaration.js +107 -0
  34. package/dist/src/components/ImportStatement.d.ts +39 -0
  35. package/dist/src/components/ImportStatement.d.ts.map +1 -0
  36. package/dist/src/components/ImportStatement.js +104 -0
  37. package/dist/src/components/MemberExpression.d.ts +97 -0
  38. package/dist/src/components/MemberExpression.d.ts.map +1 -0
  39. package/dist/src/components/MemberExpression.js +308 -0
  40. package/dist/src/components/NoNamePolicy.d.ts +23 -0
  41. package/dist/src/components/NoNamePolicy.d.ts.map +1 -0
  42. package/dist/src/components/NoNamePolicy.js +27 -0
  43. package/dist/src/components/PyDoc.d.ts +90 -0
  44. package/dist/src/components/PyDoc.d.ts.map +1 -0
  45. package/dist/src/components/PyDoc.js +280 -0
  46. package/dist/src/components/PythonBlock.d.ts +23 -0
  47. package/dist/src/components/PythonBlock.d.ts.map +1 -0
  48. package/dist/src/components/PythonBlock.js +31 -0
  49. package/dist/src/components/Reference.d.ts +13 -0
  50. package/dist/src/components/Reference.d.ts.map +1 -0
  51. package/dist/src/components/Reference.js +18 -0
  52. package/dist/src/components/SourceFile.d.ts +46 -0
  53. package/dist/src/components/SourceFile.d.ts.map +1 -0
  54. package/dist/src/components/SourceFile.js +75 -0
  55. package/dist/src/components/StatementList.d.ts +25 -0
  56. package/dist/src/components/StatementList.d.ts.map +1 -0
  57. package/dist/src/components/StatementList.js +29 -0
  58. package/dist/src/components/VariableDeclaration.d.ts +62 -0
  59. package/dist/src/components/VariableDeclaration.d.ts.map +1 -0
  60. package/dist/src/components/VariableDeclaration.js +131 -0
  61. package/dist/src/components/index.d.ts +19 -0
  62. package/dist/src/components/index.d.ts.map +1 -0
  63. package/dist/src/components/index.js +18 -0
  64. package/dist/src/create-module.d.ts +16 -0
  65. package/dist/src/create-module.d.ts.map +1 -0
  66. package/dist/src/create-module.js +64 -0
  67. package/dist/src/index.d.ts +8 -0
  68. package/dist/src/index.d.ts.map +1 -0
  69. package/dist/src/index.js +7 -0
  70. package/dist/src/name-policy.d.ts +5 -0
  71. package/dist/src/name-policy.d.ts.map +1 -0
  72. package/dist/src/name-policy.js +47 -0
  73. package/dist/src/parameter-descriptor.d.ts +31 -0
  74. package/dist/src/parameter-descriptor.d.ts.map +1 -0
  75. package/dist/src/parameter-descriptor.js +1 -0
  76. package/dist/src/symbol-creation.d.ts +4 -0
  77. package/dist/src/symbol-creation.d.ts.map +1 -0
  78. package/dist/src/symbol-creation.js +24 -0
  79. package/dist/src/symbols/custom-output-scope.d.ts +10 -0
  80. package/dist/src/symbols/custom-output-scope.d.ts.map +1 -0
  81. package/dist/src/symbols/custom-output-scope.js +25 -0
  82. package/dist/src/symbols/index.d.ts +7 -0
  83. package/dist/src/symbols/index.d.ts.map +1 -0
  84. package/dist/src/symbols/index.js +6 -0
  85. package/dist/src/symbols/python-member-scope.d.ts +7 -0
  86. package/dist/src/symbols/python-member-scope.d.ts.map +1 -0
  87. package/dist/src/symbols/python-member-scope.js +9 -0
  88. package/dist/src/symbols/python-module-scope.d.ts +25 -0
  89. package/dist/src/symbols/python-module-scope.d.ts.map +1 -0
  90. package/dist/src/symbols/python-module-scope.js +52 -0
  91. package/dist/src/symbols/python-output-symbol.d.ts +19 -0
  92. package/dist/src/symbols/python-output-symbol.d.ts.map +1 -0
  93. package/dist/src/symbols/python-output-symbol.js +22 -0
  94. package/dist/src/symbols/reference.d.ts +4 -0
  95. package/dist/src/symbols/reference.d.ts.map +1 -0
  96. package/dist/src/symbols/reference.js +60 -0
  97. package/dist/src/symbols/scopes.d.ts +5 -0
  98. package/dist/src/symbols/scopes.d.ts.map +1 -0
  99. package/dist/src/symbols/scopes.js +4 -0
  100. package/dist/src/utils.d.ts +7 -0
  101. package/dist/src/utils.d.ts.map +1 -0
  102. package/dist/src/utils.js +12 -0
  103. package/dist/test/callsignatures.test.d.ts +2 -0
  104. package/dist/test/callsignatures.test.d.ts.map +1 -0
  105. package/dist/test/callsignatures.test.js +276 -0
  106. package/dist/test/classdeclarations.test.d.ts +2 -0
  107. package/dist/test/classdeclarations.test.d.ts.map +1 -0
  108. package/dist/test/classdeclarations.test.js +397 -0
  109. package/dist/test/classinstantiations.test.d.ts +2 -0
  110. package/dist/test/classinstantiations.test.d.ts.map +1 -0
  111. package/dist/test/classinstantiations.test.js +168 -0
  112. package/dist/test/enums.test.d.ts +2 -0
  113. package/dist/test/enums.test.d.ts.map +1 -0
  114. package/dist/test/enums.test.js +211 -0
  115. package/dist/test/externals.test.d.ts +2 -0
  116. package/dist/test/externals.test.d.ts.map +1 -0
  117. package/dist/test/externals.test.js +219 -0
  118. package/dist/test/functioncallexpressions.test.d.ts +2 -0
  119. package/dist/test/functioncallexpressions.test.d.ts.map +1 -0
  120. package/dist/test/functioncallexpressions.test.js +156 -0
  121. package/dist/test/functiondeclaration.test.d.ts +2 -0
  122. package/dist/test/functiondeclaration.test.d.ts.map +1 -0
  123. package/dist/test/functiondeclaration.test.js +363 -0
  124. package/dist/test/imports.test.d.ts +2 -0
  125. package/dist/test/imports.test.d.ts.map +1 -0
  126. package/dist/test/imports.test.js +262 -0
  127. package/dist/test/memberexpressions.test.d.ts +2 -0
  128. package/dist/test/memberexpressions.test.d.ts.map +1 -0
  129. package/dist/test/memberexpressions.test.js +879 -0
  130. package/dist/test/namepolicies.test.d.ts +2 -0
  131. package/dist/test/namepolicies.test.d.ts.map +1 -0
  132. package/dist/test/namepolicies.test.js +109 -0
  133. package/dist/test/pydocs.test.d.ts +2 -0
  134. package/dist/test/pydocs.test.d.ts.map +1 -0
  135. package/dist/test/pydocs.test.js +500 -0
  136. package/dist/test/references.test.d.ts +2 -0
  137. package/dist/test/references.test.d.ts.map +1 -0
  138. package/dist/test/references.test.js +49 -0
  139. package/dist/test/sourcefiles.test.d.ts +2 -0
  140. package/dist/test/sourcefiles.test.d.ts.map +1 -0
  141. package/dist/test/sourcefiles.test.js +198 -0
  142. package/dist/test/utils.d.ts +23 -0
  143. package/dist/test/utils.d.ts.map +1 -0
  144. package/dist/test/utils.js +88 -0
  145. package/dist/test/values.test.d.ts +2 -0
  146. package/dist/test/values.test.d.ts.map +1 -0
  147. package/dist/test/values.test.js +78 -0
  148. package/dist/test/variables.test.d.ts +2 -0
  149. package/dist/test/variables.test.d.ts.map +1 -0
  150. package/dist/test/variables.test.js +173 -0
  151. package/dist/tsconfig.tsbuildinfo +1 -0
  152. package/package.json +39 -0
  153. package/src/builtins/python.ts +20 -0
  154. package/src/components/Atom.tsx +76 -0
  155. package/src/components/CallSignature.tsx +251 -0
  156. package/src/components/ClassDeclaration.tsx +98 -0
  157. package/src/components/ClassInstantiation.tsx +54 -0
  158. package/src/components/Declaration.tsx +91 -0
  159. package/src/components/EnumDeclaration.tsx +291 -0
  160. package/src/components/EnumMember.tsx +92 -0
  161. package/src/components/FunctionCallExpression.tsx +36 -0
  162. package/src/components/FunctionDeclaration.tsx +121 -0
  163. package/src/components/ImportStatement.tsx +134 -0
  164. package/src/components/MemberExpression.tsx +456 -0
  165. package/src/components/NoNamePolicy.tsx +31 -0
  166. package/src/components/PyDoc.tsx +331 -0
  167. package/src/components/PythonBlock.tsx +26 -0
  168. package/src/components/Reference.tsx +21 -0
  169. package/src/components/SourceFile.tsx +93 -0
  170. package/src/components/StatementList.tsx +28 -0
  171. package/src/components/VariableDeclaration.tsx +180 -0
  172. package/src/components/index.ts +18 -0
  173. package/src/create-module.ts +102 -0
  174. package/src/index.ts +7 -0
  175. package/src/name-policy.ts +101 -0
  176. package/src/parameter-descriptor.ts +36 -0
  177. package/src/symbol-creation.ts +36 -0
  178. package/src/symbols/custom-output-scope.ts +35 -0
  179. package/src/symbols/index.ts +6 -0
  180. package/src/symbols/python-member-scope.ts +12 -0
  181. package/src/symbols/python-module-scope.ts +89 -0
  182. package/src/symbols/python-output-symbol.ts +36 -0
  183. package/src/symbols/reference.ts +99 -0
  184. package/src/symbols/scopes.ts +9 -0
  185. package/src/utils.ts +27 -0
  186. package/temp/api.json +7207 -0
  187. package/test/callsignatures.test.tsx +256 -0
  188. package/test/classdeclarations.test.tsx +320 -0
  189. package/test/classinstantiations.test.tsx +159 -0
  190. package/test/enums.test.tsx +203 -0
  191. package/test/externals.test.tsx +190 -0
  192. package/test/functioncallexpressions.test.tsx +145 -0
  193. package/test/functiondeclaration.test.tsx +327 -0
  194. package/test/imports.test.tsx +214 -0
  195. package/test/memberexpressions.test.tsx +725 -0
  196. package/test/namepolicies.test.tsx +109 -0
  197. package/test/pydocs.test.tsx +528 -0
  198. package/test/references.test.tsx +36 -0
  199. package/test/sourcefiles.test.tsx +131 -0
  200. package/test/utils.tsx +131 -0
  201. package/test/values.test.tsx +61 -0
  202. package/test/variables.test.tsx +153 -0
  203. package/tsconfig.json +12 -0
  204. package/tsdoc-metadata.json +11 -0
  205. package/vitest.config.ts +10 -0
  206. package/vitest.setup.ts +1 -0
@@ -0,0 +1,198 @@
1
+ import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
+ import { d } from "@alloy-js/core/testing";
3
+ import { expect, it } from "vitest";
4
+ import * as py from "../src/index.js";
5
+ import { toSourceText } from "./utils.js";
6
+
7
+ /**
8
+ * toSourceText wraps the children in a SourceFile component
9
+ * and renders it to a string.
10
+ */
11
+ it("renders an empty source file", () => {
12
+ const result = toSourceText([]);
13
+ const expected = d`
14
+
15
+
16
+ `;
17
+ expect(result).toRenderTo(expected);
18
+ });
19
+ it("correct formatting of source file", () => {
20
+ const result = toSourceText([_$createComponent(py.ClassDeclaration, {
21
+ name: "someClass",
22
+ get children() {
23
+ return _$createComponent(py.StatementList, {
24
+ get children() {
25
+ return [_$createComponent(py.FunctionDeclaration, {
26
+ name: "someMethod",
27
+ returnType: "str",
28
+ get children() {
29
+ return _$createComponent(py.StatementList, {
30
+ get children() {
31
+ return [_$createComponent(py.VariableDeclaration, {
32
+ name: "x",
33
+ type: "int",
34
+ get initializer() {
35
+ return _$createComponent(py.Atom, {
36
+ jsValue: 42
37
+ });
38
+ }
39
+ }), _$createComponent(py.VariableDeclaration, {
40
+ name: "y",
41
+ type: "int",
42
+ get initializer() {
43
+ return _$createComponent(py.Atom, {
44
+ jsValue: 42
45
+ });
46
+ }
47
+ }), _$createComponent(py.FunctionCallExpression, {
48
+ target: "foo",
49
+ args: ["a", "b"]
50
+ }), _$createComponent(py.MemberExpression, {
51
+ get children() {
52
+ return [_$createComponent(py.MemberExpression.Part, {
53
+ id: "a"
54
+ }), _$createComponent(py.MemberExpression.Part, {
55
+ id: "b"
56
+ }), _$createComponent(py.MemberExpression.Part, {
57
+ key: "special-prop"
58
+ })];
59
+ }
60
+ }), _$createComponent(py.VariableDeclaration, {
61
+ name: "z",
62
+ type: "int",
63
+ get initializer() {
64
+ return _$createComponent(py.Atom, {
65
+ jsValue: 42
66
+ });
67
+ }
68
+ })];
69
+ }
70
+ });
71
+ }
72
+ }), _$createComponent(py.VariableDeclaration, {
73
+ name: "someVar",
74
+ type: "int",
75
+ get initializer() {
76
+ return _$createComponent(py.Atom, {
77
+ jsValue: 42
78
+ });
79
+ }
80
+ }), _$createComponent(py.FunctionDeclaration, {
81
+ name: "someOtherMethod",
82
+ returnType: "str"
83
+ }), _$createComponent(py.VariableDeclaration, {
84
+ name: "someOtherVar",
85
+ type: "int",
86
+ get initializer() {
87
+ return _$createComponent(py.Atom, {
88
+ jsValue: 42
89
+ });
90
+ }
91
+ })];
92
+ }
93
+ });
94
+ }
95
+ }), _$createComponent(py.FunctionDeclaration, {
96
+ name: "someFunction",
97
+ get children() {
98
+ return _$createComponent(py.StatementList, {
99
+ get children() {
100
+ return [_$createComponent(py.VariableDeclaration, {
101
+ name: "x",
102
+ type: "int",
103
+ get initializer() {
104
+ return _$createComponent(py.Atom, {
105
+ jsValue: 42
106
+ });
107
+ }
108
+ }), _$createComponent(py.VariableDeclaration, {
109
+ name: "y",
110
+ type: "int",
111
+ get initializer() {
112
+ return _$createComponent(py.Atom, {
113
+ jsValue: 42
114
+ });
115
+ }
116
+ }), _$createComponent(py.FunctionCallExpression, {
117
+ target: "foo",
118
+ args: ["a", "b"]
119
+ }), _$createComponent(py.MemberExpression, {
120
+ get children() {
121
+ return [_$createComponent(py.MemberExpression.Part, {
122
+ id: "a"
123
+ }), _$createComponent(py.MemberExpression.Part, {
124
+ id: "b"
125
+ }), _$createComponent(py.MemberExpression.Part, {
126
+ key: "special-prop"
127
+ })];
128
+ }
129
+ }), _$createComponent(py.VariableDeclaration, {
130
+ name: "z",
131
+ type: "int",
132
+ get initializer() {
133
+ return _$createComponent(py.Atom, {
134
+ jsValue: 42
135
+ });
136
+ }
137
+ })];
138
+ }
139
+ });
140
+ }
141
+ }), _$createComponent(py.ClassDeclaration, {
142
+ name: "someOtherClass",
143
+ get children() {
144
+ return _$createComponent(py.StatementList, {
145
+ get children() {
146
+ return _$createComponent(py.FunctionDeclaration, {
147
+ name: "someMethod",
148
+ returnType: "str"
149
+ });
150
+ }
151
+ });
152
+ }
153
+ }), _$createComponent(py.MemberExpression, {
154
+ get children() {
155
+ return [_$createComponent(py.MemberExpression.Part, {
156
+ id: "a"
157
+ }), _$createComponent(py.MemberExpression.Part, {
158
+ id: "b"
159
+ }), _$createComponent(py.MemberExpression.Part, {
160
+ key: "special-prop"
161
+ })];
162
+ }
163
+ })]);
164
+ const expected = d`
165
+ class SomeClass:
166
+ def some_method() -> str:
167
+ x: int = 42
168
+ y: int = 42
169
+ foo(a, b)
170
+ a.b["special-prop"]
171
+ z: int = 42
172
+
173
+ some_var: int = 42
174
+ def some_other_method() -> str:
175
+ pass
176
+
177
+ some_other_var: int = 42
178
+
179
+
180
+ def some_function():
181
+ x: int = 42
182
+ y: int = 42
183
+ foo(a, b)
184
+ a.b["special-prop"]
185
+ z: int = 42
186
+
187
+
188
+ class SomeOtherClass:
189
+ def some_method() -> str:
190
+ pass
191
+
192
+
193
+
194
+ a.b["special-prop"]
195
+
196
+ `;
197
+ expect(result).toRenderTo(expected);
198
+ });
@@ -0,0 +1,23 @@
1
+ import { Binder, Children, ContentOutputFile, NamePolicy, OutputDirectory, PrintTreeOptions, SymbolCreator } from "@alloy-js/core";
2
+ import { CustomOutputScope } from "../src/symbols/custom-output-scope.js";
3
+ import { PythonModuleScope } from "../src/symbols/index.js";
4
+ export declare function findFile(res: OutputDirectory, path: string): ContentOutputFile;
5
+ export declare function assertFileContents(res: OutputDirectory, expectedFiles: Record<string, string>): void;
6
+ export declare function toSourceTextMultiple(sourceFiles: Children[], { policy, externals, options, printOptions, }?: {
7
+ policy?: NamePolicy<string>;
8
+ externals?: SymbolCreator[];
9
+ options?: {
10
+ externals?: SymbolCreator[];
11
+ };
12
+ printOptions?: PrintTreeOptions;
13
+ }): OutputDirectory;
14
+ export declare function toSourceText(c: Children[], { policy, externals, options, printOptions, }?: {
15
+ policy?: NamePolicy<string>;
16
+ externals?: SymbolCreator[];
17
+ options?: {
18
+ externals?: SymbolCreator[];
19
+ };
20
+ printOptions?: PrintTreeOptions;
21
+ }): string;
22
+ export declare function createPythonModuleScope(name: string, parent: CustomOutputScope | undefined, binder?: Binder | undefined): PythonModuleScope;
23
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../test/utils.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EACN,QAAQ,EACR,iBAAiB,EACjB,UAAU,EAEV,eAAe,EAEf,gBAAgB,EAChB,aAAa,EAEd,MAAM,gBAAgB,CAAC;AAKxB,OAAO,EAAE,iBAAiB,EAAE,MAAM,uCAAuC,CAAC;AAC1E,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAE5D,wBAAgB,QAAQ,CACtB,GAAG,EAAE,eAAe,EACpB,IAAI,EAAE,MAAM,GACX,iBAAiB,CA2BnB;AAED,wBAAgB,kBAAkB,CAChC,GAAG,EAAE,eAAe,EACpB,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,QAMtC;AAED,wBAAgB,oBAAoB,CAClC,WAAW,EAAE,QAAQ,EAAE,EACvB,EACE,MAAM,EACN,SAAS,EACT,OAAO,EACP,YAAY,GACb,GAAE;IACD,MAAM,CAAC,EAAE,UAAU,CAAC,MAAM,CAAC,CAAC;IAC5B,SAAS,CAAC,EAAE,aAAa,EAAE,CAAC;IAC5B,OAAO,CAAC,EAAE;QAAE,SAAS,CAAC,EAAE,aAAa,EAAE,CAAA;KAAE,CAAC;IAC1C,YAAY,CAAC,EAAE,gBAAgB,CAAC;CAC5B,GACL,eAAe,CAmBjB;AAED,wBAAgB,YAAY,CAC1B,CAAC,EAAE,QAAQ,EAAE,EACb,EACE,MAAM,EACN,SAAS,EACT,OAAO,EACP,YAAY,GACb,GAAE;IACD,MAAM,CAAC,EAAE,UAAU,CAAC,MAAM,CAAC,CAAC;IAC5B,SAAS,CAAC,EAAE,aAAa,EAAE,CAAC;IAC5B,OAAO,CAAC,EAAE;QAAE,SAAS,CAAC,EAAE,aAAa,EAAE,CAAA;KAAE,CAAC;IAC1C,YAAY,CAAC,EAAE,gBAAgB,CAAC;CAC5B,GACL,MAAM,CAUR;AAGD,wBAAgB,uBAAuB,CACrC,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,iBAAiB,GAAG,SAAS,EACrC,MAAM,GAAE,MAAM,GAAG,SAAqB,GACrC,iBAAiB,CAKnB"}
@@ -0,0 +1,88 @@
1
+ import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
+ import { Output, render } from "@alloy-js/core";
3
+ import { dedent } from "@alloy-js/core/testing";
4
+ import { expect } from "vitest";
5
+ import * as py from "../src/components/index.js";
6
+ import { createPythonNamePolicy } from "../src/name-policy.js";
7
+ import { PythonModuleScope } from "../src/symbols/index.js";
8
+ export function findFile(res, path) {
9
+ const result = findFileWorker(res, path);
10
+ if (!result) {
11
+ throw new Error("Expected to find file " + path);
12
+ }
13
+ return result;
14
+ function findFileWorker(res, path) {
15
+ for (const item of res.contents) {
16
+ if (item.kind === "file") {
17
+ if (item.path === path) {
18
+ return item;
19
+ }
20
+ continue;
21
+ } else {
22
+ const found = findFileWorker(item, path);
23
+ if (found) {
24
+ return found;
25
+ }
26
+ }
27
+ }
28
+ return null;
29
+ }
30
+ }
31
+ export function assertFileContents(res, expectedFiles) {
32
+ for (const [path, contents] of Object.entries(expectedFiles)) {
33
+ const file = findFile(res, path);
34
+ expect(file.contents).toBe(dedent(contents));
35
+ }
36
+ }
37
+ export function toSourceTextMultiple(sourceFiles, {
38
+ policy,
39
+ externals,
40
+ options,
41
+ printOptions
42
+ } = {}) {
43
+ if (!policy) {
44
+ policy = createPythonNamePolicy();
45
+ }
46
+ const mergedExternals = options?.externals ?? externals;
47
+ if (printOptions === undefined) {
48
+ printOptions = {
49
+ printWidth: 80,
50
+ tabWidth: 4
51
+ };
52
+ } else {
53
+ printOptions.tabWidth = 4;
54
+ }
55
+ const content = _$createComponent(Output, {
56
+ externals: mergedExternals,
57
+ namePolicy: policy,
58
+ children: sourceFiles
59
+ });
60
+ return render(content, printOptions);
61
+ }
62
+ export function toSourceText(c, {
63
+ policy,
64
+ externals,
65
+ options,
66
+ printOptions
67
+ } = {}) {
68
+ const content = _$createComponent(py.SourceFile, {
69
+ path: "test.py",
70
+ children: c
71
+ });
72
+ const res = toSourceTextMultiple([content], {
73
+ policy,
74
+ externals,
75
+ options,
76
+ printOptions
77
+ });
78
+ const file = findFile(res, "test.py");
79
+ return file.contents;
80
+ }
81
+
82
+ // Helper function to create a PythonModuleScope to be used in tests
83
+ export function createPythonModuleScope(name, parent, binder = undefined) {
84
+ return new PythonModuleScope(name, {
85
+ parent: parent,
86
+ binder: binder
87
+ });
88
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=values.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"values.test.d.ts","sourceRoot":"","sources":["../../test/values.test.tsx"],"names":[],"mappings":""}
@@ -0,0 +1,78 @@
1
+ import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
+ import { describe, expect, it } from "vitest";
3
+ import * as py from "../src/index.js";
4
+ import { toSourceText } from "./utils.js";
5
+ describe("Atom", () => {
6
+ it("renders string value", () => {
7
+ expect(toSourceText([_$createComponent(py.Atom, {
8
+ jsValue: "Test"
9
+ })])).toRenderTo('"Test"');
10
+ });
11
+ it("renders null/undefined object", () => {
12
+ expect(toSourceText([_$createComponent(py.Atom, {
13
+ jsValue: undefined
14
+ })])).toRenderTo("None");
15
+ });
16
+ it("renders number", () => {
17
+ expect(toSourceText([_$createComponent(py.Atom, {
18
+ jsValue: 123
19
+ })])).toRenderTo("123");
20
+ });
21
+ it("renders boolean - True", () => {
22
+ expect(toSourceText([_$createComponent(py.Atom, {
23
+ jsValue: true
24
+ })])).toRenderTo("True");
25
+ });
26
+ it("renders boolean - False", () => {
27
+ expect(toSourceText([_$createComponent(py.Atom, {
28
+ jsValue: false
29
+ })])).toRenderTo("False");
30
+ });
31
+ it("renders array", () => {
32
+ expect(toSourceText([_$createComponent(py.Atom, {
33
+ jsValue: [1, 2, 3]
34
+ })])).toRenderTo("[1, 2, 3]");
35
+ });
36
+ it("renders object", () => {
37
+ expect(toSourceText([_$createComponent(py.Atom, {
38
+ jsValue: {
39
+ a: 1,
40
+ b: 2
41
+ }
42
+ })])).toRenderTo('{"a": 1, "b": 2}');
43
+ });
44
+ it("renders more complex object", () => {
45
+ expect(toSourceText([_$createComponent(py.Atom, {
46
+ jsValue: {
47
+ a: "1",
48
+ b: 2,
49
+ c: true
50
+ }
51
+ })])).toRenderTo('{"a": "1", "b": 2, "c": True}');
52
+ });
53
+ it("renders empty object", () => {
54
+ expect(toSourceText([_$createComponent(py.Atom, {
55
+ jsValue: {}
56
+ })])).toRenderTo("{}");
57
+ });
58
+ it("renders function", () => {
59
+ function Test() {
60
+ return ["Test"];
61
+ }
62
+ expect(toSourceText([_$createComponent(py.Atom, {
63
+ jsValue: Test
64
+ })])).toRenderTo("Test");
65
+ });
66
+ it("renders nested object", () => {
67
+ expect(toSourceText([_$createComponent(py.Atom, {
68
+ jsValue: {
69
+ a: {
70
+ b: {
71
+ c: 1
72
+ }
73
+ },
74
+ d: 2
75
+ }
76
+ })])).toRenderTo('{"a": {"b": {"c": 1}}, "d": 2}');
77
+ });
78
+ });
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=variables.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"variables.test.d.ts","sourceRoot":"","sources":["../../test/variables.test.tsx"],"names":[],"mappings":""}
@@ -0,0 +1,173 @@
1
+ import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
+ import { refkey } from "@alloy-js/core";
3
+ import { d } from "@alloy-js/core/testing";
4
+ import { describe, expect, it } from "vitest";
5
+ import * as py from "../src/index.js";
6
+ import { assertFileContents, toSourceText, toSourceTextMultiple } from "./utils.js";
7
+ describe("Python Variable", () => {
8
+ it("declares a python variable", () => {
9
+ const res = toSourceText([_$createComponent(py.VariableDeclaration, {
10
+ name: "myVar",
11
+ type: "int",
12
+ initializer: 42
13
+ })]);
14
+ expect(res).toBe(`my_var: int = 42`);
15
+ });
16
+ it("declares a python variable without value", () => {
17
+ const res = toSourceText([_$createComponent(py.VariableDeclaration, {
18
+ name: "myVar",
19
+ type: "int",
20
+ omitNone: true
21
+ })]);
22
+ expect(res).toBe(`my_var: int`);
23
+ });
24
+ it("declares a python variable without typeAnnotations", () => {
25
+ const res = toSourceText([_$createComponent(py.VariableDeclaration, {
26
+ name: "myVar",
27
+ initializer: 42
28
+ })]);
29
+ expect(res).toBe(`my_var = 42`);
30
+ });
31
+ it("declares a python variable as None when undefined", () => {
32
+ const res = toSourceText([_$createComponent(py.VariableDeclaration, {
33
+ name: "myVar"
34
+ })]);
35
+ expect(res).toBe(`my_var = None`);
36
+ });
37
+ it("declares a python variable as None when null", () => {
38
+ const res = toSourceText([_$createComponent(py.VariableDeclaration, {
39
+ name: "myVar",
40
+ get initializer() {
41
+ return _$createComponent(py.Atom, {
42
+ jsValue: null
43
+ });
44
+ }
45
+ })]);
46
+ expect(res).toBe(`my_var = None`);
47
+ });
48
+ it("declares a python variable that's an array", () => {
49
+ const res = toSourceText([_$createComponent(py.VariableDeclaration, {
50
+ name: "numbers",
51
+ type: "list[int]",
52
+ get initializer() {
53
+ return _$createComponent(py.Atom, {
54
+ jsValue: [1, 2, 3]
55
+ });
56
+ }
57
+ })]);
58
+ expect(res).toBe(`numbers: list[int] = [1, 2, 3]`);
59
+ });
60
+ it("declares a python variable with a python value", () => {
61
+ const res = toSourceText([_$createComponent(py.VariableDeclaration, {
62
+ name: "nameIdPairs",
63
+ get initializer() {
64
+ return _$createComponent(py.Atom, {
65
+ jsValue: {
66
+ John: 123,
67
+ Doe: 234
68
+ }
69
+ });
70
+ }
71
+ })]);
72
+ expect(res).toBe(`name_id_pairs = {"John": 123, "Doe": 234}`);
73
+ });
74
+ it("declares a python variable with omitNone", () => {
75
+ const res = toSourceText([_$createComponent(py.VariableDeclaration, {
76
+ name: "omitNoneVar",
77
+ type: "int",
78
+ omitNone: true
79
+ })]);
80
+ expect(res).toBe(`omit_none_var: int`);
81
+ });
82
+ it("declares a call statement python variable", () => {
83
+ const res = toSourceText([_$createComponent(py.VariableDeclaration, {
84
+ name: "callStmtVar",
85
+ initializer: 12,
86
+ callStatementVar: true
87
+ })]);
88
+ expect(res).toBe(`call_stmt_var=12`);
89
+ });
90
+ it("declares a call statement python variable without name", () => {
91
+ const res = toSourceText([_$createComponent(py.VariableDeclaration, {
92
+ name: "",
93
+ initializer: 12,
94
+ callStatementVar: true
95
+ })]);
96
+ expect(res).toBe(`12`);
97
+ });
98
+ it("declares a python variable with a class type", () => {
99
+ const res = toSourceText([_$createComponent(py.StatementList, {
100
+ get children() {
101
+ return [_$createComponent(py.ClassDeclaration, {
102
+ name: "MyClass"
103
+ }), _$createComponent(py.VariableDeclaration, {
104
+ name: "my_var",
105
+ get type() {
106
+ return _$createComponent(py.Reference, {
107
+ get refkey() {
108
+ return refkey("MyClass");
109
+ }
110
+ });
111
+ }
112
+ })];
113
+ }
114
+ })]);
115
+ expect(res).toBe(d`
116
+ class MyClass:
117
+ pass
118
+
119
+ my_var: MyClass = None`);
120
+ });
121
+ it("declares a python variable with a class type from a different module", () => {
122
+ const res = toSourceTextMultiple([_$createComponent(py.SourceFile, {
123
+ path: "classes.py",
124
+ get children() {
125
+ return _$createComponent(py.ClassDeclaration, {
126
+ name: "MyClass"
127
+ });
128
+ }
129
+ }), _$createComponent(py.SourceFile, {
130
+ path: "usage.py",
131
+ get children() {
132
+ return _$createComponent(py.VariableDeclaration, {
133
+ name: "my_var",
134
+ get type() {
135
+ return _$createComponent(py.Reference, {
136
+ get refkey() {
137
+ return refkey("MyClass");
138
+ }
139
+ });
140
+ }
141
+ });
142
+ }
143
+ })]);
144
+ assertFileContents(res, {
145
+ "classes.py": `
146
+ class MyClass:
147
+ pass
148
+
149
+ `,
150
+ "usage.py": `
151
+ from classes import MyClass
152
+
153
+ my_var: MyClass = None
154
+ `
155
+ });
156
+ });
157
+ it("declares a python variable receiving other variable as value", () => {
158
+ const res = toSourceText([_$createComponent(py.StatementList, {
159
+ get children() {
160
+ return [_$createComponent(py.VariableDeclaration, {
161
+ name: "my_var",
162
+ initializer: 42
163
+ }), _$createComponent(py.VariableDeclaration, {
164
+ name: "my_other_var",
165
+ get initializer() {
166
+ return refkey("my_var");
167
+ }
168
+ })];
169
+ }
170
+ })]);
171
+ expect(res).toBe(`my_var = 42\nmy_other_var = my_var`);
172
+ });
173
+ });