@typespec/emitter-framework 0.9.0-dev.7 → 0.9.0-dev.9

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 (60) hide show
  1. package/dist/src/csharp/components/class/declaration.test.js +100 -115
  2. package/dist/src/csharp/components/{enum-declaration.d.ts → enum/declaration.d.ts} +1 -1
  3. package/dist/src/csharp/components/enum/declaration.d.ts.map +1 -0
  4. package/dist/src/csharp/components/{enum-declaration.js → enum/declaration.js} +4 -4
  5. package/dist/src/csharp/components/enum/declaration.test.d.ts +2 -0
  6. package/dist/src/csharp/components/enum/declaration.test.d.ts.map +1 -0
  7. package/dist/src/csharp/components/enum/declaration.test.js +309 -0
  8. package/dist/src/csharp/components/index.d.ts +1 -1
  9. package/dist/src/csharp/components/index.js +1 -1
  10. package/dist/src/csharp/components/property/property.test.js +14 -14
  11. package/dist/src/testing/scenario-test/code-block-expectation.d.ts +33 -0
  12. package/dist/src/testing/scenario-test/code-block-expectation.d.ts.map +1 -0
  13. package/dist/src/testing/scenario-test/code-block-expectation.js +69 -0
  14. package/dist/src/testing/scenario-test/code-block-expectation.test.d.ts +2 -0
  15. package/dist/src/testing/scenario-test/code-block-expectation.test.d.ts.map +1 -0
  16. package/dist/src/testing/scenario-test/code-block-expectation.test.js +80 -0
  17. package/dist/src/testing/scenario-test/harness.d.ts +2 -2
  18. package/dist/src/testing/scenario-test/harness.d.ts.map +1 -1
  19. package/dist/src/testing/scenario-test/harness.js +69 -158
  20. package/dist/src/testing/scenario-test/index.d.ts +0 -1
  21. package/dist/src/testing/scenario-test/index.d.ts.map +1 -1
  22. package/dist/src/testing/scenario-test/index.js +1 -2
  23. package/dist/src/testing/scenario-test/snippet-extractor.d.ts +1 -1
  24. package/dist/src/testing/scenario-test/snippet-extractor.js +1 -1
  25. package/dist/test/testing/snippet-extractor-csharp.test.js +3 -3
  26. package/dist/test/testing/snippet-extractor-java.test.js +3 -3
  27. package/dist/test/testing/snippet-extractor-python.test.js +2 -2
  28. package/dist/test/testing/snippet-extractor-typescript.test.js +3 -3
  29. package/package.json +8 -6
  30. package/src/csharp/components/class/declaration.test.tsx +106 -168
  31. package/src/csharp/components/enum/declaration.test.tsx +267 -0
  32. package/src/csharp/components/{enum-declaration.tsx → enum/declaration.tsx} +4 -4
  33. package/src/csharp/components/index.ts +1 -1
  34. package/src/csharp/components/property/property.test.tsx +14 -14
  35. package/src/testing/scenario-test/code-block-expectation.test.ts +95 -0
  36. package/src/testing/scenario-test/code-block-expectation.ts +115 -0
  37. package/src/testing/scenario-test/harness.ts +91 -236
  38. package/src/testing/scenario-test/index.ts +0 -1
  39. package/src/testing/scenario-test/snippet-extractor.ts +1 -1
  40. package/test/testing/snippet-extractor-csharp.test.ts +3 -3
  41. package/test/testing/snippet-extractor-java.test.ts +3 -3
  42. package/test/testing/snippet-extractor-python.test.ts +2 -2
  43. package/test/testing/snippet-extractor-typescript.test.ts +3 -3
  44. package/dist/src/csharp/components/enum-declaration.d.ts.map +0 -1
  45. package/dist/src/testing/scenario-test/test-host.d.ts +0 -8
  46. package/dist/src/testing/scenario-test/test-host.d.ts.map +0 -1
  47. package/dist/src/testing/scenario-test/test-host.js +0 -49
  48. package/dist/test/csharp/components/enum-declaration.test.d.ts +0 -2
  49. package/dist/test/csharp/components/enum-declaration.test.d.ts.map +0 -1
  50. package/dist/test/csharp/components/enum-declaration.test.js +0 -403
  51. package/dist/test/csharp/test-host.d.ts +0 -11
  52. package/dist/test/csharp/test-host.d.ts.map +0 -1
  53. package/dist/test/csharp/test-host.js +0 -32
  54. package/dist/test/csharp/utils.d.ts +0 -3
  55. package/dist/test/csharp/utils.d.ts.map +0 -1
  56. package/dist/test/csharp/utils.js +0 -6
  57. package/src/testing/scenario-test/test-host.ts +0 -83
  58. package/test/csharp/components/enum-declaration.test.tsx +0 -337
  59. package/test/csharp/test-host.ts +0 -42
  60. package/test/csharp/utils.ts +0 -8
