ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
149 lines (147 loc) • 8.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const errors = require("./../../../errors");
const testHelpers_1 = require("./../testHelpers");
describe("NamespaceDeclaration", () => {
describe("getName", () => {
function doTest(text, expectedName) {
const { firstChild } = testHelpers_1.getInfoFromText(text);
chai_1.expect(firstChild.getName()).to.equal(expectedName);
}
it("should get the name when not using dot notation", () => {
doTest("namespace MyNamespace {}", "MyNamespace");
});
it("should get the name when using dot notation", () => {
doTest("namespace MyNamespace.Inner.MoreInner {}", "MyNamespace.Inner.MoreInner");
});
});
describe("getNameIdentifiers", () => {
function doTest(text, expectedNames) {
const { firstChild } = testHelpers_1.getInfoFromText(text);
chai_1.expect(firstChild.getNameIdentifiers().map(n => n.getText())).to.deep.equal(expectedNames);
}
it("should get the name nodes when not using dot notation", () => {
doTest("namespace MyNamespace {}", ["MyNamespace"]);
});
it("should get the name nodes when using dot notation", () => {
doTest("namespace MyNamespace.Inner.MoreInner {}", ["MyNamespace", "Inner", "MoreInner"]);
});
});
describe("rename", () => {
it("should rename a namespace that doesn't use dot notation", () => {
const { firstChild } = testHelpers_1.getInfoFromText("namespace MyNamespace {}");
firstChild.rename("NewName");
chai_1.expect(firstChild.getFullText()).to.equal("namespace NewName {}");
});
it("should throw an exception when passing in a name with a period", () => {
const { firstChild } = testHelpers_1.getInfoFromText("namespace MyNamespace {}");
chai_1.expect(() => firstChild.rename("NewName.Inner")).to.throw(errors.NotSupportedError);
});
it("should throw an exception when renaming a namespace whose name uses dot notation", () => {
const { firstChild } = testHelpers_1.getInfoFromText("namespace MyNamespace.MyInner {}");
chai_1.expect(() => firstChild.rename("NewName")).to.throw(errors.NotSupportedError);
});
});
describe("setName", () => {
it("should throw an exception when using dot notation because it's not implemented", () => {
const { firstChild } = testHelpers_1.getInfoFromText("namespace MyNamespace {}");
chai_1.expect(() => firstChild.setName("NewName.NewName")).to.throw(errors.NotImplementedError);
});
it("should throw an exception when setting a namepsace name that already uses dot notation because it's not implemented", () => {
const { firstChild } = testHelpers_1.getInfoFromText("namespace MyNamespace.Name {}");
chai_1.expect(() => firstChild.setName("NewName")).to.throw(errors.NotImplementedError);
});
it("should set the name when not using dot notation", () => {
const { firstChild } = testHelpers_1.getInfoFromText("namespace MyNamespace {}");
firstChild.setName("NewName");
chai_1.expect(firstChild.getText()).to.equal("namespace NewName {}");
});
});
describe("hasNamespaceKeyword", () => {
it("should have a namespace keyword when it has one", () => {
const { firstChild } = testHelpers_1.getInfoFromText("namespace Identifier {}");
chai_1.expect(firstChild.hasNamespaceKeyword()).is.true;
});
it("should not have a namespace keyword when it doesn't have one", () => {
const { firstChild } = testHelpers_1.getInfoFromText("module Identifier {}");
chai_1.expect(firstChild.hasNamespaceKeyword()).is.false;
});
});
describe("hasModuleKeyword", () => {
it("should have a module keyword when it has one", () => {
const { firstChild } = testHelpers_1.getInfoFromText("module Identifier {}");
chai_1.expect(firstChild.hasModuleKeyword()).is.true;
});
it("should not have a module keyword when it doesn't have one", () => {
const { firstChild } = testHelpers_1.getInfoFromText("namespace Identifier {}");
chai_1.expect(firstChild.hasModuleKeyword()).is.false;
});
});
describe("getDeclarationTypeKeyword", () => {
it("should get the declaration type keyword for a namespace", () => {
const { firstChild } = testHelpers_1.getInfoFromText("namespace Identifier {}");
chai_1.expect(firstChild.getDeclarationTypeKeyword().getText()).equals("namespace");
});
it("should get the declaration type keyword for a module", () => {
const { firstChild } = testHelpers_1.getInfoFromText("module Identifier {}");
chai_1.expect(firstChild.getDeclarationTypeKeyword().getText()).equals("module");
});
});
describe("setHasNamespaceKeyword", () => {
it("should change the declaration type when a module", () => {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText("module Identifier {}");
firstChild.setHasNamespaceKeyword();
chai_1.expect(sourceFile.getText()).equals("namespace Identifier {}");
});
it("should change the declaration type when a namespace", () => {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText("namespace Identifier {}");
firstChild.setHasNamespaceKeyword(false);
chai_1.expect(sourceFile.getText()).equals("module Identifier {}");
});
it("should do nothing when the same", () => {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText("namespace Identifier {}");
firstChild.setHasNamespaceKeyword(true);
chai_1.expect(sourceFile.getText()).equals("namespace Identifier {}");
});
});
describe("setHasModuleKeyword", () => {
it("should change the declaration type when a namespace", () => {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText("namespace Identifier {}");
firstChild.setHasModuleKeyword();
chai_1.expect(sourceFile.getText()).equals("module Identifier {}");
});
it("should change the declaration type when a module", () => {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText("module Identifier {}");
firstChild.setHasModuleKeyword(false);
chai_1.expect(sourceFile.getText()).equals("namespace Identifier {}");
});
});
describe("fill", () => {
function doTest(startingCode, structure, expectedCode) {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startingCode);
firstChild.fill(structure);
chai_1.expect(firstChild.getText()).to.equal(expectedCode);
}
it("should not modify anything if the structure doesn't change anything", () => {
doTest("namespace Identifier {\n}", {}, "namespace Identifier {\n}");
});
it("should modify when changed", () => {
const structure = {
hasModuleKeyword: true
};
doTest("namespace Identifier {\n}", structure, "module Identifier {\n}");
});
});
describe("remove", () => {
function doTest(text, index, expectedText) {
const { sourceFile } = testHelpers_1.getInfoFromText(text);
sourceFile.getNamespaces()[index].remove();
chai_1.expect(sourceFile.getFullText()).to.equal(expectedText);
}
it("should remove the namespace declaration", () => {
doTest("namespace I {}\n\nnamespace J {}\n\nnamespace K {}", 1, "namespace I {}\n\nnamespace K {}");
});
});
});
//# sourceMappingURL=namespaceDeclarationTests.js.map