From b6fcfc327f696264800ecf609ce0cd7620d0a6ca Mon Sep 17 00:00:00 2001 From: ZEROM22 Date: Tue, 17 Dec 2024 16:23:22 +0300 Subject: [PATCH] feat: add test --- .../typedoc-signature/fixtures/000/main.ts | 1 + .../typedoc-signature/fixtures/000/test.ts | 55 +++++++++++ .../fixtures/000/tsconfig.json | 3 + packages/typedoc-signature/lib/main.test.ts | 93 +++++++++++++++++++ packages/typedoc-signature/package.json | 4 + 5 files changed, 156 insertions(+) create mode 100644 packages/typedoc-signature/fixtures/000/main.ts create mode 100644 packages/typedoc-signature/fixtures/000/test.ts create mode 100644 packages/typedoc-signature/fixtures/000/tsconfig.json create mode 100644 packages/typedoc-signature/lib/main.test.ts diff --git a/packages/typedoc-signature/fixtures/000/main.ts b/packages/typedoc-signature/fixtures/000/main.ts new file mode 100644 index 000000000..0d090bd25 --- /dev/null +++ b/packages/typedoc-signature/fixtures/000/main.ts @@ -0,0 +1 @@ +function f() {} diff --git a/packages/typedoc-signature/fixtures/000/test.ts b/packages/typedoc-signature/fixtures/000/test.ts new file mode 100644 index 000000000..edeec0b0a --- /dev/null +++ b/packages/typedoc-signature/fixtures/000/test.ts @@ -0,0 +1,55 @@ +import { + type Entity, + DeclarationEntity, +} from "@onlyoffice/library-declaration/next.ts" +import { + EntityToken, + KeywordToken, + type Signature, + TextToken, + type Token, + TypeToken, +} from "@onlyoffice/signature" + +export const name = "transforms an empty project" + +export const collection: Entity[] = [] + +let e0: Entity +let s: Signature = [] +let t: Token + +e0 = new DeclarationEntity() +e0.id = 2 + +t = new KeywordToken() +t.text = "function" +s.push(t) + +t = new TextToken() +t.text = " " +s.push(t) + +t = new EntityToken() +t.text = "f" +s.push(t) + +t = new TextToken() +t.text = "(" +s.push(t) + +t = new TextToken() +t.text = ")" +s.push(t) + +t = new TextToken() +t.text = ": " +s.push(t) + +t = new TypeToken() +t.text = "void" +s.push(t) + +e0.declaration.signature.verbose = s + +collection.push(e0) diff --git a/packages/typedoc-signature/fixtures/000/tsconfig.json b/packages/typedoc-signature/fixtures/000/tsconfig.json new file mode 100644 index 000000000..bc62b62b0 --- /dev/null +++ b/packages/typedoc-signature/fixtures/000/tsconfig.json @@ -0,0 +1,3 @@ +{ + "files": ["main.ts"] +} diff --git a/packages/typedoc-signature/lib/main.test.ts b/packages/typedoc-signature/lib/main.test.ts new file mode 100644 index 000000000..c4d1e9a1a --- /dev/null +++ b/packages/typedoc-signature/lib/main.test.ts @@ -0,0 +1,93 @@ +import {readdir} from "node:fs/promises" +import path from "node:path" +import {pipeline} from "node:stream" +import {promisify} from "node:util" +import {type Entity, GroupEntity} from "@onlyoffice/library-declaration/next.ts" +import {Transformer} from "@onlyoffice/typedoc-transformer" +import {Transport} from "@onlyoffice/typedoc-transport" +import {Application, type JSONOutput as J, type TypeDocOptions} from "typedoc" +import {test} from "uvu" +import {equal as eq} from "uvu/assert" +import {Console} from "./console.ts" +import {compute} from "./main.ts" + +const pipe = promisify(pipeline) + +test.before(() => { + Console.shared.mute() +}) + +test.after(() => { + Console.shared.unmute() +}) + +for (const f of await readdir("fixtures")) { + const t = await setupTest(f) + + test(`${f}: ${t.name}`, async () => { + const o = await setupReflection(f, t.options) + + const a = t.collection + const e = await process(o) + + // TODO: handling cases where the equality test has not been met + for (const x of a) { + for (const y of e) { + if (x instanceof GroupEntity || y instanceof GroupEntity) { + continue // TODO + } + if (y.id === x.id) { + eq(x.declaration.signature.verbose, y.declaration.signature.verbose) + } + } + } + }) +} + +test.run() + +interface Test { + name: string + options: Partial + collection: Entity[] +} + +async function setupTest(n: string): Promise { + const m = await import(`../fixtures/${n}/test.ts`) + const t: Test = {name: m.name, options: {}, collection: m.collection} + if (m.options) { + t.options = m.options + } + return t +} + +async function setupReflection(n: string, opts: Partial): Promise { + const d = path.join("fixtures", n) + const e = path.join(d, "main.ts") + const c = path.join(d, "tsconfig.json") + + const a = await Application.bootstrapWithPlugins({ + entryPoints: [e], + logLevel: "None", + name: path.basename(d), + tsconfig: c, + ...opts, + }) + + const p = await a.convert() + if (!p) { + throw new Error("Project is missing") + } + + return a.serializer.projectToObject(p, d) +} + +export async function process(o: J.ProjectReflection): Promise { + const tp = new Transport(o) + const tf = new Transformer(tp) + + await pipe(tp.toReadable(), tf, tp.toWritable()) + + compute(tp) + return tp.entities +} diff --git a/packages/typedoc-signature/package.json b/packages/typedoc-signature/package.json index 19a612226..f30b1c5c0 100644 --- a/packages/typedoc-signature/package.json +++ b/packages/typedoc-signature/package.json @@ -10,9 +10,13 @@ "test": "pnpm test:types && pnpm test:unit" }, "dependencies": { + "@onlyoffice/console": "workspace:^", "@onlyoffice/library-declaration": "workspace:^", "@onlyoffice/signature": "workspace:^", + "@onlyoffice/typedoc-transformer": "workspace:^", + "@onlyoffice/typedoc-transport": "workspace:^", "@onlyoffice/typedoc-util-is-reflection": "workspace:^", + "@onlyoffice/typedoc-util-is-type": "workspace:^", "typedoc": "0.26.11", "typescript": "5.4.5" },