@alloy-js/python 0.5.0-dev.1 → 0.5.0

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 (217) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/dev/src/components/CallSignature.js +2 -2
  3. package/dist/dev/src/components/CallSignature.js.map +1 -1
  4. package/dist/dev/src/components/ConstructorDeclaration.js +1 -1
  5. package/dist/dev/src/components/ConstructorDeclaration.js.map +1 -1
  6. package/dist/dev/src/components/DataclassDeclaration.js +5 -5
  7. package/dist/dev/src/components/DataclassDeclaration.js.map +1 -1
  8. package/dist/dev/src/components/FunctionBase.js +9 -9
  9. package/dist/dev/src/components/FunctionBase.js.map +1 -1
  10. package/dist/dev/src/components/PropertyDeclaration.js +8 -8
  11. package/dist/dev/src/components/PropertyDeclaration.js.map +1 -1
  12. package/dist/dev/src/components/PyDoc.js +64 -64
  13. package/dist/dev/src/components/PyDoc.js.map +1 -1
  14. package/dist/dev/src/components/PydanticClassDeclaration.js +5 -5
  15. package/dist/dev/src/components/PydanticClassDeclaration.js.map +1 -1
  16. package/dist/dev/src/components/SourceFile.js +44 -32
  17. package/dist/dev/src/components/SourceFile.js.map +1 -1
  18. package/dist/dev/src/symbols/python-output-symbol.js.map +1 -1
  19. package/dist/dev/test/callsignatures.test.js +471 -297
  20. package/dist/dev/test/callsignatures.test.js.map +1 -1
  21. package/dist/dev/test/class-method-declaration.test.js +21 -10
  22. package/dist/dev/test/class-method-declaration.test.js.map +1 -1
  23. package/dist/dev/test/classdeclarations.test.js +459 -393
  24. package/dist/dev/test/classdeclarations.test.js.map +1 -1
  25. package/dist/dev/test/classinstantiations.test.js +201 -168
  26. package/dist/dev/test/classinstantiations.test.js.map +1 -1
  27. package/dist/dev/test/constructordeclaration.test.js +22 -11
  28. package/dist/dev/test/constructordeclaration.test.js.map +1 -1
  29. package/dist/dev/test/dataclassdeclarations.test.js +322 -368
  30. package/dist/dev/test/dataclassdeclarations.test.js.map +1 -1
  31. package/dist/dev/test/decoratorlist.test.js +96 -49
  32. package/dist/dev/test/decoratorlist.test.js.map +1 -1
  33. package/dist/dev/test/dundermethoddeclaration.test.js +22 -11
  34. package/dist/dev/test/dundermethoddeclaration.test.js.map +1 -1
  35. package/dist/dev/test/enums.test.js +218 -184
  36. package/dist/dev/test/enums.test.js.map +1 -1
  37. package/dist/dev/test/externals.test.js +57 -45
  38. package/dist/dev/test/externals.test.js.map +1 -1
  39. package/dist/dev/test/factories.test.js +124 -50
  40. package/dist/dev/test/factories.test.js.map +1 -1
  41. package/dist/dev/test/functioncallexpressions.test.js +199 -164
  42. package/dist/dev/test/functioncallexpressions.test.js.map +1 -1
  43. package/dist/dev/test/functiondeclaration.test.js +439 -272
  44. package/dist/dev/test/functiondeclaration.test.js.map +1 -1
  45. package/dist/dev/test/imports.test.js +273 -221
  46. package/dist/dev/test/imports.test.js.map +1 -1
  47. package/dist/dev/test/memberexpressions.test.js +1237 -972
  48. package/dist/dev/test/memberexpressions.test.js.map +1 -1
  49. package/dist/dev/test/methoddeclaration.test.js +142 -78
  50. package/dist/dev/test/methoddeclaration.test.js.map +1 -1
  51. package/dist/dev/test/namepolicies.test.js +130 -94
  52. package/dist/dev/test/namepolicies.test.js.map +1 -1
  53. package/dist/dev/test/propertydeclaration.test.js +88 -59
  54. package/dist/dev/test/propertydeclaration.test.js.map +1 -1
  55. package/dist/dev/test/pydanticclassdeclarations.test.js +299 -347
  56. package/dist/dev/test/pydanticclassdeclarations.test.js.map +1 -1
  57. package/dist/dev/test/pydocs.test.js +888 -715
  58. package/dist/dev/test/pydocs.test.js.map +1 -1
  59. package/dist/dev/test/references.test.js +42 -35
  60. package/dist/dev/test/references.test.js.map +1 -1
  61. package/dist/dev/test/sourcefiles.test.js +1109 -841
  62. package/dist/dev/test/sourcefiles.test.js.map +1 -1
  63. package/dist/dev/test/staticmethoddeclaration.test.js +21 -10
  64. package/dist/dev/test/staticmethoddeclaration.test.js.map +1 -1
  65. package/dist/dev/test/type-checking-imports.test.js +408 -359
  66. package/dist/dev/test/type-checking-imports.test.js.map +1 -1
  67. package/dist/dev/test/typereference.test.js +55 -40
  68. package/dist/dev/test/typereference.test.js.map +1 -1
  69. package/dist/dev/test/uniontypeexpression.test.js +222 -146
  70. package/dist/dev/test/uniontypeexpression.test.js.map +1 -1
  71. package/dist/dev/test/utils.js +39 -77
  72. package/dist/dev/test/utils.js.map +1 -1
  73. package/dist/dev/test/values.test.js +237 -101
  74. package/dist/dev/test/values.test.js.map +1 -1
  75. package/dist/dev/test/variables.test.js +321 -203
  76. package/dist/dev/test/variables.test.js.map +1 -1
  77. package/dist/dev/test/vitest.setup.js +2 -0
  78. package/dist/dev/test/vitest.setup.js.map +1 -0
  79. package/dist/src/components/CallSignature.d.ts.map +1 -1
  80. package/dist/src/components/CallSignature.js.map +1 -1
  81. package/dist/src/components/ConstructorDeclaration.d.ts.map +1 -1
  82. package/dist/src/components/ConstructorDeclaration.js.map +1 -1
  83. package/dist/src/components/DataclassDeclaration.d.ts.map +1 -1
  84. package/dist/src/components/DataclassDeclaration.js.map +1 -1
  85. package/dist/src/components/FunctionBase.d.ts.map +1 -1
  86. package/dist/src/components/FunctionBase.js.map +1 -1
  87. package/dist/src/components/PropertyDeclaration.d.ts.map +1 -1
  88. package/dist/src/components/PropertyDeclaration.js.map +1 -1
  89. package/dist/src/components/PyDoc.d.ts.map +1 -1
  90. package/dist/src/components/PyDoc.js.map +1 -1
  91. package/dist/src/components/PydanticClassDeclaration.d.ts.map +1 -1
  92. package/dist/src/components/PydanticClassDeclaration.js.map +1 -1
  93. package/dist/src/components/SourceFile.d.ts +2 -2
  94. package/dist/src/components/SourceFile.d.ts.map +1 -1
  95. package/dist/src/components/SourceFile.js +12 -0
  96. package/dist/src/components/SourceFile.js.map +1 -1
  97. package/dist/src/symbols/python-output-symbol.d.ts.map +1 -1
  98. package/dist/src/symbols/python-output-symbol.js.map +1 -1
  99. package/dist/test/callsignatures.test.js +346 -272
  100. package/dist/test/callsignatures.test.js.map +1 -1
  101. package/dist/test/class-method-declaration.test.js +7 -4
  102. package/dist/test/class-method-declaration.test.js.map +1 -1
  103. package/dist/test/classdeclarations.test.js +302 -288
  104. package/dist/test/classdeclarations.test.js.map +1 -1
  105. package/dist/test/classinstantiations.test.js +112 -103
  106. package/dist/test/classinstantiations.test.js.map +1 -1
  107. package/dist/test/constructordeclaration.test.js +7 -4
  108. package/dist/test/constructordeclaration.test.js.map +1 -1
  109. package/dist/test/dataclassdeclarations.test.js +134 -184
  110. package/dist/test/dataclassdeclarations.test.js.map +1 -1
  111. package/dist/test/decoratorlist.test.js +59 -36
  112. package/dist/test/decoratorlist.test.js.map +1 -1
  113. package/dist/test/dundermethoddeclaration.test.js +7 -4
  114. package/dist/test/dundermethoddeclaration.test.js.map +1 -1
  115. package/dist/test/enums.test.js +161 -159
  116. package/dist/test/enums.test.js.map +1 -1
  117. package/dist/test/externals.test.js +24 -24
  118. package/dist/test/externals.test.js.map +1 -1
  119. package/dist/test/factories.test.js +75 -33
  120. package/dist/test/factories.test.js.map +1 -1
  121. package/dist/test/functioncallexpressions.test.js +117 -106
  122. package/dist/test/functioncallexpressions.test.js.map +1 -1
  123. package/dist/test/functiondeclaration.test.js +247 -180
  124. package/dist/test/functiondeclaration.test.js.map +1 -1
  125. package/dist/test/imports.test.js +171 -143
  126. package/dist/test/imports.test.js.map +1 -1
  127. package/dist/test/memberexpressions.test.js +582 -453
  128. package/dist/test/memberexpressions.test.js.map +1 -1
  129. package/dist/test/methoddeclaration.test.js +66 -46
  130. package/dist/test/methoddeclaration.test.js.map +1 -1
  131. package/dist/test/namepolicies.test.js +90 -78
  132. package/dist/test/namepolicies.test.js.map +1 -1
  133. package/dist/test/propertydeclaration.test.js +25 -20
  134. package/dist/test/propertydeclaration.test.js.map +1 -1
  135. package/dist/test/pydanticclassdeclarations.test.js +134 -190
  136. package/dist/test/pydanticclassdeclarations.test.js.map +1 -1
  137. package/dist/test/pydocs.test.js +573 -532
  138. package/dist/test/pydocs.test.js.map +1 -1
  139. package/dist/test/references.test.js +31 -28
  140. package/dist/test/references.test.js.map +1 -1
  141. package/dist/test/sourcefiles.test.js +700 -580
  142. package/dist/test/sourcefiles.test.js.map +1 -1
  143. package/dist/test/staticmethoddeclaration.test.js +7 -4
  144. package/dist/test/staticmethoddeclaration.test.js.map +1 -1
  145. package/dist/test/type-checking-imports.test.js +297 -284
  146. package/dist/test/type-checking-imports.test.js.map +1 -1
  147. package/dist/test/typereference.test.js +29 -22
  148. package/dist/test/typereference.test.js.map +1 -1
  149. package/dist/test/uniontypeexpression.test.js +124 -88
  150. package/dist/test/uniontypeexpression.test.js.map +1 -1
  151. package/dist/test/utils.d.ts +10 -17
  152. package/dist/test/utils.d.ts.map +1 -1
  153. package/dist/test/utils.js +32 -74
  154. package/dist/test/utils.js.map +1 -1
  155. package/dist/test/values.test.js +135 -67
  156. package/dist/test/values.test.js.map +1 -1
  157. package/dist/test/variables.test.js +201 -151
  158. package/dist/test/variables.test.js.map +1 -1
  159. package/dist/test/vitest.setup.d.ts +2 -0
  160. package/dist/test/vitest.setup.d.ts.map +1 -0
  161. package/dist/test/vitest.setup.js +2 -0
  162. package/dist/test/vitest.setup.js.map +1 -0
  163. package/dist/tsconfig.tsbuildinfo +1 -1
  164. package/docs/api/components/FunctionalEnumDeclaration.md +3 -6
  165. package/docs/api/components/MemberExpression.md +1 -5
  166. package/docs/api/components/SourceFile.md +20 -8
  167. package/docs/api/components/index.md +0 -2
  168. package/docs/api/functions/isTypeRefContext.md +1 -1
  169. package/docs/api/index.md +2 -2
  170. package/docs/api/types/ReferenceProps.md +7 -0
  171. package/docs/api/types/TypeRefContextProps.md +7 -0
  172. package/docs/api/types/index.md +2 -0
  173. package/package.json +11 -10
  174. package/src/components/CallSignature.tsx +4 -2
  175. package/src/components/ConstructorDeclaration.tsx +4 -2
  176. package/src/components/DataclassDeclaration.tsx +1 -2
  177. package/src/components/FunctionBase.tsx +1 -2
  178. package/src/components/PropertyDeclaration.tsx +8 -4
  179. package/src/components/PyDoc.tsx +12 -6
  180. package/src/components/PydanticClassDeclaration.tsx +1 -2
  181. package/src/components/SourceFile.tsx +6 -1
  182. package/src/symbols/python-output-symbol.ts +1 -2
  183. package/temp/api.json +107 -61
  184. package/test/callsignatures.test.tsx +309 -283
  185. package/test/class-method-declaration.test.tsx +3 -4
  186. package/test/classdeclarations.test.tsx +263 -248
  187. package/test/classinstantiations.test.tsx +115 -109
  188. package/test/constructordeclaration.test.tsx +9 -6
  189. package/test/dataclassdeclarations.test.tsx +243 -361
  190. package/test/decoratorlist.test.tsx +78 -59
  191. package/test/dundermethoddeclaration.test.tsx +3 -4
  192. package/test/enums.test.tsx +65 -81
  193. package/test/externals.test.tsx +25 -25
  194. package/test/factories.test.tsx +64 -22
  195. package/test/functioncallexpressions.test.tsx +123 -109
  196. package/test/functiondeclaration.test.tsx +209 -148
  197. package/test/imports.test.tsx +119 -91
  198. package/test/memberexpressions.test.tsx +265 -207
  199. package/test/methoddeclaration.test.tsx +84 -63
  200. package/test/namepolicies.test.tsx +69 -69
  201. package/test/propertydeclaration.test.tsx +7 -8
  202. package/test/pydanticclassdeclarations.test.tsx +355 -487
  203. package/test/pydocs.test.tsx +531 -579
  204. package/test/references.test.tsx +24 -23
  205. package/test/sourcefiles.test.tsx +527 -492
  206. package/test/staticmethoddeclaration.test.tsx +3 -4
  207. package/test/type-checking-imports.test.tsx +206 -218
  208. package/test/typereference.test.tsx +15 -12
  209. package/test/uniontypeexpression.test.tsx +74 -61
  210. package/test/utils.tsx +26 -110
  211. package/test/values.test.tsx +82 -32
  212. package/test/variables.test.tsx +162 -142
  213. package/test/vitest.setup.ts +1 -0
  214. package/tsdoc-metadata.json +1 -1
  215. package/vitest.config.ts +4 -0
  216. package/docs/api/components/Reference.md +0 -31
  217. package/docs/api/components/TypeRefContext.md +0 -41
