UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

111 lines (109 loc) 6.21 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("insertNamespaces", () => { function doTest(startCode, index, structures, expectedText) { const { sourceFile } = testHelpers_1.getInfoFromText(startCode); const result = sourceFile.insertNamespaces(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", hasModuleKeyword: true }], "module Identifier {\n}\n"); }); it("should insert at the start of a file", () => { doTest("namespace Identifier2 {\n}\n", 0, [{ name: "Identifier1" }], "namespace Identifier1 {\n}\n\nnamespace Identifier2 {\n}\n"); }); it("should insert at the end of a file", () => { doTest("namespace Identifier1 {\n}\n", 1, [{ name: "Identifier2" }], "namespace Identifier1 {\n}\n\nnamespace Identifier2 {\n}\n"); }); it("should insert in the middle of children", () => { doTest("namespace Identifier1 {\n}\n\nnamespace Identifier3 {\n}\n", 1, [{ name: "Identifier2" }], "namespace Identifier1 {\n}\n\nnamespace Identifier2 {\n}\n\nnamespace Identifier3 {\n}\n"); }); it("should insert multiple", () => { doTest("namespace Identifier1 {\n}\n", 1, [{ name: "Identifier2" }, { name: "Identifier3" }], "namespace Identifier1 {\n}\n\nnamespace Identifier2 {\n}\n\nnamespace 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.insertNamespaces(0, [{ name: "Identifier" }]); chai_1.expect(sourceFile.getFullText()).to.equal("namespace Namespace {\n namespace Identifier {\n }\n}\n"); }); }); describe("insertNamespace", () => { function doTest(startCode, index, structure, expectedText) { const { sourceFile } = testHelpers_1.getInfoFromText(startCode); const result = sourceFile.insertNamespace(index, structure); chai_1.expect(sourceFile.getFullText()).to.equal(expectedText); chai_1.expect(result).to.be.instanceOf(compiler_1.NamespaceDeclaration); } it("should insert", () => { doTest("namespace Identifier2 {\n}\n", 0, { name: "Identifier1" }, "namespace Identifier1 {\n}\n\nnamespace Identifier2 {\n}\n"); }); }); describe("addNamespaces", () => { function doTest(startCode, structures, expectedText) { const { sourceFile } = testHelpers_1.getInfoFromText(startCode); const result = sourceFile.addNamespaces(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" }, { name: "Identifier3" }], "namespace Identifier1 {\n}\n\nnamespace Identifier2 {\n}\n\nnamespace Identifier3 {\n}\n"); }); }); describe("addNamespace", () => { function doTest(startCode, structure, expectedText) { const { sourceFile } = testHelpers_1.getInfoFromText(startCode); const result = sourceFile.addNamespace(structure); chai_1.expect(sourceFile.getFullText()).to.equal(expectedText); chai_1.expect(result).to.be.instanceOf(compiler_1.NamespaceDeclaration); } it("should add one", () => { doTest("namespace Identifier1 {\n}\n", { name: "Identifier2" }, "namespace Identifier1 {\n}\n\nnamespace Identifier2 {\n}\n"); }); }); describe("getNamespaces", () => { const { sourceFile } = testHelpers_1.getInfoFromText("namespace Identifier1 {}\nnamespace Identifier2 {}"); const namespaces = sourceFile.getNamespaces(); it("should have the expected number of namespaces", () => { chai_1.expect(namespaces.length).to.equal(2); }); it("should have correct type", () => { chai_1.expect(namespaces[0]).to.be.instanceOf(compiler_1.NamespaceDeclaration); }); }); describe("getNamespace", () => { const { sourceFile } = testHelpers_1.getInfoFromText("namespace Identifier1 {}\nnamespace Identifier2 {}"); it("should get a namespace by a name", () => { chai_1.expect(sourceFile.getNamespace("Identifier2").getName()).to.equal("Identifier2"); }); it("should get a namespace by a search function", () => { chai_1.expect(sourceFile.getNamespace(c => c.getName() === "Identifier1").getName()).to.equal("Identifier1"); }); it("should return undefined when the namespace doesn't exist", () => { chai_1.expect(sourceFile.getNamespace("asdf")).to.be.undefined; }); }); describe("getNamespaceOrThrow", () => { const { sourceFile } = testHelpers_1.getInfoFromText("namespace Identifier1 {}\nnamespace Identifier2 {}"); it("should get a namespace by a name", () => { chai_1.expect(sourceFile.getNamespaceOrThrow("Identifier2").getName()).to.equal("Identifier2"); }); it("should get a namespace by a search function", () => { chai_1.expect(sourceFile.getNamespaceOrThrow(c => c.getName() === "Identifier1").getName()).to.equal("Identifier1"); }); it("should throw when the namespace doesn't exist", () => { chai_1.expect(() => sourceFile.getNamespaceOrThrow("asdf")).to.throw(); }); }); }); //# sourceMappingURL=namespaceTests.js.map