Skip to content

Commit

Permalink
#28, test registration of writer functions, cloning type structures.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajvincent committed Dec 4, 2023
1 parent 84794ff commit c061df1
Showing 1 changed file with 44 additions and 9 deletions.
53 changes: 44 additions & 9 deletions stage_2_fullset/spec-snapshot/source/TypeStructures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import {
CodeBlockWriter,
} from "ts-morph";

import type {
Class
} from "type-fest";

import {
ArrayTypeStructureImpl,
ConditionalTypeStructureImpl,
Expand All @@ -27,11 +31,13 @@ import {
TypeArgumentedTypeStructureImpl,
TypeParameterDeclarationImpl,
TypeStructureKind,
type TypeStructures,
UnionTypeStructureImpl,
WriterTypeStructureImpl,
} from "#stage_two/snapshot/source/exports.js";

import {
TypeStructuresBase,
TypeStructureClassesMap
} from "#stage_two/snapshot/source/internal-exports.js";

Expand All @@ -47,12 +53,33 @@ describe("TypeStructure for ts-morph (stage 2): ", () => {

const stringBarTyped = new StringTypeStructureImpl("bar");

function checkCloneAndRegistration(
typedWriter: TypeStructures,
typeClass: Class<TypeStructures>,
singletonWriter: boolean
): void
{
const cloneWriter = TypeStructureClassesMap.clone(typedWriter);
expect(cloneWriter).toBeInstanceOf(typeClass);
if (typeof cloneWriter === "object") {
expect(
TypeStructuresBase.getTypeStructureForCallback(cloneWriter.writerFunction)
).toBe(singletonWriter ? typedWriter : cloneWriter);
expect(cloneWriter.kind).toBe(typedWriter.kind);
}
expect(
TypeStructuresBase.getTypeStructureForCallback(typedWriter.writerFunction)
).toBe(typedWriter);
}

it("ArrayTypedStructureImpl", () => {
const typedWriter = new ArrayTypeStructureImpl(fooTyped);
typedWriter.writerFunction(writer);
expect<string>(writer.toString()).toBe("foo[]");

expect(typedWriter.kind).toBe(TypeStructureKind.Array);

checkCloneAndRegistration(typedWriter, ArrayTypeStructureImpl, false);
});

it("ConditionalTypedStructureImpl", () => {
Expand All @@ -76,6 +103,7 @@ describe("TypeStructure for ts-morph (stage 2): ", () => {
);

expect(typedWriter.kind).toBe(TypeStructureKind.Conditional);
checkCloneAndRegistration(typedWriter, ConditionalTypeStructureImpl, false);
});

it("FunctionTypeStructureImpl", () => {
Expand All @@ -89,6 +117,7 @@ describe("TypeStructure for ts-morph (stage 2): ", () => {
expect<string>(writer.toString()).toBe("<UserType extends number = 6>(nst: NumberStringType): boolean");

expect(typedWriter.kind).toBe(TypeStructureKind.Function);
checkCloneAndRegistration(typedWriter, FunctionTypeStructureImpl, false);
});

it("IndexedAccessTypeStructureImpl", () => {
Expand All @@ -103,7 +132,6 @@ describe("TypeStructure for ts-morph (stage 2): ", () => {
const typedWriter = new InferTypeStructureImpl(typeParam);
typedWriter.writerFunction(writer);
expect<string>(writer.toString()).toBe(`infer UserType extends number = 6`);

expect(typedWriter.kind).toBe(TypeStructureKind.Infer);
});

Expand All @@ -115,12 +143,14 @@ describe("TypeStructure for ts-morph (stage 2): ", () => {
typedWriter.writerFunction(writer);
expect<string>(writer.toString()).toBe(`foo & NumberStringType`);
expect(typedWriter.kind).toBe(TypeStructureKind.Intersection);
checkCloneAndRegistration(typedWriter, IntersectionTypeStructureImpl, false);
});

it("LiteralTypeStructureImpl", () => {
fooTyped.writerFunction(writer);
expect<string>(writer.toString()).toBe("foo");
expect(fooTyped.kind).toBe(TypeStructureKind.Literal);
checkCloneAndRegistration(fooTyped, LiteralTypeStructureImpl, false);
});

it("MappedTypeStructureImpl", () => {
Expand All @@ -132,6 +162,7 @@ describe("TypeStructure for ts-morph (stage 2): ", () => {
expect<string>(writer.toString()).toBe(`{\n +readonly [UserType in number]: boolean;\n}`);

expect(typedWriter.kind).toBe(TypeStructureKind.Mapped);
checkCloneAndRegistration(typedWriter, MappedTypeStructureImpl, false);
});

it("MemberedObjectTypeStructureImpl", () => {
Expand All @@ -151,6 +182,7 @@ describe("TypeStructure for ts-morph (stage 2): ", () => {
expect<string>(writer.toString()).toBe(`{\n foo(firstArg: string): void;\n}`);

expect(typedWriter.kind).toBe(TypeStructureKind.MemberedObject);
checkCloneAndRegistration(typedWriter, MemberedObjectTypeStructureImpl, false);
});

it("ParameterTypeStructureImpl", () => {
Expand All @@ -163,6 +195,7 @@ describe("TypeStructure for ts-morph (stage 2): ", () => {
typedWriter.typeStructure = nstTyped;
typedWriter.writerFunction(writer);
expect<string>(writer.toString()).toBe("nst: NumberStringType");
checkCloneAndRegistration(typedWriter, ParameterTypeStructureImpl, false);
});

it("ParenthesesTypeStructureImpl", () => {
Expand All @@ -183,6 +216,8 @@ describe("TypeStructure for ts-morph (stage 2): ", () => {
typedWriter.childTypes.push("unknown")
typedWriter.writerFunction(writer);
expect<string>(writer.toString()).toBe("(false)");

checkCloneAndRegistration(typedWriter, ParenthesesTypeStructureImpl, false);
});

it("PrefixOperatorsTypeStructureImpl", () => {
Expand All @@ -195,6 +230,7 @@ describe("TypeStructure for ts-morph (stage 2): ", () => {

expect<string>(writer.toString()).toBe("typeof readonly NumberStringType");
expect(typedWriter.kind).toBe(TypeStructureKind.PrefixOperators);
checkCloneAndRegistration(typedWriter, PrefixOperatorsTypeStructureImpl, false);
});

it("QualifiedNameTypeStructureImpl", () => {
Expand All @@ -205,12 +241,14 @@ describe("TypeStructure for ts-morph (stage 2): ", () => {
typedWriter.writerFunction(writer);
expect<string>(writer.toString()).toBe(`NumberStringType.foo`);
expect(typedWriter.kind).toBe(TypeStructureKind.QualifiedName);
checkCloneAndRegistration(typedWriter, QualifiedNameTypeStructureImpl, false);
});

it("StringTypeStructureImpl", () => {
stringBarTyped.writerFunction(writer);
expect<string>(writer.toString()).toBe(`"bar"`);
expect(stringBarTyped.kind).toBe(TypeStructureKind.String);
checkCloneAndRegistration(stringBarTyped, StringTypeStructureImpl, false);
});

it("TemplateLiteralTypeStructureImpl", () => {
Expand All @@ -228,6 +266,7 @@ describe("TypeStructure for ts-morph (stage 2): ", () => {
expect<string>(writer.toString()).toBe('`one${"A" | "B"}two${"C" | "D"}three`');

expect(typedWriter.kind).toBe(TypeStructureKind.TemplateLiteral);
checkCloneAndRegistration(typedWriter, TemplateLiteralTypeStructureImpl, false);
});

it("TupleTypeStructureImpl", () => {
Expand All @@ -236,6 +275,7 @@ describe("TypeStructure for ts-morph (stage 2): ", () => {
typedWriter.writerFunction(writer);
expect<string>(writer.toString()).toBe(`[foo, NumberStringType]`);
expect(typedWriter.kind).toBe(TypeStructureKind.Tuple);
checkCloneAndRegistration(typedWriter, TupleTypeStructureImpl, false);
});

it("TypeArgumentedTypeStructureImpl", () => {
Expand All @@ -245,13 +285,15 @@ describe("TypeStructure for ts-morph (stage 2): ", () => {
expect<string>(writer.toString()).toBe(`foo<"bar", NumberStringType>`);

expect(typedWriter.kind).toBe(TypeStructureKind.TypeArgumented);
checkCloneAndRegistration(typedWriter, TypeArgumentedTypeStructureImpl, false);
});

it("UnionTypeStructureImpl", () => {
const typedWriter = new UnionTypeStructureImpl([fooTyped, nstTyped]);
typedWriter.writerFunction(writer);
expect<string>(writer.toString()).toBe(`foo | NumberStringType`);
expect(typedWriter.kind).toBe(TypeStructureKind.Union);
checkCloneAndRegistration(typedWriter, UnionTypeStructureImpl, false);
});

it("WriterTypeStructureImpl", () => {
Expand All @@ -262,6 +304,7 @@ describe("TypeStructure for ts-morph (stage 2): ", () => {
typedWriter.writerFunction(writer);
expect<string>(writer.toString()).toBe(`hi mom`);
expect(typedWriter.kind).toBe(TypeStructureKind.Writer);
checkCloneAndRegistration(typedWriter, WriterTypeStructureImpl, true);
});

it("TypeStructureClassesMap is complete", () => {
Expand All @@ -271,12 +314,4 @@ describe("TypeStructure for ts-morph (stage 2): ", () => {

expect(TypeStructureClassesMap.size).toBe(kinds.length);
});

xit("Each test covers a .clone() method", () => {
expect(true).toBe(false);
});

xit("Each test covers a registerCallback case", () => {
expect(true).toBe(false);
});
});

0 comments on commit c061df1

Please sign in to comment.