forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunions.test.ts
90 lines (76 loc) · 2.17 KB
/
unions.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
88
89
90
import assert from "assert";
import { describe, it } from "vitest";
import { emitSchema } from "./utils.js";
describe("emitting unions", () => {
it("works with declarations", async () => {
const schemas = await emitSchema(`
union Foo {
x: string;
y: boolean;
}
`);
const Foo = schemas["Foo.json"];
assert.strictEqual(Foo.$id, "Foo.json");
assert.strictEqual(Foo.$schema, "https://json-schema.org/draft/2020-12/schema");
assert.deepStrictEqual(Foo.anyOf, [{ type: "string" }, { type: "boolean" }]);
});
it("union description carries over", async () => {
const schemas = await emitSchema(`
/** Foo doc */
union Foo {
x: string;
y: boolean;
}
`);
assert.strictEqual(schemas["Foo.json"].description, "Foo doc");
});
it("works with declarations with anonymous variants", async () => {
const schemas = await emitSchema(`
union Foo {
string;
boolean;
}
`);
const Foo = schemas["Foo.json"];
assert.strictEqual(Foo.$id, "Foo.json");
assert.strictEqual(Foo.$schema, "https://json-schema.org/draft/2020-12/schema");
assert.deepStrictEqual(Foo.anyOf, [{ type: "string" }, { type: "boolean" }]);
});
it("works with references", async () => {
const schemas = await emitSchema(`
union Foo {
x: Bar;
y: Baz;
}
model Bar { };
model Baz { };
`);
const Foo = schemas["Foo.json"];
assert.deepStrictEqual(Foo.anyOf, [{ $ref: "Bar.json" }, { $ref: "Baz.json" }]);
});
it("handles union expressions", async () => {
const schemas = await emitSchema(`
model Foo {
x: 1 | "hello";
}
`);
const Foo = schemas["Foo.json"];
assert.deepStrictEqual(Foo.properties.x.anyOf, [
{ type: "number", const: 1 },
{ type: "string", const: "hello" },
]);
});
it("handles extensions", async () => {
const schemas = await emitSchema(`
@extension("x-foo", Json<true>)
union Foo {
x: Bar;
y: Baz;
}
model Bar { };
model Baz { };
`);
const Foo = schemas["Foo.json"];
assert.strictEqual(Foo["x-foo"], true);
});
});