ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
115 lines (113 loc) • 6.75 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const compiler_1 = require("./../../../../compiler");
const testHelpers_1 = require("./../../testHelpers");
describe("StatementedNode", () => {
describe("insertTypeAliases", () => {
function doTest(startCode, index, structures, expectedText) {
const { sourceFile } = testHelpers_1.getInfoFromText(startCode);
const result = sourceFile.insertTypeAliases(index, structures);
chai_1.expect(sourceFile.getFullText()).to.equal(expectedText);
chai_1.expect(result.length).to.equal(structures.length);
}
it("should insert to an empty file", () => {
doTest("", 0, [{
name: "Identifier",
type: "string"
}], "type Identifier = string;\n");
});
it("should insert at the start with two new lines for a non-type alias after", () => {
doTest("namespace Identifier2 {\n}\n", 0, [{ name: "Identifier1", type: "string" }], "type Identifier1 = string;\n\nnamespace Identifier2 {\n}\n");
});
it("should insert at the start with one new lines for a type alias after", () => {
doTest("type Identifier2 = string;\n", 0, [{ name: "Identifier1", type: "string" }], "type Identifier1 = string;\ntype Identifier2 = string;\n");
});
it("should insert at the end of a file with two new lines for a non-type alias before", () => {
doTest("namespace Identifier1 {\n}\n", 1, [{ name: "Identifier2", type: "string" }], "namespace Identifier1 {\n}\n\ntype Identifier2 = string;\n");
});
it("should insert in the middle of children", () => {
doTest("namespace Identifier1 {\n}\n\nnamespace Identifier3 {\n}\n", 1, [{ name: "Identifier2", type: "string" }], "namespace Identifier1 {\n}\n\ntype Identifier2 = string;\n\nnamespace Identifier3 {\n}\n");
});
it("should insert multiple", () => {
doTest("namespace Identifier1 {\n}\n", 1, [{ name: "Identifier2", type: "number" }, { name: "Identifier3", type: "string" }], "namespace Identifier1 {\n}\n\ntype Identifier2 = number;\ntype Identifier3 = string;\n");
});
it("should have the expected text adding to non-source file", () => {
const { sourceFile } = testHelpers_1.getInfoFromText("namespace Identifier {\n}\n");
const namespaceDec = sourceFile.getNamespaces()[0];
namespaceDec.insertTypeAliases(0, [{
name: "Identifier",
type: "string"
}]);
chai_1.expect(sourceFile.getFullText()).to.equal("namespace Identifier {\n type Identifier = string;\n}\n");
});
});
describe("insertTypeAlias", () => {
function doTest(startCode, index, structure, expectedText) {
const { sourceFile } = testHelpers_1.getInfoFromText(startCode);
const result = sourceFile.insertTypeAlias(index, structure);
chai_1.expect(sourceFile.getFullText()).to.equal(expectedText);
chai_1.expect(result).to.be.instanceOf(compiler_1.TypeAliasDeclaration);
}
it("should insert", () => {
doTest("namespace Identifier2 {\n}\n", 0, { name: "Identifier1", type: "string" }, "type Identifier1 = string;\n\nnamespace Identifier2 {\n}\n");
});
});
describe("addTypeAliases", () => {
function doTest(startCode, structures, expectedText) {
const { sourceFile } = testHelpers_1.getInfoFromText(startCode);
const result = sourceFile.addTypeAliases(structures);
chai_1.expect(sourceFile.getFullText()).to.equal(expectedText);
chai_1.expect(result.length).to.equal(structures.length);
}
it("should add multiple", () => {
doTest("namespace Identifier1 {\n}\n", [{ name: "Identifier2", type: "string" }, { name: "Identifier3", type: "number" }], "namespace Identifier1 {\n}\n\ntype Identifier2 = string;\ntype Identifier3 = number;\n");
});
});
describe("addTypeAlias", () => {
function doTest(startCode, structure, expectedText) {
const { sourceFile } = testHelpers_1.getInfoFromText(startCode);
const result = sourceFile.addTypeAlias(structure);
chai_1.expect(sourceFile.getFullText()).to.equal(expectedText);
chai_1.expect(result).to.be.instanceOf(compiler_1.TypeAliasDeclaration);
}
it("should add one", () => {
doTest("namespace Identifier1 {\n}\n", { name: "Identifier2", type: "string" }, "namespace Identifier1 {\n}\n\ntype Identifier2 = string;\n");
});
});
describe("getTypeAliases", () => {
const { sourceFile } = testHelpers_1.getInfoFromText("type Identifier1 = string;\ntype Identifier2 = number;");
const typeAliases = sourceFile.getTypeAliases();
it("should have the expected number of typeAliases", () => {
chai_1.expect(typeAliases.length).to.equal(2);
});
it("should have correct type", () => {
chai_1.expect(typeAliases[0]).to.be.instanceOf(compiler_1.TypeAliasDeclaration);
});
});
describe("getTypeAlias", () => {
const { sourceFile } = testHelpers_1.getInfoFromText("type Identifier1 = string;\ntype Identifier2 = number;");
it("should get a type alias by a name", () => {
chai_1.expect(sourceFile.getTypeAlias("Identifier2").getName()).to.equal("Identifier2");
});
it("should get a type alias by a search function", () => {
chai_1.expect(sourceFile.getTypeAlias(c => c.getName() === "Identifier1").getName()).to.equal("Identifier1");
});
it("should return undefined when the type alias doesn't exist", () => {
chai_1.expect(sourceFile.getTypeAlias("asdf")).to.be.undefined;
});
});
describe("getTypeAliasOrThrow", () => {
const { sourceFile } = testHelpers_1.getInfoFromText("type Identifier1 = string;\ntype Identifier2 = number;");
it("should get a type alias by a name", () => {
chai_1.expect(sourceFile.getTypeAliasOrThrow("Identifier2").getName()).to.equal("Identifier2");
});
it("should get a type alias by a search function", () => {
chai_1.expect(sourceFile.getTypeAliasOrThrow(c => c.getName() === "Identifier1").getName()).to.equal("Identifier1");
});
it("should throw when the type alias doesn't exist", () => {
chai_1.expect(() => sourceFile.getTypeAliasOrThrow("asdf")).to.throw();
});
});
});
//# sourceMappingURL=typeAliasTests.js.map