forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenums.test.ts
87 lines (76 loc) · 2.06 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import assert from "assert";
import { describe, it } from "vitest";
import { emitSchema } from "./utils.js";
describe("emitting enums", () => {
it("is a proper schema", async () => {
const schemas = await emitSchema(`
enum Foo { }
`);
const Foo = schemas["Foo.json"];
assert.strictEqual(Foo.$id, "Foo.json");
assert.strictEqual(Foo.$schema, "https://json-schema.org/draft/2020-12/schema");
});
it("handles numbers", async () => {
const schemas = await emitSchema(`
enum Foo {
a: 0;
b: 1;
c: 2;
}
`);
const Foo = schemas["Foo.json"];
assert.deepEqual(Foo.type, "number");
assert.deepStrictEqual(Foo.enum, [0, 1, 2]);
});
it("handles strings", async () => {
const schemas = await emitSchema(`
enum Foo {
a: "hi";
b: "bye";
}
`);
const Foo = schemas["Foo.json"];
assert.deepEqual(Foo.type, "string");
assert.deepStrictEqual(Foo.enum, ["hi", "bye"]);
});
it("handles both numbers and strings", async () => {
const schemas = await emitSchema(`
enum Foo {
a: "hi";
b: 2;
}
`);
const Foo = schemas["Foo.json"];
assert.deepStrictEqual(Foo.type, ["string", "number"]);
assert.deepStrictEqual(Foo.enum, ["hi", 2]);
});
it("handles extensions", async () => {
const schemas = await emitSchema(`
@extension("x-foo", Json<"foo">)
enum Foo {
a: "hi";
b: 2;
}
`);
const Foo = schemas["Foo.json"];
assert.strictEqual(Foo["x-foo"], "foo");
});
it("handles enum member refs", async () => {
const schemas = await emitSchema(`
enum Foo {
a: "hi";
b: 2;
c;
}
model Bar {
a: Foo.a;
b: Foo.b;
c: Foo.c;
}
`);
const Bar = schemas["Bar.json"];
assert.deepStrictEqual(Bar.properties.a, { type: "string", const: "hi" });
assert.deepStrictEqual(Bar.properties.b, { type: "number", const: 2 });
assert.deepStrictEqual(Bar.properties.c, { type: "string", const: "c" });
});
});