UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

125 lines (123 loc) 7.27 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("DecoratableNode", () => { describe("getDecorators", () => { function doTest(text, expectedLength) { const { firstChild } = testHelpers_1.getInfoFromText(text); chai_1.expect(firstChild.getDecorators().length).to.equal(expectedLength); } it("should return an empty array for no decorators", () => { doTest("class Identifier {}", 0); }); it("should get the decorators when they exist", () => { doTest("@decorator\n@decorator2()\n@decorator3('str')\nclass Identifier {}", 3); }); }); describe("insertDecorators", () => { describe("class decorators", () => { function doTest(startCode, index, structures, expectedCode) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startCode); const result = firstChild.insertDecorators(index, structures); chai_1.expect(result.length).to.equal(structures.length); chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode); } it("should insert when there are no decorators", () => { doTest("class MyClass {}", 0, [{ name: "dec" }], "@dec\nclass MyClass {}"); }); it("should insert with arguments", () => { doTest("class MyClass {}", 0, [{ name: "dec", arguments: [] }, { name: "dec2", arguments: ["1"] }, { name: "dec3", arguments: ["1", "2"] }], "@dec()\n@dec2(1)\n@dec3(1, 2)\nclass MyClass {}"); }); it("should insert on the same indentation level", () => { doTest(" class MyClass {}", 0, [{ name: "dec" }, { name: "dec2" }], " @dec\n @dec2\n class MyClass {}"); }); it("should insert at the start", () => { doTest("@dec3\nclass MyClass {}", 0, [{ name: "dec" }, { name: "dec2" }], "@dec\n@dec2\n@dec3\nclass MyClass {}"); }); it("should insert multiple in the middle", () => { doTest("@dec\n@dec4\nclass MyClass {}", 1, [{ name: "dec2" }, { name: "dec3" }], "@dec\n@dec2\n@dec3\n@dec4\nclass MyClass {}"); }); it("should insert one in the middle at the same indentation", () => { doTest(" @dec\n @dec3\nclass MyClass {}", 1, [{ name: "dec2" }], " @dec\n @dec2\n @dec3\nclass MyClass {}"); }); it("should insert multiple in the middle at the same indentation", () => { doTest(" @dec\n @dec5\nclass MyClass {}", 1, [{ name: "dec2" }, { name: "dec3" }, { name: "dec4" }], " @dec\n @dec2\n @dec3\n @dec4\n @dec5\nclass MyClass {}"); }); it("should insert when the decorators are on the same line", () => { doTest(" @dec @dec3\n class MyClass {}", 1, [{ name: "dec2" }], " @dec @dec2 @dec3\n class MyClass {}"); }); it("should insert at the end", () => { doTest("@dec\nclass MyClass {}", 1, [{ name: "dec2" }, { name: "dec3" }], "@dec\n@dec2\n@dec3\nclass MyClass {}"); }); }); describe("parameter decorator", () => { function doTest(startCode, index, structures, expectedCode) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startCode); const result = firstChild.getInstanceMethods()[0].getParameters()[0].insertDecorators(index, structures); chai_1.expect(result.length).to.equal(structures.length); chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode); } it("should insert on the same line when none exists", () => { doTest("class MyClass { myMethod(param) {} }", 0, [{ name: "dec" }], "class MyClass { myMethod(@dec param) {} }"); }); it("should insert at the start on the same line", () => { doTest("class MyClass { myMethod(@dec2 param) {} }", 0, [{ name: "dec1" }], "class MyClass { myMethod(@dec1 @dec2 param) {} }"); }); it("should insert at the end on the same line", () => { doTest("class MyClass { myMethod(@dec1 param) {} }", 1, [{ name: "dec2" }], "class MyClass { myMethod(@dec1 @dec2 param) {} }"); }); }); }); describe("insertDecorator", () => { function doTest(startCode, index, structure, expectedCode) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startCode); const result = firstChild.insertDecorator(index, structure); chai_1.expect(result).to.be.instanceOf(compiler_1.Decorator); chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode); } it("should insert in the middle", () => { doTest("@dec\n@dec3\nclass MyClass {}", 1, { name: "dec2" }, "@dec\n@dec2\n@dec3\nclass MyClass {}"); }); }); describe("addDecorator", () => { function doTest(startCode, structure, expectedCode) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startCode); const result = firstChild.addDecorator(structure); chai_1.expect(result).to.be.instanceOf(compiler_1.Decorator); chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode); } it("should add when one doesn't exists", () => { doTest("class MyClass {}", { name: "dec" }, "@dec\nclass MyClass {}"); }); it("should add when one exists", () => { doTest("@dec\nclass MyClass {}", { name: "dec2" }, "@dec\n@dec2\nclass MyClass {}"); }); }); describe("addDecorators", () => { function doTest(startCode, structures, expectedCode) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startCode); const result = firstChild.addDecorators(structures); chai_1.expect(result.length).to.equal(structures.length); chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode); } it("should add multiple", () => { doTest("@dec\nclass MyClass {}", [{ name: "dec2" }, { name: "dec3" }], "@dec\n@dec2\n@dec3\nclass 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 modify when setting", () => { doTest("class Identifier {}", { decorators: [{ name: "dec1" }, { name: "dec2" }] }, "@dec1\n@dec2\nclass Identifier {}"); }); it("should not modify anything if the structure doesn't change anything", () => { doTest("class Identifier {}", {}, "class Identifier {}"); }); }); }); //# sourceMappingURL=decoratableNodeTests.js.map