UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

83 lines (81 loc) 4.97 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("ScopedNode", () => { function getInfoWithFirstPropertyFromText(text) { const result = testHelpers_1.getInfoFromText(text); return Object.assign({}, result, { firstProperty: result.firstChild.getInstanceProperties()[0] }); } describe("getScope", () => { it("should get the correct scope when there is no modifier", () => { const { firstProperty } = getInfoWithFirstPropertyFromText("class Identifier {\nprop: string;}\n"); chai_1.expect(firstProperty.getScope()).to.be.equal(compiler_1.Scope.Public); }); it("should get the correct scope when there is a public modifier", () => { const { firstProperty } = getInfoWithFirstPropertyFromText("class Identifier {\npublic prop: string;}\n"); chai_1.expect(firstProperty.getScope()).to.be.equal(compiler_1.Scope.Public); }); it("should get the correct scope when there is a protected modifier", () => { const { firstProperty } = getInfoWithFirstPropertyFromText("class Identifier {\nprotected prop: string;}\n"); chai_1.expect(firstProperty.getScope()).to.be.equal(compiler_1.Scope.Protected); }); it("should get the correct scope when there is a private modifier", () => { const { firstProperty } = getInfoWithFirstPropertyFromText("class Identifier {\nprivate prop: string;}\n"); chai_1.expect(firstProperty.getScope()).to.be.equal(compiler_1.Scope.Private); }); }); describe("hasScopeKeyword", () => { it("should not have a scope keyword when there isn't one", () => { const { firstProperty } = getInfoWithFirstPropertyFromText("class Identifier {\nprop: string;}\n"); chai_1.expect(firstProperty.hasScopeKeyword()).to.be.false; }); it("should have a scope keyword when there is one", () => { const { firstProperty } = getInfoWithFirstPropertyFromText("class Identifier {\npublic prop: string;}\n"); chai_1.expect(firstProperty.hasScopeKeyword()).to.be.true; }); }); describe("setScope", () => { function doTest(startText, scope, expectedText) { const { firstChild, firstProperty } = getInfoWithFirstPropertyFromText(startText); firstProperty.setScope(scope); chai_1.expect(firstChild.getText()).to.be.equal(expectedText); } it("should clear the scope keyword if set to public", () => { doTest("class Identifier { private prop: string; }", compiler_1.Scope.Public, "class Identifier { prop: string; }"); }); it("should clear the scope keyword if set to public even when public", () => { doTest("class Identifier { public prop: string; }", compiler_1.Scope.Public, "class Identifier { prop: string; }"); }); it("should set the scope keyword to protected when specified", () => { doTest("class Identifier { private prop: string; }", compiler_1.Scope.Protected, "class Identifier { protected prop: string; }"); }); it("should set the scope keyword to private when specified", () => { doTest("class Identifier { protected prop: string; }", compiler_1.Scope.Private, "class Identifier { private prop: string; }"); }); it("should set the scope keyword when none exists and setting to not public", () => { doTest("class Identifier { prop: string; }", compiler_1.Scope.Private, "class Identifier { private prop: string; }"); }); it("should set the scope keyword when none exists, a decorator exists, and setting to not public", () => { doTest("class Identifier { @dec prop: string; }", compiler_1.Scope.Private, "class Identifier { @dec private prop: string; }"); }); }); describe("fill", () => { function doTest(startCode, structure, expectedCode) { const { firstProperty, sourceFile } = getInfoWithFirstPropertyFromText(startCode); firstProperty.fill(structure); chai_1.expect(sourceFile.getText()).to.equal(expectedCode); } it("should not modify when not set and structure empty", () => { doTest("class MyClass { prop: string; }", {}, "class MyClass { prop: string; }"); }); it("should not modify when set and structure empty", () => { doTest("class MyClass { public prop: string; }", {}, "class MyClass { public prop: string; }"); }); it("should modify when setting", () => { doTest("class MyClass { prop: string; }", { scope: compiler_1.Scope.Protected }, "class MyClass { protected prop: string; }"); }); }); }); //# sourceMappingURL=scopedNodeTests.js.map