@alloy-js/python 0.5.0-dev.0 → 0.5.0-dev.2

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 (276) hide show
  1. package/dist/dev/src/builtins/python.js +46 -0
  2. package/dist/dev/src/builtins/python.js.map +1 -1
  3. package/dist/dev/src/components/ClassDeclaration.js +19 -10
  4. package/dist/dev/src/components/ClassDeclaration.js.map +1 -1
  5. package/dist/dev/src/components/ClassMethodDeclaration.js +20 -5
  6. package/dist/dev/src/components/ClassMethodDeclaration.js.map +1 -1
  7. package/dist/dev/src/components/DataclassDeclaration.js +14 -12
  8. package/dist/dev/src/components/DataclassDeclaration.js.map +1 -1
  9. package/dist/dev/src/components/DecoratorList.js +55 -0
  10. package/dist/dev/src/components/DecoratorList.js.map +1 -0
  11. package/dist/dev/src/components/EnumDeclaration.js +21 -12
  12. package/dist/dev/src/components/EnumDeclaration.js.map +1 -1
  13. package/dist/dev/src/components/FunctionBase.js +21 -10
  14. package/dist/dev/src/components/FunctionBase.js.map +1 -1
  15. package/dist/dev/src/components/FutureStatement.js +1 -1
  16. package/dist/dev/src/components/MethodBase.js +16 -4
  17. package/dist/dev/src/components/MethodBase.js.map +1 -1
  18. package/dist/dev/src/components/PropertyDeclaration.js +68 -17
  19. package/dist/dev/src/components/PropertyDeclaration.js.map +1 -1
  20. package/dist/dev/src/components/PydanticClassDeclaration.js +136 -0
  21. package/dist/dev/src/components/PydanticClassDeclaration.js.map +1 -0
  22. package/dist/dev/src/components/SourceFile.js +44 -32
  23. package/dist/dev/src/components/SourceFile.js.map +1 -1
  24. package/dist/dev/src/components/StaticMethodDeclaration.js +19 -5
  25. package/dist/dev/src/components/StaticMethodDeclaration.js.map +1 -1
  26. package/dist/dev/src/components/index.js +1 -0
  27. package/dist/dev/src/components/index.js.map +1 -1
  28. package/dist/dev/test/callsignatures.test.js +471 -297
  29. package/dist/dev/test/callsignatures.test.js.map +1 -1
  30. package/dist/dev/test/class-method-declaration.test.js +21 -10
  31. package/dist/dev/test/class-method-declaration.test.js.map +1 -1
  32. package/dist/dev/test/classdeclarations.test.js +480 -381
  33. package/dist/dev/test/classdeclarations.test.js.map +1 -1
  34. package/dist/dev/test/classinstantiations.test.js +201 -168
  35. package/dist/dev/test/classinstantiations.test.js.map +1 -1
  36. package/dist/dev/test/constructordeclaration.test.js +22 -11
  37. package/dist/dev/test/constructordeclaration.test.js.map +1 -1
  38. package/dist/dev/test/dataclassdeclarations.test.js +345 -358
  39. package/dist/dev/test/dataclassdeclarations.test.js.map +1 -1
  40. package/dist/dev/test/decoratorlist.test.js +131 -0
  41. package/dist/dev/test/decoratorlist.test.js.map +1 -0
  42. package/dist/dev/test/dundermethoddeclaration.test.js +22 -11
  43. package/dist/dev/test/dundermethoddeclaration.test.js.map +1 -1
  44. package/dist/dev/test/enums.test.js +231 -166
  45. package/dist/dev/test/enums.test.js.map +1 -1
  46. package/dist/dev/test/externals.test.js +57 -45
  47. package/dist/dev/test/externals.test.js.map +1 -1
  48. package/dist/dev/test/factories.test.js +124 -50
  49. package/dist/dev/test/factories.test.js.map +1 -1
  50. package/dist/dev/test/functioncallexpressions.test.js +199 -164
  51. package/dist/dev/test/functioncallexpressions.test.js.map +1 -1
  52. package/dist/dev/test/functiondeclaration.test.js +452 -265
  53. package/dist/dev/test/functiondeclaration.test.js.map +1 -1
  54. package/dist/dev/test/imports.test.js +273 -221
  55. package/dist/dev/test/imports.test.js.map +1 -1
  56. package/dist/dev/test/memberexpressions.test.js +1237 -972
  57. package/dist/dev/test/memberexpressions.test.js.map +1 -1
  58. package/dist/dev/test/methoddeclaration.test.js +200 -45
  59. package/dist/dev/test/methoddeclaration.test.js.map +1 -1
  60. package/dist/dev/test/namepolicies.test.js +130 -94
  61. package/dist/dev/test/namepolicies.test.js.map +1 -1
  62. package/dist/dev/test/propertydeclaration.test.js +177 -46
  63. package/dist/dev/test/propertydeclaration.test.js.map +1 -1
  64. package/dist/dev/test/pydanticclassdeclarations.test.js +1089 -0
  65. package/dist/dev/test/pydanticclassdeclarations.test.js.map +1 -0
  66. package/dist/dev/test/pydocs.test.js +888 -715
  67. package/dist/dev/test/pydocs.test.js.map +1 -1
  68. package/dist/dev/test/references.test.js +42 -35
  69. package/dist/dev/test/references.test.js.map +1 -1
  70. package/dist/dev/test/sourcefiles.test.js +1109 -841
  71. package/dist/dev/test/sourcefiles.test.js.map +1 -1
  72. package/dist/dev/test/staticmethoddeclaration.test.js +21 -10
  73. package/dist/dev/test/staticmethoddeclaration.test.js.map +1 -1
  74. package/dist/dev/test/type-checking-imports.test.js +408 -359
  75. package/dist/dev/test/type-checking-imports.test.js.map +1 -1
  76. package/dist/dev/test/typereference.test.js +55 -40
  77. package/dist/dev/test/typereference.test.js.map +1 -1
  78. package/dist/dev/test/uniontypeexpression.test.js +222 -146
  79. package/dist/dev/test/uniontypeexpression.test.js.map +1 -1
  80. package/dist/dev/test/utils.js +39 -77
  81. package/dist/dev/test/utils.js.map +1 -1
  82. package/dist/dev/test/values.test.js +237 -101
  83. package/dist/dev/test/values.test.js.map +1 -1
  84. package/dist/dev/test/variables.test.js +321 -203
  85. package/dist/dev/test/variables.test.js.map +1 -1
  86. package/dist/dev/test/vitest.setup.js +2 -0
  87. package/dist/dev/test/vitest.setup.js.map +1 -0
  88. package/dist/src/builtins/python.d.ts +30 -0
  89. package/dist/src/builtins/python.d.ts.map +1 -1
  90. package/dist/src/builtins/python.js +46 -0
  91. package/dist/src/builtins/python.js.map +1 -1
  92. package/dist/src/components/ClassDeclaration.d.ts +21 -0
  93. package/dist/src/components/ClassDeclaration.d.ts.map +1 -1
  94. package/dist/src/components/ClassDeclaration.js +6 -1
  95. package/dist/src/components/ClassDeclaration.js.map +1 -1
  96. package/dist/src/components/ClassMethodDeclaration.d.ts +5 -1
  97. package/dist/src/components/ClassMethodDeclaration.d.ts.map +1 -1
  98. package/dist/src/components/ClassMethodDeclaration.js +14 -3
  99. package/dist/src/components/ClassMethodDeclaration.js.map +1 -1
  100. package/dist/src/components/DataclassDeclaration.d.ts.map +1 -1
  101. package/dist/src/components/DataclassDeclaration.js +10 -4
  102. package/dist/src/components/DataclassDeclaration.js.map +1 -1
  103. package/dist/src/components/DecoratorList.d.ts +43 -0
  104. package/dist/src/components/DecoratorList.d.ts.map +1 -0
  105. package/dist/src/components/DecoratorList.js +47 -0
  106. package/dist/src/components/DecoratorList.js.map +1 -0
  107. package/dist/src/components/EnumDeclaration.d.ts +9 -0
  108. package/dist/src/components/EnumDeclaration.d.ts.map +1 -1
  109. package/dist/src/components/EnumDeclaration.js +6 -1
  110. package/dist/src/components/EnumDeclaration.js.map +1 -1
  111. package/dist/src/components/FunctionBase.d.ts +31 -1
  112. package/dist/src/components/FunctionBase.d.ts.map +1 -1
  113. package/dist/src/components/FunctionBase.js +9 -2
  114. package/dist/src/components/FunctionBase.js.map +1 -1
  115. package/dist/src/components/FutureStatement.d.ts +1 -1
  116. package/dist/src/components/FutureStatement.js +1 -1
  117. package/dist/src/components/MethodBase.d.ts.map +1 -1
  118. package/dist/src/components/MethodBase.js +10 -2
  119. package/dist/src/components/MethodBase.js.map +1 -1
  120. package/dist/src/components/PropertyDeclaration.d.ts +29 -0
  121. package/dist/src/components/PropertyDeclaration.d.ts.map +1 -1
  122. package/dist/src/components/PropertyDeclaration.js +48 -1
  123. package/dist/src/components/PropertyDeclaration.js.map +1 -1
  124. package/dist/src/components/PydanticClassDeclaration.d.ts +120 -0
  125. package/dist/src/components/PydanticClassDeclaration.d.ts.map +1 -0
  126. package/dist/src/components/PydanticClassDeclaration.js +116 -0
  127. package/dist/src/components/PydanticClassDeclaration.js.map +1 -0
  128. package/dist/src/components/SourceFile.d.ts +2 -2
  129. package/dist/src/components/SourceFile.d.ts.map +1 -1
  130. package/dist/src/components/SourceFile.js +12 -0
  131. package/dist/src/components/SourceFile.js.map +1 -1
  132. package/dist/src/components/StaticMethodDeclaration.d.ts +3 -0
  133. package/dist/src/components/StaticMethodDeclaration.d.ts.map +1 -1
  134. package/dist/src/components/StaticMethodDeclaration.js +13 -3
  135. package/dist/src/components/StaticMethodDeclaration.js.map +1 -1
  136. package/dist/src/components/index.d.ts +1 -0
  137. package/dist/src/components/index.d.ts.map +1 -1
  138. package/dist/src/components/index.js +1 -0
  139. package/dist/src/components/index.js.map +1 -1
  140. package/dist/test/callsignatures.test.js +346 -272
  141. package/dist/test/callsignatures.test.js.map +1 -1
  142. package/dist/test/class-method-declaration.test.js +7 -4
  143. package/dist/test/class-method-declaration.test.js.map +1 -1
  144. package/dist/test/classdeclarations.test.js +316 -277
  145. package/dist/test/classdeclarations.test.js.map +1 -1
  146. package/dist/test/classinstantiations.test.js +112 -103
  147. package/dist/test/classinstantiations.test.js.map +1 -1
  148. package/dist/test/constructordeclaration.test.js +7 -4
  149. package/dist/test/constructordeclaration.test.js.map +1 -1
  150. package/dist/test/dataclassdeclarations.test.js +153 -178
  151. package/dist/test/dataclassdeclarations.test.js.map +1 -1
  152. package/dist/test/decoratorlist.test.d.ts +2 -0
  153. package/dist/test/decoratorlist.test.d.ts.map +1 -0
  154. package/dist/test/decoratorlist.test.js +83 -0
  155. package/dist/test/decoratorlist.test.js.map +1 -0
  156. package/dist/test/dundermethoddeclaration.test.js +7 -4
  157. package/dist/test/dundermethoddeclaration.test.js.map +1 -1
  158. package/dist/test/enums.test.js +172 -143
  159. package/dist/test/enums.test.js.map +1 -1
  160. package/dist/test/externals.test.js +24 -24
  161. package/dist/test/externals.test.js.map +1 -1
  162. package/dist/test/factories.test.js +75 -33
  163. package/dist/test/factories.test.js.map +1 -1
  164. package/dist/test/functioncallexpressions.test.js +117 -106
  165. package/dist/test/functioncallexpressions.test.js.map +1 -1
  166. package/dist/test/functiondeclaration.test.js +256 -173
  167. package/dist/test/functiondeclaration.test.js.map +1 -1
  168. package/dist/test/imports.test.js +171 -143
  169. package/dist/test/imports.test.js.map +1 -1
  170. package/dist/test/memberexpressions.test.js +582 -453
  171. package/dist/test/memberexpressions.test.js.map +1 -1
  172. package/dist/test/methoddeclaration.test.js +106 -19
  173. package/dist/test/methoddeclaration.test.js.map +1 -1
  174. package/dist/test/namepolicies.test.js +90 -78
  175. package/dist/test/namepolicies.test.js.map +1 -1
  176. package/dist/test/propertydeclaration.test.js +90 -15
  177. package/dist/test/propertydeclaration.test.js.map +1 -1
  178. package/dist/test/pydanticclassdeclarations.test.d.ts +2 -0
  179. package/dist/test/pydanticclassdeclarations.test.d.ts.map +1 -0
  180. package/dist/test/pydanticclassdeclarations.test.js +773 -0
  181. package/dist/test/pydanticclassdeclarations.test.js.map +1 -0
  182. package/dist/test/pydocs.test.js +573 -532
  183. package/dist/test/pydocs.test.js.map +1 -1
  184. package/dist/test/references.test.js +31 -28
  185. package/dist/test/references.test.js.map +1 -1
  186. package/dist/test/sourcefiles.test.js +700 -580
  187. package/dist/test/sourcefiles.test.js.map +1 -1
  188. package/dist/test/staticmethoddeclaration.test.js +7 -4
  189. package/dist/test/staticmethoddeclaration.test.js.map +1 -1
  190. package/dist/test/type-checking-imports.test.js +297 -284
  191. package/dist/test/type-checking-imports.test.js.map +1 -1
  192. package/dist/test/typereference.test.js +29 -22
  193. package/dist/test/typereference.test.js.map +1 -1
  194. package/dist/test/uniontypeexpression.test.js +124 -88
  195. package/dist/test/uniontypeexpression.test.js.map +1 -1
  196. package/dist/test/utils.d.ts +10 -17
  197. package/dist/test/utils.d.ts.map +1 -1
  198. package/dist/test/utils.js +32 -74
  199. package/dist/test/utils.js.map +1 -1
  200. package/dist/test/values.test.js +135 -67
  201. package/dist/test/values.test.js.map +1 -1
  202. package/dist/test/variables.test.js +201 -151
  203. package/dist/test/variables.test.js.map +1 -1
  204. package/dist/test/vitest.setup.d.ts +2 -0
  205. package/dist/test/vitest.setup.d.ts.map +1 -0
  206. package/dist/test/vitest.setup.js +2 -0
  207. package/dist/test/vitest.setup.js.map +1 -0
  208. package/dist/tsconfig.tsbuildinfo +1 -1
  209. package/docs/api/components/ClassDeclaration.md +10 -7
  210. package/docs/api/components/ClassEnumDeclaration.md +9 -6
  211. package/docs/api/components/ClassMethodDeclaration.md +7 -5
  212. package/docs/api/components/DataclassDeclaration.md +9 -5
  213. package/docs/api/components/DunderMethodDeclaration.md +7 -5
  214. package/docs/api/components/FunctionDeclaration.md +9 -5
  215. package/docs/api/components/FutureStatement.md +1 -1
  216. package/docs/api/components/MethodDeclaration.md +11 -6
  217. package/docs/api/components/PropertyDeclaration.md +11 -8
  218. package/docs/api/components/PydanticClassDeclaration.md +146 -0
  219. package/docs/api/components/SourceFile.md +20 -8
  220. package/docs/api/components/StaticMethodDeclaration.md +7 -5
  221. package/docs/api/components/index.md +1 -0
  222. package/docs/api/index.md +3 -3
  223. package/docs/api/types/CommonFunctionProps.md +4 -3
  224. package/docs/api/types/PydanticModelConfigDictProps.md +32 -0
  225. package/docs/api/types/index.md +1 -0
  226. package/docs/api/variables/index.md +3 -0
  227. package/docs/api/variables/pydanticModule.md +27 -0
  228. package/docs/api/variables/pydanticSettingsModule.md +7 -0
  229. package/docs/api/variables/typingModule.md +9 -0
  230. package/package.json +4 -4
  231. package/src/builtins/python.ts +539 -1
  232. package/src/components/ClassDeclaration.tsx +23 -0
  233. package/src/components/ClassMethodDeclaration.tsx +9 -1
  234. package/src/components/DataclassDeclaration.tsx +18 -11
  235. package/src/components/DecoratorList.tsx +50 -0
  236. package/src/components/EnumDeclaration.tsx +11 -0
  237. package/src/components/FunctionBase.tsx +34 -3
  238. package/src/components/FutureStatement.tsx +1 -1
  239. package/src/components/MethodBase.tsx +6 -2
  240. package/src/components/PropertyDeclaration.tsx +48 -1
  241. package/src/components/PydanticClassDeclaration.tsx +222 -0
  242. package/src/components/SourceFile.tsx +6 -1
  243. package/src/components/StaticMethodDeclaration.tsx +7 -1
  244. package/src/components/index.ts +1 -0
  245. package/temp/api.json +1158 -86
  246. package/test/callsignatures.test.tsx +309 -283
  247. package/test/class-method-declaration.test.tsx +3 -4
  248. package/test/classdeclarations.test.tsx +277 -235
  249. package/test/classinstantiations.test.tsx +115 -109
  250. package/test/constructordeclaration.test.tsx +9 -6
  251. package/test/dataclassdeclarations.test.tsx +256 -349
  252. package/test/decoratorlist.test.tsx +114 -0
  253. package/test/dundermethoddeclaration.test.tsx +3 -4
  254. package/test/enums.test.tsx +84 -71
  255. package/test/externals.test.tsx +25 -25
  256. package/test/factories.test.tsx +64 -22
  257. package/test/functioncallexpressions.test.tsx +123 -109
  258. package/test/functiondeclaration.test.tsx +218 -140
  259. package/test/imports.test.tsx +119 -91
  260. package/test/memberexpressions.test.tsx +265 -207
  261. package/test/methoddeclaration.test.tsx +115 -24
  262. package/test/namepolicies.test.tsx +69 -69
  263. package/test/propertydeclaration.test.tsx +71 -7
  264. package/test/pydanticclassdeclarations.test.tsx +704 -0
  265. package/test/pydocs.test.tsx +531 -579
  266. package/test/references.test.tsx +24 -23
  267. package/test/sourcefiles.test.tsx +527 -492
  268. package/test/staticmethoddeclaration.test.tsx +3 -4
  269. package/test/type-checking-imports.test.tsx +206 -218
  270. package/test/typereference.test.tsx +15 -12
  271. package/test/uniontypeexpression.test.tsx +74 -61
  272. package/test/utils.tsx +26 -110
  273. package/test/values.test.tsx +82 -32
  274. package/test/variables.test.tsx +162 -142
  275. package/test/vitest.setup.ts +1 -0
  276. package/vitest.config.ts +3 -0
@@ -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
  });