@@ -1,83 +0,0 @@
1
- import type { CompilerOptions, Diagnostic } from "@typespec/compiler";
2
- import {
3
- type BasicTestRunner,
4
- createTestHost,
5
- createTestWrapper,
6
- type TestHostConfig,
7
- type TypeSpecTestLibrary,
8
- } from "@typespec/compiler/testing";
9
- import { HttpTestLibrary } from "@typespec/http/testing";
10
- import { RestTestLibrary } from "@typespec/rest/testing";
11
- import { join, relative } from "path";
12
-
13
- export interface EmittedFile {
14
- path: string;
15
- content: string;
16
- }
17
-
18
- async function createEmitterTestRunner(
19
- testLibrary: TypeSpecTestLibrary,
20
- options: {
21
- testHostConfig?: TestHostConfig;
22
- autoImports?: string[];
23
- autoUsings?: string[];
24
- compilerOptions?: CompilerOptions;
25
- } = {},
26
- ) {
27
- const libraries = options.testHostConfig?.libraries ?? [
28
- testLibrary,
29
- HttpTestLibrary,
30
- RestTestLibrary,
31
- ];
32
- const host = await createTestHost({ libraries });
33
-
34
- return createTestWrapper(host, {
35
- autoImports: options.autoImports ?? ["@typespec/http", "@typespec/rest"],
36
- autoUsings: options.autoUsings ?? ["TypeSpec.Http", "TypeSpec.Rest"],
37
- compilerOptions: options.compilerOptions ?? {
38
- noEmit: false,
39
- emit: [testLibrary.name],
40
- },
41
- });
42
- }
43
-
44
- export async function emitWithDiagnostics(
45
- testLibrary: TypeSpecTestLibrary,
46
- emitterOutputDir: string,
47
- code: string,
48
- ): Promise<[EmittedFile[], readonly Diagnostic[]]> {
49
- const runner = await createEmitterTestRunner(testLibrary);
50
- await runner.compileAndDiagnose(code, {
51
- outputDir: "tsp-output",
52
- });
53
- const result = await readFilesRecursively(emitterOutputDir, emitterOutputDir, runner);
54
- return [result, runner.program.diagnostics];
55
- }
56
-
57
- async function readFilesRecursively(
58
- currentDir: string,
59
- emitterOutputDir: string,
60
- runner: BasicTestRunner,
61
- ): Promise<EmittedFile[]> {
62
- const entries = await runner.program.host.readDir(currentDir);
63
- const result: EmittedFile[] = [];
64
-
65
- for (const entry of entries) {
66
- const fullPath = join(currentDir, entry);
67
- const stat = await runner.program.host.stat(fullPath);
68
-
69
- if (stat.isDirectory()) {
70
- // Recursively read files in the directory
71
- const nestedFiles = await readFilesRecursively(fullPath, emitterOutputDir, runner);
72
- result.push(...nestedFiles);
73
- } else if (stat.isFile()) {
74
- // Read the file
75
- // Read the file and store it with a relative path
76
- const relativePath = relative(emitterOutputDir, fullPath);
77
- const fileContent = await runner.program.host.readFile(fullPath);
78
- result.push({ path: relativePath, content: fileContent.text });
79
- }
80
- }
81
-
82
- return result;
83
- }
@@ -1,337 +0,0 @@
1
- import { render } from "@alloy-js/core";
2
- import { d } from "@alloy-js/core/testing";
3
- import * as cs from "@alloy-js/csharp";
4
- import { Namespace, SourceFile } from "@alloy-js/csharp";
5
- import type { Enum } from "@typespec/compiler";
6
- import type { BasicTestRunner } from "@typespec/compiler/testing";
7
- import { beforeEach, it } from "vitest";
8
- import { Output } from "../../../src/core/index.js";
9
- import { EnumDeclaration } from "../../../src/csharp/index.js";
10
- import { createEmitterFrameworkTestRunner } from "../test-host.js";
11
- import { assertFileContents } from "../utils.js";
12
-
13
- let runner: BasicTestRunner;
14
-
15
- beforeEach(async () => {
16
- runner = await createEmitterFrameworkTestRunner();
17
- });
18
-
19
- it("renders a basic enum declaration", async () => {
20
- const { TestEnum } = (await runner.compile(`
21
- @test enum TestEnum {
22
- Value1;
23
- Value2;
24
- Value3;
25
- }
26
- `)) as { TestEnum: Enum };
27
-
28
- const res = render(
29
- <Output program={runner.program}>
30
- <Namespace name="TestNamespace">
31
- <SourceFile path="test.cs">
32
- <EnumDeclaration type={TestEnum} />
33
- </SourceFile>
34
- </Namespace>
35
- </Output>,
36
- );
37
-
38
- assertFileContents(
39
- res,
40
- d`
41
- namespace TestNamespace
42
- {
43
- enum TestEnum
44
- {
45
- Value1,
46
- Value2,
47
- Value3
48
- }
49
- }
50
- `,
51
- );
52
- });
53
-
54
- it("renders an empty enum declaration", async () => {
55
- const { TestEnum } = (await runner.compile(`
56
- @test enum TestEnum {}
57
- `)) as { TestEnum: Enum };
58
-
59
- const res = render(
60
- <Output program={runner.program}>
61
- <Namespace name="TestNamespace">
62
- <SourceFile path="test.cs">
63
- <EnumDeclaration type={TestEnum} />
64
- </SourceFile>
65
- </Namespace>
66
- </Output>,
67
- );
68
-
69
- assertFileContents(
70
- res,
71
- d`
72
- namespace TestNamespace
73
- {
74
- enum TestEnum
75
- {
76
-
77
- }
78
- }
79
- `,
80
- );
81
- });
82
-
83
- it("can override enum name", async () => {
84
- const { TestEnum } = (await runner.compile(`
85
- @test enum TestEnum {
86
- Value1;
87
- Value2;
88
- }
89
- `)) as { TestEnum: Enum };
90
-
91
- const res = render(
92
- <Output program={runner.program}>
93
- <Namespace name="TestNamespace">
94
- <SourceFile path="test.cs">
95
- <EnumDeclaration type={TestEnum} name="CustomEnumName" />
96
- </SourceFile>
97
- </Namespace>
98
- </Output>,
99
- );
100
-
101
- assertFileContents(
102
- res,
103
- d`
104
- namespace TestNamespace
105
- {
106
- enum CustomEnumName
107
- {
108
- Value1,
109
- Value2
110
- }
111
- }
112
- `,
113
- );
114
- });
115
-
116
- it("renders an enum with access modifiers", async () => {
117
- const { TestEnum } = (await runner.compile(`
118
- @test enum TestEnum {
119
- Value1;
120
- Value2;
121
- }
122
- `)) as { TestEnum: Enum };
123
-
124
- const res = render(
125
- <Output program={runner.program}>
126
- <Namespace name="TestNamespace">
127
- <SourceFile path="test.cs">
128
- <EnumDeclaration type={TestEnum} internal />
129
- </SourceFile>
130
- </Namespace>
131
- </Output>,
132
- );
133
-
134
- assertFileContents(
135
- res,
136
- d`
137
- namespace TestNamespace
138
- {
139
- internal enum TestEnum
140
- {
141
- Value1,
142
- Value2
143
- }
144
- }
145
- `,
146
- );
147
- });
148
-
149
- it("renders enum with C# naming conventions", async () => {
150
- const { TestEnum } = (await runner.compile(`
151
- @test enum TestEnum {
152
- value_one;
153
- value_two;
154
- value_three;
155
- }
156
- `)) as { TestEnum: Enum };
157
-
158
- const res = render(
159
- <Output program={runner.program} namePolicy={cs.createCSharpNamePolicy()}>
160
- <Namespace name="TestNamespace">
161
- <SourceFile path="test.cs">
162
- <EnumDeclaration type={TestEnum} />
163
- </SourceFile>
164
- </Namespace>
165
- </Output>,
166
- );
167
-
168
- assertFileContents(
169
- res,
170
- d`
171
- namespace TestNamespace
172
- {
173
- enum TestEnum
174
- {
175
- ValueOne,
176
- ValueTwo,
177
- ValueThree
178
- }
179
- }
180
- `,
181
- );
182
- });
183
-
184
- it("renders enum with single value", async () => {
185
- const { TestEnum } = (await runner.compile(`
186
- @test enum TestEnum {
187
- OnlyValue;
188
- }
189
- `)) as { TestEnum: Enum };
190
-
191
- const res = render(
192
- <Output program={runner.program}>
193
- <Namespace name="TestNamespace">
194
- <SourceFile path="test.cs">
195
- <EnumDeclaration type={TestEnum} />
196
- </SourceFile>
197
- </Namespace>
198
- </Output>,
199
- );
200
-
201
- assertFileContents(
202
- res,
203
- d`
204
- namespace TestNamespace
205
- {
206
- enum TestEnum
207
- {
208
- OnlyValue
209
- }
210
- }
211
- `,
212
- );
213
- });
214
-
215
- it("renders enum with numeric-like member names", async () => {
216
- const { TestEnum } = (await runner.compile(`
217
- @test enum TestEnum {
218
- Value0;
219
- Value1;
220
- Value10;
221
- Value100;
222
- }
223
- `)) as { TestEnum: Enum };
224
-
225
- const res = render(
226
- <Output program={runner.program}>
227
- <Namespace name="TestNamespace">
228
- <SourceFile path="test.cs">
229
- <EnumDeclaration type={TestEnum} />
230
- </SourceFile>
231
- </Namespace>
232
- </Output>,
233
- );
234
-
235
- assertFileContents(
236
- res,
237
- d`
238
- namespace TestNamespace
239
- {
240
- enum TestEnum
241
- {
242
- Value0,
243
- Value1,
244
- Value10,
245
- Value100
246
- }
247
- }
248
- `,
249
- );
250
- });
251
-
252
- it("renders multiple enums in the same namespace", async () => {
253
- const { TestEnum1, TestEnum2 } = (await runner.compile(`
254
- @test enum TestEnum1 {
255
- Value1;
256
- Value2;
257
- }
258
- @test enum TestEnum2 {
259
- OptionA;
260
- OptionB;
261
- OptionC;
262
- }
263
- `)) as { TestEnum1: Enum; TestEnum2: Enum };
264
-
265
- const res = render(
266
- <Output program={runner.program}>
267
- <Namespace name="TestNamespace">
268
- <SourceFile path="test.cs">
269
- <EnumDeclaration type={TestEnum1} />
270
- <hbr />
271
- <EnumDeclaration type={TestEnum2} />
272
- </SourceFile>
273
- </Namespace>
274
- </Output>,
275
- );
276
-
277
- assertFileContents(
278
- res,
279
- d`
280
- namespace TestNamespace
281
- {
282
- enum TestEnum1
283
- {
284
- Value1,
285
- Value2
286
- }
287
- enum TestEnum2
288
- {
289
- OptionA,
290
- OptionB,
291
- OptionC
292
- }
293
- }
294
- `,
295
- );
296
- });
297
-
298
- it("renders an enum with doc comments", async () => {
299
- const { TestEnum } = (await runner.compile(`
300
- @test enum TestEnum {
301
- @doc("This is value one")
302
- Value1;
303
- /** This is value two */
304
- Value2;
305
- }
306
- `)) as { TestEnum: Enum };
307
-
308
- const res = render(
309
- <Output program={runner.program}>
310
- <Namespace name="TestNamespace">
311
- <SourceFile path="test.cs">
312
- <EnumDeclaration type={TestEnum} />
313
- </SourceFile>
314
- </Namespace>
315
- </Output>,
316
- );
317
-
318
- assertFileContents(
319
- res,
320
- d`
321
- namespace TestNamespace
322
- {
323
- enum TestEnum
324
- {
325
- /// <summary>
326
- /// This is value one
327
- /// </summary>
328
- Value1,
329
- /// <summary>
330
- /// This is value two
331
- /// </summary>
332
- Value2
333
- }
334
- }
335
- `,
336
- );
337
- });
@@ -1,42 +0,0 @@
1
- import type { Program } from "@typespec/compiler";
2
- import {
3
- createTestHost,
4
- createTestWrapper,
5
- expectDiagnosticEmpty,
6
- type TypeSpecTestLibrary,
7
- } from "@typespec/compiler/testing";
8
- import { HttpTestLibrary } from "@typespec/http/testing";
9
-
10
- export async function createTypespecCliTestHost(
11
- options: { libraries: "Http"[] } = { libraries: [] },
12
- ) {
13
- const libraries: TypeSpecTestLibrary[] = [];
14
- if (options.libraries.includes("Http")) {
15
- libraries.push(HttpTestLibrary);
16
- }
17
- return createTestHost({
18
- libraries,
19
- });
20
- }
21
-
22
- export async function createEmitterFrameworkTestRunner(options: { autoUsings?: string[] } = {}) {
23
- const host = await createTypespecCliTestHost();
24
- return createTestWrapper(host, {
25
- autoUsings: options.autoUsings,
26
- });
27
- }
28
-
29
- export async function getProgram(
30
- code: string,
31
- options: { libraries: "Http"[] } = { libraries: [] },
32
- ): Promise<Program> {
33
- const host = await createTypespecCliTestHost(options);
34
- const wrapper = createTestWrapper(host, {
35
- compilerOptions: {
36
- noEmit: true,
37
- },
38
- });
39
- const [_, diagnostics] = await wrapper.compileAndDiagnose(code);
40
- expectDiagnosticEmpty(diagnostics);
41
- return wrapper.program;
42
- }
@@ -1,8 +0,0 @@
1
- import type { OutputDirectory } from "@alloy-js/core";
2
- import { assert } from "vitest";
3
-
4
- export function assertFileContents(res: OutputDirectory, contents: string) {
5
- const testFile = res.contents.find((file) => file.path === "test.cs")!;
6
- assert(testFile, "test.cs file not rendered");
7
- assert.equal(testFile.contents, contents);
8
- }