ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
111 lines (109 loc) • 6.27 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("insertFunctions", () => {
function doTest(startCode, index, structures, expectedText) {
const { sourceFile } = testHelpers_1.getInfoFromText(startCode);
const result = sourceFile.insertFunctions(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"
}], "function Identifier() {\n}\n");
});
it("should insert at the start of a file", () => {
doTest("function Identifier2() {\n}\n", 0, [{ name: "Identifier1" }], "function Identifier1() {\n}\n\nfunction Identifier2() {\n}\n");
});
it("should insert at the end of a file", () => {
doTest("function Identifier1() {\n}\n", 1, [{ name: "Identifier2" }], "function Identifier1() {\n}\n\nfunction Identifier2() {\n}\n");
});
it("should insert in the middle of children", () => {
doTest("function Identifier1() {\n}\n\nfunction Identifier3() {\n}\n", 1, [{ name: "Identifier2" }], "function Identifier1() {\n}\n\nfunction Identifier2() {\n}\n\nfunction Identifier3() {\n}\n");
});
it("should insert multiple", () => {
doTest("function Identifier1() {\n}\n", 1, [{ name: "Identifier2" }, { name: "Identifier3" }], "function Identifier1() {\n}\n\nfunction Identifier2() {\n}\n\nfunction Identifier3() {\n}\n");
});
it("should have the expected text adding to non-source file", () => {
const { sourceFile } = testHelpers_1.getInfoFromText("namespace Namespace {\n}\n");
const namespaceDec = sourceFile.getNamespaces()[0];
namespaceDec.insertFunctions(0, [{
name: "Identifier"
}]);
chai_1.expect(sourceFile.getFullText()).to.equal("namespace Namespace {\n function Identifier() {\n }\n}\n");
});
});
describe("insertFunction", () => {
function doTest(startCode, index, structure, expectedText) {
const { sourceFile } = testHelpers_1.getInfoFromText(startCode);
const result = sourceFile.insertFunction(index, structure);
chai_1.expect(sourceFile.getFullText()).to.equal(expectedText);
chai_1.expect(result).to.be.instanceOf(compiler_1.FunctionDeclaration);
}
it("should insert", () => {
doTest("function Identifier2() {\n}\n", 0, { name: "Identifier1" }, "function Identifier1() {\n}\n\nfunction Identifier2() {\n}\n");
});
});
describe("addFunctions", () => {
function doTest(startCode, structures, expectedText) {
const { sourceFile } = testHelpers_1.getInfoFromText(startCode);
const result = sourceFile.addFunctions(structures);
chai_1.expect(sourceFile.getFullText()).to.equal(expectedText);
chai_1.expect(result.length).to.equal(structures.length);
}
it("should add multiple", () => {
doTest("function Identifier1() {\n}\n", [{ name: "Identifier2" }, { name: "Identifier3" }], "function Identifier1() {\n}\n\nfunction Identifier2() {\n}\n\nfunction Identifier3() {\n}\n");
});
});
describe("addFunction", () => {
function doTest(startCode, structure, expectedText) {
const { sourceFile } = testHelpers_1.getInfoFromText(startCode);
const result = sourceFile.addFunction(structure);
chai_1.expect(sourceFile.getFullText()).to.equal(expectedText);
chai_1.expect(result).to.be.instanceOf(compiler_1.FunctionDeclaration);
}
it("should add one", () => {
doTest("function Identifier1() {\n}\n", { name: "Identifier2" }, "function Identifier1() {\n}\n\nfunction Identifier2() {\n}\n");
});
});
describe("getFunctions", () => {
const { sourceFile } = testHelpers_1.getInfoFromText("function Identifier1();function Identifier1() {}\nfunction Identifier2() {}" +
"declare function Identifier3(); declare function Identifier3();");
const functions = sourceFile.getFunctions();
it("should have the expected number of functions", () => {
chai_1.expect(functions.length).to.equal(4);
});
it("should have correct type", () => {
chai_1.expect(functions[0]).to.be.instanceOf(compiler_1.FunctionDeclaration);
});
});
describe("getFunction", () => {
const { sourceFile } = testHelpers_1.getInfoFromText("function Identifier1() {}\nfunction Identifier2() {}");
it("should get a function by a name", () => {
chai_1.expect(sourceFile.getFunction("Identifier2").getName()).to.equal("Identifier2");
});
it("should get a function by a search function", () => {
chai_1.expect(sourceFile.getFunction(c => c.getName() === "Identifier1").getName()).to.equal("Identifier1");
});
it("should return undefined when the function doesn't exist", () => {
chai_1.expect(sourceFile.getFunction("asdf")).to.be.undefined;
});
});
describe("getFunctionOrThrow", () => {
const { sourceFile } = testHelpers_1.getInfoFromText("function Identifier1() {}\nfunction Identifier2() {}");
it("should get a function by a name", () => {
chai_1.expect(sourceFile.getFunctionOrThrow("Identifier2").getName()).to.equal("Identifier2");
});
it("should get a function by a search function", () => {
chai_1.expect(sourceFile.getFunctionOrThrow(c => c.getName() === "Identifier1").getName()).to.equal("Identifier1");
});
it("should throw when the function doesn't exist", () => {
chai_1.expect(() => sourceFile.getFunctionOrThrow("asdf")).to.throw();
});
});
});
//# sourceMappingURL=functionTests.js.map