|
4 | 4 | import { Meta } from "test-utils/mod.ts";
|
5 | 5 | import { Typegraph } from "@metatype/typegate/typegraph/types.ts";
|
6 | 6 | import { assert, assertEquals } from "@std/assert";
|
| 7 | +import { FunctionNode } from "@metatype/typegate/typegraph/type_node.ts"; |
7 | 8 |
|
8 |
| -Meta.test({ |
9 |
| - name: "dedup test", |
10 |
| -}, async (_t) => { |
| 9 | +Meta.test("dedup test", async ({ t }) => { |
11 | 10 | const { stdout } = await Meta.cli(
|
12 | 11 | "serialize",
|
13 | 12 | "--pretty",
|
14 | 13 | "-f",
|
15 | 14 | "dedup/tg.ts",
|
16 | 15 | );
|
17 | 16 | const tg: Typegraph[] = JSON.parse(stdout);
|
| 17 | + |
18 | 18 | console.log(tg);
|
19 |
| - const ints = []; |
20 |
| - const strs = []; |
21 |
| - for (const ty of tg[0].types) { |
22 |
| - if (ty.type == "integer") { |
23 |
| - ints.push(ty); |
24 |
| - } |
25 |
| - if (ty.type == "string") { |
26 |
| - strs.push(ty); |
| 19 | + |
| 20 | + const [objTg, matTg] = tg; |
| 21 | + |
| 22 | + await t.step("object dedup", () => { |
| 23 | + const ints = []; |
| 24 | + const strs = []; |
| 25 | + for (const ty of objTg.types) { |
| 26 | + if (ty.type == "integer") { |
| 27 | + ints.push(ty); |
| 28 | + } |
| 29 | + if (ty.type == "string") { |
| 30 | + strs.push(ty); |
| 31 | + } |
27 | 32 | }
|
28 |
| - } |
29 |
| - assertEquals(ints.length, 2); |
30 |
| - assert(ints.some((ty) => ty.title == "namedInt")); |
31 |
| - assertEquals(strs.length, 3); |
32 |
| - assert(strs.some((ty) => ty.format == "uuid")); |
33 |
| - assertEquals(strs.filter((ty) => ty.format == "date-time").length, 1); |
| 33 | + assertEquals(ints.length, 2); |
| 34 | + assert(ints.some((ty) => ty.title == "namedInt")); |
| 35 | + assertEquals(strs.length, 3); |
| 36 | + assert(strs.some((ty) => ty.format == "uuid")); |
| 37 | + assertEquals(strs.filter((ty) => ty.format == "date-time").length, 1); |
| 38 | + }); |
| 39 | + |
| 40 | + await t.step("materializer dedup", () => { |
| 41 | + const fnInput = matTg.types.findIndex( |
| 42 | + ({ title }) => title === "root_f1_fn_input", |
| 43 | + ); |
| 44 | + const fnOutput = matTg.types.findIndex( |
| 45 | + ({ title }) => title === "root_f1_fn_output", |
| 46 | + ); |
| 47 | + |
| 48 | + const f1 = matTg.types.find( |
| 49 | + ({ title }) => title === "root_f1_fn", |
| 50 | + ) as FunctionNode; |
| 51 | + const f2 = matTg.types.find( |
| 52 | + ({ title }) => title === "root_f2_fn", |
| 53 | + ) as FunctionNode; |
| 54 | + |
| 55 | + assertEquals(f1.input, fnInput); |
| 56 | + assertEquals(f2.input, fnInput); |
| 57 | + assertEquals(f1.output, fnOutput); |
| 58 | + assertEquals(f2.output, fnOutput); |
| 59 | + }); |
| 60 | + |
| 61 | + await t.step("unique type names", () => { |
| 62 | + const typeNames = objTg.types.concat(matTg.types).map(({ title }) => title); |
| 63 | + const nameSet = new Set(typeNames); |
| 64 | + |
| 65 | + assertEquals(typeNames.length, nameSet.size); |
| 66 | + }); |
34 | 67 | });
|
0 commit comments