ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
80 lines (78 loc) • 4.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const compiler_1 = require("./../../../compiler");
const testHelpers_1 = require("./../testHelpers");
describe("ScopeableNode", () => {
function getFirstParameter(text) {
const result = testHelpers_1.getInfoFromText(text);
return Object.assign({}, result, { firstParam: result.firstChild.getConstructors()[0].getParameters()[0] });
}
describe("getScope", () => {
it("should return undefined when there's no scope", () => {
const { firstParam } = getFirstParameter("class Identifier { constructor(param: string) {} }");
chai_1.expect(firstParam.getScope()).to.be.undefined;
});
it("should return public when public", () => {
const { firstParam } = getFirstParameter("class Identifier { constructor(public param: string) {} }");
chai_1.expect(firstParam.getScope()).to.equal(compiler_1.Scope.Public);
});
it("should return protected when protected", () => {
const { firstParam } = getFirstParameter("class Identifier { constructor(protected param: string) {} }");
chai_1.expect(firstParam.getScope()).to.equal(compiler_1.Scope.Protected);
});
it("should return private when private", () => {
const { firstParam } = getFirstParameter("class Identifier { constructor(private param: string) {} }");
chai_1.expect(firstParam.getScope()).to.equal(compiler_1.Scope.Private);
});
});
describe("hasScopeKeyword", () => {
it("should not have one when there's no scope", () => {
const { firstParam } = getFirstParameter("class Identifier { constructor(param: string) {} }");
chai_1.expect(firstParam.hasScopeKeyword()).to.be.false;
});
it("should have one when there is a scope", () => {
const { firstParam } = getFirstParameter("class Identifier { constructor(public param: string) {} }");
chai_1.expect(firstParam.hasScopeKeyword()).to.be.true;
});
});
describe("setScope", () => {
it("should set to public when set to public", () => {
const { firstChild, firstParam } = getFirstParameter("class Identifier { constructor(param: string) {} }");
firstParam.setScope(compiler_1.Scope.Public);
chai_1.expect(firstChild.getText()).to.be.equal("class Identifier { constructor(public param: string) {} }");
});
it("should set to protected when set to protected", () => {
const { firstChild, firstParam } = getFirstParameter("class Identifier { constructor(public param: string) {} }");
firstParam.setScope(compiler_1.Scope.Protected);
chai_1.expect(firstChild.getText()).to.be.equal("class Identifier { constructor(protected param: string) {} }");
});
it("should set to private when set to private", () => {
const { firstChild, firstParam } = getFirstParameter("class Identifier { constructor(public param: string) {} }");
firstParam.setScope(compiler_1.Scope.Private);
chai_1.expect(firstChild.getText()).to.be.equal("class Identifier { constructor(private param: string) {} }");
});
it("should clear when set to undefined", () => {
const { firstChild, firstParam } = getFirstParameter("class Identifier { constructor(public param: string) {} }");
firstParam.setScope(undefined);
chai_1.expect(firstChild.getText()).to.be.equal("class Identifier { constructor(param: string) {} }");
});
});
describe("fill", () => {
function doTest(startCode, structure, expectedCode) {
const { firstParam, sourceFile } = getFirstParameter(startCode);
firstParam.fill(structure);
chai_1.expect(sourceFile.getText()).to.equal(expectedCode);
}
it("should not modify when not set and structure empty", () => {
doTest("class MyClass { constructor(param: string) {} }", {}, "class MyClass { constructor(param: string) {} }");
});
it("should not modify when set and structure empty", () => {
doTest("class MyClass { constructor(public param: string) {} }", {}, "class MyClass { constructor(public param: string) {} }");
});
it("should modify when setting", () => {
doTest("class MyClass { constructor(param: string) {} }", { scope: compiler_1.Scope.Protected }, "class MyClass { constructor(protected param: string) {} }");
});
});
});
//# sourceMappingURL=scopeableNodeTests.js.map