UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

92 lines (90 loc) 4.92 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("ParameteredNode", () => { describe("getParameters", () => { const { firstChild } = testHelpers_1.getInfoFromText("function func(param1: string, param2: number){}"); const parameters = firstChild.getParameters(); it("should get the right number of parameters", () => { chai_1.expect(parameters.length).to.equal(2); }); it("should have parameter of type ParameterDeclaration", () => { chai_1.expect(parameters[0]).to.be.instanceOf(compiler_1.ParameterDeclaration); }); }); describe("addParameter", () => { function doTest(startCode, structure, expectedCode) { const { firstChild } = testHelpers_1.getInfoFromText(startCode); const result = firstChild.addParameter(structure); chai_1.expect(firstChild.getText()).to.equal(expectedCode); chai_1.expect(result).to.be.instanceof(compiler_1.ParameterDeclaration); } it("should add when none exists", () => { doTest("function identifier() {}", { name: "param" }, "function identifier(param) {}"); }); it("should add when one exists", () => { doTest("function identifier(param1) {}", { name: "param2" }, "function identifier(param1, param2) {}"); }); }); describe("addParameters", () => { function doTest(startCode, structures, expectedCode) { const { firstChild } = testHelpers_1.getInfoFromText(startCode); const result = firstChild.addParameters(structures); chai_1.expect(firstChild.getText()).to.equal(expectedCode); chai_1.expect(result.length).to.equal(structures.length); } it("should add multiple", () => { doTest("function identifier(param1) {}", [{ name: "param2" }, { name: "param3" }], "function identifier(param1, param2, param3) {}"); }); }); describe("insertParameter", () => { function doTest(startCode, index, structure, expectedCode) { const { firstChild } = testHelpers_1.getInfoFromText(startCode); const result = firstChild.insertParameter(index, structure); chai_1.expect(firstChild.getText()).to.equal(expectedCode); chai_1.expect(result).to.be.instanceof(compiler_1.ParameterDeclaration); } it("should insert when none exists", () => { doTest("function identifier() {}", 0, { name: "param" }, "function identifier(param) {}"); }); it("should insert when one exists", () => { doTest("function identifier(param2) {}", 0, { name: "param1" }, "function identifier(param1, param2) {}"); }); }); describe("insertParameters", () => { function doTest(startCode, insertIndex, structures, expectedCode) { const { firstChild } = testHelpers_1.getInfoFromText(startCode); const result = firstChild.insertParameters(insertIndex, structures); chai_1.expect(firstChild.getText()).to.equal(expectedCode); chai_1.expect(result.length).to.equal(structures.length); } it("should insert when none exists", () => { doTest("function identifier() {}", 0, [{ name: "param1", isReadonly: true }, { name: "param2", type: "string[]", isRestParameter: true }], "function identifier(readonly param1, ...param2: string[]) {}"); }); it("should insert at the start", () => { doTest("function identifier(param2) {}", 0, [{ name: "param1" }], "function identifier(param1, param2) {}"); }); it("should insert at the end", () => { doTest("function identifier(param1) {}", 1, [{ name: "param2" }], "function identifier(param1, param2) {}"); }); it("should insert in the middle", () => { doTest("function identifier(param1, param3) {}", 1, [{ name: "param2" }], "function identifier(param1, param2, param3) {}"); }); }); 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("function identifier() {}", { parameters: [{ name: "param" }] }, "function identifier(param) {}"); }); it("should not modify anything if the structure doesn't change anything", () => { doTest("function identifier() {}", {}, "function identifier() {}"); }); }); }); //# sourceMappingURL=parameteredNodeTests.js.map