Skip to content

Commit

Permalink
fix: wip renderer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
schoero committed Mar 15, 2023
1 parent a63a52d commit b3bb6f0
Show file tree
Hide file tree
Showing 13 changed files with 311 additions and 72 deletions.
27 changes: 27 additions & 0 deletions src/compiler/ast/entities/module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import { ts } from "unwritten:tests:utils/template.js";
scope("Compiler", EntityKind.Module, () => {

const testFileContent = ts`
/**
* Module description
* @remarks Module remarks
* @example Module example
* @deprecated
* @beta
*/
export module TestModule {
export type TestType = string;
}
Expand All @@ -24,6 +31,26 @@ scope("Compiler", EntityKind.Module, () => {
expect(exportedModule.name).to.equal("TestModule");
});

it("should have a matching description", () => {
expect(exportedModule.description).to.equal("Module description");
});

it("should have a matching remarks", () => {
expect(exportedModule.remarks).to.equal("Module remarks");
});

it("should have a matching example", () => {
expect(exportedModule.example).to.equal("Module example");
});

it("should be deprecated", () => {
expect(exportedModule).to.haveOwnProperty("deprecated");
});

it("should be beta", () => {
expect(exportedModule).to.haveOwnProperty("beta");
});

it("should have parsed exports", () => {
expect(exportedModule.exports.length).to.equal(1);
});
Expand Down
14 changes: 13 additions & 1 deletion src/compiler/ast/entities/module.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getDescriptionByDeclaration, getJSDocTagsByDeclaration } from "unwritten:compiler/ast/shared/jsdoc.js";
import { getPositionByDeclaration } from "unwritten:compiler/ast/shared/position.js";
import { EntityKind } from "unwritten:compiler:enums/entities.js";

import { createSourceFileEntity } from "./source-file.js";
Expand All @@ -11,11 +13,21 @@ import type { CompilerContext } from "unwritten:type-definitions/context.d.js";
export function createModuleEntity(ctx: CompilerContext, symbol: Symbol): ModuleEntity {

const fromSourceFile = createSourceFileEntity(ctx, symbol);

const declaration = symbol.valueDeclaration ?? symbol.declarations?.[0];

const description = declaration && getDescriptionByDeclaration(ctx, declaration);
const jsdocTags = declaration && getJSDocTagsByDeclaration(ctx, declaration);
const position = declaration && getPositionByDeclaration(ctx, declaration);

const kind = EntityKind.Module;

return {
...fromSourceFile,
kind
description,
...jsdocTags,
kind,
position
};

}
27 changes: 27 additions & 0 deletions src/compiler/ast/entities/namespace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import { ts } from "unwritten:tests:utils/template.js";
scope("Compiler", EntityKind.Namespace, () => {

const testFileContent = ts`
/**
* Namespace description
* @remarks Namespace remarks
* @example Namespace example
* @deprecated
* @beta
*/
export namespace TestNamespace {
export type TestType = string;
}
Expand All @@ -24,6 +31,26 @@ scope("Compiler", EntityKind.Namespace, () => {
expect(exportedNamespace.name).to.equal("TestNamespace");
});

it("should have a matching description", () => {
expect(exportedNamespace.description).to.equal("Namespace description");
});

it("should have a matching remarks", () => {
expect(exportedNamespace.remarks).to.equal("Namespace remarks");
});

it("should have a matching example", () => {
expect(exportedNamespace.example).to.equal("Namespace example");
});

it("should be deprecated", () => {
expect(exportedNamespace).to.haveOwnProperty("deprecated");
});

it("should be beta", () => {
expect(exportedNamespace).to.haveOwnProperty("beta");
});

it("should have parsed exports", () => {
expect(exportedNamespace.exports.length).to.equal(1);
});
Expand Down
14 changes: 13 additions & 1 deletion src/compiler/ast/entities/namespace.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getDescriptionByDeclaration, getJSDocTagsByDeclaration } from "unwritten:compiler/ast/shared/jsdoc.js";
import { getPositionByDeclaration } from "unwritten:compiler/ast/shared/position.js";
import { createSourceFileEntity } from "unwritten:compiler:entities";
import { EntityKind } from "unwritten:compiler:enums/entities.js";

Expand All @@ -10,11 +12,21 @@ import type { CompilerContext } from "unwritten:type-definitions/context.d.js";
export function createNamespaceEntity(ctx: CompilerContext, symbol: Symbol): NamespaceEntity {

const fromSourceFile = createSourceFileEntity(ctx, symbol);

const declaration = symbol.valueDeclaration ?? symbol.declarations?.[0];

const description = declaration && getDescriptionByDeclaration(ctx, declaration);
const jsdocTags = declaration && getJSDocTagsByDeclaration(ctx, declaration);
const position = declaration && getPositionByDeclaration(ctx, declaration);

const kind = EntityKind.Namespace;

return {
...fromSourceFile,
kind
description,
...jsdocTags,
kind,
position
};

}
3 changes: 1 addition & 2 deletions src/compiler/ast/types/mapped.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ scope("Compiler", TypeKind.Mapped, () => {
expect(exportedTypeAlias.type.members[0]!.keyType.value).to.equal("A");
expect(exportedTypeAlias.type.members[1]!.keyType.value).to.equal("B");

expect(exportedTypeAlias.type.members[0]!.valueType.kind).to.equal(TypeKind.String);
expect(exportedTypeAlias.type.members[1]!.valueType.kind).to.equal(TypeKind.Number);
expect(exportedTypeAlias.type.members[0]!.valueType.kind).to.equal(TypeKind.Conditional);
});

it("should have a correct type parameter", () => {
Expand Down
8 changes: 6 additions & 2 deletions src/compiler/type-definitions/entities.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,11 @@ export interface MemberEntity extends Entity<EntityKind.Member>, JSDocTags {

//-- Module

export interface ModuleEntity extends Entity<EntityKind.Module> {
export interface ModuleEntity extends Entity<EntityKind.Module>, JSDocTags {
exports: ExportableEntities[];
name: Name;
description?: Description;
position?: Position;
}


Expand All @@ -267,9 +269,11 @@ export interface SourceFileEntity extends Entity<EntityKind.SourceFile> {

//-- Namespace

export interface NamespaceEntity extends Entity<EntityKind.Namespace> {
export interface NamespaceEntity extends Entity<EntityKind.Namespace>, JSDocTags {
exports: ExportableEntities[];
name: Name;
description?: Description;
position?: Position;
}


Expand Down
91 changes: 65 additions & 26 deletions src/renderer/markup/ast-converter/entities/module.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { expect, it } from "vitest";

import { EntityKind } from "unwritten:compiler:enums/entities.js";
import { TypeKind } from "unwritten:compiler:enums/types.js";
import {
convertModuleEntityForDocumentation,
convertModuleEntityForTableOfContents
Expand All @@ -19,43 +18,83 @@ scope("MarkupRenderer", EntityKind.Module, () => {

// #region Entity

const testFunction: Testable<ModuleEntity> = {
exports: [
{
description: undefined,
kind: EntityKind.TypeAlias,
name: "Test",
position: {
column: 2,
file: "/file.ts",
line: 2
},
type: {
kind: TypeKind.String,
name: "string"
},
typeParameters: undefined
}
],
const moduleEntity: Testable<ModuleEntity> = {
beta: undefined,
deprecated: undefined,
description: "Module description",
example: "Module example",
exports: [],
id: 4053,
kind: EntityKind.Module,
name: "Module"
name: "Module",
position: {
column: 4,
file: "/file.ts",
line: 9
},
remarks: "Module remarks"
};

// #endregion

// #region Source

// /**
// * Module description
// *
// * @remarks Module remarks
// * @example Module example
// * @deprecated
// * @beta
// */
// export module Module {

// }

// #endregion

const ctx = createRenderContext();

const renderedFunctionForTableOfContents = convertModuleEntityForTableOfContents(ctx, testFunction as ModuleEntity);
const renderedFunctionForDocumentation = convertModuleEntityForDocumentation(ctx, testFunction as ModuleEntity);
const renderedModuleForTableOfContents = convertModuleEntityForTableOfContents(ctx, moduleEntity as ModuleEntity);
const renderedModuleForDocumentation = convertModuleEntityForDocumentation(ctx, moduleEntity as ModuleEntity);

const [
position,
tags,
description,
remarks,
example,
childrenContainer
] = renderedModuleForDocumentation.children;

it("should have a matching title", () => {
expect(renderedFunctionForTableOfContents.children).to.equal("Module");
expect(renderedFunctionForDocumentation.title).to.equal("Module");
expect(renderedModuleForTableOfContents.title).to.equal("Module");
expect(renderedModuleForDocumentation.title).to.equal("Module");
});

it("should have a matching description", () => {
expect(description.children[0]).to.equal("Module description");
});

it("should have a matching remarks", () => {
expect(remarks.children[0]).to.equal("Module remarks");
});

it("should have a matching example", () => {
expect(example.children[0]).to.equal("Module example");
});

it("should have a matching tags", () => {
expect(tags.children[0]).to.include("deprecated");
expect(tags.children[0]).to.include("beta");
});

it("should have a position", () => {
expect(position).to.not.equal(undefined);
});

it("should have matching children", () => {
expect(renderedFunctionForTableOfContents.children).to.have.lengthOf(1);
expect(renderedFunctionForDocumentation.children).to.have.lengthOf(1);
expect(childrenContainer.children).to.have.lengthOf(0);
});

}
Expand Down
27 changes: 25 additions & 2 deletions src/renderer/markup/ast-converter/entities/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import {
convertEntityForDocumentation,
convertEntityForTableOfContents
} from "unwritten:renderer/markup/ast-converter/index.js";
import { createTitleNode } from "unwritten:renderer/markup/utils/nodes.js";
import { convertJSDocTags } from "unwritten:renderer/markup/ast-converter/shared/jsdoc-tags.js";
import { convertPosition } from "unwritten:renderer/markup/ast-converter/shared/position.js";
import {
createContainerNode,
createParagraphNode,
createSmallNode,
createTitleNode
} from "unwritten:renderer/markup/utils/nodes.js";

import type { ModuleEntity } from "unwritten:compiler:type-definitions/entities.js";
import type { MarkupRenderContexts } from "unwritten:renderer/markup/types-definitions/markup.d.js";
Expand Down Expand Up @@ -33,12 +40,28 @@ export function convertModuleEntityForDocumentation(ctx: MarkupRenderContexts, m
const name = moduleEntity.name;
const id = moduleEntity.id;

const description = moduleEntity.description ?? "";
const remarks = moduleEntity.remarks ?? "";
const example = moduleEntity.example ?? "";

const position = moduleEntity.position ? convertPosition(ctx, moduleEntity.position) : "";
const jsdocTags = convertJSDocTags(ctx, moduleEntity);

const children = moduleEntity.exports.map(exportedEntity => convertEntityForDocumentation(ctx, exportedEntity));

return createTitleNode(
name,
id,
children
[
createSmallNode(position),
createParagraphNode(jsdocTags),
createParagraphNode(description),
createParagraphNode(remarks),
createParagraphNode(example),
createContainerNode(
...children
)
]
);

}
Loading

0 comments on commit b3bb6f0

Please sign in to comment.