Skip to content

Commit 2006978

Browse files
Unify test utils (#42)
* Add Atom * Add typing * Fix * Fix * Add typing * Fix * Unify test utils * pnpm format * Remove exports * Fix comment * Add TODOs
1 parent 79581e6 commit 2006978

File tree

6 files changed

+56
-164
lines changed

6 files changed

+56
-164
lines changed
Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import { render, type Children } from "@alloy-js/core";
21
import { d } from "@alloy-js/core/testing";
3-
import { SourceFile } from "@alloy-js/python";
42
import type { BasicTestRunner } from "@typespec/compiler/testing";
5-
import { beforeEach, describe, it } from "vitest";
6-
import { Output } from "../../../../src/core/components/output.jsx";
3+
import { beforeEach, describe, expect, it } from "vitest";
74
import { createEmitterFrameworkTestRunner } from "../../test-host.js";
8-
import { assertFileContents, compileModelPropertyType, getExternals } from "../../test-utils.js";
5+
import { compileModelPropertyType, getOutput } from "../../test-utils.js";
96
import { TypeExpression } from "../type-expression/type-expression.jsx";
107

118
let runner: BasicTestRunner;
@@ -14,28 +11,12 @@ beforeEach(async () => {
1411
runner = await createEmitterFrameworkTestRunner();
1512
});
1613

17-
function Wrapper(props: { children: Children }) {
18-
return (
19-
<Output program={runner.program} externals={getExternals()}>
20-
<SourceFile path="test.py">{props.children}</SourceFile>
21-
</Output>
22-
);
23-
}
24-
2514
describe("map array expression to Python list", () => {
2615
it.each([["string[]", "list[str]"]])("%s => %s", async (tspType, pythonType) => {
2716
const type = await compileModelPropertyType(tspType, runner);
28-
const res = render(
29-
<Wrapper>
30-
<TypeExpression type={type} />
31-
</Wrapper>,
32-
);
3317

34-
assertFileContents(
35-
res,
36-
d`
37-
${pythonType}
38-
`,
39-
);
18+
expect(getOutput(runner.program, [<TypeExpression type={type} />])).toRenderTo(d`
19+
${pythonType}
20+
`);
4021
});
4122
});

packages/emitter-framework/src/python/components/function-declaration/function-declaration.test.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { SourceFile } from "@alloy-js/typescript";
21
import type { Namespace } from "@typespec/compiler";
32
import { describe, expect, it } from "vitest";
4-
import { Output } from "../../../../src/core/components/output.jsx";
53
import { getProgram } from "../../test-host.js";
4+
import { getOutput } from "../../test-utils.js";
65
import { FunctionDeclaration } from "./function-declaration.jsx";
6+
77
describe("Typescript Function Declaration", () => {
88
describe("Function bound to Typespec Types", () => {
99
describe("Bound to Operation", () => {
@@ -16,13 +16,7 @@ describe("Typescript Function Declaration", () => {
1616
const [namespace] = program.resolveTypeReference("DemoService");
1717
const operation = Array.from((namespace as Namespace).operations.values())[0];
1818

19-
expect(
20-
<Output program={program}>
21-
<SourceFile path="test.ts">
22-
<FunctionDeclaration type={operation} />
23-
</SourceFile>
24-
</Output>,
25-
).toRenderTo(`
19+
expect(getOutput(program, [<FunctionDeclaration type={operation} />])).toRenderTo(`
2620
def get_name(id: str) -> str:
2721
pass
2822
Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import { render, type Children } from "@alloy-js/core";
21
import { d } from "@alloy-js/core/testing";
3-
import { SourceFile } from "@alloy-js/python";
42
import type { BasicTestRunner } from "@typespec/compiler/testing";
5-
import { beforeEach, describe, it } from "vitest";
6-
import { Output } from "../../../../src/core/components/output.jsx";
3+
import { beforeEach, describe, expect, it } from "vitest";
74
import { createEmitterFrameworkTestRunner } from "../../test-host.js";
8-
import { assertFileContents, compileModelPropertyType, getExternals } from "../../test-utils.js";
5+
import { compileModelPropertyType, getOutput } from "../../test-utils.js";
96
import { TypeExpression } from "../type-expression/type-expression.jsx";
107

118
let runner: BasicTestRunner;
@@ -14,28 +11,12 @@ beforeEach(async () => {
1411
runner = await createEmitterFrameworkTestRunner();
1512
});
1613

17-
function Wrapper(props: { children: Children }) {
18-
return (
19-
<Output program={runner.program} externals={getExternals()}>
20-
<SourceFile path="test.py">{props.children}</SourceFile>
21-
</Output>
22-
);
23-
}
24-
2514
describe("map Record to Python dict", () => {
2615
it.each([["Record<boolean>", "dict[str, bool]"]])("%s => %s", async (tspType, pythonType) => {
2716
const type = await compileModelPropertyType(tspType, runner);
28-
const res = render(
29-
<Wrapper>
30-
<TypeExpression type={type} />
31-
</Wrapper>,
32-
);
3317

34-
assertFileContents(
35-
res,
36-
d`
37-
${pythonType}
38-
`,
39-
);
18+
expect(getOutput(runner.program, [<TypeExpression type={type} />])).toRenderTo(d`
19+
${pythonType}
20+
`);
4021
});
4122
});

packages/emitter-framework/src/python/components/type-alias-declaration/type-alias-declaration.test.tsx

Lines changed: 14 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { SourceFile } from "@alloy-js/python";
21
import type { Namespace } from "@typespec/compiler";
32
import { describe, expect, it } from "vitest";
4-
import { Output } from "../../../../src/core/components/output.jsx";
53
import { getProgram } from "../../test-host.js";
6-
import { getExternals } from "../../test-utils.js";
4+
import { getOutput } from "../../test-utils.js";
75
import { TypeAliasDeclaration } from "./type-alias-declaration.jsx";
86

97
describe("Python Declaration equivalency to Type Alias", () => {
@@ -18,16 +16,10 @@ describe("Python Declaration equivalency to Type Alias", () => {
1816
const [namespace] = program.resolveTypeReference("DemoService");
1917
const scalar = Array.from((namespace as Namespace).scalars.values())[0];
2018

21-
expect(
22-
<Output program={program} externals={getExternals()}>
23-
<SourceFile path="test.py">
24-
<TypeAliasDeclaration type={scalar} />
25-
</SourceFile>
26-
</Output>,
27-
).toRenderTo(`
19+
expect(getOutput(program, [<TypeAliasDeclaration type={scalar} />])).toRenderTo(`
2820
from datetime import datetime
2921
30-
MyDate: datetime`);
22+
my_date: datetime`);
3123
});
3224

3325
it("creates a type alias declaration with JSDoc", async () => {
@@ -42,17 +34,11 @@ describe("Python Declaration equivalency to Type Alias", () => {
4234
const [namespace] = program.resolveTypeReference("DemoService");
4335
const scalar = Array.from((namespace as Namespace).scalars.values())[0];
4436

45-
expect(
46-
<Output program={program} externals={getExternals()}>
47-
<SourceFile path="test.py">
48-
<TypeAliasDeclaration type={scalar} />
49-
</SourceFile>
50-
</Output>,
51-
).toRenderTo(`
37+
expect(getOutput(program, [<TypeAliasDeclaration type={scalar} />])).toRenderTo(`
5238
from datetime import datetime
5339
5440
# Type to represent a date
55-
MyDate: datetime`);
41+
my_date: datetime`);
5642
});
5743

5844
it("can override JSDoc", async () => {
@@ -67,17 +53,12 @@ describe("Python Declaration equivalency to Type Alias", () => {
6753
const [namespace] = program.resolveTypeReference("DemoService");
6854
const scalar = Array.from((namespace as Namespace).scalars.values())[0];
6955

70-
expect(
71-
<Output program={program} externals={getExternals()}>
72-
<SourceFile path="test.py">
73-
<TypeAliasDeclaration doc={"Overridden Doc"} type={scalar} />
74-
</SourceFile>
75-
</Output>,
76-
).toRenderTo(`
56+
expect(getOutput(program, [<TypeAliasDeclaration doc={"Overridden Doc"} type={scalar} />]))
57+
.toRenderTo(`
7758
from datetime import datetime
7859
7960
# Overridden Doc
80-
MyDate: datetime`);
61+
my_date: datetime`);
8162
});
8263

8364
it("creates a type alias declaration for a utcDateTime with unixTimeStamp encoding", async () => {
@@ -90,16 +71,10 @@ describe("Python Declaration equivalency to Type Alias", () => {
9071
const [namespace] = program.resolveTypeReference("DemoService");
9172
const scalar = Array.from((namespace as Namespace).scalars.values())[0];
9273

93-
expect(
94-
<Output program={program} externals={getExternals()}>
95-
<SourceFile path="test.py">
96-
<TypeAliasDeclaration type={scalar} />
97-
</SourceFile>
98-
</Output>,
99-
).toRenderTo(`
74+
expect(getOutput(program, [<TypeAliasDeclaration type={scalar} />])).toRenderTo(`
10075
from datetime import datetime
10176
102-
MyDate: datetime`);
77+
my_date: datetime`);
10378
});
10479

10580
it("creates a type alias declaration for a utcDateTime with rfc7231 encoding", async () => {
@@ -112,16 +87,10 @@ describe("Python Declaration equivalency to Type Alias", () => {
11287
const [namespace] = program.resolveTypeReference("DemoService");
11388
const scalar = Array.from((namespace as Namespace).scalars.values())[0];
11489

115-
expect(
116-
<Output program={program} externals={getExternals()}>
117-
<SourceFile path="test.py">
118-
<TypeAliasDeclaration type={scalar} />
119-
</SourceFile>
120-
</Output>,
121-
).toRenderTo(`
90+
expect(getOutput(program, [<TypeAliasDeclaration type={scalar} />])).toRenderTo(`
12291
from datetime import datetime
12392
124-
MyDate: datetime`);
93+
my_date: datetime`);
12594
});
12695

12796
it("creates a type alias declaration for a utcDateTime with rfc3339 encoding", async () => {
@@ -134,16 +103,10 @@ describe("Python Declaration equivalency to Type Alias", () => {
134103
const [namespace] = program.resolveTypeReference("DemoService");
135104
const scalar = Array.from((namespace as Namespace).scalars.values())[0];
136105

137-
expect(
138-
<Output program={program} externals={getExternals()}>
139-
<SourceFile path="test.py">
140-
<TypeAliasDeclaration type={scalar} />
141-
</SourceFile>
142-
</Output>,
143-
).toRenderTo(`
106+
expect(getOutput(program, [<TypeAliasDeclaration type={scalar} />])).toRenderTo(`
144107
from datetime import datetime
145108
146-
MyDate: datetime`);
109+
my_date: datetime`);
147110
});
148111
});
149112
});
Lines changed: 15 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
import { render, type Children } from "@alloy-js/core";
1+
import { render } from "@alloy-js/core";
22
import { d } from "@alloy-js/core/testing";
3-
import { SourceFile } from "@alloy-js/python";
43
import type { BasicTestRunner } from "@typespec/compiler/testing";
5-
import { beforeEach, describe, it } from "vitest";
6-
import { Output } from "../../../../src/core/components/output.jsx";
4+
import { beforeEach, describe, expect, it } from "vitest";
75
import { createEmitterFrameworkTestRunner } from "../../test-host.js";
86
import {
97
assertFileContents,
108
compileCodeModelPropertyType,
119
compileModelProperty,
1210
compileModelPropertyType,
13-
getExternals,
11+
getOutput,
1412
} from "../../test-utils.js";
1513
import { TypeExpression } from "./type-expression.jsx";
1614

@@ -20,14 +18,6 @@ beforeEach(async () => {
2018
runner = await createEmitterFrameworkTestRunner();
2119
});
2220

23-
function Wrapper(props: { children: Children }) {
24-
return (
25-
<Output program={runner.program} externals={getExternals()}>
26-
<SourceFile path="test.py">{props.children}</SourceFile>
27-
</Output>
28-
);
29-
}
30-
3121
describe("map Typespec types to Python built-in types", () => {
3222
it.each([
3323
["unknown", "Any", "from typing import Any"],
@@ -61,19 +51,11 @@ describe("map Typespec types to Python built-in types", () => {
6151
["url", "str"],
6252
])("%s => %s", async (tspType, pythonType, extraImport = "") => {
6353
const type = await compileModelPropertyType(tspType, runner);
64-
const res = render(
65-
<Wrapper>
66-
<TypeExpression type={type} />
67-
</Wrapper>,
68-
);
6954
const extraImportText = extraImport ? `${extraImport}\n\n` : "";
7055

71-
assertFileContents(
72-
res,
73-
d`
74-
${extraImportText}${pythonType}
75-
`,
76-
);
56+
expect(getOutput(runner.program, [<TypeExpression type={type} />])).toRenderTo(d`
57+
${extraImportText}${pythonType}
58+
`);
7759
});
7860
});
7961

@@ -89,11 +71,7 @@ describe("map scalar to Python types", () => {
8971
`,
9072
runner,
9173
);
92-
const res = render(
93-
<Wrapper>
94-
<TypeExpression type={type} />
95-
</Wrapper>,
96-
);
74+
const res = render(getOutput(runner.program, [<TypeExpression type={type} />]));
9775

9876
assertFileContents(
9977
res,
@@ -107,39 +85,23 @@ describe("map scalar to Python types", () => {
10785
describe("map tuple to Python types", () => {
10886
it.each([["[int32, int32]", "Tuple[int, int]"]])("%s => %s", async (tspType, pythonType) => {
10987
const type = await compileModelPropertyType(tspType, runner);
110-
const res = render(
111-
<Wrapper>
112-
<TypeExpression type={type} />
113-
</Wrapper>,
114-
);
11588

116-
assertFileContents(
117-
res,
118-
d`
119-
from typing import Tuple
89+
expect(getOutput(runner.program, [<TypeExpression type={type} />])).toRenderTo(d`
90+
from typing import Tuple
12091
121-
${pythonType}
122-
`,
123-
);
92+
${pythonType}
93+
`);
12494
});
12595
});
12696

12797
describe("correctly solves a ModelProperty to Python types", () => {
12898
it.each([["[int32, int32]", "Tuple[int, int]"]])("%s => %s", async (tspType, pythonType) => {
12999
const type = await compileModelProperty(tspType, runner);
130-
const res = render(
131-
<Wrapper>
132-
<TypeExpression type={type} />
133-
</Wrapper>,
134-
);
135100

136-
assertFileContents(
137-
res,
138-
d`
139-
from typing import Tuple
101+
expect(getOutput(runner.program, [<TypeExpression type={type} />])).toRenderTo(d`
102+
from typing import Tuple
140103
141-
${pythonType}
142-
`,
143-
);
104+
${pythonType}
105+
`);
144106
});
145107
});

0 commit comments

Comments
 (0)