UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

82 lines (80 loc) 5.24 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("FunctionDeclaration", () => { describe("insertOverloads", () => { function doTest(startCode, index, structures, expectedCode) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startCode); const result = firstChild.insertOverloads(index, structures); chai_1.expect(result.length).to.equal(structures.length); chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode); } it("should insert when no other overloads exist", () => { doTest("function identifier() {}\n", 0, [{ returnType: "number" }, {}], "function identifier(): number;\nfunction identifier();\nfunction identifier() {}\n"); }); it("should insert with the ambientable and exportable nodes the same as the implementation signature unless overwritten", () => { doTest("declare async function* identifier(param: string): string {}\n", 0, [{ returnType: "number", hasDeclareKeyword: false }, {}], "function identifier(): number;\ndeclare function identifier();\ndeclare async function* identifier(param: string): string {}\n"); }); it("should be able to insert at start when another overload exists", () => { doTest("function identifier();\nfunction identifier() {}\n", 0, [{ returnType: "string" }], "function identifier(): string;\nfunction identifier();\nfunction identifier() {}\n"); }); it("should be able to insert at end when another overload exists", () => { doTest("function identifier();\nfunction identifier() {}\n", 1, [{ returnType: "string" }], "function identifier();\nfunction identifier(): string;\nfunction identifier() {}\n"); }); it("should be able to insert in the middle when other overloads exists", () => { doTest("function identifier();\nfunction identifier();\nfunction identifier() {}\n", 1, [{ returnType: "string" }], "function identifier();\nfunction identifier(): string;\nfunction identifier();\nfunction identifier() {}\n"); }); }); describe("insertOverload", () => { function doTest(startCode, index, structure, expectedCode) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startCode); const result = firstChild.insertOverload(index, structure); chai_1.expect(result).to.be.instanceOf(compiler_1.FunctionDeclaration); chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode); } it("should insert", () => { doTest("function identifier();\nfunction identifier();\nfunction identifier() {}\n", 1, { returnType: "number" }, "function identifier();\nfunction identifier(): number;\nfunction identifier();\nfunction identifier() {}\n"); }); }); describe("addOverloads", () => { function doTest(startCode, structures, expectedCode) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startCode); const result = firstChild.addOverloads(structures); chai_1.expect(result.length).to.equal(structures.length); chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode); } it("should add at the end", () => { doTest("function identifier();\nfunction identifier() {}\n", [{ returnType: "number" }, { returnType: "string" }], "function identifier();\nfunction identifier(): number;\nfunction identifier(): string;\nfunction identifier() {}\n"); }); }); describe("addOverload", () => { function doTest(startCode, structure, expectedCode) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startCode); const result = firstChild.addOverload(structure); chai_1.expect(result).to.be.instanceOf(compiler_1.FunctionDeclaration); chai_1.expect(sourceFile.getFullText()).to.equal(expectedCode); } it("should add at the end", () => { doTest("function identifier();\nfunction identifier() {}\n", { returnType: "number" }, "function identifier();\nfunction identifier(): number;\nfunction identifier() {}\n"); }); }); describe("fill", () => { function doTest(startingCode, structure, expectedCode) { const { firstChild, sourceFile } = testHelpers_1.getInfoFromText(startingCode); firstChild.fill(structure); chai_1.expect(sourceFile.getText()).to.equal(expectedCode); } it("should not modify anything if the structure doesn't change anything", () => { doTest("function identifier() {\n}", {}, "function identifier() {\n}"); }); it("should modify when changed", () => { const structure = { overloads: [{ returnType: "string" }] }; doTest("function identifier() {\n}", structure, "function identifier(): string;\nfunction identifier() {\n}"); }); }); }); //# sourceMappingURL=functionDeclarationTests.js.map