ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
104 lines (102 loc) • 5.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const testHelpers_1 = require("./../testHelpers");
describe("AmbientableNode", () => {
describe("navigation", () => {
const { sourceFile: mainSourceFile } = testHelpers_1.getInfoFromText("declare var ambientedVar; var myExplicitVar: string;");
const statements = mainSourceFile.getVariableStatements();
const ambientedStatement = statements[0];
const notAmbientedStatement = statements[1];
describe("hasDeclareKeyword", () => {
it("should have a declare keyword when it has one", () => {
chai_1.expect(ambientedStatement.hasDeclareKeyword()).to.be.true;
});
it("should not have an declare keyword when it doesn't have one", () => {
chai_1.expect(notAmbientedStatement.hasDeclareKeyword()).to.be.false;
});
});
describe("getDeclareKeyword", () => {
it("should have an declare keyword when it has one", () => {
chai_1.expect(ambientedStatement.getDeclareKeyword().getText()).to.equal("declare");
});
it("should not have an declare keyword when it doesn't have one", () => {
chai_1.expect(notAmbientedStatement.getDeclareKeyword()).to.be.undefined;
});
});
describe("getDeclareKeywordOrThrow", () => {
it("should have an declare keyword when it has one", () => {
chai_1.expect(ambientedStatement.getDeclareKeywordOrThrow().getText()).to.equal("declare");
});
it("should not have an declare keyword when it doesn't have one", () => {
chai_1.expect(() => notAmbientedStatement.getDeclareKeywordOrThrow()).to.throw();
});
});
describe("isAmbient", () => {
it("should not be ambient when not", () => {
const { firstChild } = testHelpers_1.getInfoFromText("class Identifier {}");
chai_1.expect(firstChild.isAmbient()).to.be.false;
});
it("should be ambient when it has a declare keyword", () => {
const { firstChild } = testHelpers_1.getInfoFromText("declare class Identifier {}");
chai_1.expect(firstChild.isAmbient()).to.be.true;
});
it("should be ambient when it's in a definition file", () => {
const { firstChild } = testHelpers_1.getInfoFromText("class Identifier {}", { isDefinitionFile: true });
chai_1.expect(firstChild.isAmbient()).to.be.true;
});
it("should be ambient when it's parent is ambient", () => {
const { firstChild } = testHelpers_1.getInfoFromText("declare namespace Identifier { class Identifier {} }");
const innerClass = firstChild.getClasses()[0];
chai_1.expect(innerClass.isAmbient()).to.be.true;
});
it("should always be ambient for interfaces", () => {
const { firstChild } = testHelpers_1.getInfoFromText("interface Identifier {}");
chai_1.expect(firstChild.isAmbient()).to.be.true;
});
it("should always be ambient for type aliases", () => {
const { firstChild } = testHelpers_1.getInfoFromText("type Identifier = string;");
chai_1.expect(firstChild.isAmbient()).to.be.true;
});
});
});
describe("toggleDeclareKeyword", () => {
function doTest(text, value, expected) {
const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(text);
if (value !== undefined)
firstChild.toggleDeclareKeyword(value);
else
firstChild.toggleDeclareKeyword();
chai_1.expect(sourceFile.getText()).to.equal(expected);
}
it("should add declare keyword when doesn't have one", () => {
doTest("class MyClass { }", undefined, "declare class MyClass { }");
});
it("should remove declare keyword when it has one", () => {
doTest("declare class MyClass { }", undefined, "class MyClass { }");
});
it("should add declare keyword when explicitly toggling it", () => {
doTest("class MyClass { }", true, "declare class MyClass { }");
});
it("should remove declare keyword when explicitly toggling it", () => {
doTest("declare class MyClass { }", false, "class MyClass { }");
});
});
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 when not set and structure empty", () => {
doTest("class MyClass {}", {}, "class MyClass {}");
});
it("should not modify when set and structure empty", () => {
doTest("declare class MyClass {}", {}, "declare class MyClass {}");
});
it("should modify when setting true", () => {
doTest("class MyClass {}", { hasDeclareKeyword: true }, "declare class MyClass {}");
});
});
});
//# sourceMappingURL=ambientableNodeTests.js.map