UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

110 lines (108 loc) 6.17 kB
"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("insertInterfaces", () => { function doTest(startCode, index, structures, expectedText) { const { sourceFile } = testHelpers_1.getInfoFromText(startCode); const result = sourceFile.insertInterfaces(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" }], "interface Identifier {\n}\n"); }); it("should insert at the start of a file", () => { doTest("interface Identifier2 {\n}\n", 0, [{ name: "Identifier1" }], "interface Identifier1 {\n}\n\ninterface Identifier2 {\n}\n"); }); it("should insert at the end of a file", () => { doTest("interface Identifier1 {\n}\n", 1, [{ name: "Identifier2" }], "interface Identifier1 {\n}\n\ninterface Identifier2 {\n}\n"); }); it("should insert in the middle of children", () => { doTest("interface Identifier1 {\n}\n\ninterface Identifier3 {\n}\n", 1, [{ name: "Identifier2" }], "interface Identifier1 {\n}\n\ninterface Identifier2 {\n}\n\ninterface Identifier3 {\n}\n"); }); it("should insert multiple", () => { doTest("interface Identifier1 {\n}\n", 1, [{ name: "Identifier2" }, { name: "Identifier3" }], "interface Identifier1 {\n}\n\ninterface Identifier2 {\n}\n\ninterface 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.insertInterfaces(0, [{ name: "Identifier" }]); chai_1.expect(sourceFile.getFullText()).to.equal("namespace Namespace {\n interface Identifier {\n }\n}\n"); }); }); describe("insertInterface", () => { function doTest(startCode, index, structure, expectedText) { const { sourceFile } = testHelpers_1.getInfoFromText(startCode); const result = sourceFile.insertInterface(index, structure); chai_1.expect(sourceFile.getFullText()).to.equal(expectedText); chai_1.expect(result).to.be.instanceOf(compiler_1.InterfaceDeclaration); } it("should insert", () => { doTest("interface Identifier2 {\n}\n", 0, { name: "Identifier1" }, "interface Identifier1 {\n}\n\ninterface Identifier2 {\n}\n"); }); }); describe("addInterfaces", () => { function doTest(startCode, structures, expectedText) { const { sourceFile } = testHelpers_1.getInfoFromText(startCode); const result = sourceFile.addInterfaces(structures); chai_1.expect(sourceFile.getFullText()).to.equal(expectedText); chai_1.expect(result.length).to.equal(structures.length); } it("should add multiple", () => { doTest("interface Identifier1 {\n}\n", [{ name: "Identifier2" }, { name: "Identifier3" }], "interface Identifier1 {\n}\n\ninterface Identifier2 {\n}\n\ninterface Identifier3 {\n}\n"); }); }); describe("addInterface", () => { function doTest(startCode, structure, expectedText) { const { sourceFile } = testHelpers_1.getInfoFromText(startCode); const result = sourceFile.addInterface(structure); chai_1.expect(sourceFile.getFullText()).to.equal(expectedText); chai_1.expect(result).to.be.instanceOf(compiler_1.InterfaceDeclaration); } it("should add one", () => { doTest("interface Identifier1 {\n}\n", { name: "Identifier2" }, "interface Identifier1 {\n}\n\ninterface Identifier2 {\n}\n"); }); }); describe("getInterfaces", () => { const { sourceFile } = testHelpers_1.getInfoFromText("interface Identifier1 {}\ninterface Identifier2 {}"); const interfaces = sourceFile.getInterfaces(); it("should have the expected number of interfaces", () => { chai_1.expect(interfaces.length).to.equal(2); }); it("should have correct type", () => { chai_1.expect(interfaces[0]).to.be.instanceOf(compiler_1.InterfaceDeclaration); }); }); describe("getInterface", () => { const { sourceFile } = testHelpers_1.getInfoFromText("interface Identifier1 {}\ninterface Identifier2 {}"); it("should get an interface by a name", () => { chai_1.expect(sourceFile.getInterface("Identifier2").getName()).to.equal("Identifier2"); }); it("should get a interface by a search function", () => { chai_1.expect(sourceFile.getInterface(c => c.getName() === "Identifier1").getName()).to.equal("Identifier1"); }); it("should return undefined when the interface doesn't exist", () => { chai_1.expect(sourceFile.getInterface("asdf")).to.be.undefined; }); }); describe("getInterfaceOrThrow", () => { const { sourceFile } = testHelpers_1.getInfoFromText("interface Identifier1 {}\ninterface Identifier2 {}"); it("should get an interface by a name", () => { chai_1.expect(sourceFile.getInterfaceOrThrow("Identifier2").getName()).to.equal("Identifier2"); }); it("should get a interface by a search function", () => { chai_1.expect(sourceFile.getInterfaceOrThrow(c => c.getName() === "Identifier1").getName()).to.equal("Identifier1"); }); it("should throw when the interface doesn't exist", () => { chai_1.expect(() => sourceFile.getInterfaceOrThrow("asdf")).to.throw(); }); }); }); //# sourceMappingURL=interfaceTests.js.map