Skip to content

Commit 2a985a6

Browse files
authored
[Emitter] Add scalar.test.ts for AzureLocation (Azure#48371)
1 parent a3957dc commit 2a985a6

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { TestHost } from "@typespec/compiler/testing";
2+
import { strictEqual } from "assert";
3+
import { beforeEach, describe, it } from "vitest";
4+
import { createModel } from "@typespec/http-client-csharp";
5+
import {
6+
createCSharpSdkContext,
7+
createEmitterContext,
8+
createEmitterTestHost,
9+
typeSpecCompile,
10+
} from "./test-util.js";
11+
12+
describe("Test GetInputType for scalar", () => {
13+
let runner: TestHost;
14+
15+
beforeEach(async () => {
16+
runner = await createEmitterTestHost();
17+
});
18+
19+
it("azureLocation scalar", async () => {
20+
const program = await typeSpecCompile(
21+
`
22+
op test(@query location: azureLocation): void;
23+
`,
24+
runner,
25+
{ IsAzureCoreNeeded: true },
26+
);
27+
const context = await createCSharpSdkContext(createEmitterContext(program));
28+
const model = createModel(context);
29+
30+
const inputParamArray = model.Clients[0].Operations[0].Parameters.filter(
31+
(p) => p.Name === "location",
32+
);
33+
strictEqual(1, inputParamArray.length);
34+
const type = inputParamArray[0].Type;
35+
strictEqual(type.kind, "string");
36+
strictEqual(type.name, "azureLocation");
37+
strictEqual(type.crossLanguageDefinitionId, "Azure.Core.azureLocation");
38+
strictEqual(type.baseType?.kind, "string");
39+
strictEqual(type.baseType.name, "string");
40+
strictEqual(type.baseType.crossLanguageDefinitionId, "TypeSpec.string");
41+
});
42+
});
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import { AzureCoreTestLibrary } from "@azure-tools/typespec-azure-core/testing";
2+
import {
3+
createSdkContext,
4+
CreateSdkContextOptions,
5+
} from "@azure-tools/typespec-client-generator-core";
6+
import { SdkTestLibrary } from "@azure-tools/typespec-client-generator-core/testing";
7+
import {
8+
CompilerOptions,
9+
EmitContext,
10+
Program
11+
} from "@typespec/compiler";
12+
import { createTestHost, TestHost } from "@typespec/compiler/testing";
13+
import { CSharpEmitterContext, CSharpEmitterOptions, Logger, LoggerLevel } from "@typespec/http-client-csharp";
14+
import { HttpTestLibrary } from "@typespec/http/testing";
15+
import { RestTestLibrary } from "@typespec/rest/testing";
16+
import { VersioningTestLibrary } from "@typespec/versioning/testing";
17+
import { XmlTestLibrary } from "@typespec/xml/testing";
18+
19+
export async function createEmitterTestHost(): Promise<TestHost> {
20+
return createTestHost({
21+
libraries: [
22+
RestTestLibrary,
23+
HttpTestLibrary,
24+
VersioningTestLibrary,
25+
AzureCoreTestLibrary,
26+
SdkTestLibrary,
27+
XmlTestLibrary,
28+
],
29+
});
30+
}
31+
32+
export interface TypeSpecCompileOptions {
33+
IsNamespaceNeeded?: boolean;
34+
IsAzureCoreNeeded?: boolean;
35+
IsTCGCNeeded?: boolean;
36+
IsXmlNeeded?: boolean;
37+
AuthDecorator?: string;
38+
}
39+
40+
export async function typeSpecCompile(
41+
content: string,
42+
host: TestHost,
43+
options?: TypeSpecCompileOptions,
44+
) {
45+
const needNamespaces = options?.IsNamespaceNeeded ?? true;
46+
const needAzureCore = options?.IsAzureCoreNeeded ?? false;
47+
const needTCGC = options?.IsTCGCNeeded ?? false;
48+
const needXml = options?.IsXmlNeeded ?? false;
49+
const authDecorator =
50+
options?.AuthDecorator ?? `@useAuth(ApiKeyAuth<ApiKeyLocation.header, "api-key">)`;
51+
const namespace = `
52+
@versioned(Versions)
53+
${authDecorator}
54+
@service({
55+
title: "Azure Csharp emitter Testing",
56+
})
57+
58+
namespace Azure.Csharp.Testing;
59+
60+
enum Versions {
61+
${needAzureCore ? "@useDependency(Azure.Core.Versions.v1_0_Preview_1)" : ""}
62+
"2023-01-01-preview"
63+
}
64+
65+
`;
66+
const fileContent = `
67+
import "@typespec/rest";
68+
import "@typespec/http";
69+
import "@typespec/versioning";
70+
${needXml ? 'import "@typespec/xml";' : ""}
71+
${needAzureCore ? 'import "@azure-tools/typespec-azure-core";' : ""}
72+
${needTCGC ? 'import "@azure-tools/typespec-client-generator-core";' : ""}
73+
using TypeSpec.Rest;
74+
using TypeSpec.Http;
75+
using TypeSpec.Versioning;
76+
${needXml ? "using TypeSpec.Xml;" : ""}
77+
${needAzureCore ? "using Azure.Core;\nusing Azure.Core.Traits;" : ""}
78+
${needTCGC ? "using Azure.ClientGenerator.Core;" : ""}
79+
80+
${needNamespaces ? namespace : ""}
81+
${content}
82+
`;
83+
host.addTypeSpecFile("main.tsp", fileContent);
84+
const cliOptions = {
85+
warningAsError: false,
86+
} as CompilerOptions;
87+
await host.compile("./", cliOptions);
88+
return host.program;
89+
}
90+
91+
export function createEmitterContext(program: Program): EmitContext<CSharpEmitterOptions> {
92+
return {
93+
program: program,
94+
emitterOutputDir: "./",
95+
options: {
96+
outputFile: "tspCodeModel.json",
97+
logFile: "log.json",
98+
"new-project": false,
99+
"clear-output-folder": false,
100+
"save-inputs": false,
101+
"generate-protocol-methods": true,
102+
"generate-convenience-methods": true,
103+
"package-name": undefined,
104+
},
105+
} as EmitContext<CSharpEmitterOptions>;
106+
}
107+
108+
/* We always need to pass in the emitter name now that it is required so making a helper to do this. */
109+
export async function createCSharpSdkContext(
110+
program: EmitContext<CSharpEmitterOptions>,
111+
sdkContextOptions: CreateSdkContextOptions = {},
112+
): Promise<CSharpEmitterContext> {
113+
const context = await createSdkContext(
114+
program,
115+
"@typespec/http-client-csharp",
116+
sdkContextOptions,
117+
);
118+
return {
119+
...context,
120+
logger: new Logger(program.program, LoggerLevel.INFO),
121+
__typeCache: {
122+
types: new Map(),
123+
models: new Map(),
124+
enums: new Map(),
125+
},
126+
};
127+
}

0 commit comments

Comments
 (0)