@@ -1,27 +1,28 @@
1
1
  import { refkey } from "@alloy-js/core";
2
- import { d } from "@alloy-js/core/testing";
3
2
  import { describe, expect, it } from "vitest";
4
3
  import * as py from "../src/index.js";
5
- import {
6
- assertFileContents,
7
- toSourceText,
8
- toSourceTextMultiple,
9
- } from "./utils.jsx";
4
+ import { TestOutput, TestOutputDirectory } from "./utils.js";
10
5
 
11
6
  describe("UnionTypeExpression", () => {
12
7
  it("renders a Python union expression - 1 item", () => {
13
8
  expect(
14
- toSourceText([<py.UnionTypeExpression children={["int"]} />]),
9
+ <TestOutput>
10
+ <py.UnionTypeExpression children={["int"]} />
11
+ </TestOutput>,
15
12
  ).toRenderTo("int");
16
13
  });
14
+
17
15
  it("renders a Python union expression - 2 items", () => {
18
16
  expect(
19
- toSourceText([<py.UnionTypeExpression children={["int", "str"]} />]),
17
+ <TestOutput>
18
+ <py.UnionTypeExpression children={["int", "str"]} />
19
+ </TestOutput>,
20
20
  ).toRenderTo("int | str");
21
21
  });
22
+
22
23
  it("renders a Python union expression - N items", () => {
23
24
  expect(
24
- toSourceText([
25
+ <TestOutput>
25
26
  <py.UnionTypeExpression
26
27
  children={[
27
28
  "int",
@@ -38,9 +39,10 @@ describe("UnionTypeExpression", () => {
38
39
  "memoryview",
39
40
  "complex",
40
41
  ]}
41
- />,
42
- ]),
43
- ).toRenderTo(d`
42
+ />
43
+ </TestOutput>,
44
+ ).toRenderTo(
45
+ `
44
46
  (
45
47
  int
46
48
  | str
@@ -55,31 +57,36 @@ describe("UnionTypeExpression", () => {
55
57
  | bytearray
56
58
  | memoryview
57
59
  | complex
58
- )`);
60
+ )`,
61
+ );
59
62
  });
63
+
60
64
  it("renders a Python union expression - 2 items again", () => {
61
65
  expect(
62
- toSourceText([<py.UnionTypeExpression children={["int", "str"]} />]),
66
+ <TestOutput>
67
+ <py.UnionTypeExpression children={["int", "str"]} />
68
+ </TestOutput>,
63
69
  ).toRenderTo("int | str");
64
70
  });
71
+
65
72
  it("renders a Python union expression - 2 items with None", () => {
66
73
  expect(
67
- toSourceText([
68
- <py.UnionTypeExpression children={["int", "str", "None"]} />,
69
- ]),
74
+ <TestOutput>
75
+ <py.UnionTypeExpression children={["int", "str", "None"]} />
76
+ </TestOutput>,
70
77
  ).toRenderTo("int | str | None");
71
78
  });
72
79
 
73
80
  it("renders a Python union with generic types", () => {
74
81
  expect(
75
- toSourceText([
82
+ <TestOutput>
76
83
  <py.UnionTypeExpression
77
84
  children={[
78
85
  <py.TypeReference name="list" typeArgs={["int"]} />,
79
86
  <py.TypeReference name="dict" typeArgs={["str", "int"]} />,
80
87
  ]}
81
- />,
82
- ]),
88
+ />
89
+ </TestOutput>,
83
90
  ).toRenderTo("list[int] | dict[str, int]");
84
91
  });
85
92
 
@@ -88,7 +95,7 @@ describe("UnionTypeExpression", () => {
88
95
  const otherClassRefkey = refkey();
89
96
 
90
97
  expect(
91
- toSourceText([
98
+ <TestOutput>
92
99
  <py.StatementList>
93
100
  <py.ClassDeclaration
94
101
  name="Bar"
@@ -104,9 +111,10 @@ describe("UnionTypeExpression", () => {
104
111
  <py.Reference refkey={otherClassRefkey} />,
105
112
  ]}
106
113
  />
107
- </py.StatementList>,
108
- ]),
109
- ).toRenderTo(d`
114
+ </py.StatementList>
115
+ </TestOutput>,
116
+ ).toRenderTo(
117
+ `
110
118
  class Bar:
111
119
  pass
112
120
 
@@ -114,38 +122,40 @@ describe("UnionTypeExpression", () => {
114
122
  pass
115
123
 
116
124
  Bar | Foo
117
- `);
125
+ `,
126
+ );
118
127
  });
119
128
 
120
129
  it("emits import for TypeReference with refkey and typeArgs across files", () => {
121
130
  const classRefkey = refkey();
122
- const res = toSourceTextMultiple([
123
- <py.SourceFile path="defs.py">
124
- <py.ClassDeclaration name="Bar" refkey={classRefkey} />
125
- </py.SourceFile>,
126
- <py.SourceFile path="use.py">
127
- <py.StatementList>
128
- <py.VariableDeclaration
129
- name="v"
130
- type={<py.TypeReference refkey={classRefkey} typeArgs={["T"]} />}
131
- />
132
- </py.StatementList>
133
- </py.SourceFile>,
134
- ]);
135
- assertFileContents(res, {
131
+ expect(
132
+ <TestOutputDirectory>
133
+ <py.SourceFile path="defs.py">
134
+ <py.ClassDeclaration name="Bar" refkey={classRefkey} />
135
+ </py.SourceFile>
136
+ <py.SourceFile path="use.py">
137
+ <py.StatementList>
138
+ <py.VariableDeclaration
139
+ name="v"
140
+ type={<py.TypeReference refkey={classRefkey} typeArgs={["T"]} />}
141
+ />
142
+ </py.StatementList>
143
+ </py.SourceFile>
144
+ </TestOutputDirectory>,
145
+ ).toRenderTo({
136
146
  "defs.py": `
137
- class Bar:
138
- pass
147
+ class Bar:
148
+ pass
139
149
 
140
- `,
150
+ `,
141
151
  "use.py": `
142
- from typing import TYPE_CHECKING
152
+ from typing import TYPE_CHECKING
143
153
 
144
- if TYPE_CHECKING:
145
- from defs import Bar
154
+ if TYPE_CHECKING:
155
+ from defs import Bar
146
156
 
147
- v: Bar[T] = None
148
- `,
157
+ v: Bar[T] = None
158
+ `,
149
159
  });
150
160
  });
151
161
  });
@@ -159,11 +169,11 @@ describe("TypeExpression in different scenarios", () => {
159
169
  />
160
170
  );
161
171
  expect(
162
- toSourceText([
172
+ <TestOutput>
163
173
  <py.ClassDeclaration
164
174
  name="Foo"
165
175
  refkey={classRefkey}
166
- ></py.ClassDeclaration>,
176
+ ></py.ClassDeclaration>
167
177
  <py.FunctionDeclaration
168
178
  name="fooFunction"
169
179
  parameters={[
@@ -175,9 +185,10 @@ describe("TypeExpression in different scenarios", () => {
175
185
  args={true}
176
186
  kwargs={true}
177
187
  returnType={type}
178
- />,
179
- ]),
180
- ).toRenderTo(d`
188
+ />
189
+ </TestOutput>,
190
+ ).toRenderTo(
191
+ `
181
192
  class Foo:
182
193
  pass
183
194
 
@@ -185,9 +196,10 @@ describe("TypeExpression in different scenarios", () => {
185
196
  def foo_function(x: int | str | Foo, *args, **kwargs) -> int | str | Foo:
186
197
  pass
187
198
 
188
-
189
- `);
199
+ `,
200
+ );
190
201
  });
202
+
191
203
  it("renders an UnionTypeExpression as a variable type", () => {
192
204
  const classRefkey = refkey();
193
205
  const type = (
@@ -196,20 +208,21 @@ describe("TypeExpression in different scenarios", () => {
196
208
  />
197
209
  );
198
210
  expect(
199
- toSourceText([
211
+ <TestOutput>
200
212
  <py.ClassDeclaration
201
213
  name="Foo"
202
214
  refkey={classRefkey}
203
- ></py.ClassDeclaration>,
204
- <py.VariableDeclaration name="fooVariable" type={type} />,
205
- ]),
206
- ).toRenderTo(d`
215
+ ></py.ClassDeclaration>
216
+ <py.VariableDeclaration name="fooVariable" type={type} />
217
+ </TestOutput>,
218
+ ).toRenderTo(
219
+ `
207
220
  class Foo:
208
221
  pass
209
222
 
210
223
 
211
224
  foo_variable: int | str | Foo = None
212
-
213
- `);
225
+ `,
226
+ );
214
227
  });
215
228
  });
package/test/utils.tsx CHANGED
@@ -1,133 +1,49 @@
1
1
  import {
2
2
  Binder,
3
3
  Children,
4
- ContentOutputFile,
5
4
  NamePolicy,
6
5
  Output,
7
- OutputDirectory,
8
- OutputFile,
9
6
  OutputScope,
10
- PrintTreeOptions,
11
7
  SymbolCreator,
12
- render,
13
8
  } from "@alloy-js/core";
14
- import { dedent } from "@alloy-js/core/testing";
15
- import { expect } from "vitest";
16
9
  import * as py from "../src/components/index.js";
17
10
  import { pythonNameConflictResolver } from "../src/name-conflict-resolver.js";
18
11
  import { createPythonNamePolicy } from "../src/name-policy.js";
19
12
  import { PythonModuleScope } from "../src/symbols/index.js";
20
13
 
21
- export function findFile(
22
- res: OutputDirectory,
23
- path: string,
24
- ): ContentOutputFile {
25
- const result = findFileWorker(res, path);
26
-
27
- if (!result) {
28
- throw new Error("Expected to find file " + path);
29
- }
30
- return result as ContentOutputFile;
31
-
32
- function findFileWorker(
33
- res: OutputDirectory,
34
- path: string,
35
- ): OutputFile | null {
36
- for (const item of res.contents) {
37
- if (item.kind === "file") {
38
- if (item.path === path) {
39
- return item;
40
- }
41
- continue;
42
- } else {
43
- const found = findFileWorker(item, path);
44
- if (found) {
45
- return found;
46
- }
47
- }
48
- }
49
- return null;
50
- }
51
- }
52
-
53
- export function assertFileContents(
54
- res: OutputDirectory,
55
- expectedFiles: Record<string, string>,
56
- ) {
57
- for (const [path, contents] of Object.entries(expectedFiles)) {
58
- const file = findFile(res, path);
59
- expect(file.contents).toBe(dedent(contents));
60
- }
61
- }
62
-
63
- export function toSourceTextMultiple(
64
- sourceFiles: Children[],
65
- {
66
- policy,
67
- externals,
68
- options,
69
- printOptions,
70
- }: {
71
- policy?: NamePolicy<string>;
72
- externals?: SymbolCreator[];
73
- options?: { externals?: SymbolCreator[] };
74
- printOptions?: PrintTreeOptions;
75
- } = {},
76
- ): OutputDirectory {
77
- if (!policy) {
78
- policy = createPythonNamePolicy();
79
- }
80
- const mergedExternals = options?.externals ?? externals;
81
- if (printOptions === undefined) {
82
- printOptions = {
83
- printWidth: 80,
84
- tabWidth: 4,
85
- insertFinalNewLine: false,
86
- };
87
- } else {
88
- printOptions.insertFinalNewLine = false;
89
- printOptions.tabWidth = 4;
90
- }
91
- const content = (
14
+ export function TestOutput(props: {
15
+ children?: Children;
16
+ externals?: SymbolCreator[];
17
+ namePolicy?: NamePolicy<string>;
18
+ path?: string;
19
+ }) {
20
+ return (
92
21
  <Output
93
- externals={mergedExternals}
94
- namePolicy={policy}
22
+ externals={props.externals}
23
+ namePolicy={props.namePolicy ?? createPythonNamePolicy()}
95
24
  nameConflictResolver={pythonNameConflictResolver}
96
25
  >
97
- {sourceFiles}
26
+ <py.SourceFile path={props.path ?? "test.py"} printWidth={80}>
27
+ {props.children}
28
+ </py.SourceFile>
98
29
  </Output>
99
30
  );
100
- return render(content, printOptions);
101
31
  }
102
32
 
103
- export function toSourceText(
104
- content: Children | Children[],
105
- {
106
- policy,
107
- externals,
108
- options,
109
- printOptions,
110
- }: {
111
- policy?: NamePolicy<string>;
112
- externals?: SymbolCreator[];
113
- options?: { externals?: SymbolCreator[] };
114
- printOptions?: PrintTreeOptions;
115
- } = {},
116
- path: string = "test.py",
117
- ): string {
118
- // If content is an array, wrap it in a default SourceFile
119
- const sourceFile =
120
- Array.isArray(content) ?
121
- <py.SourceFile path={path}>{content}</py.SourceFile>
122
- : content;
123
-
124
- const res = toSourceTextMultiple([sourceFile], {
125
- policy,
126
- externals,
127
- options,
128
- printOptions,
129
- });
130
- return findFile(res, path).contents;
33
+ export function TestOutputDirectory(props: {
34
+ children: Children;
35
+ externals?: SymbolCreator[];
36
+ namePolicy?: NamePolicy<string>;
37
+ }) {
38
+ return (
39
+ <Output
40
+ externals={props.externals}
41
+ namePolicy={props.namePolicy ?? createPythonNamePolicy()}
42
+ nameConflictResolver={pythonNameConflictResolver}
43
+ >
44
+ {props.children}
45
+ </Output>
46
+ );
131
47
  }
132
48
 
133
49
  // Helper function to create a PythonModuleScope to be used in tests
@@ -1,82 +1,126 @@
1
1
  import { describe, expect, it } from "vitest";
2
2
  import * as py from "../src/index.js";
3
- import { toSourceText } from "./utils.jsx";
3
+ import { TestOutput } from "./utils.js";
4
4
 
5
5
  describe("Atom", () => {
6
6
  it("renders string value", () => {
7
- expect(toSourceText([<py.Atom jsValue={"Test"} />])).toRenderTo('"Test"');
7
+ expect(
8
+ <TestOutput>
9
+ <py.Atom jsValue={"Test"} />
10
+ </TestOutput>,
11
+ ).toRenderTo('"Test"');
8
12
  });
9
13
 
10
14
  it("renders null/undefined object", () => {
11
- expect(toSourceText([<py.Atom jsValue={undefined} />])).toRenderTo("None");
15
+ expect(
16
+ <TestOutput>
17
+ <py.Atom jsValue={undefined} />
18
+ </TestOutput>,
19
+ ).toRenderTo("None");
12
20
  });
13
21
 
14
22
  it("renders number", () => {
15
- expect(toSourceText([<py.Atom jsValue={123} />])).toRenderTo("123");
23
+ expect(
24
+ <TestOutput>
25
+ <py.Atom jsValue={123} />
26
+ </TestOutput>,
27
+ ).toRenderTo("123");
16
28
  });
17
29
 
18
30
  it("renders floating point number", () => {
19
- expect(toSourceText([<py.Atom jsValue={123.456} />])).toRenderTo("123.456");
31
+ expect(
32
+ <TestOutput>
33
+ <py.Atom jsValue={123.456} />
34
+ </TestOutput>,
35
+ ).toRenderTo("123.456");
20
36
  });
21
37
 
22
38
  it("renders floating point number when hinted", () => {
23
- expect(toSourceText([<py.Atom jsValue={123.456} asFloat />])).toRenderTo(
24
- "123.456",
25
- );
39
+ expect(
40
+ <TestOutput>
41
+ <py.Atom jsValue={123.456} asFloat />
42
+ </TestOutput>,
43
+ ).toRenderTo("123.456");
26
44
  });
27
45
 
28
46
  it("renders floating point number with decimal point zero when hinted", () => {
29
- expect(toSourceText([<py.Atom jsValue={123.0} asFloat />])).toRenderTo(
30
- "123.0",
31
- );
47
+ expect(
48
+ <TestOutput>
49
+ <py.Atom jsValue={123.0} asFloat />
50
+ </TestOutput>,
51
+ ).toRenderTo("123.0");
32
52
  });
33
53
 
34
54
  it("renders integer as float when hinted", () => {
35
- expect(toSourceText([<py.Atom jsValue={123} asFloat />])).toRenderTo(
36
- "123.0",
37
- );
55
+ expect(
56
+ <TestOutput>
57
+ <py.Atom jsValue={123} asFloat />
58
+ </TestOutput>,
59
+ ).toRenderTo("123.0");
38
60
  });
39
61
 
40
62
  it("renders small positive float when hinted", () => {
41
- expect(toSourceText([<py.Atom jsValue={0.07} asFloat />])).toRenderTo(
42
- "0.07",
43
- );
63
+ expect(
64
+ <TestOutput>
65
+ <py.Atom jsValue={0.07} asFloat />
66
+ </TestOutput>,
67
+ ).toRenderTo("0.07");
44
68
  });
45
69
 
46
70
  it("renders large positive numbers as float when hinted", () => {
47
- expect(toSourceText([<py.Atom jsValue={2 ** 64} asFloat />])).toRenderTo(
48
- "18446744073709551616.0",
49
- );
71
+ expect(
72
+ <TestOutput>
73
+ <py.Atom jsValue={2 ** 64} asFloat />
74
+ </TestOutput>,
75
+ ).toRenderTo("18446744073709551616.0");
50
76
  });
51
77
 
52
78
  it("renders boolean - True", () => {
53
- expect(toSourceText([<py.Atom jsValue={true} />])).toRenderTo("True");
79
+ expect(
80
+ <TestOutput>
81
+ <py.Atom jsValue={true} />
82
+ </TestOutput>,
83
+ ).toRenderTo("True");
54
84
  });
55
85
 
56
86
  it("renders boolean - False", () => {
57
- expect(toSourceText([<py.Atom jsValue={false} />])).toRenderTo("False");
87
+ expect(
88
+ <TestOutput>
89
+ <py.Atom jsValue={false} />
90
+ </TestOutput>,
91
+ ).toRenderTo("False");
58
92
  });
59
93
 
60
94
  it("renders array", () => {
61
- expect(toSourceText([<py.Atom jsValue={[1, 2, 3]} />])).toRenderTo(
62
- "[1, 2, 3]",
63
- );
95
+ expect(
96
+ <TestOutput>
97
+ <py.Atom jsValue={[1, 2, 3]} />
98
+ </TestOutput>,
99
+ ).toRenderTo("[1, 2, 3]");
64
100
  });
65
101
 
66
102
  it("renders object", () => {
67
- expect(toSourceText([<py.Atom jsValue={{ a: 1, b: 2 }} />])).toRenderTo(
68
- '{"a": 1, "b": 2}',
69
- );
103
+ expect(
104
+ <TestOutput>
105
+ <py.Atom jsValue={{ a: 1, b: 2 }} />
106
+ </TestOutput>,
107
+ ).toRenderTo('{"a": 1, "b": 2}');
70
108
  });
71
109
 
72
110
  it("renders more complex object", () => {
73
111
  expect(
74
- toSourceText([<py.Atom jsValue={{ a: "1", b: 2, c: true }} />]),
112
+ <TestOutput>
113
+ <py.Atom jsValue={{ a: "1", b: 2, c: true }} />
114
+ </TestOutput>,
75
115
  ).toRenderTo('{"a": "1", "b": 2, "c": True}');
76
116
  });
77
117
 
78
118
  it("renders empty object", () => {
79
- expect(toSourceText([<py.Atom jsValue={{}} />])).toRenderTo("{}");
119
+ expect(
120
+ <TestOutput>
121
+ <py.Atom jsValue={{}} />
122
+ </TestOutput>,
123
+ ).toRenderTo("{}");
80
124
  });
81
125
 
82
126
  it("renders function", () => {
@@ -84,12 +128,18 @@ describe("Atom", () => {
84
128
  return <>Test</>;
85
129
  }
86
130
 
87
- expect(toSourceText([<py.Atom jsValue={Test} />])).toRenderTo("Test");
131
+ expect(
132
+ <TestOutput>
133
+ <py.Atom jsValue={Test} />
134
+ </TestOutput>,
135
+ ).toRenderTo("Test");
88
136
  });
89
137
 
90
138
  it("renders nested object", () => {
91
139
  expect(
92
- toSourceText([<py.Atom jsValue={{ a: { b: { c: 1 } }, d: 2 }} />]),
140
+ <TestOutput>
141
+ <py.Atom jsValue={{ a: { b: { c: 1 } }, d: 2 }} />
142
+ </TestOutput>,
93
143
  ).toRenderTo('{"a": {"b": {"c": 1}}, "d": 2}');
94
144
  });
95
145
  });