forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenums.test.ts
50 lines (43 loc) · 1.55 KB
/
enums.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { expectDiagnostics } from "@typespec/compiler/testing";
import { deepStrictEqual, strictEqual } from "assert";
import { it } from "vitest";
import { worksFor } from "./works-for.js";
worksFor(["3.0.0", "3.1.0"], ({ diagnoseOpenApiFor, oapiForModel }) => {
it("throws diagnostics for empty enum definitions", async () => {
const diagnostics = await diagnoseOpenApiFor(`enum PetType {}`);
expectDiagnostics(diagnostics, {
code: "@typespec/openapi3/empty-enum",
message: "Empty enums are not supported for OpenAPI v3 - enums must have at least one value.",
});
});
it("supports summary on enums", async () => {
const res = await oapiForModel(
"Foo",
`
@summary("FooEnum")
enum Foo {
y: 0;
};
`,
);
strictEqual(res.schemas.Foo.title, "FooEnum");
});
});
worksFor(["3.0.0"], ({ diagnoseOpenApiFor }) => {
it("throws diagnostics for enum with different types", async () => {
const diagnostics = await diagnoseOpenApiFor(`enum PetType {asString: "dog", asNumber: 1}`);
expectDiagnostics(diagnostics, {
code: "@typespec/openapi3/enum-unique-type",
message: "Enums are not supported unless all options are literals of the same type.",
});
});
});
worksFor(["3.1.0"], ({ oapiForModel }) => {
it("supports enum with different types", async () => {
const res = await oapiForModel("PetType", `enum PetType {asString: "dog", asNumber: 1}`);
deepStrictEqual(res.schemas.PetType, {
type: ["string", "number"],
enum: ["dog", 1],
});
});
});